-
Notifications
You must be signed in to change notification settings - Fork 0
/
GroundAI.cs
120 lines (102 loc) · 5.67 KB
/
GroundAI.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class GroundAI : AbstractAI {
public Transform body;
NavMeshAgent agent;
public override void Start() {
agent = GetComponent<NavMeshAgent>();
base.Start();
}
protected override void SwitchMode(BehaviorType nextMode) {
mode = nextMode;
AbstractBehavior b = CurBehavior();
agent.speed = b.data.moveSpeed;
agent.angularSpeed = b.data.turnSpeed;
agent.acceleration = b.data.acceleration;
}
public override void Alert(Vector3 direction, bool origin, Vector3 point, AbstractAI alerter) {
if (health == null || health.IsDead() || alerter == null) return;
if (mode == BehaviorType.patrol) SwitchMode(BehaviorType.alert);
if (origin) {
RaycastHit hit;
if (Physics.Raycast(transform.position, direction, out hit, 100, ~ManageFactions.instance.deployableShield, QueryTriggerInteraction.Ignore)) {
Vector3 investigatePoint = hit.point - direction * investigateDistance;
if (Physics.Raycast(investigatePoint, Vector3.down, out hit, CurBehavior().data.hoverHeight.x, ~ManageFactions.instance.deployableShield, QueryTriggerInteraction.Ignore)) {
CurBehavior().Alert(direction, origin, hit.point + Vector3.up * CurBehavior().data.hoverHeight.x, alerter);
Debug.DrawLine(transform.position, hit.point + Vector3.up * CurBehavior().data.hoverHeight.x, Color.cyan);
}
else {
CurBehavior().Alert(direction, origin, investigatePoint, alerter);
}
foreach (Transform unit in ManageFactions.instance.GetAllyFactionList(health.faction)) {
if (unit != null && unit != transform && Vector3.Distance(transform.position, unit.transform.position) < CurBehavior().data.searchRadius) unit.GetComponent<AbstractAI>().Alert(direction, false, investigatePoint, alerter);
}
}
}
else if (Random.Range(0f, 1f) < investigateChance) {
CurBehavior().Alert(direction, origin, point, alerter);
}
}
public override void Alerted(Vector3 direction) {
if (health.IsDead()) return;
Alert(direction, true, Vector3.zero, this);
}
public override AbstractBehavior CurBehavior() {
switch (mode) {
case BehaviorType.patrol: return behaviorList[0];
case BehaviorType.alert: return behaviorList[1];
case BehaviorType.combat: return behaviorList[2];
default: return behaviorList[0];
}
}
public override Transform GetAttackTarget() {
return attackTarget;
}
protected override void Attack() {
if (weaponTimer <= 0 && weapons[curWeapon].Ready() && Vector3.Distance(transform.position, attackTarget.position) < weapons[curWeapon].data.effectiveRange) {
StartCoroutine(weapons[curWeapon].Fire(true));
weaponTimer = weapons[curWeapon].FireRate * delayMult;
burst += weaponTimer;
if (burst >= burstTime) {
weaponTimer = weapons[curWeapon].FireRate * burstMult;
burst = 0;
}
}
if (weapons[curWeapon].IsReloading() && reloadTimer == 0) {
reloadTimer = weapons[curWeapon].data.reload * reloadMult;
}
}
protected override void LookAtTarget(Vector3 look) {
if (look == Vector3.zero) return;
look.Normalize();
if (mode == BehaviorType.patrol) {
body.localRotation = Quaternion.RotateTowards(body.localRotation, Quaternion.identity, (CurBehavior().data.turnSpeed) * Time.deltaTime);
Vector3 gunLook = Vector3.up * -.3f + Vector3.forward * (.7f);
Debug.DrawRay(gunArm.position, look.normalized * 2, Color.magenta);
gunArm.localRotation = Quaternion.RotateTowards(gunArm.localRotation, Quaternion.LookRotation(gunLook), (CurBehavior().data.turnSpeed) * Time.deltaTime);
}
else {
Vector3 bodyLook = new Vector3(look.x, 0, look.z).normalized;
body.rotation = Quaternion.RotateTowards(body.rotation, Quaternion.LookRotation(bodyLook), (CurBehavior().data.turnSpeed) * Time.deltaTime);
Debug.DrawRay(gunArm.position, look * 20, Color.magenta);
gunArm.rotation = Quaternion.RotateTowards(gunArm.rotation, Quaternion.LookRotation(look), (CurBehavior().data.turnSpeed) * Time.deltaTime);
//Vector3 gunLook = Vector3.up * look.y + Vector3.forward * (1 - look.y);
//gunArm.localRotation = Quaternion.RotateTowards(gunArm.localRotation, Quaternion.LookRotation(gunLook), (CurBehavior().data.turnSpeed) * Time.deltaTime);
}
}
protected override void MoveToTarget(Vector3 moveTo) {
foreach (Transform unit in ManageFactions.instance.GetAllyFactionList(health.faction)) {
if (unit != null && unit != transform && Vector3.Distance(transform.position, unit.position) < repulsionDistance) {
Vector3 rep = (transform.position - unit.position).normalized * (1 - Vector3.Distance(transform.position, unit.position) / repulsionDistance) * repulsionForce;
Debug.DrawRay(transform.position, rep, Color.cyan);
moveTo += rep;
}
}
agent.SetDestination(moveTo);
}
protected override void StopMove() {
rig.velocity = Vector3.Lerp(rig.velocity, Vector3.zero, CurBehavior().data.acceleration * Time.deltaTime);
}
}