-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTurret.cs
More file actions
163 lines (136 loc) · 4.55 KB
/
Turret.cs
File metadata and controls
163 lines (136 loc) · 4.55 KB
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Collections.LowLevel.Unsafe;
using Sirenix.OdinInspector;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace _Game
{
public class Turret : MonoBehaviour
{
public event System.Action<Turret, ICharacter> OnFireAtTarget;
public Transform ProjectileSpawnPoint;
public GameObject ProjectilePrefab;
[Range(0, 180f)]
[SerializeField] float Angle = 45f;
[SerializeField] float MaxTurnSpeed = 90f;
[SerializeField] float CooldownSecs = 3f;
[SerializeField] float MaxRange = 200f;
[ShowInInspector] [ReadOnly] public ICharacter Target { get; private set; } = null;
List<Hated> Hatelist = new List<Hated>();
void OnEnable()
{
StartCoroutine(AcquireTargetCo());
}
void Update()
{
if (Target != null && Target.NetworkObject == null)
Target = null;
if (Target != null)
Aim(Target.NetworkObject.transform.position);
}
public void SetHatelist(List<Hated> hl) {
Hatelist = hl;
}
public void ClearTarget() {
Target = null;
}
float CooldownTime = 0f;
IEnumerator AcquireTargetCo()
{
while(Application.isPlaying)
{
List<Hated> removeList = new List<Hated>();
Hated nearestTarget = null;
float nearestDistance = float.MaxValue;
foreach(var hated in Hatelist)
{
if (hated.Target == null || hated.Target.transform == null || !hated.Target.IsAlive)
{
Hatelist.Remove(hated);
break;
}
var distance = Vector3.Distance(hated.Target.transform.position, transform.position);
if (distance <= nearestDistance)
{
nearestDistance = distance;
nearestTarget = hated;
}
}
if (nearestTarget != null)
Target = nearestTarget.Target;
else
Target = null;
//if (Target != null)
//{
// if (Aim(Target.NetworkObject.transform.position))
// {
// if (GameManager.IsServer)
// OnFireAtTarget?.Invoke(Target);
// yield return new WaitForSecondsRealtime(CooldownTime);
// break;
// }
//}
yield return new WaitForSecondsRealtime(1f);
}
}
bool Aim(Vector3 targetPoint)
{
if (Vector3.Distance(targetPoint, transform.position) > MaxRange)
return false;
var turret = transform;
var hardpoint = turret.parent;
var targetDirection = targetPoint - turret.position;
targetDirection = Vector3.ProjectOnPlane(targetDirection, hardpoint.up);
var signedAngle = Vector3.SignedAngle(hardpoint.forward, targetDirection, hardpoint.up);
bool outOfRange = false;
if (Mathf.Abs(signedAngle) > Angle)
{
outOfRange = true;
targetDirection = hardpoint.rotation * Quaternion.Euler(0, Mathf.Clamp(signedAngle, -Angle, Angle), 0) * Vector3.forward;
}
var targetRotation = Quaternion.LookRotation(targetDirection, hardpoint.up);
//bool aimed = false;
//if (Quaternion.Angle(targetRotation, transform.rotation) <= 45f && !outOfRange)
//aimed = true;
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, MaxTurnSpeed * Time.deltaTime);
if (!outOfRange && CooldownTime <= Time.time)
{
CooldownTime = Time.time + CooldownSecs;
if (GameManager.IsServer)
OnFireAtTarget?.Invoke(this, Target);
}
return !outOfRange;
}
void OnDrawGizmos()
{
#if UNITY_EDITOR
var range = MaxRange;
var dashLineSize = 2f;
var turret = transform;
var origin = turret.position;
var hardpoint = turret.parent;
if (!hardpoint) return;
var from = Quaternion.AngleAxis(-Angle, hardpoint.up) * hardpoint.forward;
Handles.color = new Color(0, 1, 0, .2f);
Handles.DrawSolidArc(origin, turret.up, from, Angle * 2, range);
if (Target == null) return;
var projection = Vector3.ProjectOnPlane(Target.NetworkObject.transform.position - turret.position, hardpoint.up);
// projection line
Handles.color = Color.white;
Handles.DrawDottedLine(Target.NetworkObject.transform.position, turret.position + projection, dashLineSize);
// do not draw target indicator when out of angle
if (Vector3.Angle(hardpoint.forward, projection) > Angle) return;
// target line
Handles.color = Color.red;
Handles.DrawLine(turret.position, turret.position + projection);
// range line
Handles.color = Color.green;
Handles.DrawWireArc(origin, turret.up, from, Angle * 2, projection.magnitude);
Handles.DrawSolidDisc(turret.position + projection, turret.up, .5f);
#endif
}
}
}