/
ProtectorCombat.cs
169 lines (159 loc) · 7.36 KB
/
ProtectorCombat.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProtectorCombat : AbstractBehavior {
public float combatDistance;
public float evadeTime;
public float evadeDistance;
public float protectDistance;
public GameObject shield;
public GameObject indicator;
public float deployDistance;
public float deployRechargeTime;
Transform attackTarget;
Transform protectTarget;
float combatHeight;
//bool hasView;
float underFireTimer;
Vector3 evadeDirection;
float protectStartDistance;
Transform deployedShield;
LineRenderer deployedIndicator;
float rechargeTimer;
public ProtectorCombat(ProtectorBehaviorData data, Transform transform) {
this.data = data;
this.transform = transform;
combatDistance = data.f1;
evadeTime = data.f2;
evadeDistance = data.f3;
protectDistance = data.f4;
deployDistance = data.f5;
deployRechargeTime = data.f6;
shield = data.o1;
indicator = data.o2;
}
public override bool Step() {
if (underFireTimer > 0) underFireTimer -= Time.deltaTime;
if (rechargeTimer > 0) rechargeTimer -= Time.deltaTime;
if (deployedShield != null) {
if (protectTarget == null || protectTarget.GetComponent<Health>().IsDead()) {
GameObject.Destroy(deployedShield.gameObject);
GameObject.Destroy(deployedIndicator.gameObject);
deployedShield = null;
protectTarget = null;
return false;
}
deployedShield.position = protectTarget.position + protectTarget.forward * deployDistance;
deployedShield.rotation = protectTarget.rotation;
deployedIndicator.SetPosition(0, transform.position);
deployedIndicator.SetPosition(1, protectTarget.position);
}
else if (protectTarget != null && deployedShield == null) {
protectTarget = null;
rechargeTimer = deployRechargeTime;
GameObject.Destroy(deployedIndicator.gameObject);
return false;
}
return true;
}
public override Transform Search(ManageFactions.Faction faction, Vector3 head) {
RaycastHit hit;
if (attackTarget != null) {
Debug.DrawRay(head, attackTarget.position - transform.position);
if (Physics.Raycast(head, attackTarget.position - transform.position, out hit, 100, ~ManageFactions.instance.deployableShield, QueryTriggerInteraction.Ignore)) {
if (hit.transform == attackTarget) {
//hasView = true;
return attackTarget;
}
}
//hasView = false;
return null;
}
if (protectTarget != null) {
Debug.DrawRay(head, protectTarget.position - transform.position);
if (Physics.Raycast(head, protectTarget.position - transform.position, out hit, protectDistance, ~ManageFactions.instance.deployableShield, QueryTriggerInteraction.Ignore)) {
if (hit.transform == protectTarget) {
if (hit.transform.GetComponent<Health>().IsDead()) {
protectTarget = null;
//hasView = false;
return null;
}
//hasView = true;
return protectTarget;
}
}
//hasView = false;
return protectTarget;
}
return null;
}
public override Vector3 GetMove() {
if (attackTarget != null) {
Vector3 lookDir = (attackTarget.position - transform.position).normalized;
Vector3 h = (-(new Vector3(lookDir.x, 0, lookDir.z)).normalized * combatDistance + Vector3.up * combatHeight).normalized * combatDistance;
Vector3 evade = underFireTimer > 0 ? evadeDirection : Vector3.zero;
Debug.DrawLine(transform.position, attackTarget.position + h + evade, Color.red);
return attackTarget.position + h + evade;
}
else if (protectTarget != null) {
Vector3 lookDir = (protectTarget.position - transform.position).normalized;
Vector3 h = -(new Vector3(lookDir.x, 0, lookDir.z)).normalized * protectStartDistance;
Debug.DrawLine(transform.position, protectTarget.position + h, Color.blue);
return protectTarget.position + h;
}
else {
return transform.position;
}
}
public override Vector3 GetLook() {
if (attackTarget != null) {
return attackTarget.position - transform.position;
}
if (protectTarget != null) {
return protectTarget.position - transform.position;
}
return transform.position;
}
public override void Alert(Vector3 direction, bool origin, Vector3 point, AbstractAI alerter) {
if (origin) {
if (underFireTimer <= 0) {
underFireTimer = evadeTime;
evadeDirection = transform.up * data.hoverDelta * (Random.Range(0f, 1f) < .5f ? -1 : 1) + transform.right * evadeDistance * (Random.Range(0f, 1f) < .5f ? -1 : 1) - transform.forward * evadeDistance;
}
}
else {
if (protectTarget == null) {
foreach (Transform drone in ManageFactions.instance.GetAllyFactionList(transform.GetComponent<Health>().faction)) {
if (drone.GetComponent<DroneAI>().GetAttackTarget() == alerter.transform) {
if (underFireTimer <= 0) {
underFireTimer = evadeTime;
evadeDirection = transform.up * data.hoverDelta * (Random.Range(0f, 1f) < .5f ? -1 : 1) + transform.right * evadeDistance * (Random.Range(0f, 1f) < .5f ? -1 : 1) - transform.forward * evadeDistance;
}
return;
}
}
SetAttackTarget(alerter.transform);
}
}
}
public override void SetAttackTarget(Transform target) {
if (target.GetComponent<Health>().faction == transform.GetComponent<Health>().faction) {
if (deployedShield == null && target != transform && rechargeTimer <= 0) {
protectTarget = target;
combatHeight = 0;
protectStartDistance = Vector3.Distance(target.position, transform.position);
attackTarget = null;
deployedShield = GameObject.Instantiate(shield, target.position + target.forward * deployDistance, target.rotation).transform;
deployedShield.GetComponent<DeployableShield>().SetCaster(transform.GetComponent<Health>());
deployedIndicator = GameObject.Instantiate(indicator, transform.position, Quaternion.identity, transform).GetComponent<LineRenderer>();
deployedIndicator.SetPosition(0, transform.position);
deployedIndicator.SetPosition(1, target.position);
}
}
else {
attackTarget = target;
combatHeight = Random.Range(data.hoverHeight.x, data.hoverHeight.y);
protectTarget = null;
}
}
}