-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnemyAI.cs
166 lines (152 loc) · 6.77 KB
/
EnemyAI.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityEngine;
public class EnemyAI : MonoBehaviour
{
private Transform playerLocation; // Reference variable pointing to the player's location (same as gameobject.transform).
public CountScore score; // Reference to where score is controlled.
private Vector3 direction; // Vector used to check which direction the enemy is facing.
private Animator animator; // The animator referenced by the enemy for its animation frames in different states.
[SerializeField]
private float distFromPlayer; // The offset of the enemy and the player location.
[SerializeField]
private bool playerInRange; // Bool used to check if the enemy is within a specified distance.
[SerializeField]
private float health; // Serialized health to test for the ideal enemy default.
[SerializeField]
private float walkSpeed = 5f; // The default walking speed of the enemy.
[SerializeField]
private float damage = 8f; // The default damage dealt by an enemy.
private bool isWaiting;
private PlayerFighting player; // Gets the reference of the player object where its damage/attack is defined.
private bool playerclose;
private float enemyScale; // Sets the default scale of the enemy to make sure that all enemies are the same size.
void Start()
{
UpdateDistFromPlayer(); // Grab initial player distance reference.
player = GameObject.Find("Player").GetComponent<PlayerFighting>(); // Target player health/damage object.
score = GameObject.Find("ScoreUI").GetComponent<CountScore>(); // Target the gameobject handled within the CountScore script.
animator = gameObject.GetComponent<Animator>(); // Get reference of the enemy animator.
playerLocation = GameObject.Find("Player").transform;
// Set initial values of enemy variants:
if (gameObject.name == "Boss")
{
health = 1000f;
enemyScale = 0.1f; // Make the boss scale larger than regular enemies.
damage = 20f; // Boss deals higher damage than regular enemies.
}
else if (gameObject.name == "Miniboss") // Set miniboss to intermediate stage of damage, health etc. (between regular enemy and boss).
{
health = 250f;
enemyScale = 0.09f;
}
else
{
health = 100f;
enemyScale = 0.07f;
}
}
void Update()
{
UpdateDistFromPlayer();
//If Player in Range (Range of detection)
if (playerInRange)
{
if (distFromPlayer < 1) // If they are close enough (1 unity unit)
{
if (!isWaiting) // and they are not waiting (delayed between attack and idle)
{
Fight(); // Attempt to deal damage to the player.
StartCoroutine(Wait(2f)); // Delay
}
if (player.PlayerAttackCheck() == 1) // If the player does a punch attack:
{
TakeDamage(6); // Deal smaller damage.
}
else if (player.PlayerAttackCheck() == 2) // If the player does a kick attack:
{
TakeDamage(10); // Deal larger damage.
}
}
else
{
playerclose = false; // The player is not close enough.
FollowPlayer(); // Try to follow the player.
}
}
}
void FixedUpdate()
{
animator.SetFloat("distToPlayer", distFromPlayer); // Update parameters used within the animator to define enemy state
animator.SetBool("playerInRange", playerInRange);
UpdateDistFromPlayer();
}
void FollowPlayer()
{
if (Vector3.Distance(transform.position, playerLocation.position) >= 1) // If the player distance is greater than 1 (the value where they are defined as close):
{
direction = playerLocation.position - transform.position; // Find the direction of the player
transform.position += direction * walkSpeed * Time.deltaTime; // Begin to follow the player at the defined speed over time.
if (direction.x < 1) transform.localScale = new Vector3(-enemyScale, enemyScale, 1); // If the x position is negative, make the enemy face left.
else transform.localScale = new Vector3(enemyScale, enemyScale, 1); // If x position is positive, face the enemy right.
}
}
void Fight()
{
player.TakeDamage(damage); // Make the player take the defined amount of damage.
Debug.Log("Enemy Health" + health); // Used to check the enemie's remaining health during value and input verification tests.
}
void UpdateDistFromPlayer()
{
playerLocation = GameObject.Find("Player").transform;
distFromPlayer = playerLocation.position.x - transform.position.x;
if (distFromPlayer < 0) // If the distance is negative:
{
distFromPlayer *= -1; // Make the value positive for use in calculations.
}
}
void TakeDamage(float value)
{
if (gameObject.name == "Boss" && health - value < 1) // If the boss is defeated:
{
score.AddScore(100); // Add higher score value.
SceneManager.LoadScene("Good_GameOver"); // Send the player to the good game-over scene.
Destroy(gameObject);
}
else if (health - value < 1) // If the enemy is anyhting else and defeated:
{
score.AddScore(50); // Add lower score value.
Destroy(gameObject);
}
else // Otherwise if the enemy is not defeated:
{
health -= value; // Remove the specified amount of health from the enemy.
}
}
void OnTriggerEnter2D(Collider2D collider) // When an object enters the detection collider:
{
if (collider.name == "Player") // If the object is the player:
{
playerInRange = true; // Set the player to in range.
}
}
void OnTriggerExit2D(Collider2D collider) // When an object exits the detection collider:
{
if (collider.name == "Player") // If the object is the player:
{
playerInRange = false; // Set the player as out of range.
}
}
IEnumerator Wait(float seconds) // Used so that the enemy will wait for the set duration in the attack.
{
isWaiting = true;
yield return new WaitForSeconds(seconds);
isWaiting = false;
}
public float GetHealth() // Returns the enemy health.
{
return health;
}
}