-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStates.cs
749 lines (639 loc) · 17.9 KB
/
States.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public enum EStates
{
ES_Idle = 0,
ES_Flee,
ES_Patrol,
ES_Watch,
ES_Chase,
ES_Dead,
ES_Rescue,
ES_Wander,
ES_Count
}
public class States : ScriptableObject {
protected AI_Agent m_owner;
public AI_Agent Owner
{
get { return m_owner; }
set { m_owner = value; }
}
protected EStates m_stateId;
public EStates StateID
{
get { return m_stateId; }
set { m_stateId = value; }
}
virtual public void Init() { }
virtual public void Shutdown() { }
virtual public void Update() { }
}
public class IdleState : States // IDLE
{
override public void Init()
{
base.Init();
StateID = EStates.ES_Idle;
//Debug.Log(Owner.gameObject.name + " Entered Idle");
}
override public void Update()
{
if (CheckThreats() && TestTransition())
{
GameObject newThreat = Owner.Vision.Threats[0];
if (newThreat)
Owner.Squad.Notify(newThreat);
if (Owner.m_personality == AI_Agent.PersonalityType.PT_Aggressive)
TransitionToChase();
else if (Owner.m_personality == AI_Agent.PersonalityType.PT_Coward)
TransitionToFlee();
else if (Owner.m_personality == AI_Agent.PersonalityType.PT_Careful)
TransitionToWatch();
}
}
bool CheckThreats()
{
bool result = false;
if (Owner.Vision.Threats.Count > 0)
{
return true;
}
return result;
}
public bool TestTransition()
{
if (Owner.Vision.Threats.Count > 0)
{
Rigidbody rb = Owner.Vision.Threats[0].GetComponent<Rigidbody>();
return rb.velocity.sqrMagnitude > 0.0005f;
}
return false;
}
public void TransitionToChase()
{
ChaseState chaseState = CreateInstance<ChaseState>();
chaseState.Owner = Owner;
Owner.FSM.ChangeState(chaseState);
}
public void TransitionToFlee()
{
FleeState fleeState = CreateInstance<FleeState>();
fleeState.Owner = Owner;
Owner.FSM.ChangeState(fleeState);
}
public void TransitionToWatch()
{
WatchState watchState = CreateInstance<WatchState>();
watchState.Owner = Owner;
Owner.FSM.ChangeState(watchState);
}
}
/// end IDLE state
public class WatchState : States // WATCH
{
Vector3 TargetLastPosition = Vector3.zero;
public override void Init()
{
base.Init();
StateID = EStates.ES_Watch;
Owner.SearchLight.gameObject.SetActive(true);
//Debug.Log(Owner.gameObject.name + " Entered Watch");
}
public override void Update()
{
base.Update();
if (Owner.Vision.HasVisualOnTarget())
{
if (!Owner.SearchLight.gameObject.activeInHierarchy)
Owner.SearchLight.gameObject.SetActive(true);
foreach (GameObject obj in Owner.Vision.Threats)
{
/// Aim Heat Ray
Vector3 ThreatPosition = obj.transform.position;
Vector3 LocalPosition = Owner.SearchLight.position;
Owner.SearchLight.GetComponent<LineRenderer>().SetPosition(0, LocalPosition);
Owner.SearchLight.GetComponent<LineRenderer>().SetPosition(1, ThreatPosition);
/// Face to Target
Vector3 FaceDirection = (ThreatPosition - Owner.transform.position).normalized;
Vector3 LookDirection = Vector3.Slerp(Owner.transform.forward, FaceDirection, Time.deltaTime * 0.77f);
Owner.transform.rotation = Quaternion.LookRotation(LookDirection);
Rigidbody rb = Owner.GetComponent<Rigidbody>();
if (rb)
{
rb.angularVelocity *= 0.99f;
}
}
TargetLastPosition = Owner.Vision.Threats[0].transform.position;
}
else/// target is beyond range and angle
{
Owner.SearchLight.gameObject.SetActive(false);
VisuallyInvestigate(TargetLastPosition);
}
}
public void VisuallyInvestigate(Vector3 InterestPoint)
{
Debug.Log(Owner.gameObject.name + " investigating lost target");
Vector3 FaceDirection = (InterestPoint - Owner.transform.position).normalized;
Vector3 LookDirection = Vector3.Slerp(Owner.transform.forward, FaceDirection, Time.deltaTime * 0.77f);
Owner.transform.rotation = Quaternion.LookRotation(LookDirection);
if (Owner.transform.rotation == Quaternion.Euler(LookDirection))
{
if (Owner.Vision.Threats.Count == 0)
{
IdleState idleState = CreateInstance<IdleState>();
idleState.Owner = Owner;
Owner.FSM.ChangeState(idleState);
Debug.Log(Owner.gameObject.name + " went to Idle");
}
}
}
public override void Shutdown()
{
Owner.SearchLight.gameObject.SetActive(false);
}
}
/// end WATCH state
public class FleeState : States // FLEE
{
Rigidbody rb;
GameObject lights;
Navigator navigator;
GameObject RetreatObj;
GameObject P_Terror;
Vector3 RetreatPosition = Vector3.zero;
float DistanceToThreat = 0;
public override void Init()
{
base.Init();
StateID = EStates.ES_Flee;
RetreatObj = Owner.NavPoint;
P_Terror = Owner.TerrorEffect;
P_Terror.SetActive(true);
lights = Owner.EngineLight;
lights.SetActive(true);
rb = Owner.GetComponent<Rigidbody>();
rb.mass = 25;
navigator = Owner.GetComponent<Navigator>();
//Debug.Log(Owner.gameObject.name + " Entered Flee");
}
public override void Update()
{
base.Update();
Light tempLight = lights.GetComponent<Light>();
tempLight.intensity = Random.Range(7, 8);
Vector3 LocalPosition = Owner.transform.position;
if (Owner.Vision.Threats.Count > 0)
{
/// Set Retreat Vector
Vector3 ThreatPosition = Owner.Vision.Threats[0].transform.position;
Vector3 AwayFromThreat = (LocalPosition - ThreatPosition).normalized;
if (RetreatPosition == Vector3.zero)
RetreatPosition = AwayFromThreat * 800;
float DistToRetreat = Vector3.Distance(LocalPosition, RetreatPosition);
DistanceToThreat = Vector3.Distance(ThreatPosition, LocalPosition);
/// Generate Retreat Destination
if (RetreatObj.transform.position == Vector3.zero)
{
RetreatObj = (GameObject)Instantiate(RetreatObj, RetreatPosition, Quaternion.identity);
RetreatObj.transform.position = RetreatPosition;
}
/// Navigate to Destination
if (DistToRetreat > Owner.ApproachRange * 1.5f)
{
navigator.NavigateTo(RetreatObj.transform);
Debug.DrawLine(LocalPosition, RetreatObj.transform.position, Color.yellow);
}
else
{
Owner.Vision.Threats.Clear();
if (Owner.Vision.HasVisualOnTarget() == false)// && DistanceToThreat > Owner.Vision.VisionRange)
{
if (CheckTransitionToWander())
{
TransitionToWander();
}
}
}
}
else if (DistanceToThreat > Owner.Vision.VisionRange)
{
Debug.Log("/////");
if (CheckTransitionToWander())
{
TransitionToWander();
}
}
}
bool CheckTransitionToWander()
{
bool result = false;
if (Owner.Vision.HasVisualOnTarget() == false)
result = true;
return result;
}
void TransitionToWander()
{
WanderState wanderState = CreateInstance<WanderState>();
wanderState.Owner = Owner;
Owner.FSM.ChangeState(wanderState);
}
public override void Shutdown()
{
rb.mass = 30;
lights.SetActive(false);
P_Terror.SetActive(false);
}
}
/// end FLEE state
public class ChaseState : States // CHASE
{
public Rigidbody rb;
public Navigator navi;
public float DragReset;
Transform ChaseTransform;
public override void Init()
{
base.Init();
StateID = EStates.ES_Chase;
rb = Owner.GetComponent<Rigidbody>();
navi = Owner.GetComponent<Navigator>();
DragReset = Owner.ShipDrag;
//Debug.Log(Owner.gameObject.name + " Entered Chase");
}
public override void Update()
{
base.Update();
if (Owner.Vision.Threats.Count > 0)
{
ChaseTransform = Owner.Vision.Threats[0].transform;
float DistanceToTarget = Vector3.Distance(Owner.transform.position, ChaseTransform.position);
if (DistanceToTarget > Owner.ApproachRange)
{
navi.NavigateTo(ChaseTransform);
rb.drag = Owner.ShipDrag;
rb.angularVelocity *= 0.99f;
}
else
{
rb.drag *= 1.0001f;
///Debug.Log("drag: " + rb.drag);
}
}
else { InvestigateAt(ChaseTransform.position); }
}
public void InvestigateAt(Vector3 CheckPosition)
{
float DistanceToTarget = Vector3.Distance(Owner.transform.position, CheckPosition);
if (DistanceToTarget > Owner.ApproachRange)
{
navi.NavigateTo(ChaseTransform);
rb.drag = Owner.ShipDrag;
rb.angularVelocity *= 0.99f;
}
else
{
TransitionToIdle();
}
}
public void TransitionToIdle()
{
Shutdown();
IdleState idleState = CreateInstance<IdleState>();
idleState.Owner = Owner;
Owner.FSM.ChangeState(idleState);
}
public override void Shutdown()
{
rb.drag = Owner.ShipDrag;
}
}
/// end CHASE state
public class PatrolState : States // PATROL
{
int NumPatrolPoints = 4;
int m_patrolIndex = 0;
float PatrolRadius;
Vector3 PatrolOrigin;
List<Transform> patrolPath;
Transform CurrentDestination;
Navigator navigator;
GameObject NavPointPrefab;
override public void Init()
{
base.Init();
StateID = EStates.ES_Patrol;
PatrolOrigin = Owner.PatrolCentre.position;
navigator = Owner.GetComponent<Navigator>();
NavPointPrefab = Owner.NavPoint;
patrolPath = new List<Transform>();
CreatePatrolPath();
//Debug.Log(Owner.gameObject.name + " Entered Patrol at range " + PatrolRadius);
}
public override void Update()
{
base.Update();
/// Visualise patrol plan
foreach (Transform point in patrolPath)
Debug.DrawLine(PatrolOrigin, point.position, Color.green);
/// Patrol
if (CheckDestination())
OnReachDestination();
else
GoToDestination();
/// Transitions
if (CheckTransitionToWander())
TransitionToWander();
if (CheckTransitionToFlee())
TransitionToFlee();
}
void CreatePatrolPath()
{
PatrolRadius = Owner.WanderRange;
float AnglePerPoint = 360.0f / NumPatrolPoints;
Vector3 Radius = PatrolOrigin + Vector3.forward * PatrolRadius;
for (int i = 0; i < NumPatrolPoints; i++)
{
Radius = Quaternion.Euler(0, AnglePerPoint, 0) * Radius;
GameObject newOBJ = Instantiate(NavPointPrefab, Radius, Quaternion.identity) as GameObject;
newOBJ.transform.position = Radius;
Transform newT = newOBJ.transform;
patrolPath.Add(newT);
}
}
bool CheckDestination()
{
bool result = false;
CurrentDestination = patrolPath.ElementAt(m_patrolIndex);
float dist = Vector3.Distance(Owner.transform.position, CurrentDestination.position);
if (dist <= Owner.ApproachRange)
result = true;
return result;
}
void OnReachDestination()
{
m_patrolIndex++;
m_patrolIndex = m_patrolIndex >= patrolPath.Count ? 0 : m_patrolIndex;
}
void GoToDestination()
{
Transform CurrentTransform = patrolPath[m_patrolIndex].transform;
navigator.NavigateTo(CurrentTransform);
///Debug.DrawLine(Owner.transform.position, CurrentTransform.position, Color.white);
}
bool CheckTransitionToWander()
{
bool result = false;
if (Input.GetButtonDown("Submit"))
result = true;
return result;
}
bool CheckTransitionToFlee()
{
bool result = false;
if (Owner.Vision.Threats.Count > 0 || Owner.Vision.HasVisualOnTarget())
{
GameObject newThreat = Owner.Vision.Threats[0];
if (newThreat && Owner.Squad)
Owner.Squad.Notify(newThreat);
result = true;
}
return result;
}
void TransitionToWander()
{
WanderState wanderState = CreateInstance<WanderState>();
wanderState.Owner = Owner;
Owner.FSM.ChangeState(wanderState);
}
void TransitionToFlee()
{
FleeState fleeState = CreateInstance<FleeState>();
fleeState.Owner = Owner;
Owner.FSM.ChangeState(fleeState);
}
}
/// end PATROL state
public class DeadState : States { // DEAD
bool bCallSent = false;
GameObject EngineFX;
public override void Init()
{
EngineFX = Owner.GetComponent<Navigator>().EngineFX;
if (EngineFX)
EngineFX.SetActive(false);
else
Debug.Log("No engine");
base.Init();
StateID = EStates.ES_Dead;
//Debug.Log(Owner.gameObject.name + " entered Death");
}
public override void Update()
{
base.Update();
if (!bCallSent)
DistressCall();
}
public void DistressCall()
{
Vector3 LocalPosition = Owner.transform.position;
Collider[] surrounding = Physics.OverlapSphere(LocalPosition, 5000, -1);
foreach (Collider col in surrounding)
{
if (col.GetComponent<AI_Agent>())
{
RaycastHit hit;
if (Physics.Linecast(LocalPosition, col.transform.position, out hit))
{
AI_Agent Rescuer = col.GetComponent<AI_Agent>();
if (Rescuer.m_personality == AI_Agent.PersonalityType.PT_Aggressive)
{
if (Rescuer.GetCurrentState().StateID != EStates.ES_Dead)
{
RescueState rescueState = CreateInstance<RescueState>();
rescueState.Owner = Rescuer;
rescueState.RescueTransform = Owner.transform;
Rescuer.FSM.ChangeState(rescueState);
Debug.Log(Owner.transform.name + " Sent Distress Call");
if (Rescuer.FSM.GetCurrentState().StateID == EStates.ES_Rescue)
bCallSent = true;
break;
}
}
}
}
}
}///end DistressCall()
public override void Shutdown()
{
//
}
}
/// end DEAD state
public class RescueState : States // RESCUE
{
public Transform RescueTransform;
Rigidbody rb;
Navigator navigator;
public override void Init()
{
base.Init();
StateID = EStates.ES_Rescue;
rb = Owner.GetComponent<Rigidbody>();
rb.drag = Owner.ShipDrag;
navigator = Owner.GetComponent<Navigator>();
//Debug.Log(Owner.gameObject.name + " entered Rescue");
}
public override void Update()
{
base.Update();
if (CheckTransitionToFlee())
TransitionToFlee();
Vector3 RescuePostion = RescueTransform.position;
float DistToTarget = Vector3.Distance(Owner.transform.position, RescuePostion);
if (DistToTarget > Owner.RescueRange)
navigator.NavigateTo(RescueTransform);//Approach(RescuePostion);
else
Revive(RescuePostion);
}
void Revive(Vector3 TargetPositon)
{
Debug.Log(Owner.gameObject.name + " reviving");
/// Decelerate to heal
if (rb)
{
rb.velocity *= 0.975f;
}
/// Heal
Vector3 LocalPosition = Owner.transform.position;
Vector3 ToTarget = TargetPositon - LocalPosition;
Debug.DrawRay(LocalPosition, ToTarget, Color.green);
RaycastHit hit;
if (Physics.Raycast(LocalPosition, ToTarget, out hit))
{
if (hit.transform.GetComponent<Health>())
{
Health otherHealth = hit.transform.GetComponent<Health>();
if (otherHealth.GetHealth() < otherHealth.MaxHealth)
{
otherHealth.Heal(0.01f);
//Debug.Log(hit.transform.name + " health: " + otherHealth.GetHealth());
}
else/// heal complete
{
if (otherHealth.GetComponent<AI_Agent>().GetCurrentState().StateID != EStates.ES_Idle)
{
WanderState wanderState = CreateInstance<WanderState>();
wanderState.Owner = otherHealth.GetComponent<AI_Agent>();
otherHealth.GetComponent<AI_Agent>().FSM.ChangeState(wanderState);
WanderState wander = CreateInstance<WanderState>();
wander.Owner = Owner;
Owner.FSM.ChangeState(wander);
}
}
}
}
}///end Res()
bool CheckTransitionToFlee()
{
bool result = false;
if (Owner.Vision.Threats.Count > 0)
{
result = true;
}
return result;
}
void TransitionToFlee()
{
FleeState fleeState = CreateInstance<FleeState>();
fleeState.Owner = Owner;
Owner.FSM.ChangeState(fleeState);
}
public override void Shutdown()
{
base.Shutdown();
}
}
/// end RESCUE state
public class WanderState : States
{
Rigidbody rb;
Navigator navi;
GameObject CurrentTarget = null;
GameObject NavPointPrefab;
float MaxRange;
bool isMoving;
public override void Init()
{
base.Init();
StateID = EStates.ES_Rescue;
rb = Owner.GetComponent<Rigidbody>();
rb.drag = Owner.ShipDrag;
MaxRange = Owner.WanderRange;
navi = Owner.GetComponent<Navigator>();
NavPointPrefab = Owner.NavPoint;
CurrentTarget = GeneratePoint(MaxRange);
//Debug.Log(Owner.gameObject.name + " entered Wander");
}
public override void Update()
{
base.Update();
if (CheckTransitionToFlee())
{
GameObject newThreat = Owner.Vision.Threats[0];
if (newThreat && Owner.Squad)
Owner.Squad.Notify(newThreat);
TransitionToFlee();
}
Vector3 LocalPosition = Owner.transform.position;
if (CurrentTarget)
{
Vector3 TargetPosition = CurrentTarget.transform.position;
///Debug.DrawLine(LocalPosition, TargetPosition, Color.white);
float DistToTarget = Vector3.Distance(LocalPosition, TargetPosition);
if (DistToTarget >= Owner.ApproachRange)
{
navi.NavigateTo(CurrentTarget.transform);
rb.drag = Owner.ShipDrag;
rb.angularVelocity *= 0.99f;
isMoving = true;
}
else if (isMoving)/// reached destination
{
Destroy(CurrentTarget);
isMoving = false;
CurrentTarget = GeneratePoint(MaxRange);
rb.velocity *= 0.5f;
}
}
else { CurrentTarget = GeneratePoint(MaxRange); }
}
public GameObject GeneratePoint(float range)
{
Vector3 PointPosition = Vector3.zero + Random.insideUnitSphere * range;
PointPosition.y *= 0.66f;/// kern vertical pos for "gameplay" lol
GameObject result = (GameObject)Instantiate(NavPointPrefab, PointPosition, Quaternion.identity);
return result;
}
bool CheckTransitionToFlee()
{
bool result = false;
if (Owner.Vision.Threats.Count > 0)
result = true;
return result;
}
void TransitionToFlee()
{
FleeState fleeState = CreateInstance<FleeState>();
fleeState.Owner = Owner;
Owner.FSM.ChangeState(fleeState);
}
public override void Shutdown()
{
base.Shutdown();
}
}
/// end WANDER state