-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameController.cs
241 lines (210 loc) · 7.5 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
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
230
231
232
233
234
235
236
237
238
239
240
241
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
{
[SerializeField] GameObject monster_Prefab;
[SerializeField] GameObject encounter_spawner;
[SerializeField] GameObject monster_spawn_point_spawner;
[SerializeField] GameObject Empty_Wall;
UnitStats m_unitstats, p_unitstats;
GameObject instanceMonsterwall,instancePlayerwall;
BoxCollider2D e_Collider;
Vector3 spawner_pos;
PlayerController p_Control;
MonsterController m_Control;
BattleHandler Battle_Control;
MonsterBattleHandler M_Battle_Control;
private State state;
private Monster_state m_state;
private Attack_Order_State a_order_state;
float player_start_value,monster_start_value;
public int turnCounter;
public float Roller;
private enum Attack_Order_State
{
PlayerCanAttack,
MonsterCanAttack,
}
private enum Monster_state
{
MonsterSpawned,
SpawnEmpty,
}
private enum State
{
WaitingForPlayer,
Reached,
Busy,
}
void Start()
{
Start_Varibles();
state = State.WaitingForPlayer;
m_state=Monster_state.SpawnEmpty;
a_order_state=Attack_Order_State.MonsterCanAttack;
}
void Awake()
{
}
void FixedUpdate()
{
if(m_state==Monster_state.SpawnEmpty)
{
onEncounter(p_Control.collider_triggered, () => {
m_state=Monster_state.MonsterSpawned;
});
state=State.WaitingForPlayer;
}
Start_Attack();
}
void Start_Varibles()
{
//GameObject spawner=GameObject.FindGameObjectWithTag("encounter_Spawn");
//spawner_pos=spawner.transform.position;
monster_Prefab=GameObject.FindGameObjectWithTag("Enemy");
p_Control=GameObject.Find("Test_obj").GetComponent<PlayerController>();
m_Control=GameObject.FindGameObjectWithTag("Enemy").GetComponent<MonsterController>();
Battle_Control=GameObject.Find("Test_obj").GetComponent<BattleHandler>();
M_Battle_Control=GameObject.FindGameObjectWithTag("Enemy").GetComponent<MonsterBattleHandler>();
m_unitstats=GameObject.FindGameObjectWithTag("Enemy").GetComponent<UnitStats>();
p_unitstats=GameObject.Find("Test_obj").GetComponent<UnitStats>();
turnCounter=1;//will be needed for continues attack cycle
e_Collider=Empty_Wall.GetComponent<BoxCollider2D>();
}
void Spawn_Enemy()
{
Vector3 distance=new Vector3(3,0,0);
monster_Prefab.transform.position=GameObject.FindGameObjectWithTag("encounter_Spawn").transform.position+distance;
Spawn_BackWalls();
}
void Spawn_BackWalls()
{
Vector3 Sides=new Vector3(0.15f,0,0);
instancePlayerwall = Instantiate (Empty_Wall, GameObject.FindGameObjectWithTag("encounter_Spawn").transform.position
- Sides, Quaternion.identity) as GameObject;
instanceMonsterwall = Instantiate (Empty_Wall, GameObject.FindGameObjectWithTag("MonsterSpawnP").transform.position
+ Sides, Quaternion.identity) as GameObject;
}
void onEncounter(bool create,Action onMonsterSpawn )
{
if(create)
{
Spawn_Enemy();
onMonsterSpawn();
}
}
void First_Attacker_Roller()
{
player_start_value = UnityEngine.Random.Range(p_unitstats.speed - 3f,p_unitstats.speed + 3f);
monster_start_value = UnityEngine.Random.Range(m_unitstats.speed - 3f,m_unitstats.speed + 3f);
Debug.Log("player_start_value rolled: "+player_start_value+"/ monster_start_value rolled: "+monster_start_value);
}
void Attack_Turns()
{
if(player_start_value>=monster_start_value)
a_order_state=Attack_Order_State.PlayerCanAttack;
else
a_order_state=Attack_Order_State.MonsterCanAttack;
}
void Start_Attack()
{
if(m_state==Monster_state.MonsterSpawned && state==State.WaitingForPlayer)
{
Game_Checker();
First_Attacker_Roller();
Attack_Turns();
if(a_order_state==Attack_Order_State.MonsterCanAttack)
{
p_unitstats.health -= Attack_Damage_Roller(m_unitstats);
Debug.Log("Monster Attack Started");
M_Battle_Control.Monster_Attack(() => {
state = State.WaitingForPlayer;
});
}
if(a_order_state==Attack_Order_State.PlayerCanAttack)
{
m_unitstats.health -= Attack_Damage_Roller(p_unitstats);
Debug.Log("Player Attack Started");
Battle_Control.Player_Attack(() => {
state = State.WaitingForPlayer;
});
p_Control.Backwards_Movement();//for player to turn back original position
}
state=State.Busy;
}
Turn_Ender();
}
void Turn_Counter()
{
state=State.WaitingForPlayer;
Debug.Log("Turn: "+turnCounter);
turnCounter++;
}
void Turn_Ender()
{
if(Battle_Control.PlayerAttackEnded)
{
Battle_Control.PlayerAttackEnded=false;
Debug.Log("Battle_Control.PlayerAttackEnded");
Turn_Counter();
a_order_state=Attack_Order_State.MonsterCanAttack;
}
if(M_Battle_Control.MonsterAttackEnded)
{
M_Battle_Control.MonsterAttackEnded=false;
Debug.Log("Battle_Control.MonsterAttackEnded");
Turn_Counter();
a_order_state=Attack_Order_State.PlayerCanAttack;
}
}
void Dead_Control()
{
if(m_unitstats.health<=0)
{
m_unitstats.dead=true;
}
if(p_unitstats.health<=0)
{
p_unitstats.dead=true;
}
}
void Game_Checker()
{
Dead_Control();
if(m_unitstats.dead)
{
p_Control.collider_triggered=false;
Debug.Log("collider Triggered : "+p_Control.collider_triggered);
Destroy(instancePlayerwall);
Destroy(instanceMonsterwall);
state=State.Busy;
Random_Spawn_Point_Spawner();
}
if(p_unitstats.dead)
Debug.Log("Game Over Player IS DEAD");
}
void Random_Spawn_Point_Spawner()
{ float handler=7;
float spawn_creater=UnityEngine.Random.Range(handler,handler+10);
Vector3 spawn_vector = new Vector3(spawn_creater,0.07f,0);
Vector3 distance_spawner=new Vector3(3,0,0);
if(handler+10<=40)
{
GameObject instance_encounter_spawner = Instantiate (encounter_spawner, spawn_vector, Quaternion.identity) as GameObject;
GameObject instance_monster_spawn_spawner =Instantiate (monster_spawn_point_spawner, spawn_vector + distance_spawner, Quaternion.identity) as GameObject;
}
handler+=4;
}
float Attack_Damage_Roller(UnitStats unitstats)
{
Roller = UnityEngine.Random.Range(unitstats.attack-5,unitstats.attack+5);
int crit_Roller=UnityEngine.Random.Range(1,6);
if(crit_Roller==6)
{
Roller *= 3;
}
return Roller;
}
}