aboutsummaryrefslogtreecommitdiffstats
path: root/Assets/Scripts/Actors/Enemy.cs
diff options
context:
space:
mode:
authorcross28 <45377355+cross28@users.noreply.github.com>2022-04-15 06:30:32 -0500
committerGitHub <noreply@github.com>2022-04-15 06:30:32 -0500
commitce047b021d4d7d66ec57973d8e3b27a0f223c35e (patch)
tree891dc998c66b26d7d1df85c11d7c93c23ad852a5 /Assets/Scripts/Actors/Enemy.cs
parentee708901a74b9d442e2e8397b699a568928a8dc7 (diff)
parentb80e48181fb54dd80d4f344d8354e768200beb7f (diff)
Merge pull request #4 from MontanaJohns/feat/enemy-ai
Feat/enemy ai
Diffstat (limited to 'Assets/Scripts/Actors/Enemy.cs')
-rw-r--r--Assets/Scripts/Actors/Enemy.cs57
1 files changed, 57 insertions, 0 deletions
diff --git a/Assets/Scripts/Actors/Enemy.cs b/Assets/Scripts/Actors/Enemy.cs
new file mode 100644
index 0000000..9160e44
--- /dev/null
+++ b/Assets/Scripts/Actors/Enemy.cs
@@ -0,0 +1,57 @@
+using UnityEngine;
+using System.Collections;
+
+namespace MontanaJohns.Actors
+{
+ [RequireComponent(typeof(Rigidbody2D))]
+ public class Enemy : Actor
+ {
+ protected GameObject player;
+ protected float attackRate = 0.5f;
+ protected float nextAttackTime = 0f;
+
+ protected override void Awake()
+ {
+ base.Awake();
+ player = GameObject.FindGameObjectWithTag("Player");
+ }
+
+ // Update is called once per frame
+ void Update()
+ {
+ MoveTowardsPlayer();
+ Attack();
+ CheckHealth();
+ }
+
+ void MoveTowardsPlayer()
+ {
+ if (player.transform.position.x < transform.position.x) Move(-stats.speedMultiplier * 0.5f);
+ else Move(stats.speedMultiplier * 0.5f);
+ }
+
+ void CheckHealth()
+ {
+ if (health <= 0) Destroy(gameObject);
+ }
+
+ void Attack()
+ {
+ if (Mathf.Abs(player.transform.position.x - transform.position.x) <= 5 && Time.time >= nextAttackTime) {
+ _animator.SetTrigger("attack");
+ player.GetComponent<Actor>().TakeDamage(1);
+ nextAttackTime = Time.time + 1f / attackRate;
+ }
+ }
+
+ private void OnCollisionEnter2D(Collision2D other)
+ {
+ if (other.gameObject.tag == "Projectile")
+ {
+ Debug.Log("Enemy: Hit by projectile");
+ TakeDamage(1);
+ Destroy(other.gameObject);
+ }
+ }
+ }
+}