Files
Gnome-s-Bounty/Assets/Scripts/Controllers/PlayerController.cs
T

112 lines
2.8 KiB
C#

using System;
using UnityEngine;
public class PlayerController : Character
{
[SerializeField]
private Transform _hammerSpawnPoint;
[SerializeField]
private GameObject _hammerPrefab;
[SerializeField]
private Sprite _regularSprite;
[SerializeField]
private Sprite _noHammerSprite;
public static Player Instance { get; private set; }
private GameObject _hammer;
private bool _isHoldingHammer = true;
public event EventHandler<TreasureType> OnPlayerTakeItem;
private InputManager _inputManager;
private PlayerState _playerState;
private void Awake()
{
if (Instance != null)
{
Destroy(gameObject);
Debug.Log("There's more than one player instance");
return;
}
Instance = this;
DontDestroyOnLoad(gameObject);
_inputManager = new InputManager();
_playerState = GetComponent<PlayerState>();
_inputManager.OnMovementInput += OnMovementInput;
_inputManager.OnFireButtonPressed += OnFireButtonPressed;
}
private void OnEnable()
{
_inputManager.Enable();
}
private void OnDisable()
{
_inputManager.Disable();
}
private void Update()
{
if (_hammer == null && !_isHoldingHammer)
{
_spriteRenderer.sprite = _regularSprite;
_isHoldingHammer = true;
}
var move = _inputManager.OnMovementInput.ReadValue<Vector2>();
base.MoveTo(move.x, isAllowVertical ? move.y : 0);
}
private void OnFireButtonPressed()
{
if (_hammer == null)
{
_animator.SetTrigger("Body_ThrowHammer");
}
}
public void ThrowHammerObject()
{
_isHoldingHammer = false;
_spriteRenderer.sprite = _noHammerSprite;
_hammer = Instantiate(_hammerPrefab, _hammerSpawnPoint.position, _hammerSpawnPoint.rotation);
_hammer.transform.localScale = new Vector2(_hammer.transform.localScale.x * (_facingRight ? 1 : -1), _hammer.transform.localScale.y);
_hammer.GetComponent<Rigidbody2D>().linearVelocity = new Vector2(gameObject.transform.localScale.x * _hammerSpeed, 0);
}
protected override void SetWalkingAnimation(bool isWalking)
{
_bonesBack.SetActive(false);
_bonesSide.SetActive(true);
_animator.SetBool("Legs_Walk", isWalking);
_animator.SetBool("Body_Walk", isWalking);
}
protected override void SetClimbingAnimation(bool isClimbing)
{
if (isClimbing)
{
_bonesBack.SetActive(true);
_bonesSide.SetActive(false);
}
_animator.SetBool("Climb", isClimbing);
}
protected void OnDeath()
{
_playerState.Lives--;
if (_playerState.Lives == 0)
{
Debug.Log("Game over");
}
}
}