-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tile.cs
229 lines (199 loc) · 5.41 KB
/
Tile.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using UnityEngine;
using System.Collections;
public class Tile : MonoBehaviour {
private Renderer rend;
private Vector2 firePosition;
private Vector2 playerPosition;
public Vector2 tilePosition;
private bool chosen;
private bool current;
private bool currenttgt;
private bool rightHold;
[SerializeField] Color mouseOver;
[SerializeField] Color chosenColor;
[SerializeField] Color attackColor;
[SerializeField] Color wunengColor; //wuneng is chinese for invalid
[SerializeField] Color jumpColor;
[SerializeField] bool valid;
[SerializeField] bool shootable;
[SerializeField] bool mouseon;
public GameObject controller;
[SerializeField] GameController gctrl;
public GameObject statsCtrl;
[SerializeField] statsController dataBase;
private string sp;
private string temp;
void Start() {
rend = GetComponent<Renderer>();
//establish connection with other scripts
controller = GameObject.Find ("GameController(Clone)");
gctrl=controller.GetComponent<GameController>();
statsCtrl = GameObject.Find("gameData");
dataBase = statsCtrl.GetComponent<statsController> ();
playerPosition = gctrl.playerPosition;//they should be in sync at all times
}
void OnMouseEnter() {
if (!gctrl.commandable)
return;
if (gctrl.hold && shootable) { //if in targeting mode the current target becomes red
gctrl.targetEnd = tilePosition;
gctrl.attackCommand(tilePosition);
rend.material.color = attackColor;
}
mouseon = true;
}
void OnMouseOver(){
if (!gctrl.commandable)
return;
if (Input.GetButton ("Fire2") && (current || tilePosition == firePosition)) {
rightHold = true;
} else {
if(rightHold){
gctrl.cancelCommand ();
rightHold = false;
}
}
}
void OnMouseDown() {
if (!gctrl.commandable)
return;
if (firePosition==tilePosition) {
gctrl.hold = true; //enter targeting mode
gctrl.targetStart = tilePosition;
}
}
void OnMouseExit() {
mouseon = false;
rightHold = false;
}
void OnMouseUp(){
gctrl.hold = false;
if (valid&&mouseon) {
gctrl.moveCommand(tilePosition);
}
}
void Update(){ //this part is waht determines the display on each tile
playerPosition = gctrl.playerPosition;
firePosition = gctrl.firePosition; //in the case of weapons with recoil effect, fireposition!=playerposition
if (near (playerPosition, tilePosition)&&gctrl.moveindex<gctrl.maxSteps) {
valid = true;
} else {
valid = false;
}
if (tilePosition == playerPosition){ //current is prioritised over other colors
current = true;
chosen = true;
}else{
current=false;
if(gctrl.playerMovement.Contains(tilePosition)){ //mark the trail with mouseover color
chosen = true;
}else{
chosen = false;
}
}
if (!gctrl.commandable) {
mouseon=false;
}
if (gctrl.playerAttack.Contains(tilePosition)) {
rend.material.color = attackColor;
}else if (current) {
rend.material.color = chosenColor;
}else if(chosen||(mouseon&&valid)){
rend.material.color = mouseOver;
}else if(firePosition==tilePosition){
rend.material.color = jumpColor;
}else{
rend.material.color = Color.white;
}
//some special cases of targeting
sp=dataBase.weapons[gctrl.currentWeapon].special;
if (gctrl.hold) {
if(distance(firePosition,tilePosition)>dataBase.weapons[gctrl.currentWeapon].range
||distance(firePosition,tilePosition)==0){
rend.material.color = wunengColor;
shootable = false;
}else{
//look for weapons that have thrust effects
temp = gctrl.getSp(sp,"move");
shootable = true;
if (temp!=null && mouseon){
int amount = int.Parse(temp.Substring(0,temp.IndexOf(";"))); //get the thrust amount(it's usually 1)
if(align(firePosition,tilePosition)){
Vector2 difference = normal(tilePosition-firePosition) ;
Vector2 newPosition;
newPosition = firePosition + difference*-amount;
gctrl.extraMovement[gctrl.moveindex]=difference*-amount;
if (newPosition!=playerPosition){
gctrl.playerMovement[gctrl.playerMovement.Count-1]=newPosition;
gctrl.playerPosition = newPosition;
}
}else{
gctrl.extraMovement[gctrl.moveindex]=Vector2.zero;
gctrl.playerMovement[gctrl.playerMovement.Count-1] = firePosition;
gctrl.playerPosition = firePosition;
}
}
}
//look for specially ranged weapons
temp = gctrl.getSp(sp,">");
if (temp!=null){
int amount = int.Parse(temp.Substring(0,temp.IndexOf(";")));
if(distance(firePosition,tilePosition)<amount){
rend.material.color = wunengColor;
shootable = false;
}
}
}
}
bool near(Vector2 v1,Vector2 v2){
if(Mathf.Abs(Vector2.Distance(v1,v2))==1 || v1==v2+new Vector2(1,-1)||v2==v1+new Vector2(1,-1)){
return true;
}else{
return false;
}
}
float distance(Vector2 v1, Vector2 v2){
float x;
float y;
if (v1.x > v2.x) {
x=v1.x-v2.x;
y=v1.y-v2.y;
}else{
x=v2.x-v1.x;
y=v2.y-v1.y;
}
if (y>=0) {
return x+y;
}else if(x>=-y){
return x;
}else{
return -y;
}
}
bool align(Vector2 v1,Vector2 v2){
Vector2 v0 = v1 - v2;
if(v0.x==0||v0.y==0||v0.x==-v0.y){
return true;
}else{
return false;
}
}
Vector2 normal(Vector2 v){ //return the identity vector along the 6 directions
int x, y;
if (v.x == 0) {
x = 0;
} else if (v.x > 0) {
x = 1;
} else {
x=-1;
}
if (v.y == 0) {
y = 0;
} else if (v.y > 0) {
y = 1;
} else {
y =-1;
}
return new Vector2 (x, y);
}
}