-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoar.cs
124 lines (93 loc) · 2.63 KB
/
Boar.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Boar : MonoBehaviour {
private Animator anim;
private NavMeshAgent navAgent;
public float currentHealth;
public float maxHealth = 2f;
public GameObject Hero;
private bool isDead;
Vector3 originalPosition;
public GameObject healthBar;
public GameObject healthBarPrefab;
public Transform healthBarTransform;
public CharacterMovement cm;
void Start () {
anim = GetComponent<Animator> ();
navAgent = GetComponent<NavMeshAgent> ();
currentHealth = maxHealth;
healthBar = Instantiate (healthBarPrefab, healthBarTransform.transform) as GameObject;
healthBar.transform.GetComponentInChildren<Slider> ().value = (currentHealth / maxHealth);
originalPosition = transform.position;
}
void FixedUpdate () {
if (!isDead) {
healthBar.transform.position = new Vector3 (gameObject.transform.position.x, gameObject.transform.position.y+2f, gameObject.transform.position.z);
healthBar.transform.rotation = Quaternion.LookRotation (Camera.main.transform.forward, Camera.main.transform.up);
transform.LookAt (Hero.transform);
}
if (CharacterMovement.isDead) {
CancelInvoke ();
}
}
public void TakeDamage (){
if (!isDead) {
anim.SetTrigger ("Take_Damage");
currentHealth--;
healthBar.GetComponentInChildren<Slider> ().value = (currentHealth / maxHealth);
if (currentHealth <= 0) {
Die ();
}
}
}
void ChaseHero(){
navAgent.SetDestination (Hero.transform.position);
anim.SetTrigger ("Moving");
navAgent.speed = 1.0f;
if (navAgent.remainingDistance <= navAgent.stoppingDistance) {
if (!IsInvoking() && !CharacterMovement.isDead) {
InvokeRepeating ("Attack", 1f, 2f);
}
} else {
CancelInvoke ();
}
}
void Attack(){
if (!isDead) {
anim.SetTrigger ("Basic_Attack");
cm.TakeDamage (2);
}
}
void Die (){
anim.SetTrigger ("Dead");
isDead = true;
//Debug.Log ("Boar is dead. isDead = "+ isDead);
CancelInvoke ("Attack");
gameObject.GetComponent<Collider> ().enabled = false;
Invoke ("EndGame", 3f);
}
void EndGame(){
SceneManager.LoadScene (2);
}
void OnTriggerStay (Collider col){
if (col.name == "Hero" && !isDead) {
//Debug.Log ("Boar: Enemy in sight!");
ChaseHero ();
}
}
void OnTriggerExit (Collider col){
if (col.name == "Hero" && !isDead) {
//Debug.Log ("Boar: Well, it's time to go home");
StopChaseHero ();
}
}
//Boar returns to its original position
void StopChaseHero(){
navAgent.SetDestination (originalPosition);
navAgent.speed = 0.8f;
}
}