-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameDirector.cs
167 lines (134 loc) · 4.56 KB
/
GameDirector.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameDirector_TankData
{
public TankController tank;
public bool turn;
}
public class GameDirector : MonoBehaviour {
public int tankCount = 2;
public readonly int spawnCount = 12;
public GameDirector_TankData[] tankList;
public GameObject[] spawnList;
public static GameDirector instance;
// Use this for initialization
void Start () {
StartCoroutine(GameRules());
tankList = new GameDirector_TankData[tankCount];
for (int i = 0; i < tankCount; i++) tankList[i] = new GameDirector_TankData();
spawnList = new GameObject[spawnCount];
for (int i = 0; i < spawnCount; i++) spawnList[i] = GameObject.Find("SpawnPoint" + i);
for (int i = 0; i < tankCount; i++)
{
GameObject go = Instantiate(Resources.Load<GameObject>("Prefabs/tank"), spawnList[Random.Range(0, spawnCount)].transform.position, new Quaternion());
if (go == null)
{
Debug.LogError("Can't find tank. check tank prefabs");
continue;
}
go.name = "tank" + i;
}
instance = this;
}
IEnumerator GameRules()
{
yield return new WaitForSeconds(1);
TankController leastDelayedTank = null;
int leastDelayCount = int.MaxValue;
for(int i = 0; i < tankCount; i++)
{
GameObject go = GameObject.Find("tank" + i);
if(go == null)
{
Debug.LogError("Can't find tank. check tank is valid");
continue;
}
TankController tank = go.GetComponent<TankController>();
tankList[i].tank = tank;
tank.ShowMe();
int delay = tank.tankData.m_iDelay;
if(delay < leastDelayCount)
{
leastDelayCount = delay;
leastDelayedTank = tank;
}
yield return new WaitForSeconds(1);
}
TurnTank(leastDelayedTank);
}
public void TurnTank(TankController turn)
{
for (int i = 0; i < tankCount; i++)
{
GameObject go = GameObject.Find("tank" + i);
if (go == null)
continue;
TankController tank = go.GetComponent<TankController>();
if(turn == tank)
{
tank.HasTurn();
tankList[i].turn = true;
}
}
}
// Update is called once per frame
void Update () {
}
public static TankController GetNextTank(bool forceTurn = true)
{
TankController leastDelayedTank = null;
int leastDelayCount = int.MaxValue, lastIndex = 0, remain_turn = 0, alive_count = 0;
for (int i = 0; i < instance.tankCount; i++)
{
GameObject go = GameObject.Find("tank" + i);
if (go == null)
{
Debug.LogError("Can't find tank. check tank name");
continue;
}
TankController tank = go.GetComponent<TankController>();
if (tank.tankData.m_isDeadTank)
continue;
alive_count++;
if (instance.tankList[i].turn)
continue;
int delay = tank.tankData.m_iDelay;
if (delay < leastDelayCount)
{
leastDelayCount = delay;
leastDelayedTank = tank;
lastIndex = i;
}
remain_turn++;
}
if(alive_count <= 1)
{
// Game End
return null;
}
// all player had turn.
if (remain_turn <= 1)
{
for (int i = 0; i < instance.tankCount; i++)
{
GameObject go = GameObject.Find("tank" + i);
if (go == null)
{
continue;
}
TankController tank = go.GetComponent<TankController>();
// 죽은 탱크는 배제한다.
if (tank.tankData.m_isDeadTank)
continue;
instance.tankList[i].turn = false;
}
}
if (remain_turn == 0 && alive_count > 0)
{
return GetNextTank(forceTurn);
}
instance.tankList[lastIndex].turn = forceTurn;
return leastDelayedTank;
}
}