-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemy.cs
156 lines (124 loc) · 3.63 KB
/
Enemy.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
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(NavMeshAgent))]
public class Enemy : LivingEntity
{
public enum State { Idle, Chasing, Attacking }
State currentState;
public ParticleSystem deathEffect;
NavMeshAgent pathfinder;
Transform target;
LivingEntity targetEntity;
Material skinMaterial;
Color originalColor;
float attackDistanceThreshold = 0.5f;
float timeBetweenAttacks = 1;
float damage = 1;
float nextAttackTime;
float myCollisionRadius;
float targetCollisionRadius;
bool hasTarget;
void Awake()
{
pathfinder = GetComponent<NavMeshAgent>();
if (GameObject.FindGameObjectWithTag("Player") != null)
{
hasTarget = true;
target = GameObject.FindGameObjectWithTag("Player").transform;
targetEntity = target.GetComponent<LivingEntity>();
myCollisionRadius = GetComponent<CapsuleCollider>().radius;
targetCollisionRadius = target.GetComponent<CapsuleCollider>().radius;
}
}
protected override void Start()
{
base.Start();
if (hasTarget)
{
currentState = State.Chasing;
targetEntity.OnDeath += OnTargetDeath;
StartCoroutine(UpdatePath());
}
}
public void SetCharacteristics(float moveSpeed, int hitsToKillPlayer, float enemyHealth, Color skinColor)
{
pathfinder.speed = moveSpeed;
if (hasTarget)
{
damage = Mathf.Ceil(targetEntity.startingHealth / hitsToKillPlayer);
}
startingHealth = enemyHealth;
skinMaterial = GetComponent<Renderer>().material;
skinMaterial.color = skinColor;
originalColor = skinMaterial.color;
}
public override void TakeHit(float damage, Vector3 hitPoint, Vector3 hitDirection)
{
if (damage >= health)
{
Destroy(Instantiate(deathEffect.gameObject, hitPoint, Quaternion.FromToRotation(Vector3.forward, hitDirection)) as GameObject, deathEffect.startLifetime);
}
base.TakeHit(damage, hitPoint, hitDirection);
}
void OnTargetDeath()
{
hasTarget = false;
currentState = State.Idle;
}
void Update ()
{
if (hasTarget)
{
if (Time.time > nextAttackTime)
{
var sqrDistToTarget = (target.position - transform.position).sqrMagnitude;
if (sqrDistToTarget < Mathf.Pow(attackDistanceThreshold + myCollisionRadius + targetCollisionRadius, 2))
{
nextAttackTime = Time.time + timeBetweenAttacks;
StartCoroutine(Attack());
}
}
}
}
IEnumerator Attack()
{
currentState = State.Attacking;
pathfinder.enabled = false;
var originalPosition = transform.position;
var dirToTarget = (target.position - transform.position).normalized;
var attackPosition = target.position - dirToTarget * (myCollisionRadius);
var attackSpeed = 3f;
var percent = 0f;
skinMaterial.color = Color.red;
bool hasAppliedDamage = false;
while (percent <= 1)
{
if (percent >= 0.5f && !hasAppliedDamage)
{
hasAppliedDamage = true;
targetEntity.TakeDamage(damage);
}
percent += Time.deltaTime * attackSpeed;
var interpolation = (-Mathf.Pow(percent, 2) + percent) * 4;
transform.position = Vector3.Lerp(originalPosition, attackPosition, interpolation);
yield return null;
}
skinMaterial.color = originalColor;
currentState = State.Chasing;
pathfinder.enabled = true;
}
IEnumerator UpdatePath()
{
float refreshRate = 0.25f;
while (hasTarget)
{
if (currentState == State.Chasing)
{
var dirToTarget = (target.position - transform.position).normalized;
var targetPosition = target.position - dirToTarget * (myCollisionRadius + targetCollisionRadius + attackDistanceThreshold / 2);
if (!isDead) pathfinder.SetDestination(targetPosition);
}
yield return new WaitForSeconds(refreshRate);
}
}
}