Refactor and reorganize scripts for improved structure and functionality
- Removed obsolete scripts: Chest, BreakableWall, Door, DoorInteract, and UiManager. - Moved Chest and BreakableWall scripts to EnvironmentObjects folder. - Introduced HammerThrower class to manage hammer throwing mechanics. - Updated PlayerController to utilize new InputManager and HammerThrower. - Refactored PlayerState to manage player state and item collection. - Enhanced UI management in UiManager to reflect player item collection. - Updated EnemyAI to remove unused movement logic. - Adjusted KeyChest to interact with PlayerState instead of Player. - Cleaned up code and improved event handling for item collection.
This commit is contained in:
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0ade875a254231c4d8c43cacc7b27bfb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4d7148405c33f2f45ae0479592e8cb6e
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -3,18 +3,11 @@ 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;
|
||||
@@ -23,32 +16,25 @@ public class PlayerController : Character
|
||||
|
||||
private InputManager _inputManager;
|
||||
private PlayerState _playerState;
|
||||
private HammerThrower _hammerThrower;
|
||||
|
||||
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();
|
||||
_inputManager = GetComponent<InputManager>();
|
||||
_playerState = GetComponent<PlayerState>();
|
||||
_hammerThrower = GetComponent<HammerThrower>();
|
||||
|
||||
_inputManager.OnMovementInput += OnMovementInput;
|
||||
_inputManager.OnFireButtonPressed += OnFireButtonPressed;
|
||||
_inputManager.OnFire += OnFireButtonPressed;
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_inputManager.Enable();
|
||||
_inputManager.OnEnable();
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
_inputManager.Disable();
|
||||
_inputManager.OnDisable();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
@@ -59,8 +45,8 @@ public class PlayerController : Character
|
||||
_isHoldingHammer = true;
|
||||
}
|
||||
|
||||
var move = _inputManager.OnMovementInput.ReadValue<Vector2>();
|
||||
base.MoveTo(move.x, isAllowVertical ? move.y : 0);
|
||||
Vector2 move = _inputManager.Movement;
|
||||
MoveTo(move.x, isAllowVertical ? move.y : 0);
|
||||
}
|
||||
|
||||
private void OnFireButtonPressed()
|
||||
@@ -71,15 +57,19 @@ public class PlayerController : Character
|
||||
}
|
||||
}
|
||||
|
||||
// Animation event
|
||||
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);
|
||||
_hammerThrower.ThrowHammer();
|
||||
UpdatePlayerSprite();
|
||||
}
|
||||
|
||||
private void UpdatePlayerSprite()
|
||||
{
|
||||
_spriteRenderer.sprite = _hammerThrower.HasHammer
|
||||
? _regularSprite
|
||||
: _noHammerSprite;
|
||||
}
|
||||
protected override void SetWalkingAnimation(bool isWalking)
|
||||
{
|
||||
_bonesBack.SetActive(false);
|
||||
|
||||
+27
-27
@@ -13,39 +13,39 @@ public class EnemyAI : Character
|
||||
|
||||
private void Update()
|
||||
{
|
||||
float horizontal = 0;
|
||||
float vertical = 0;
|
||||
// float horizontal = 0;
|
||||
// float vertical = 0;
|
||||
|
||||
Vector2 directionToPlayer = Player.Instance.transform.position - transform.position;
|
||||
directionToPlayer.Normalize();
|
||||
// Vector2 directionToPlayer = Player.Instance.transform.position - transform.position;
|
||||
// directionToPlayer.Normalize();
|
||||
|
||||
float verticalDistance = Player.Instance.transform.position.y - transform.position.y;
|
||||
// float verticalDistance = Player.Instance.transform.position.y - transform.position.y;
|
||||
|
||||
if (Mathf.Abs(verticalDistance) > 0.1f && (isCanClimbUp || isCanGoDown))
|
||||
{
|
||||
vertical = Mathf.Sign(verticalDistance);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Mathf.Abs(Player.Instance.transform.position.x - transform.position.x) < 0.1f)
|
||||
{
|
||||
horizontal = 0;
|
||||
}
|
||||
else if (directionToPlayer.x < 0)
|
||||
{ horizontal = -1; }
|
||||
else if (directionToPlayer.x > 0)
|
||||
{ horizontal = 1; }
|
||||
}
|
||||
// if (Mathf.Abs(verticalDistance) > 0.1f && (isCanClimbUp || isCanGoDown))
|
||||
// {
|
||||
// vertical = Mathf.Sign(verticalDistance);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// if (Mathf.Abs(Player.Instance.transform.position.x - transform.position.x) < 0.1f)
|
||||
// {
|
||||
// horizontal = 0;
|
||||
// }
|
||||
// else if (directionToPlayer.x < 0)
|
||||
// { horizontal = -1; }
|
||||
// else if (directionToPlayer.x > 0)
|
||||
// { horizontal = 1; }
|
||||
// }
|
||||
|
||||
if (Input.GetKey(KeyCode.T))
|
||||
{ vertical = 1; }
|
||||
if (Input.GetKey(KeyCode.G))
|
||||
{ vertical = -1; }
|
||||
// if (Input.GetKey(KeyCode.T))
|
||||
// { vertical = 1; }
|
||||
// if (Input.GetKey(KeyCode.G))
|
||||
// { vertical = -1; }
|
||||
|
||||
Debug.Log($"Enemy Position: {transform.position}, Player Position: {Player.Instance.transform.position}");
|
||||
Debug.Log($"Vertical Distance: {verticalDistance}, Vertical Movement: {vertical}");
|
||||
// Debug.Log($"Enemy Position: {transform.position}, Player Position: {Player.Instance.transform.position}");
|
||||
// Debug.Log($"Vertical Distance: {verticalDistance}, Vertical Movement: {vertical}");
|
||||
|
||||
base.MoveTo(horizontal, vertical);
|
||||
// base.MoveTo(horizontal, vertical);
|
||||
}
|
||||
|
||||
private float VerticalMove(float verticalDistance)
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 86cce1993173eb04daddb1edd164da94
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8251b2e2708d87d4ebc446c84ec5ed01
|
||||
@@ -21,18 +21,18 @@ public class Chest : MonoBehaviour
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D collider)
|
||||
{
|
||||
var player=collider.GetComponent<Player>();
|
||||
if (player != null && !_isOpen)
|
||||
var playerState=collider.GetComponent<PlayerState>();
|
||||
if (playerState != null && !_isOpen)
|
||||
{
|
||||
_isOpen = true;
|
||||
animator.SetTrigger("OpenChest");
|
||||
switch (_treasureSO.Treasure)
|
||||
{
|
||||
case TreasureType.Coin:
|
||||
player.AddCoin();
|
||||
playerState.AddCoin();
|
||||
break;
|
||||
case TreasureType.Key:
|
||||
player.SetKey();
|
||||
playerState.SetKey();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ded23e667cffbfd46b2633ae22204797
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: af69aa896ad8a3947947ed21a9b9cacd
|
||||
+3
-3
@@ -13,10 +13,10 @@ public class DoorInteract : MonoBehaviour
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D collider)
|
||||
{
|
||||
var player = collider.GetComponent<Player>();
|
||||
if (player!=null)
|
||||
var playerState = collider.GetComponent<PlayerState>();
|
||||
if (playerState!=null)
|
||||
{
|
||||
if (player.IsHasKey())
|
||||
if (playerState.HasKey)
|
||||
{
|
||||
_door.OpenDoor();
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 73fd9f6c116c2de4ab3d773fbb908df4
|
||||
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class HammerThrower : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Transform _spawnPoint;
|
||||
[SerializeField] private GameObject _hammerPrefab;
|
||||
[SerializeField] private float _throwSpeed = 10f;
|
||||
|
||||
private GameObject _currentHammer;
|
||||
private bool _hasHammer = true;
|
||||
private bool _facingRight = true;
|
||||
|
||||
public bool HasHammer => _hasHammer;
|
||||
|
||||
public void SetFacingDirection(bool facingRight)
|
||||
{
|
||||
_facingRight = facingRight;
|
||||
}
|
||||
|
||||
public void ThrowHammer()
|
||||
{
|
||||
if (!_hasHammer)
|
||||
return;
|
||||
|
||||
_hasHammer = false;
|
||||
|
||||
_currentHammer = Instantiate(_hammerPrefab, _spawnPoint.position, _spawnPoint.rotation);
|
||||
|
||||
float direction = _facingRight ? 1f : -1f;
|
||||
|
||||
var rb = _currentHammer.GetComponent<Rigidbody2D>();
|
||||
rb.linearVelocity = new Vector2(direction * _throwSpeed, 0);
|
||||
|
||||
// Flip hammer visually
|
||||
var scale = _currentHammer.transform.localScale;
|
||||
scale.x = Mathf.Abs(scale.x) * direction;
|
||||
_currentHammer.transform.localScale = scale;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Hammer destroyed → hammer returned
|
||||
if (!_hasHammer && _currentHammer == null)
|
||||
{
|
||||
_hasHammer = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 59cb4a98d6866124793e8758b2ec958a
|
||||
@@ -1,14 +1,16 @@
|
||||
using System.Security;
|
||||
using UnityEngine;
|
||||
|
||||
public class KeyChest : MonoBehaviour
|
||||
{
|
||||
private void OnTriggerEnter2D(Collider2D collider)
|
||||
{
|
||||
var player = collider.GetComponent<Player>();
|
||||
if (player != null)
|
||||
var playerState = collider.GetComponent<PlayerState>();
|
||||
if (playerState != null)
|
||||
{
|
||||
player.SetKey();
|
||||
playerState.SetKey();
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c118ced0938185545aca68bbd218fcc0
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,24 +1,43 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class InputManager : MonoBehaviour
|
||||
{
|
||||
private InputActions _inputActions;
|
||||
private InputActions _actions;
|
||||
|
||||
public event EventHandler<Vector2> OnMovementInput;
|
||||
public event EventHandler OnFireButtonPressed;
|
||||
public Vector2 Movement { get; private set; }
|
||||
public event Action OnFire;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_inputActions = new InputActions();
|
||||
_inputActions.Player.Movement.performed += ctx => OnMovementInput?.Invoke(ctx.ReadValue<Vector2>());
|
||||
_inputActions.Player.Fire.performed += ctx => OnFireButtonPressed?.Invoke();
|
||||
_actions = new InputActions();
|
||||
|
||||
// Movement
|
||||
_actions.Player.Movement.performed += OnMovementPerformed;
|
||||
_actions.Player.Movement.canceled += OnMovementCanceled;
|
||||
|
||||
// Fire
|
||||
_actions.Player.Fire.performed += ctx => OnFire?.Invoke();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
public void OnEnable()
|
||||
{
|
||||
_inputActions.Enable();
|
||||
_actions.Enable();
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
public void OnDisable()
|
||||
{
|
||||
_inputActions.Disable();
|
||||
_actions.Disable();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnMovementPerformed(InputAction.CallbackContext ctx)
|
||||
{
|
||||
Movement = ctx.ReadValue<Vector2>();
|
||||
}
|
||||
|
||||
private void OnMovementCanceled(InputAction.CallbackContext ctx)
|
||||
{
|
||||
Movement = Vector2.zero;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 45d7bd38990996d4488582b07cbe7e40
|
||||
@@ -0,0 +1,44 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
public class UiManager : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private TextMeshProUGUI _totalCoins;
|
||||
[SerializeField]
|
||||
private GameObject _keyIcon;
|
||||
|
||||
private PlayerState _playerState;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
_playerState = GetComponent<PlayerState>();
|
||||
if (_playerState != null)
|
||||
{
|
||||
_playerState.OnPlayerTakeItem += UpdateUi;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateUi(object sender, TreasureType e)
|
||||
{
|
||||
switch (e)
|
||||
{
|
||||
case TreasureType.Coin:
|
||||
_totalCoins.text = _playerState.TotalCoins.ToString();
|
||||
break;
|
||||
case TreasureType.Key:
|
||||
_keyIcon.SetActive(true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
if (_playerState != null)
|
||||
{
|
||||
_playerState.OnPlayerTakeItem -= UpdateUi;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ae8d1d59de450ae4e8be540c1a9d37ff
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c718ee77619077344be4784233d84940
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b3ead8e41e973dd43b93a8638ea30a36
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c43502b5415adc044b631292348c83e8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,3 +1,6 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class PlayerState : MonoBehaviour
|
||||
{
|
||||
public int Lives = 3;
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 907b91debf5d5864780e9466f4017f38
|
||||
@@ -1,29 +0,0 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
public class UiManager : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private TextMeshProUGUI _totalCoins;
|
||||
[SerializeField]
|
||||
private GameObject _keyIcon;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
Player.Instance.OnPlayerTakeItem += Instance_OnPlayerTakeItem;
|
||||
}
|
||||
|
||||
private void Instance_OnPlayerTakeItem(object sender, TreasureType e)
|
||||
{
|
||||
switch(e)
|
||||
{
|
||||
case TreasureType.Coin:
|
||||
_totalCoins.text = Player.Instance.TotalCoins.ToString();
|
||||
break;
|
||||
case TreasureType.Key:
|
||||
_keyIcon.SetActive(true);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ff6c3960de97db14f89d6633159838dc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user