aboutsummaryrefslogtreecommitdiffstats
path: root/Assets/Scripts/Actors/Actor.cs
blob: 52a67d313bb61d58235322fb53277fd9bb3fbf59 (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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using MontanaJohns.Core;
using MontanaJohns.Items;
using UnityEngine;

namespace MontanaJohns.Actors
{
    [RequireComponent(typeof(Rigidbody2D))]
    [RequireComponent(typeof(CapsuleCollider2D))]
    public abstract class Actor : MonoBehaviour
    {
        [SerializeField] protected float gravityScale = 1.5f;
        [SerializeField] protected Stats baseStats = Stats.DefaultBaseStats();
        [SerializeField] protected LayerMask groundLayer;
        [SerializeField] protected Transform groundCheckPoint;

        protected CapsuleCollider2D _collider;
        protected Rigidbody2D _rigidBody;
        protected SpriteRenderer _renderer;
        protected Animator _animator;
        protected Transform _transform;

        protected Stats stats;
        protected Active activeItem;
        Collection<Item> items;
        bool isFalling;

        protected int jumpCount;
        protected Vector2 acceleration;

        protected virtual void Awake()
        {
            _collider = GetComponent<CapsuleCollider2D>();
            _rigidBody = GetComponent<Rigidbody2D>();
            _transform = GetComponent<Transform>();
            _renderer = GetComponent<SpriteRenderer>();
            _animator = GetComponent<Animator>();
        }

        protected virtual void Start()
        {
            _rigidBody.freezeRotation = true;
            _rigidBody.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
            _rigidBody.gravityScale = gravityScale;

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

        protected virtual void FixedUpdate()
        {
            if (!_animator.GetBool("airborn") && !Physics2D.OverlapCircle(groundCheckPoint.position, 0.2f, groundLayer))
            {
                _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) > 0.001);

            if (_rigidBody.velocity.x < -0.001)
                _renderer.flipX = true;
            else if (_rigidBody.velocity.x > 0.001)
                _renderer.flipX = false;
        }

        public virtual void Jump()
        {
            if (jumpCount++ <= stats.maxJumps)
            {
                Debug.Log($"Jumping! Force: {stats.jumpForce}");
                _rigidBody.AddForce(Vector2.up * stats.jumpForce);
                _animator.SetTrigger("jump");
                _animator.SetBool("airborn", true);
                StartCoroutine(Falling());
            }
        }

        public virtual void Use()
        {
            Debug.Log("Using!");
            if (activeItem != null)
                activeItem.Use();
        }

        public virtual void AddItem(Item item)
        {
            Debug.Log($"Adding item: {item}!");
            items.Add(item);
            stats = baseStats + items.Select(i => i.stats).Sum();
        }

        IEnumerator Falling()
        {
            Debug.Log("Falling!");

            _animator.SetBool("airborn", true);

            while (_rigidBody.velocity.y > 0 || !Physics2D.OverlapCircle(groundCheckPoint.position, 0.2f, groundLayer))
            {
                yield return new WaitForFixedUpdate();
            }

            jumpCount = 0;
            _animator.SetBool("airborn", false);

            Debug.Log("Fell!");

            yield break;
        }
    }
}