-
Notifications
You must be signed in to change notification settings - Fork 0
/
FiniStateMachine.cs
229 lines (212 loc) · 6.26 KB
/
FiniStateMachine.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 System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class FiniStateMachine : MonoBehaviour,IFroze,ITornado
{
float flyHeight = 1;
Vector3 vortexPos;
Quaternion vortexRotation;
bool isInTheVortex = false;
public AIState currentState;
AIState pastState;
Dictionary<stateType, AIState> validStates;
NavMeshAgent agent;
KitchenHelper helper;
//internal bool canMove;
private bool isDead;
B_Player player;
Animator anim;
public GameObject pancakePiece;
public GameObject WeaponModel;
float frozingTime = 5;
public bool isfrozen { get; set;}
float invincibleCounter = 5;
[SerializeField]
ParticleSystem effect = null;
bool isInvincible=false;
float vortexTime = 8;
//bool charge = true;
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
anim = GetComponent<Animator>();
agent = GetComponent<NavMeshAgent>();
helper = GetComponent<KitchenHelper>();
player = helper.getPlayer();
WeaponModel.SetActive(false);
EventsInitializing();
STATUS status = helper.status;
validStates = new Dictionary<stateType, AIState>();
validStates.Add(stateType.idle, new IdleSM(this.agent, this, helper, status, stateType.idle, anim));
validStates.Add(stateType.attack, new AttackSM(this.agent, this, helper, status, stateType.attack, anim));
validStates.Add(stateType.defence, new DefenceSM(this.agent, this, helper, status, stateType.defence, anim));
validStates.Add(stateType.cake, new MakeCakeSM(this.agent, this, helper, status, stateType.cake, anim));
currentState = validStates[stateType.defence];
pastState = validStates[stateType.cake];
changeState(stateType.idle);
isfrozen = false;
}
void Update()
{
if (isInTheVortex)
{
vorterxUpdate();
}
if (isDead) return;
if (isfrozen)
{
froze();
return;
}
updateAnimator();
currentState.UPDATING();
if (isInvincible)
{
invincibleCounter -= Time.deltaTime;
if (invincibleCounter <= 0)
{
invincibleCounter = 5;
becomeInvincible(false);
}
}
}
internal void changeState(stateType statetype)
{
pastState = currentState;
currentState.ExitState();
if (validStates.ContainsKey(statetype))
{
currentState = validStates[statetype];
}
else
{
Debug.LogError("don't have this state assigned");
}
if(pastState.stateType != statetype||statetype==stateType.attack)
{
currentState.EnterState();
}
}
void updateAnimator()
{
Vector3 velocity = agent.velocity;
anim.SetFloat(StaticStrings.move, velocity.magnitude);
anim.SetBool(StaticStrings.HAVEMATERIAL, helper.haveMaterial);
pancakePiece.SetActive(helper.haveMaterial);
}
void EventsInitializing()
{
HealthManager health = GetComponent<HealthManager>();
if (health != null)
{
health.onDeath += onDeath;
}
player.onChangeOrder += changeState;
}
public void onDeath(bool value)
{
isDead = value;
Animator anim = GetComponent<Animator>();
anim.SetTrigger(StaticStrings.death);
}
public void WeaponCollider(int v)
{
bool value;
if (v == 1)
{
value = true;
}
else
{
value = false;
}
WeaponModel.gameObject.GetComponentInChildren<Collider>().enabled = value;
}
public string randomAttack()
{
string atk;
int num = UnityEngine.Random.Range(1,helper.status.getCurrentweapon.numOfattack);
switch (num)
{
case 1:
atk = StaticStrings.attack1;
break;
case 2:
atk = StaticStrings.attack2;
break;
case 3:
atk = StaticStrings.attack3;
break;
default:
atk = StaticStrings.attack1;
break;
}
return atk;
}
private void froze()
{
frozingTime -= Time.deltaTime;
agent.SetDestination(transform.position);
if (frozingTime <= 0)
{
isfrozen = false;
frozingTime = 5;
}
}
public void frozing()
{
isfrozen = true;
}
public void becomeInvincible(bool v)
{
if (effect == null) return;
isInvincible = v;
if (v)
effect.Play();
else
effect.Stop();
HealthManager h = GetComponent<HealthManager>();
if (h)
{
h.becameInvincible(v);
}
}
public void Vortex()
{
if (isInTheVortex) return;
isInTheVortex = true;
vortexPos = transform.position;
vortexRotation = transform.rotation;
if (effect != null) effect.Play();
isDead = true;
agent.ResetPath();
rb.isKinematic = false;
rb.useGravity = false;
rb.mass = 0;
rb.constraints = RigidbodyConstraints.None;
rb.AddTorque(Vector3.up, ForceMode.Impulse);
}
void vorterxUpdate()
{
transform.Translate(0, vortexPos.y + flyHeight, 0, Space.World);
flyHeight += Time.deltaTime;
vortexTime -= Time.deltaTime;
if (vortexTime <= 0)
{
isInTheVortex = false;
vortexTime = 8;
rb.isKinematic = true;
rb.useGravity = true;
rb.mass = 1;
rb.constraints = RigidbodyConstraints.FreezeAll;
isDead = false;
transform.rotation = vortexRotation;
transform.position = vortexPos;
if (effect != null) effect.Stop();
flyHeight = 1;
}
}
}