aboutsummaryrefslogtreecommitdiffstats
path: root/Assets/Scripts/Actors/Enemy.cs
blob: 600fcaf56cf47bd1d24a26eae446f3e7cde46a15 (plain)
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
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<PlayerInput>();

            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<Actor>().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);
            }
        }
    }
}