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(); playerInput = GetComponent(); 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(); ResetStats(); spawnPoint = transform.position; } protected void Update() { ((IFollowable)this).Follow(); if(isGrappling) { base.Grapple(move.ReadValue().x, move.ReadValue().y, (Vector2)grapplePoint); } else { base.Move(move.ReadValue().x); if (isMoving && !_animator.GetBool("airborn") && !_audio.isPlaying("RunningOnGrass")) _audio.Play("RunningOnGrass"); else if (!isMoving || _animator.GetBool("airborn")) _audio.Stop("RunningOnGrass"); } DeathCheck(); } protected void DeathCheck() { if(health <= 0) { MainCamera.GetComponent().ResetLevel(); if (isGrappling) Use(); ResetStats(); health = stats.maxHealth; transform.position = spawnPoint; } } protected void ResetStats() { stats = baseStats + activeItem.stats; } protected void Fire() { Instantiate(projectilePrefab, firePoint.position, firePoint.rotation); _audio.Play("Gunshot"); } } }