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:
+147
-32
@@ -1,56 +1,171 @@
|
||||
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);
|
||||
_animator.SetBool("Walk", isWalking);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_patrolTarget = transform.position;
|
||||
SetState(EnemyState.Patrol);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
// float horizontal = 0;
|
||||
// float vertical = 0;
|
||||
// Update stun timer
|
||||
if (_currentState == EnemyState.Stunned)
|
||||
{
|
||||
_stunTimer -= Time.deltaTime;
|
||||
if (_stunTimer <= 0f)
|
||||
{
|
||||
SetState(EnemyState.Patrol);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Vector2 directionToPlayer = Player.Instance.transform.position - transform.position;
|
||||
// directionToPlayer.Normalize();
|
||||
// Get player position if available
|
||||
var player = Player.Instance;
|
||||
if (player == null)
|
||||
{
|
||||
HandlePatrol();
|
||||
return;
|
||||
}
|
||||
|
||||
// float verticalDistance = Player.Instance.transform.position.y - transform.position.y;
|
||||
float distanceToPlayer = Vector3.Distance(transform.position, player.transform.position);
|
||||
|
||||
// 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; }
|
||||
// }
|
||||
// State transitions
|
||||
switch (_currentState)
|
||||
{
|
||||
case EnemyState.Patrol:
|
||||
if (distanceToPlayer < _investigateRange)
|
||||
{
|
||||
SetState(EnemyState.Chase);
|
||||
}
|
||||
else
|
||||
{
|
||||
HandlePatrol();
|
||||
}
|
||||
break;
|
||||
|
||||
// if (Input.GetKey(KeyCode.T))
|
||||
// { vertical = 1; }
|
||||
// if (Input.GetKey(KeyCode.G))
|
||||
// { vertical = -1; }
|
||||
case EnemyState.Investigate:
|
||||
if (distanceToPlayer < _chaseRange)
|
||||
{
|
||||
SetState(EnemyState.Chase);
|
||||
}
|
||||
else if (Vector3.Distance(transform.position, _investigatePosition) < 0.5f)
|
||||
{
|
||||
SetState(EnemyState.Patrol);
|
||||
}
|
||||
else
|
||||
{
|
||||
HandleInvestigate();
|
||||
}
|
||||
break;
|
||||
|
||||
// Debug.Log($"Enemy Position: {transform.position}, Player Position: {Player.Instance.transform.position}");
|
||||
// Debug.Log($"Vertical Distance: {verticalDistance}, Vertical Movement: {vertical}");
|
||||
|
||||
// base.MoveTo(horizontal, vertical);
|
||||
case EnemyState.Chase:
|
||||
if (distanceToPlayer > _chaseRange)
|
||||
{
|
||||
SetState(EnemyState.Patrol);
|
||||
}
|
||||
else
|
||||
{
|
||||
HandleChase(player.transform.position);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private float VerticalMove(float verticalDistance)
|
||||
private void SetState(EnemyState newState)
|
||||
{
|
||||
float verticalDirection = Mathf.Sign(verticalDistance);
|
||||
return verticalDirection;
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user