Add player controller, state management, and input handling

- Implemented PlayerController.cs to manage player movement and actions.
- Created PlayerState.cs to track player lives, coins, and key status.
- Added CameraFollow.cs for smooth camera movement following the player.
- Developed Character.cs as an abstract class for character behavior.
- Introduced Enums.cs for defining TreasureType and MapElementType.
- Added IDoor interface for door interactions.
- Created InputActions.cs for handling player input actions.
- Implemented MainMenu.cs for basic menu functionality including play and exit options.
This commit is contained in:
2026-06-17 22:43:59 +03:00
parent dabd056e8b
commit 39e4e51866
70 changed files with 1807 additions and 99 deletions
+69
View File
@@ -0,0 +1,69 @@
using System;
using UnityEngine;
public class HammerThrower : MonoBehaviour
{
[SerializeField] private Transform _spawnPoint;
[SerializeField] private GameObject _hammerPrefab;
[SerializeField] private float _throwSpeed = 5f;
[SerializeField] private float _throwCooldown = 1.5f;
private GameObject _currentHammer;
private bool _hasHammer = true;
private bool _facingRight = true;
private float _cooldownTimer = 0f;
public bool HasHammer => _hasHammer;
public bool CanThrow => _hasHammer && _cooldownTimer <= 0f;
public float CooldownRemaining => Mathf.Max(0f, _cooldownTimer);
public void SetFacingDirection(bool facingRight)
{
_facingRight = facingRight;
}
public bool TryThrowHammer()
{
if (!CanThrow)
return false;
ThrowHammer();
return true;
}
private void ThrowHammer()
{
_hasHammer = false;
_cooldownTimer = _throwCooldown;
_currentHammer = Instantiate(_hammerPrefab, _spawnPoint.position, _spawnPoint.rotation);
// Initialize hammer with direction and speed
var hammerComponent = _currentHammer.GetComponent<Hammer>();
if (hammerComponent != null)
{
hammerComponent.Initialize(_facingRight, _throwSpeed);
}
// Flip hammer visual based on direction
_currentHammer.transform.localScale = new Vector2(
_currentHammer.transform.localScale.x * (_facingRight ? 1 : -1),
_currentHammer.transform.localScale.y
);
}
private void Update()
{
// Update cooldown timer
if (_cooldownTimer > 0f)
{
_cooldownTimer -= Time.deltaTime;
}
// Hammer destroyed → hammer returned
if (!_hasHammer && _currentHammer == null)
{
_hasHammer = true;
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 59cb4a98d6866124793e8758b2ec958a
+102
View File
@@ -0,0 +1,102 @@
using System;
using UnityEngine;
public class PlayerController : Character
{
[SerializeField]
private Sprite _regularSprite;
[SerializeField]
private Sprite _noHammerSprite;
private GameObject _hammer;
private bool _isHoldingHammer = true;
public event EventHandler<TreasureType> OnPlayerTakeItem;
private InputManager _inputManager;
private PlayerState _playerState;
private HammerThrower _hammerThrower;
private void Awake()
{
_inputManager = GetComponent<InputManager>();
_playerState = GetComponent<PlayerState>();
_hammerThrower = GetComponent<HammerThrower>();
_inputManager.OnFire += OnFireButtonPressed;
}
private void OnEnable()
{
_inputManager.OnEnable();
}
private void OnDisable()
{
_inputManager.OnDisable();
}
private void Update()
{
if (_hammer == null && !_isHoldingHammer)
{
_spriteRenderer.sprite = _regularSprite;
_isHoldingHammer = true;
}
Vector2 move = _inputManager.Movement;
MoveTo(move.x, isAllowVertical ? move.y : 0);
_hammerThrower.SetFacingDirection(_facingRight);
}
private void OnFireButtonPressed()
{
if (_hammerThrower.CanThrow)
{
_animator.SetTrigger("Body_ThrowHammer");
}
}
// Animation event
public void ThrowHammerObject()
{
_hammerThrower.TryThrowHammer();
UpdatePlayerSprite();
}
private void UpdatePlayerSprite()
{
_spriteRenderer.sprite = _hammerThrower.HasHammer
? _regularSprite
: _noHammerSprite;
}
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");
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9c83b0150e991b443858a82f5a1eea57
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+30
View File
@@ -0,0 +1,30 @@
using System;
using UnityEngine;
public class PlayerState : MonoBehaviour
{
public int Lives = 3;
public int TotalCoins { get; private set; }
public bool HasKey { get; private set; }
public event EventHandler<TreasureType> OnPlayerTakeItem;
public void AddCoin()
{
TotalCoins++;
OnPlayerTakeItem?.Invoke(this, TreasureType.Coin);
Debug.Log($"Player has {TotalCoins} coins");
}
public void SetKey()
{
HasKey = true;
OnPlayerTakeItem?.Invoke(this, TreasureType.Key);
Debug.Log("Player has key");
}
public void RemoveKey()
{
HasKey = false;
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 907b91debf5d5864780e9466f4017f38