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:
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
public static GameManager Instance { get; private set; }
|
||||
|
||||
[SerializeField] private bool _debugMode = false;
|
||||
|
||||
private bool _hasKey = false;
|
||||
private int _treasureCount = 0;
|
||||
|
||||
public bool HasKey => _hasKey;
|
||||
public int TreasureCount => _treasureCount;
|
||||
|
||||
public event EventHandler<bool> OnKeyStateChanged;
|
||||
public event EventHandler<int> OnTreasureCountChanged;
|
||||
public event EventHandler OnLevelComplete;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// Singleton pattern
|
||||
if (Instance != null && Instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
Instance = this;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
|
||||
public void SetKeyState(bool hasKey)
|
||||
{
|
||||
if (_hasKey == hasKey)
|
||||
return;
|
||||
|
||||
_hasKey = hasKey;
|
||||
OnKeyStateChanged?.Invoke(this, _hasKey);
|
||||
|
||||
if (_debugMode)
|
||||
Debug.Log($"[GameManager] Key state changed: {_hasKey}");
|
||||
}
|
||||
|
||||
public void AddTreasure(int amount = 1)
|
||||
{
|
||||
_treasureCount += amount;
|
||||
OnTreasureCountChanged?.Invoke(this, _treasureCount);
|
||||
|
||||
if (_debugMode)
|
||||
Debug.Log($"[GameManager] Treasure count: {_treasureCount}");
|
||||
}
|
||||
|
||||
public void CompletLevel()
|
||||
{
|
||||
if (_debugMode)
|
||||
Debug.Log("[GameManager] Level completed!");
|
||||
|
||||
OnLevelComplete?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_hasKey = false;
|
||||
_treasureCount = 0;
|
||||
|
||||
if (_debugMode)
|
||||
Debug.Log("[GameManager] Reset");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class LevelManager : MonoBehaviour
|
||||
{
|
||||
public static LevelManager Instance { get; private set; }
|
||||
|
||||
[SerializeField] private bool _debugMode = false;
|
||||
[SerializeField] private Door _doorReference;
|
||||
|
||||
private bool _keyCollected = false;
|
||||
private bool _levelComplete = false;
|
||||
|
||||
public bool KeyCollected => _keyCollected;
|
||||
public bool LevelComplete => _levelComplete;
|
||||
|
||||
public event EventHandler OnKeyCollected;
|
||||
public event EventHandler OnDoorUnlocked;
|
||||
public event EventHandler OnLevelComplete;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// Singleton pattern
|
||||
if (Instance != null && Instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// Subscribe to GameManager key state changes
|
||||
if (GameManager.Instance != null)
|
||||
{
|
||||
GameManager.Instance.OnKeyStateChanged += HandleKeyStateChanged;
|
||||
}
|
||||
}
|
||||
|
||||
public void NotifyKeyCollected()
|
||||
{
|
||||
if (_keyCollected)
|
||||
return;
|
||||
|
||||
_keyCollected = true;
|
||||
|
||||
if (_debugMode)
|
||||
Debug.Log("[LevelManager] Key collected!");
|
||||
|
||||
OnKeyCollected?.Invoke(this, EventArgs.Empty);
|
||||
UnlockDoor();
|
||||
}
|
||||
|
||||
public void UnlockDoor()
|
||||
{
|
||||
if (_doorReference == null)
|
||||
{
|
||||
Debug.LogWarning("[LevelManager] Door reference not assigned!");
|
||||
return;
|
||||
}
|
||||
|
||||
_doorReference.OpenDoor();
|
||||
|
||||
if (_debugMode)
|
||||
Debug.Log("[LevelManager] Door unlocked!");
|
||||
|
||||
OnDoorUnlocked?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
public void NotifyLevelComplete()
|
||||
{
|
||||
if (_levelComplete)
|
||||
return;
|
||||
|
||||
_levelComplete = true;
|
||||
|
||||
if (_debugMode)
|
||||
Debug.Log("[LevelManager] Level complete!");
|
||||
|
||||
OnLevelComplete?.Invoke(this, EventArgs.Empty);
|
||||
|
||||
// Also notify GameManager
|
||||
if (GameManager.Instance != null)
|
||||
{
|
||||
GameManager.Instance.CompletLevel();
|
||||
}
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_keyCollected = false;
|
||||
_levelComplete = false;
|
||||
|
||||
if (_debugMode)
|
||||
Debug.Log("[LevelManager] Reset");
|
||||
}
|
||||
|
||||
private void HandleKeyStateChanged(object sender, bool hasKey)
|
||||
{
|
||||
if (hasKey && !_keyCollected)
|
||||
{
|
||||
NotifyKeyCollected();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (GameManager.Instance != null)
|
||||
{
|
||||
GameManager.Instance.OnKeyStateChanged -= HandleKeyStateChanged;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class NoiseSystem : MonoBehaviour
|
||||
{
|
||||
public static NoiseSystem Instance { get; private set; }
|
||||
|
||||
[SerializeField] private bool _debugMode = false;
|
||||
[SerializeField] private LayerMask _enemyLayer = LayerMask.GetMask("Enemy");
|
||||
|
||||
public event EventHandler<NoiseEventArgs> OnNoiseEmitted;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
// Singleton pattern
|
||||
if (Instance != null && Instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
public void Emit(Vector3 position, float radius)
|
||||
{
|
||||
if (_debugMode)
|
||||
Debug.Log($"[NoiseSystem] Noise emitted at {position} with radius {radius}");
|
||||
|
||||
OnNoiseEmitted?.Invoke(this, new NoiseEventArgs { Position = position, Radius = radius });
|
||||
|
||||
// Find all enemies in range and notify them
|
||||
Collider2D[] hitColliders = Physics2D.OverlapCircleAll(position, radius, _enemyLayer);
|
||||
|
||||
foreach (Collider2D collider in hitColliders)
|
||||
{
|
||||
var enemy = collider.GetComponent<EnemyAI>();
|
||||
if (enemy != null)
|
||||
{
|
||||
enemy.OnNoise(position);
|
||||
|
||||
if (_debugMode)
|
||||
Debug.Log($"[NoiseSystem] Notified enemy at {collider.transform.position}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
// Debug visualization in editor
|
||||
if (!_debugMode)
|
||||
return;
|
||||
|
||||
Gizmos.color = Color.yellow;
|
||||
// This would need to be called from a debug script to visualize noise spheres
|
||||
}
|
||||
}
|
||||
|
||||
public class NoiseEventArgs : EventArgs
|
||||
{
|
||||
public Vector3 Position { get; set; }
|
||||
public float Radius { get; set; }
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
public class UiManager : MonoBehaviour
|
||||
public class UIManager : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private TextMeshProUGUI _totalCoins;
|
||||
|
||||
Reference in New Issue
Block a user