-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAIplayer.cs
172 lines (156 loc) · 5.46 KB
/
AIplayer.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
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
public class AIplayer : MonoBehaviour
{
public SpawnControl spawnControl;
// mana related variables
[SerializeField]
private int currentMana;
[SerializeField]
private int manaAmount;
[SerializeField]
private float manaRegenTime;
// array of total creature types of hostile side
[SerializeField]
private DefaultCreature[] creatureArray;
// array of spawnable number of creature
[SerializeField]
private int[] creatureFlag = new int[PublicLevel.hostileTypeCreatureNum];
// array of ratio of creature
[SerializeField]
private float[] creatureRatio = new float[PublicLevel.hostileTypeCreatureNum];
// array of spawnable creature types
[SerializeField]
private GameObject[] hostileCreatureList;
// time interval between creature spawns
[SerializeField]
private float creatureSpawnTime;
// minimum cost of spawnable minions
private int minimumCost;
// Overall Initialization of AIPlayer in the scene, called in GameControl
public void AIplayerStart()
{
creatureArray = new DefaultCreature[PublicLevel.hostileTypeCreatureNum];
InitAI();
InvokeRepeating("GainMana", manaRegenTime, manaRegenTime);
InvokeRepeating("ChooseCreature", creatureSpawnTime, creatureSpawnTime);
}
// Stop spawn and mana gain of aiplayer
public void AIplayerStop()
{
CancelInvoke("GainMana");
CancelInvoke("ChooseCreature");
}
// Initialization of variables and data structures in AIPlayer
void InitAI()
{
currentMana = 0;
// initialize mana variables according to PublicLevel
this.manaAmount = PublicLevel.GetManaAmount();
this.manaRegenTime = PublicLevel.GetManaRegenTime();
this.creatureSpawnTime = PublicLevel.GetCreatureSpawnTime();
hostileCreatureList = new GameObject[PublicLevel.hostileTypeCreatureNum];
PublicLevel.GetHostileCreatureList(hostileCreatureList);
// deciding how much creatures will be spawned, and its composition
SetCreatureArray();
minimumCost = GetMinimumCost();
SetCreatureRatio();
SetCreatureFlag();
}
// choosing next creature to spawn, regarding flags and random generator
void ChooseCreature()
{
int currentIndex;
int currentLane;
if (minimumCost > currentMana)
{
//when none of the unit is available
return;
}
while (true)
{
currentIndex = UnityEngine.Random.Range(1, 5);
if (creatureFlag[currentIndex] == 0)
{
// when chosen unit was spawned too much
continue;
}
else
{
// choose random lane and spawn the creature
currentLane = UnityEngine.Random.Range(0, 3);
UseMana(creatureArray[currentIndex].GetManaCost());
creatureFlag[currentIndex]--;
spawnControl.SummonCreature(currentLane, GameControl.Sides.Hostile, currentIndex);
break;
}
}
}
// decide how frequently a creature is going to be spawned
void SetCreatureRatio()
{
float totalRatio = 0.0f;
for (int i = 0; i < 5; ++i)
{
// maximum number of creatures spawn possible, regarding managain rate
creatureRatio[i] = (float)Math.Round( (manaAmount / manaRegenTime) / (creatureArray[i].GetManaCost() / creatureSpawnTime), 2 );
totalRatio += creatureRatio[i];
}
for (int i = 0; i < 5; ++i)
{
// ratio of creature spawn, relatively
creatureRatio[i] = (float)Math.Round( creatureRatio[i] / totalRatio, 2);
}
}
// decide the number of creatures spawn possible in certain period
void SetCreatureFlag()
{
for (int i = 0; i < 5; ++i)
{
// multiply ratio of creature to get maximum number of creatures spawned in period
creatureFlag[i] = Mathf.RoundToInt( creatureRatio[i] * manaRegenTime / creatureSpawnTime ) ;
if(creatureFlag[i] == 0)
{
// expensive units can still be spawned
creatureFlag[i] = 1;
}
}
}
// increase currentMana
void GainMana()
{
currentMana += manaAmount;
SetCreatureFlag();
}
// decrease currentMana
void UseMana(int currentManaCost)
{
currentMana -= currentManaCost;
}
// creature array will use only a part of total type of creatures
void SetCreatureArray()
{
for (int i = 0; i < PublicLevel.hostileTypeCreatureNum; ++i)
{
creatureArray[i] = hostileCreatureList[i].GetComponent<DefaultCreature>();
}
}
// get lowest cost of spawnable creatures
int GetMinimumCost()
{
minimumCost = int.MaxValue;
int currentCost = 0;
for(int i = 0; i < 5; ++i)
{
currentCost = creatureArray[i].GetManaCost();
if (minimumCost > currentCost)
{
//when new minimum cost is found
minimumCost = currentCost;
}
}
return minimumCost;
}
}