-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemy.cs
115 lines (86 loc) · 2.94 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent (typeof (UnityEngine.AI.NavMeshAgent))]
public class Enemy : LivingEntity {
public enum State {Idle, Chasing, Attacking};
State currentState;
UnityEngine.AI.NavMeshAgent pathfinder;
Transform target;
LivingEntity targetEntity;
Material skinMaterial;
Color originalColour;
float attackDistanceThreshold = .5f;
float timeBetweenAttacks = 1;
float damage = 1;
float nextAttackTime;
float myCollisionRadius;
float targetCollisionRadius;
bool hasTarget;
protected override void Start () {
base.Start ();
pathfinder = GetComponent<UnityEngine.AI.NavMeshAgent> ();
skinMaterial = GetComponent<Renderer> ().material;
originalColour = skinMaterial.color;
if (GameObject.FindGameObjectWithTag ("Player") != null) {
currentState = State.Chasing;
hasTarget = true;
target = GameObject.FindGameObjectWithTag ("Player").transform;
targetEntity = target.GetComponent<LivingEntity> ();
targetEntity.OnDeath += OnTargetDeath;
StartCoroutine (UpdatePath ());
}
}
void OnTargetDeath() {
hasTarget = false;
currentState = State.Idle;
}
void Update () {
if (hasTarget) {
if (Time.time > nextAttackTime) {
float sqrDstToTarget = (target.position - transform.position).sqrMagnitude;
if (sqrDstToTarget < Mathf.Pow (attackDistanceThreshold + myCollisionRadius + targetCollisionRadius, 2)) {
nextAttackTime = Time.time + timeBetweenAttacks;
StartCoroutine (Attack ());
}
}
}
}
IEnumerator Attack() {
currentState = State.Attacking;
pathfinder.enabled = false;
Vector3 originalPosition = transform.position;
Vector3 dirToTarget = (target.position - transform.position).normalized;
Vector3 attackPosition = target.position - dirToTarget * (myCollisionRadius);
float attackSpeed = 3;
float percent = 0;
skinMaterial.color = Color.red;
bool hasAppliedDamage = false;
while (percent <= 1) {
if (percent >= .5f && !hasAppliedDamage) {
hasAppliedDamage = true;
targetEntity.TakeDamage(damage);
}
percent += Time.deltaTime * attackSpeed;
float interpolation = (-Mathf.Pow(percent,2) + percent) * 4;
transform.position = Vector3.Lerp(originalPosition, attackPosition, interpolation);
yield return null;
}
skinMaterial.color = originalColour;
currentState = State.Chasing;
pathfinder.enabled = true;
}
IEnumerator UpdatePath() {
float refreshRate = .25f;
while (hasTarget) {
if (currentState == State.Chasing) {
Vector3 dirToTarget = (target.position - transform.position).normalized;
Vector3 targetPosition = target.position - dirToTarget * (myCollisionRadius + targetCollisionRadius + attackDistanceThreshold/2);
if (!dead) {
pathfinder.SetDestination (targetPosition);
}
}
yield return new WaitForSeconds(refreshRate);
}
}
}