-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHoja.cs
103 lines (89 loc) · 3.28 KB
/
Hoja.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Hoja : MonoBehaviour {
public Camera cam;
public int lifes;
public float maxDist;
public float speed;
public LifesPannel lifesPanel;
public Sprite[] characterSprites;
public float maxSpeed;
public GameObject drop;
public AudioClip [] sounds;
private AudioSource mySource;
private Rigidbody2D myRigidbody;
private Animator myAnimator;
private bool isInvencible;
private SpriteRenderer mySprite;
// Use this for initialization
void Start () {
cam = Camera.main;
mySource = GetComponent<AudioSource>();
mySprite = GetComponent<SpriteRenderer>();
mySprite.sprite = characterSprites[PlayerPrefsManager.GetCharacter()];
myRigidbody = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
myRigidbody.drag = 0.5f;
InvokeRepeating("moveLeaf", 0.01f, 0.4f);
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)) {
moveLeaf();
}
}
void moveLeaf() {
if (Input.GetMouseButton(0) && Time.timeScale != 0)
{
Vector3 tapPos = CalculateWorldPointOfMouseClick();
Vector2 distance = new Vector2(transform.position.x - tapPos.x, transform.position.y - tapPos.y);
AddForce(distance.normalized, Mathf.Max(0, maxDist - distance.magnitude));
}
}
Vector2 CalculateWorldPointOfMouseClick() {
float mouseX = Input.mousePosition.x;
float mouseY = Input.mousePosition.y;
float distanceFromCamera = 5f; //We don't use it! :D
Vector3 mouseClickPos = new Vector3(mouseX, mouseY, distanceFromCamera);
Vector2 worldPos = cam.ScreenToWorldPoint(mouseClickPos);
Instantiate(drop, worldPos, Quaternion.identity);
return worldPos;
}
public void AddForce(Vector2 force, float module) {
myRigidbody.AddForce(force*module*speed);
if (myRigidbody.velocity.magnitude > maxSpeed) {
myRigidbody.velocity = myRigidbody.velocity.normalized * maxSpeed;
}
}
public void toggleInvencible() {
isInvencible = false;
}
public void OnTriggerEnter2D(Collider2D col) {
if (col.CompareTag("Obstacle")) {
if (!isInvencible) {
isInvencible = true;
myAnimator.SetTrigger("damaged");
mySource.clip = sounds[0];
mySource.Play();
lifes--;
if (col.gameObject.name == "Ju-on") {
lifes = 0;
//Add here Ju-on's gameover screen
}
if (lifes <= 0) {
SceneManager.LoadScene("Perder");
}
lifesPanel.updateLifes(lifes);
}
}else if (col.CompareTag("Fly")) {
lifes++;
lifes = Mathf.Min(3, lifes);
lifesPanel.updateLifes(lifes);
mySource.clip = sounds[1];
mySource.Play();
Destroy(col.gameObject);
}
}
}