using UnityEngine; using System.Collections; using UnityEngine.InputSystem; namespace MontanaJohns.Actors { [RequireComponent(typeof(Rigidbody2D))] public class Enemy : Actor { protected GameObject player; protected float attackRate = 0.5f; protected float nextAttackTime = 0f; private bool playerSeen; private PlayerInput playerInput; private InputAction jump; protected override void Awake() { base.Awake(); player = GameObject.FindGameObjectWithTag("Player"); playerInput = player.GetComponent(); jump = playerInput.currentActionMap.FindAction("Jump"); jump.started += context => Jump(); } // Update is called once per frame void Update() { if(!playerSeen) { if (Mathf.Abs(player.transform.position.x - transform.position.x) <= 50 && Mathf.Abs(player.transform.position.y - transform.position.y) <= 15) playerSeen = true; } else { MoveTowardsPlayer(); Attack(); } CheckHealth(); } protected override void FixedUpdate() { // Temp override while missing falling logic/animations } 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 && Mathf.Abs(player.transform.position.y - transform.position.y) <= 2 && Time.time >= nextAttackTime) { _animator.SetTrigger("attack"); player.GetComponent().TakeDamage(1); nextAttackTime = Time.time + 1f / attackRate; } } public override void Jump() { if (Physics2D.OverlapCircle(groundCheckPoint1.position, 0.2f, groundLayers) || Physics2D.OverlapCircle(groundCheckPoint2.position, 0.2f, groundLayers)) { jumpCount = stats.maxJumps; hangCount = hangTime; } else { hangCount -= Time.deltaTime; } if (jumpCount > 0 && hangCount > 0f) { jumpCount--; _rigidBody.AddForce(Vector2.up * stats.jumpForce); } } private void OnCollisionEnter2D(Collision2D other) { if (other.gameObject.tag == "Projectile") { // TODO update once projectile is made an Active Item TakeDamage(1); Destroy(other.gameObject); } } } }