-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOgre.cs
188 lines (157 loc) · 4.73 KB
/
Ogre.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using UnityEngine;
using System.Collections;
public class Ogre : MonoBehaviour {
//state
public float health = 100;
public aiState ogreState;
private aiState prevState;
public float patrolingTimer;
public bool bAlive = true;
//settings
public float walkingSpeed = 10;
public float visionArea = 30;
public float attackDamage = 20;
public float attackRange = 5;
public bool bCanDoRangeAttack = false;
public float meleAttackRange = 2;
public float attackSpeed = 1;
public float calmAwarenes = 3;
public float attackedAwareness = 14;
public float seenAwareness = 7;
public float patrolStopTime = 4;
public Vector3[] patrolPoints = new Vector3[2];
public int curPatrolignPoint;
//other stuff
public bool bReachedDestination = true;
//druid info
public Vector3 directionToDruid;
public Vector3 lastSeenPosition;
public float distanceToDruid;
public bool bDruidVisible;
public bool bDruidAttackable;
public float awarenesLevel;
//reset stuff
public Vector3 startingLocation;
//references
private Animator anim;
public enum aiState {
patrolling,
aware,
attacking,
chasing
}
private void Start() {
patrolingTimer = patrolStopTime;
for(int i = 0; i<transform.childCount; i++) {
if(transform.GetChild(i).tag == Tags.PatrolPoint) patrolPoints[i] = transform.GetChild(i).position;
transform.GetChild(i).gameObject.SetActive(false);
}
}
private void Update() {
if(!bAlive) return;
if(anim==null) anim = GetComponent<Animator>();
if(Druid.instance.curShape != shapeshiftStates.stone) {
directionToDruid = (Druid.instance.transform.position-transform.position).normalized;
distanceToDruid = Vector3.Distance(transform.position, Druid.instance.transform.position);
if((distanceToDruid<=attackRange && bCanDoRangeAttack) || (distanceToDruid<=meleAttackRange)) {
bDruidAttackable = true;
//ogreState = aiState.attacking;
}
else bDruidAttackable = false;
if(distanceToDruid<=visionArea) {
Vector3 direction = Druid.instance.transform.position - transform.position;
RaycastHit hit;
Ray r = new Ray(transform.position + transform.up,direction.normalized);
// ... and if a raycast towards the player hits something...
if(Physics.Raycast(r, out hit, visionArea))
{
// ... and if the raycast hits the player...
if(hit.collider.gameObject.tag == Tags.Druid)
{
Debug.DrawLine (transform.position, hit.point, Color.cyan);
}
}
bDruidVisible = true;
//ogreState = aiState.chasing;
lastSeenPosition = Druid.instance.transform.position;
}
}
else {
bDruidVisible = false;
bDruidAttackable = false;
}
UpdateAI();
}
private void UpdateAI() {
if(ogreState!=prevState) {
patrolingTimer = patrolStopTime;
StopAllCoroutines();
bReachedDestination = true;
Debug.Log("State changed to "+ogreState);
}
switch (ogreState) {
case aiState.patrolling:
if(patrolingTimer>patrolStopTime) {
//go to next point;
StartCoroutine(GotoPoint(NextPatrolingPoint()));
patrolingTimer = 0;
}
else if(bReachedDestination){
patrolingTimer+=Time.deltaTime;
}
if(bDruidVisible && !bDruidAttackable) ogreState = aiState.chasing;
else if(bDruidAttackable) ogreState = aiState.attacking;
break;
case aiState.chasing:
if(bReachedDestination) {
Debug.Log("SHUFFELING!");
StartCoroutine(GotoPoint(lastSeenPosition));
}
awarenesLevel = seenAwareness;
if(!bDruidVisible) ogreState = aiState.aware;
if(bDruidAttackable) ogreState = aiState.attacking;
break;
case aiState.attacking:
Attack();
awarenesLevel = attackedAwareness;
if(!bDruidAttackable && bDruidVisible) ogreState = aiState.chasing;
else if (!bDruidVisible) ogreState = aiState.aware;
break;
case aiState.aware:
awarenesLevel-=Time.deltaTime;
if(awarenesLevel<=calmAwarenes) ogreState = aiState.patrolling;
break;
}
prevState = ogreState;
}
private void Attack() {
Debug.Log("Should attack!");
}
private Vector3 NextPatrolingPoint() {
curPatrolignPoint++;
if(curPatrolignPoint>=patrolPoints.Length) curPatrolignPoint = 0;
return patrolPoints[curPatrolignPoint];
}
public void TakeDamage(float dmg) {
health-=dmg;
if(health<=0) {
Die();
}
}
public void Die() {
}
IEnumerator GotoPoint(Vector3 point) {
bReachedDestination = false;
point.y = transform.position.y;
point.z = transform.position.z;
Vector3 dir = (point-transform.position).normalized;
while(Vector3.Distance(point,transform.position)>=0.1f) {
//Debug.Log(Time.frameCount+" gotopoint coroutine in progress");
if(ogreState == aiState.attacking) break;
if(!bAlive) break;
transform.Translate(dir*Time.deltaTime*walkingSpeed);
yield return new WaitForEndOfFrame();
}
bReachedDestination = true;
}
}