-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enemy.cs
195 lines (159 loc) · 5.31 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
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
189
190
191
192
193
194
195
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public int maxHealth = 100;
int currentHealth;
private Animator enemyAnim;
private BoxCollider2D boxCollider;
private Vector3 moveDelta;
private RaycastHit2D hit;
private float ySpeed = 0.5f;
private float xSpeed = 0.75f;
public Transform playerTransform;
private Transform selfTransform;
private Vector3 startingPosition;
private bool isChasing;
private bool isCollidingWithPlayer;
public float triggerLength = 1.0f;
public float chaseLength = 5.0f;
//hitbox
public ContactFilter2D filter;
private Collider2D[] hits = new Collider2D[10];
//Combat
public Vector3 attackOffset;
public float attackRange = 1f;
public LayerMask attackMask;
float attackCooldown = 1.0f;
float lastSwing;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
enemyAnim = GetComponent<Animator>();
boxCollider = GetComponent<BoxCollider2D>();
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
startingPosition = transform.position;
GetComponent<Collider2D>().enabled = false;
this.enabled = false;
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
enemyAnim.SetTrigger("Hit");
if (currentHealth <= 0)
{
Die();
}
}
void Die()
{
Debug.Log("Enemy Death");
enemyAnim.SetBool("isDead", true);
GetComponent<Collider2D>().enabled = false;
this.enabled = false;
Destroy(gameObject);
}
public void Attack()
{
Vector3 pos = transform.position;
pos += transform.right * attackOffset.x;
pos += transform.up * attackOffset.y;
Collider2D colInfo = Physics2D.OverlapCircle(pos, attackRange, attackMask);
if (colInfo != null && colInfo.tag == "Player")
{
colInfo.GetComponent<PlayerHealth>().TakeDamage(10);
}
}
void OnDrawGizmosSelected()
{
Vector3 pos = transform.position;
pos += transform.right * attackOffset.x;
pos += transform.up * attackOffset.y;
Gizmos.DrawWireSphere(pos, attackRange);
}
// Update is called once per frame
void FixedUpdate()
{
enemyAnim.SetBool("isMoving", false);
if (Time.time - lastSwing > attackCooldown)
{
if (Vector2.Distance(playerTransform.position, transform.position) <= attackRange)
{
enemyAnim.SetTrigger("Attack");
lastSwing = Time.time;
}
}
if (Vector3.Distance(playerTransform.position, startingPosition) < chaseLength)
{
if (Vector3.Distance(playerTransform.position, startingPosition) < triggerLength)
{
isChasing = true;
}
if (isChasing)
{
if (!isCollidingWithPlayer)
{
UpdateMotor((playerTransform.position - transform.position).normalized);
}
}
else
{
UpdateMotor(startingPosition - transform.position);
}
}
else
{
UpdateMotor(startingPosition - transform.position);
isChasing = false;
}
//Check for overlap
isCollidingWithPlayer = false;
boxCollider.OverlapCollider(filter, hits);
for (int i = 0; i < hits.Length; i++)
{
if (hits[i] == null)
continue;
if (hits[i].name == "Protagonist_0")
{
isCollidingWithPlayer = true;
}
//Clean Array
hits[i] = null;
}
}
public void Rebirth()
{
GetComponent<Collider2D>().enabled = true;
this.enabled = true;
}
private void UpdateMotor(Vector3 input)
{
// Reset MoveDelta
moveDelta = new Vector3(input.x * xSpeed, input.y * ySpeed, 0);
//Swap sprite direction
if (moveDelta.x > 0)
{
transform.localScale = Vector3.one;
}
else if (moveDelta.x < 0)
{
transform.localScale = new Vector3(-1, 1, 1);
}
hit = Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(0, moveDelta.y), Mathf.Abs(moveDelta.y * Time.deltaTime), LayerMask.GetMask("Actor", "Blocking"));
if (hit.collider == null)
{
//Make that thang move!
transform.Translate(0, moveDelta.y * Time.deltaTime, 0);
enemyAnim.SetBool("isMoving", true);
}
hit = Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(moveDelta.x, 0), Mathf.Abs(moveDelta.x * Time.deltaTime), LayerMask.GetMask("Actor", "Blocking"));
if (hit.collider == null)
{
//Make that thang move!
transform.Translate(moveDelta.x * Time.deltaTime, 0, 0);
enemyAnim.SetBool("isMoving", true);
}
}
}