aboutsummaryrefslogtreecommitdiffstats
path: root/Assets/Scripts/Actors/Actor.cs
blob: eb8185eff63f1e197712f70c6d300cdba66cbcb9 (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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
using System.Collections;
using System.Collections.ObjectModel;
using System.Linq;
using MontanaJohns.Core;
using MontanaJohns.Items;
using UnityEngine;

namespace MontanaJohns.Actors
{
    [RequireComponent(typeof(Rigidbody2D))]
    public abstract class Actor : MonoBehaviour
    {
        [SerializeField] protected float gravityScale = 1.5f;
        [SerializeField] protected Stats baseStats = Stats.DefaultBaseStats();
        [SerializeField] protected Transform groundCheckPoint1;
        [SerializeField] protected Transform groundCheckPoint2;
        [SerializeField] protected float invulnTime = 0f;
        [SerializeField] protected float hangTime;

        protected Rigidbody2D _rigidBody;
        protected SpriteRenderer _renderer;
        protected Animator _animator;
        protected Transform _transform;
        protected AudioManager _audio;

        protected Active activeItem;
        protected bool isGrappling;
        protected bool isMoving;
        protected Vector2? grapplePoint = null;
        protected LayerMask groundLayers;
        protected Collection<Item> items;
        protected int jumpCount;
        protected Vector2 acceleration;
        protected bool invuln;
        protected float hangCount;
        
        public Stats stats;
        public int health;


        protected virtual void Awake()
        {
            _rigidBody = GetComponent<Rigidbody2D>();
            _transform = GetComponent<Transform>();
            _renderer = GetComponent<SpriteRenderer>();
            _animator = GetComponent<Animator>();
            _audio = FindObjectOfType<AudioManager>();
            groundLayers = LayerMask.GetMask("Grapple", "Ground");

            _rigidBody.freezeRotation = true;
            _rigidBody.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
            _rigidBody.gravityScale = gravityScale;

            items = new();
            stats = baseStats;
            health = stats.maxHealth;
            jumpCount = stats.maxJumps;
        }

        protected virtual void Start()
        {
            
        }

        protected virtual void FixedUpdate()
        {
            if (!_animator.GetBool("airborn") && !(Physics2D.OverlapCircle(groundCheckPoint1.position, 0.2f, groundLayers) || Physics2D.OverlapCircle(groundCheckPoint2.position, 0.2f, groundLayers)))
            {
                _animator.SetTrigger("fall");
                StartCoroutine(Falling());
            }
        }

        public virtual void Move(float input)
        {
            var target = new Vector2(input * stats.speedMultiplier * 10, _rigidBody.velocity.y);
            _rigidBody.velocity = Vector2.SmoothDamp(_rigidBody.velocity, target, ref acceleration, .05f);
            _animator.SetBool("moving", Mathf.Abs(_rigidBody.velocity.x) > 1);
            isMoving = Mathf.Abs(_rigidBody.velocity.x) > 1;

            if (_rigidBody.velocity.x < -0.1) {
                _renderer.flipX = true;
                for (int i = 0; i < transform.childCount; i++) {
                    var rot = transform.GetChild(i).rotation;
                    rot.y = 180f;
                    transform.GetChild(i).rotation = rot;
                }
            }
            else if (_rigidBody.velocity.x > 0.1) {
                _renderer.flipX = false;
                for (int i = 0; i < transform.childCount; i++) {
                    var rot = transform.GetChild(i).rotation;
                    rot.y = 0f;
                    transform.GetChild(i).rotation = rot;
                }
            }
        }
        public virtual void MoveY(float input)
        {
            var target = new Vector2(_rigidBody.velocity.x, input * stats.speedMultiplier * 10);
            _rigidBody.velocity = Vector2.SmoothDamp(_rigidBody.velocity, target, ref acceleration, .05f);
        }

        public virtual void Jump()
        {
            if (isGrappling)
                Use();
            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);
                _animator.SetTrigger("jump");
                _animator.SetBool("airborn", true);
                StartCoroutine(Falling());
            }
        }

        public virtual void Use()
        {
            Vector2? grapplePoint;
            if (activeItem != null)
            {
                grapplePoint = activeItem.Use();
                if (grapplePoint != null)
                    isGrappling = true;
                else
                    isGrappling = false;
            }
            else
                grapplePoint = null;
            this.grapplePoint = grapplePoint;
        }

        public virtual void Cancel()
        {
            if (PauseMenu.isPaused)
            {
                PauseMenu.Resume();
            }
            else
            {
                PauseMenu.Pause();
            }
        }

        public virtual void AddItem(Item item)
        {
            items.Add(item);
            stats = baseStats + items.Select(i => i.stats).Sum();
        }

        public virtual void Grapple(float xInput, float yInput, Vector2 grapplePoint)
        {
            Move(xInput);
            MoveY(yInput);
        }

        public virtual void TakeDamage(int damage)
        {
            StartCoroutine(Damage(damage));
        }

        private IEnumerator Damage(int damage)
        {
            if(!invuln)
            {
                invuln = true;
                health -= damage;
                StartCoroutine(DamageAnimation());
                yield return new WaitForSeconds(invulnTime);
                invuln = false;
            }
        }

        private IEnumerator DamageAnimation()
        {
            _renderer.color = Color.red;
            yield return new WaitForSeconds(0.5f);
            _renderer.color = Color.white;
        }

        private IEnumerator Falling()
        {
            _animator.SetBool("airborn", true);

            bool falling = true;
            while (falling)
            {
                if (Physics2D.OverlapCircle(groundCheckPoint1.position, 0.2f, groundLayers) || Physics2D.OverlapCircle(groundCheckPoint2.position, 0.2f, groundLayers))
                    falling = false;
                yield return new WaitForEndOfFrame();
            }

            _animator.SetBool("airborn", false);

            yield break;
        }
    }
}