Refactor player and enemy scripts; remove unused classes and improve initialization
- Added base initialization call in PlayerController. - Changed player reference in EnemyAI to be serialized for better inspector visibility. - Ensured characters are initialized upon spawning in EnemySpawner. - Removed obsolete EnemyAI and Chest scripts to streamline codebase. - Added KeyChest script for key collection functionality. - Introduced new meta files for better organization and tracking. - Created launch configuration for Unity debugging.
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1894a524a4002784fbf2d10e74478b33
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4d7148405c33f2f45ae0479592e8cb6e
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -25,6 +25,7 @@ public class PlayerController : Character
|
||||
_hammerThrower = GetComponent<HammerThrower>();
|
||||
|
||||
_inputManager.OnFire += OnFireButtonPressed;
|
||||
base.Init();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 41c6c94d439ebee4a885826d8ddc05a1
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 239d5eb73385f2d4ea050bc2c19298a2
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -24,7 +24,7 @@ public class EnemyAI : Character
|
||||
private float _stunTimer = 0f;
|
||||
private float _patrolDirection = 1f;
|
||||
|
||||
private static readonly Player _player = null;
|
||||
[SerializeField] private Character _player = null;
|
||||
|
||||
public EnemyState CurrentState => _currentState;
|
||||
|
||||
@@ -58,7 +58,7 @@ public class EnemyAI : Character
|
||||
}
|
||||
|
||||
// Get player position if available
|
||||
var player = Player.Instance;
|
||||
var player = _player;
|
||||
if (player == null)
|
||||
{
|
||||
HandlePatrol();
|
||||
|
||||
@@ -40,6 +40,7 @@ public class EnemySpawner : MonoBehaviour
|
||||
_respawnElementTimer = _respawnTimeout;
|
||||
var prefab= Instantiate(_prefab, _spawnPoint.position, _spawnPoint.rotation);
|
||||
var character=prefab.GetComponent<Character>();
|
||||
character.Init();
|
||||
character.OnCharacterDeath+=OnCharacterDeath;
|
||||
|
||||
_characters.Add(character);
|
||||
|
||||
@@ -1,171 +0,0 @@
|
||||
using UnityEngine;
|
||||
using System;
|
||||
|
||||
public enum EnemyState
|
||||
{
|
||||
Patrol,
|
||||
Investigate,
|
||||
Chase,
|
||||
Stunned
|
||||
}
|
||||
|
||||
public class EnemyAI : Character
|
||||
{
|
||||
[SerializeField] private float _patrolSpeed = 1f;
|
||||
[SerializeField] private float _patrolRange = 5f;
|
||||
[SerializeField] private float _investigateRange = 8f;
|
||||
[SerializeField] private float _chaseRange = 10f;
|
||||
[SerializeField] private float _stunDuration = 1f;
|
||||
[SerializeField] private bool _debugMode = false;
|
||||
|
||||
private EnemyState _currentState = EnemyState.Patrol;
|
||||
private Vector3 _patrolTarget;
|
||||
private Vector3 _investigatePosition;
|
||||
private float _stunTimer = 0f;
|
||||
private float _patrolDirection = 1f;
|
||||
|
||||
private static readonly Player _player = null;
|
||||
|
||||
public EnemyState CurrentState => _currentState;
|
||||
|
||||
protected override void SetClimbingAnimation(bool isClimbing)
|
||||
{
|
||||
// Implement climbing animation if needed
|
||||
}
|
||||
|
||||
protected override void SetWalkingAnimation(bool isWalking)
|
||||
{
|
||||
_animator.SetBool("Walk", isWalking);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_patrolTarget = transform.position;
|
||||
SetState(EnemyState.Patrol);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Update stun timer
|
||||
if (_currentState == EnemyState.Stunned)
|
||||
{
|
||||
_stunTimer -= Time.deltaTime;
|
||||
if (_stunTimer <= 0f)
|
||||
{
|
||||
SetState(EnemyState.Patrol);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Get player position if available
|
||||
var player = Player.Instance;
|
||||
if (player == null)
|
||||
{
|
||||
HandlePatrol();
|
||||
return;
|
||||
}
|
||||
|
||||
float distanceToPlayer = Vector3.Distance(transform.position, player.transform.position);
|
||||
|
||||
// State transitions
|
||||
switch (_currentState)
|
||||
{
|
||||
case EnemyState.Patrol:
|
||||
if (distanceToPlayer < _investigateRange)
|
||||
{
|
||||
SetState(EnemyState.Chase);
|
||||
}
|
||||
else
|
||||
{
|
||||
HandlePatrol();
|
||||
}
|
||||
break;
|
||||
|
||||
case EnemyState.Investigate:
|
||||
if (distanceToPlayer < _chaseRange)
|
||||
{
|
||||
SetState(EnemyState.Chase);
|
||||
}
|
||||
else if (Vector3.Distance(transform.position, _investigatePosition) < 0.5f)
|
||||
{
|
||||
SetState(EnemyState.Patrol);
|
||||
}
|
||||
else
|
||||
{
|
||||
HandleInvestigate();
|
||||
}
|
||||
break;
|
||||
|
||||
case EnemyState.Chase:
|
||||
if (distanceToPlayer > _chaseRange)
|
||||
{
|
||||
SetState(EnemyState.Patrol);
|
||||
}
|
||||
else
|
||||
{
|
||||
HandleChase(player.transform.position);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void SetState(EnemyState newState)
|
||||
{
|
||||
if (_currentState == newState)
|
||||
return;
|
||||
|
||||
if (_debugMode)
|
||||
Debug.Log($"[EnemyAI] State changed: {_currentState} -> {newState}");
|
||||
|
||||
_currentState = newState;
|
||||
}
|
||||
|
||||
private void HandlePatrol()
|
||||
{
|
||||
// Simple back-and-forth patrol
|
||||
if (Vector3.Distance(transform.position, _patrolTarget) < 0.3f)
|
||||
{
|
||||
_patrolDirection *= -1f;
|
||||
_patrolTarget = transform.position + Vector3.right * _patrolRange * _patrolDirection;
|
||||
}
|
||||
|
||||
float direction = _patrolTarget.x > transform.position.x ? 1f : -1f;
|
||||
MoveTo(direction * _patrolSpeed, 0f);
|
||||
}
|
||||
|
||||
private void HandleInvestigate()
|
||||
{
|
||||
float direction = _investigatePosition.x > transform.position.x ? 1f : -1f;
|
||||
MoveTo(direction * _patrolSpeed, 0f);
|
||||
}
|
||||
|
||||
private void HandleChase(Vector3 playerPosition)
|
||||
{
|
||||
float direction = playerPosition.x > transform.position.x ? 1f : -1f;
|
||||
MoveTo(direction * _patrolSpeed, 0f);
|
||||
}
|
||||
|
||||
public void OnNoise(Vector3 noisePosition)
|
||||
{
|
||||
if (_currentState == EnemyState.Stunned)
|
||||
return;
|
||||
|
||||
if (_currentState != EnemyState.Chase)
|
||||
{
|
||||
_investigatePosition = noisePosition;
|
||||
SetState(EnemyState.Investigate);
|
||||
|
||||
if (_debugMode)
|
||||
Debug.Log($"[EnemyAI] Investigating noise at {noisePosition}");
|
||||
}
|
||||
}
|
||||
|
||||
public void OnHitByHammer(float stunDuration)
|
||||
{
|
||||
_stunTimer = stunDuration;
|
||||
SetState(EnemyState.Stunned);
|
||||
|
||||
if (_debugMode)
|
||||
Debug.Log($"[EnemyAI] Stunned for {stunDuration} seconds");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: de0c7abb53bb1e645a77e9b03418ae21
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,52 +0,0 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Chest : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private Animator animator;
|
||||
[SerializeField]
|
||||
private TreasureSO _treasureSO;
|
||||
|
||||
private Transform _treasureObject;
|
||||
private bool _isOpen = false;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_treasureObject = transform.GetChild(1);
|
||||
var spriteRenderer = _treasureObject.GetComponent<SpriteRenderer>();
|
||||
|
||||
spriteRenderer.sprite = _treasureSO.Image;
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D collider)
|
||||
{
|
||||
var playerState = collider.GetComponent<PlayerState>();
|
||||
if (playerState != null && !_isOpen)
|
||||
{
|
||||
_isOpen = true;
|
||||
animator.SetTrigger("OpenChest");
|
||||
|
||||
switch (_treasureSO.Treasure)
|
||||
{
|
||||
case TreasureType.Coin:
|
||||
playerState.AddCoin();
|
||||
if (GameManager.Instance != null)
|
||||
{
|
||||
GameManager.Instance.AddTreasure(1);
|
||||
}
|
||||
break;
|
||||
case TreasureType.Key:
|
||||
playerState.SetKey();
|
||||
if (GameManager.Instance != null)
|
||||
{
|
||||
GameManager.Instance.SetKeyState(true);
|
||||
}
|
||||
if (LevelManager.Instance != null)
|
||||
{
|
||||
LevelManager.Instance.NotifyKeyCollected();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ded23e667cffbfd46b2633ae22204797
|
||||
@@ -1,63 +0,0 @@
|
||||
using Assets.Scripts;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class Door : MonoBehaviour, IDoor
|
||||
{
|
||||
[SerializeField]
|
||||
private Sprite _openDoor;
|
||||
[SerializeField]
|
||||
private bool _debugMode = false;
|
||||
|
||||
private SpriteRenderer _spriteRenderer;
|
||||
private BoxCollider2D _boxCollider;
|
||||
private bool _isLocked = true;
|
||||
|
||||
public bool IsLocked => _isLocked;
|
||||
|
||||
public event EventHandler OnDoorOpened;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_spriteRenderer = GetComponentInChildren<SpriteRenderer>();
|
||||
_boxCollider = gameObject.GetComponent<BoxCollider2D>();
|
||||
}
|
||||
|
||||
public void OpenDoor()
|
||||
{
|
||||
if (!_isLocked)
|
||||
return;
|
||||
|
||||
_isLocked = false;
|
||||
|
||||
// Update visuals
|
||||
if (_spriteRenderer != null && _openDoor != null)
|
||||
{
|
||||
_spriteRenderer.sprite = _openDoor;
|
||||
}
|
||||
|
||||
// Disable collision
|
||||
if (_boxCollider != null)
|
||||
{
|
||||
_boxCollider.enabled = false;
|
||||
}
|
||||
|
||||
if (_debugMode)
|
||||
Debug.Log("[Door] Door opened!");
|
||||
|
||||
OnDoorOpened?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
public void LockDoor()
|
||||
{
|
||||
_isLocked = true;
|
||||
|
||||
if (_boxCollider != null)
|
||||
{
|
||||
_boxCollider.enabled = true;
|
||||
}
|
||||
|
||||
if (_debugMode)
|
||||
Debug.Log("[Door] Door locked!");
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: af69aa896ad8a3947947ed21a9b9cacd
|
||||
@@ -1,45 +0,0 @@
|
||||
using Assets.Scripts;
|
||||
using UnityEngine;
|
||||
|
||||
public class DoorInteract : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameObject _doorGameObject;
|
||||
[SerializeField] private bool _debugMode = false;
|
||||
|
||||
private IDoor _door;
|
||||
private bool _hasTriggered = false;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_door = _doorGameObject.GetComponent<IDoor>();
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D collider)
|
||||
{
|
||||
if (_hasTriggered)
|
||||
return;
|
||||
|
||||
var playerState = collider.GetComponent<PlayerState>();
|
||||
if (playerState != null)
|
||||
{
|
||||
// Check if player has key through GameManager
|
||||
if (GameManager.Instance != null && GameManager.Instance.HasKey)
|
||||
{
|
||||
_hasTriggered = true;
|
||||
|
||||
if (_debugMode)
|
||||
Debug.Log("[DoorInteract] Player exiting with key!");
|
||||
|
||||
// Notify LevelManager that level is complete
|
||||
if (LevelManager.Instance != null)
|
||||
{
|
||||
LevelManager.Instance.NotifyLevelComplete();
|
||||
}
|
||||
}
|
||||
else if (_debugMode)
|
||||
{
|
||||
Debug.Log("[DoorInteract] Player reached door but does not have key!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 73fd9f6c116c2de4ab3d773fbb908df4
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 87152ae05a4335f4cbf3907d245027eb
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 86cce1993173eb04daddb1edd164da94
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,82 +0,0 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class BreakableWall : MapElement
|
||||
{
|
||||
[SerializeField] private float _noiseRadius = 10f;
|
||||
[SerializeField] private bool _emitNoiseOnBreak = true;
|
||||
|
||||
private float _respawnElementTimer;
|
||||
private int _respawnTimeout = 4;
|
||||
private bool _needRespawn = false;
|
||||
private bool _characterInRange = false;
|
||||
|
||||
private BoxCollider2D _boxCollider;
|
||||
private SpriteRenderer _spriteRenderer;
|
||||
|
||||
[SerializeField]
|
||||
private GameObject _hitParticles;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_respawnElementTimer = _respawnTimeout;
|
||||
_boxCollider = GetComponent<BoxCollider2D>();
|
||||
_spriteRenderer = GetComponentInChildren<SpriteRenderer>();
|
||||
}
|
||||
|
||||
public override void Hit()
|
||||
{
|
||||
IsEnabled = false;
|
||||
_boxCollider.isTrigger = true;
|
||||
_spriteRenderer.enabled = IsEnabled;
|
||||
|
||||
Instantiate(_hitParticles, transform.position, Quaternion.identity);
|
||||
|
||||
// Emit noise when wall breaks
|
||||
if (_emitNoiseOnBreak && NoiseSystem.Instance != null)
|
||||
{
|
||||
NoiseSystem.Instance.Emit(transform.position, _noiseRadius);
|
||||
}
|
||||
|
||||
_respawnElementTimer = _respawnTimeout;
|
||||
_needRespawn = true;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_needRespawn)
|
||||
{
|
||||
_respawnElementTimer -= Time.deltaTime;
|
||||
if (_respawnElementTimer <= 0)
|
||||
{
|
||||
_respawnElementTimer = _respawnTimeout;
|
||||
|
||||
if (_characterInRange)
|
||||
{
|
||||
print("Character is dead");
|
||||
}
|
||||
IsEnabled = true;
|
||||
_boxCollider.isTrigger = false;
|
||||
_spriteRenderer.enabled = IsEnabled;
|
||||
_needRespawn = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D collider)
|
||||
{
|
||||
var character = collider.GetComponent<Character>();
|
||||
if (character)
|
||||
{
|
||||
_characterInRange = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnTriggerExit2D(Collider2D collider)
|
||||
{
|
||||
var character = collider.GetComponent<Character>();
|
||||
if (character)
|
||||
{
|
||||
_characterInRange = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d25d392c71acf0a43a02538234591802
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5b7fcd68a9cc8f64bbc27a6eb9ec68a1
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6f6373350bf47454e899b0333377f443
|
||||
@@ -1,97 +0,0 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Hammer : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float _lifespan = 5f;
|
||||
[SerializeField] private float _stunDuration = 1f;
|
||||
[SerializeField] private float _impactNoiseRadius = 10f;
|
||||
[SerializeField] private bool _emitNoiseOnImpact = true;
|
||||
|
||||
private float _lifeTimer;
|
||||
private Vector2 _velocity;
|
||||
private bool _facingRight;
|
||||
private Rigidbody2D _rigidbody;
|
||||
private bool _hasCollided = false;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_rigidbody = GetComponent<Rigidbody2D>();
|
||||
_lifeTimer = _lifespan;
|
||||
}
|
||||
|
||||
public void Initialize(bool facingRight, float speed)
|
||||
{
|
||||
_facingRight = facingRight;
|
||||
_velocity = new Vector2(facingRight ? speed : -speed, 0);
|
||||
|
||||
if (_rigidbody != null)
|
||||
{
|
||||
_rigidbody.linearVelocity = _velocity;
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// Self-destruct after lifespan expires
|
||||
_lifeTimer -= Time.deltaTime;
|
||||
if (_lifeTimer <= 0)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnCollisionEnter2D(Collision2D collision)
|
||||
{
|
||||
if (_hasCollided)
|
||||
return;
|
||||
|
||||
_hasCollided = true;
|
||||
|
||||
// Check for enemy collision (stun)
|
||||
var enemy = collision.gameObject.GetComponent<Character>();
|
||||
if (enemy != null)
|
||||
{
|
||||
HandleEnemyCollision(enemy, collision.relativeVelocity);
|
||||
EmitImpactNoise(collision.GetContact(0).point);
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for breakable wall collision
|
||||
var mapElement = collision.collider.GetComponent<MapElement>();
|
||||
if (mapElement != null && mapElement is BreakableWall)
|
||||
{
|
||||
mapElement.Hit();
|
||||
EmitImpactNoise(collision.GetContact(0).point);
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
// Fallback: destroy on any collision
|
||||
EmitImpactNoise(collision.GetContact(0).point);
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
private void HandleEnemyCollision(Character enemy, Vector2 impactVelocity)
|
||||
{
|
||||
// Apply stun to enemy
|
||||
var enemyAI = enemy as EnemyAI;
|
||||
if (enemyAI != null)
|
||||
{
|
||||
enemyAI.OnHitByHammer(_stunDuration);
|
||||
}
|
||||
}
|
||||
|
||||
private void EmitImpactNoise(Vector2 position)
|
||||
{
|
||||
if (!_emitNoiseOnImpact)
|
||||
return;
|
||||
|
||||
// Check if NoiseSystem exists and emit noise
|
||||
var noiseSystem = NoiseSystem.Instance;
|
||||
if (noiseSystem != null)
|
||||
{
|
||||
noiseSystem.Emit(position, _impactNoiseRadius);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class KeyChest : MonoBehaviour
|
||||
{
|
||||
private bool _isOpened = false;
|
||||
|
||||
private void OnTriggerEnter2D(Collider2D collider)
|
||||
{
|
||||
if (_isOpened)
|
||||
return;
|
||||
|
||||
var playerState = collider.GetComponent<PlayerState>();
|
||||
if (playerState != null)
|
||||
{
|
||||
_isOpened = true;
|
||||
|
||||
// Update player state
|
||||
playerState.SetKey();
|
||||
|
||||
// Notify GameManager of key collection
|
||||
if (GameManager.Instance != null)
|
||||
{
|
||||
GameManager.Instance.SetKeyState(true);
|
||||
}
|
||||
|
||||
// Notify LevelManager of key collection
|
||||
if (LevelManager.Instance != null)
|
||||
{
|
||||
LevelManager.Instance.NotifyKeyCollected();
|
||||
}
|
||||
|
||||
// Destroy the chest
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8f37247791475bc4bb2887347a0f905a
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 95776c7bbfb13d646b34788f8187536f
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 897c2741fe0100e4990a32480e24d9ae
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f581a5f2ca3d85c4e802df5ab7e0968b
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,102 +0,0 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9c83b0150e991b443858a82f5a1eea57
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2f79ba21609b9ec4e8dc6985c2ec27f5
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -37,7 +37,7 @@ public abstract class Character : MonoBehaviour
|
||||
|
||||
public event EventHandler OnCharacterDeath;
|
||||
|
||||
private void Start()
|
||||
public void Init()
|
||||
{
|
||||
_body = GetComponent<Rigidbody2D>();
|
||||
_capsuleCollider = GetComponent<CapsuleCollider2D>();
|
||||
|
||||
Reference in New Issue
Block a user