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,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);
|
||||
}
|
||||
|
||||
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,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 852eca26194d18e4fac550283d37f0a1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class EnemySpawner : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private GameObject _prefab;
|
||||
|
||||
[SerializeField]
|
||||
private int _maxCharacters=1;
|
||||
|
||||
[SerializeField]
|
||||
private Transform _spawnPoint;
|
||||
|
||||
private int _respawnTimeout = 4;
|
||||
private float _respawnElementTimer;
|
||||
|
||||
private List<Character> _characters;
|
||||
|
||||
// Start is called before the first frame update
|
||||
private void Start()
|
||||
{
|
||||
if(_spawnPoint==null)
|
||||
{
|
||||
_spawnPoint=gameObject.transform;
|
||||
}
|
||||
_characters=new List<Character>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
private void Update()
|
||||
{
|
||||
if(_characters.Count<_maxCharacters)
|
||||
{
|
||||
_respawnElementTimer -= Time.deltaTime;
|
||||
if (_respawnElementTimer <= 0)
|
||||
{
|
||||
_respawnElementTimer = _respawnTimeout;
|
||||
var prefab= Instantiate(_prefab, _spawnPoint.position, _spawnPoint.rotation);
|
||||
var character=prefab.GetComponent<Character>();
|
||||
character.OnCharacterDeath+=OnCharacterDeath;
|
||||
|
||||
_characters.Add(character);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnCharacterDeath(object sender, EventArgs e)
|
||||
{
|
||||
var character=(sender as Character);
|
||||
character.OnCharacterDeath-=OnCharacterDeath;
|
||||
_characters.Remove(character);
|
||||
Destroy(character.gameObject);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4eeeff2f5d376d04793a6175a82f037c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user