/
Player.cs
139 lines (112 loc) · 3.29 KB
/
Player.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Player : MovingObject {
public int wallDamage = 1;
public int pointsPerFood = 10;
public int pointsPerSoda = 20;
public float restartLevelDelay = 1f;
public AudioClip moveSound1;
public AudioClip moveSound2;
public AudioClip eatSound1;
public AudioClip eatSound2;
public AudioClip drinkSound1;
public AudioClip drinkSound2;
public AudioClip gameOverSound;
private Vector2 touchOrigin = -Vector2.one;
private Animator animator;
private int food;
public Text foodText;
protected override void Start () {
animator = GetComponent<Animator> ();
food = GameManager.instance.playerFoodPoints;
foodText.text = "Food: " + food;
base.Start ();
}
private void OnDisable () {
GameManager.instance.playerFoodPoints = food;
}
void Update () {
if (!GameManager.instance.playersTurn)
return;
int horizontal = 0;
int vertical = 0;
#if UNITY_STANDALONE || UNITY_WEBPLAYER || UNITY_WEBGL
horizontal = (int)Input.GetAxisRaw ("Horizontal");
vertical = (int)Input.GetAxisRaw ("Vertical");
if (horizontal != 0) {
vertical = 0;
}
#else
if(Input.touchCount > 0) {
Touch myTouch = Input.touches[0];
if (myTouch.phase == TouchPhase.Began) {
touchOrigin = myTouch.position;
} else if (myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0) {
Vector2 touchEnd = myTouch.position;
float x = touchEnd.x - touchOrigin.x;
float y = touchEnd.y - touchOrigin.y;
touchOrigin.x = -1;
if (Mathf.Abs(x) > Mathf.Abs(y)) {
horizontal = x > 0 ? 1 : -1;
} else {
vertical = y > 0 ? 1 : -1;
}
}
}
#endif
if (horizontal != 0 || vertical != 0) {
AttemptMove<Wall> (horizontal, vertical);
}
}
private void OnTriggerEnter2D (Collider2D other) {
if (other.tag == "Exit") {
Invoke ("Restart", restartLevelDelay);
enabled = false;
} else if (other.tag == "Food") {
food += pointsPerFood;
foodText.text = "+" + pointsPerFood + " Food: " + food;
SoundManager.instance.RandomizeSfx (eatSound1, eatSound2);
other.gameObject.SetActive (false);
} else if (other.tag == "Soda") {
food += pointsPerSoda;
foodText.text = "+" + pointsPerSoda + " Food: " + food;
SoundManager.instance.RandomizeSfx (drinkSound1, drinkSound2);
other.gameObject.SetActive (false);
}
}
private void Restart () {
SceneManager.LoadScene (0);
}
public void LoseFood (int loss) {
animator.SetTrigger ("playerHit");
food -= loss;
foodText.text = "-" + loss + " Food: " + food;
CheckIfGameOver ();
}
protected override void AttemptMove <T> (int xDir, int yDir) {
food--;
foodText.text = "Food: " + food;
Debug.Log("base move");
base.AttemptMove <T> (xDir, yDir);
RaycastHit2D hit;
if (Move (xDir, yDir, out hit)) {
SoundManager.instance.RandomizeSfx (moveSound1, moveSound2);
}
CheckIfGameOver ();
GameManager.instance.playersTurn = false;
}
protected override void OnCantMove <T> (T component) {
Wall hitWall = component as Wall;
hitWall.DamageWall (wallDamage);
animator.SetTrigger ("playerChop");
}
private void CheckIfGameOver () {
if (food <= 0) {
SoundManager.instance.PlaySingle (gameOverSound);
SoundManager.instance.musicSource.Stop ();
GameManager.instance.GameOver ();
}
}
}