aboutsummaryrefslogtreecommitdiffstats
path: root/Assets/Scripts/Actors/Player.cs
blob: 6201cadf6be7cadf043f6b50ce01dd256ce7f910 (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
using MontanaJohns.Core.Interfaces;
using MontanaJohns.Items;
using UnityEngine;
using UnityEngine.InputSystem;

namespace MontanaJohns.Actors
{
    [RequireComponent(typeof(Rigidbody2D))]
    public class Player : Actor, IFollowable
    {
        public Transform ActorTransform => _transform;
        public Camera MainCamera => _camera;
        public GameObject projectilePrefab;
        public Transform firePoint;
        public Vector3 spawnPoint;

        private Camera _camera;
        private PlayerInput playerInput;
        private InputAction use, move, jump, attack, cancel;

        protected override void Awake()
        {
            base.Awake();
            _camera = FindObjectOfType<Camera>();
            playerInput = GetComponent<PlayerInput>();
            use = move = jump = attack = cancel = null;
            move = playerInput.currentActionMap.FindAction("Move");
            jump = playerInput.currentActionMap.FindAction("Jump");
            attack = playerInput.currentActionMap.FindAction("Attack");
            use = playerInput.currentActionMap.FindAction("Use");
            cancel = playerInput.currentActionMap.FindAction("Cancel");

            jump.started += context => Jump();
            use.started += context => Use();
            attack.started += context => Fire();
            cancel.started += context => Cancel();
        }

        protected override void Start()
        {
            base.Start();
            GameObject loadedItem = (GameObject)Instantiate(Resources.Load("ActiveItems/Whip"));
            activeItem = loadedItem.GetComponent<Whip>();
            ResetStats();
            spawnPoint = transform.position;
        }

        protected void Update()
        {
            ((IFollowable)this).Follow();
            if(isGrappling)
            {
                base.Grapple(move.ReadValue<Vector2>().x, move.ReadValue<Vector2>().y, (Vector2)grapplePoint);
            }
            else
            {
                base.Move(move.ReadValue<Vector2>().x);
            }
            DeathCheck();
        }

        protected void DeathCheck()
        {
            if(health <= 0)
            {
                MainCamera.GetComponent<LevelController>().ResetLevel();
                ResetStats();
                health = stats.maxHealth;
                transform.position = spawnPoint;
            }
        }

        protected void ResetStats()
        {
            stats = baseStats + activeItem.stats;
        }

        protected void Fire()
        {
            Instantiate(projectilePrefab, firePoint.position, firePoint.rotation);
            SoundManager.PlaySound("Gunshot");
        }
    }
}