-
Notifications
You must be signed in to change notification settings - Fork 2
/
GameController.cs
201 lines (172 loc) · 5.77 KB
/
GameController.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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameController : MonoBehaviour {
//currently this monobehaviour has too many public variables
//we might want to sort that out
[SerializeField] float piecesize;
[SerializeField] int gridsize;
public int moveindex;
[SerializeField] GameObject piece;
[SerializeField] GameObject player;
[SerializeField] GameObject turret;
[SerializeField] Vector2[] turretSpawnPoint = new Vector2[5];
public Vector2 playerPosition;
public Vector2 firePosition;
public menu playerMenu;
//list and arrays - I don't know which one is better!
public List<Vector2> playerMovement = new List <Vector2> ();
public List<Vector2> playerAttack = new List <Vector2> ();
public List<int> attackWeapon = new List <int> ();
public Vector2[] extraMovement = new Vector2[10];
const float ratio = 0.866f;
Transform boardHolder;
int step;
public bool hold;
public Vector3 targetStart;
public Vector3 targetEnd;
LineRenderer targetLine;
public bool[] upgradeOn;
public int currentWeapon;
public bool commandable;
public int maxSteps;
public int readyPlayers;
const float pi = 3.1415f;
void Start(){
player = GameObject.Find ("actionController");
playerMenu = GameObject.Find ("currentWeaponMenu").GetComponent<menu>();
targetLine = GetComponent <LineRenderer> ();
BoardSetup ();
clearSetup (new Vector2(0,0));
commandable = true;
}
void BoardSetup () { //create a virtual board for the player to make commands
boardHolder = new GameObject ("Board").transform;
Vector3 pieceposition;
for(int i=1;i<gridsize*2;i++){
for(int j=1;j<2*gridsize-Mathf.Abs(gridsize-i);j++){
//crazy math formulas for the actuall positions, don't delve in if you value your life!
pieceposition= new Vector3 (piecesize*(Mathf.Abs(gridsize-i)*0.5f+j-gridsize),piecesize*ratio*(i-gridsize),0f);
GameObject instance = Instantiate(piece,pieceposition,Quaternion.identity) as GameObject;
Tile tile = instance.GetComponent<Tile>();
//the same craziness here
tile.tilePosition=(new Vector2(j-0.5f*(gridsize+i-Mathf.Abs(i-gridsize)),i-gridsize));
instance.transform.SetParent (boardHolder);
}
}
for (int i=0; i<=5; i++) {
GameObject newTurret = Instantiate(turret,positionTransform(turretSpawnPoint[i]),Quaternion.Euler(0,90,90)) as GameObject;
//if we spawn real models, we have to sort the rotation out
newTurret.GetComponent<turret>().turretPosition=turretSpawnPoint[i];
}
}
public void moveCommand(Vector2 position) {
moveindex++;
playerAttack.Insert(moveindex, new Vector3 (.5f, .5f));//(0.5,0.5) basically stands for "nothing"
attackWeapon.Insert(moveindex,0);
playerMovement.Insert(moveindex,position);
playerPosition = position;
targetLine.enabled = false;
firePosition = playerPosition;
}
public void clearSetup(Vector2 position){ //reset after each round
playerPosition = position;
firePosition = playerPosition;
moveindex = 0;
playerMovement.Clear ();
playerAttack.Clear ();
attackWeapon.Clear ();
extraMovement=new Vector2[10];
playerMovement.Insert(0,playerPosition);
playerAttack.Insert (0, new Vector2 (.5f, .5f));
attackWeapon.Insert(0,0);
targetEnd = new Vector3 (.5f, .5f, 0);
targetStart = playerPosition;
targetLine.enabled = false;
commandable = true;
readyPlayers = 0;
}
public void cancelCommand(){
if (playerMovement.Count == 1) {
clearAttack();
return;
}
playerAttack.RemoveAt (moveindex);
attackWeapon.RemoveAt (moveindex);
playerMovement.RemoveAt(moveindex);
extraMovement [moveindex] = Vector2.zero;
moveindex--;
playerPosition = playerMovement[moveindex];
firePosition = playerPosition-extraMovement[moveindex];
//print (moveindex);
targetLine.enabled = false;
targetStart = firePosition;
targetEnd = playerAttack[moveindex];
playerMenu.select (attackWeapon[moveindex]);
if (targetEnd != new Vector3(.5f,.5f,0)){
drawLine();
}
}
//this is the speacial condition where the player wants to cancel his or her attack command
//in the first time segment
public void clearAttack(){
playerMovement[moveindex]=playerMovement[moveindex]-extraMovement[moveindex];
extraMovement[moveindex]=Vector2.zero;
playerPosition = playerMovement [moveindex];
playerAttack[moveindex]=new Vector2 (.5f, .5f);
attackWeapon[moveindex]=0;
targetLine.enabled = false;
return;
}
public void attackCommand(Vector2 position){
playerAttack[moveindex] = position;
attackWeapon[moveindex] = currentWeapon;
drawLine ();
}
void drawLine(){
targetLine.enabled = true;
targetLine.SetPosition (0, positionTransform (targetStart)+Vector3.back);
targetLine.SetPosition (1, positionTransform(targetEnd)+Vector3.back);
}
public string getSp (string sp,string name){
if (sp.IndexOf (name) != -1) {
string temp = sp.Substring (sp.IndexOf (name) + name.Length);
return temp;
} else {
return null;
}
}
public Vector3 positionTransform (Vector2 position){
return new Vector3 (piecesize*(position.x+0.5f*position.y),piecesize*ratio*position.y,0);
}
public bool isOutOfBounds(Vector2 positon){
float x = positon.x;
float y = positon.y;
if (x >= 0) {
if(y<=0){
return (x>=gridsize)||(y<=-gridsize);
}else{
return (x+y>=gridsize);
}
}else if(y>=0){
return (x<=-gridsize)||(y>=gridsize);
}else{
return (x+y<=-gridsize);
}
}
//this is used to spwan a collision detector with correct rotation
//we probably will need to use this more when creating projectile animations
public Quaternion vector3ToQuaternion(Vector3 v){
float x = v.x;
float y = v.y;
float theta1;
if (x>0||(x==0&&y>0)){
theta1 = Mathf.Atan (y / x);
}else{
theta1 = pi+Mathf.Atan (y / x);
}
Quaternion q = new Quaternion();
q = Quaternion.Euler(0,0,Mathf.Rad2Deg*theta1);
return q;
}
}