mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-06-10 23:25:54 +00:00
Enemy Control
This commit is contained in:
parent
85f0ae95d7
commit
6e8031da10
8 changed files with 184 additions and 17 deletions
|
|
@ -26,6 +26,8 @@ public partial class Enemy : CharacterBody2D
|
|||
|
||||
[Export] public Weapon EquippedWeapon;
|
||||
[Export] public Node2D DefeatScript;
|
||||
|
||||
[Export] public AiState Ai { get; private set; }
|
||||
|
||||
protected float _currentHealth = 0f;
|
||||
|
||||
|
|
@ -34,6 +36,13 @@ public partial class Enemy : CharacterBody2D
|
|||
private NavigationAgent2D _navigationAgent;
|
||||
|
||||
protected bool _invulnerable = false;
|
||||
|
||||
#region Manual Movement
|
||||
|
||||
private Vector2 _movementDirection { get; set; } = Vector2.Zero;
|
||||
private Vector2 _facingDirection { get; set; } = Vector2.Right;
|
||||
|
||||
#endregion
|
||||
|
||||
private bool IsPlayerInRange => _playerDetection is { IsPlayerInRange: true };
|
||||
|
||||
|
|
@ -50,7 +59,7 @@ public partial class Enemy : CharacterBody2D
|
|||
private AlarmManager _alarmManager;
|
||||
public bool NavigationEnabled
|
||||
{
|
||||
get => _navigationEnabled && _navigationAgent != null;
|
||||
get => Ai is AiState.Enabled && _navigationEnabled && _navigationAgent != null;
|
||||
set => _navigationEnabled = value;
|
||||
}
|
||||
|
||||
|
|
@ -95,23 +104,55 @@ public partial class Enemy : CharacterBody2D
|
|||
// Called every frame. 'delta' is the elapsed time since the previous frame.
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
switch (_currentState)
|
||||
// switch (_currentState)
|
||||
// {
|
||||
// case EnemyState.Idle:
|
||||
//
|
||||
// break;
|
||||
// case EnemyState.Alert:
|
||||
// break;
|
||||
//
|
||||
// //case EnemyState.Shooting:
|
||||
// // Shoot
|
||||
// //break;
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
|
||||
if (Ai is AiState.Controlled && !_isDestroyed)
|
||||
{
|
||||
case EnemyState.Idle:
|
||||
_movementDirection = GetInput();
|
||||
_facingDirection = _movementDirection;
|
||||
}
|
||||
}
|
||||
|
||||
private Vector2 GetInput()
|
||||
{
|
||||
return Input.GetVector("left", "right", "up", "down");
|
||||
}
|
||||
|
||||
break;
|
||||
case EnemyState.Alert:
|
||||
break;
|
||||
|
||||
//case EnemyState.Shooting:
|
||||
// Shoot
|
||||
//break;
|
||||
default:
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
if (_isDestroyed) return;
|
||||
|
||||
switch (Ai)
|
||||
{
|
||||
case AiState.Enabled:
|
||||
HandleAi(delta);
|
||||
return;
|
||||
case AiState.Controlled:
|
||||
HandleManualControl(delta);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
private void HandleManualControl(double delta)
|
||||
{
|
||||
Velocity = _movementDirection * (float)(WalkSpeed * delta);
|
||||
MoveAndSlide();
|
||||
}
|
||||
|
||||
private void HandleAi(double delta)
|
||||
{
|
||||
switch (_currentState)
|
||||
{
|
||||
|
|
@ -261,10 +302,23 @@ public partial class Enemy : CharacterBody2D
|
|||
return _isDestroyed;
|
||||
}
|
||||
|
||||
private enum EnemyState
|
||||
public void AssumeControl()
|
||||
{
|
||||
GD.Print("Assuming direct control");
|
||||
Ai = AiState.Controlled;
|
||||
}
|
||||
|
||||
protected enum EnemyState
|
||||
{
|
||||
Idle,
|
||||
Alert,
|
||||
Patrolling
|
||||
}
|
||||
|
||||
public enum AiState
|
||||
{
|
||||
Enabled,
|
||||
Disabled,
|
||||
Controlled
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,6 +24,9 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
|
|||
|
||||
[Export]
|
||||
public string GameOverScene { get; set; }
|
||||
|
||||
[Export]
|
||||
public Texture2D WingsSprite { get; set; }
|
||||
|
||||
private Selector _selector;
|
||||
|
||||
|
|
@ -332,7 +335,7 @@ public partial class PlayerMovement : CharacterBody2D, IDestructible
|
|||
{
|
||||
if (!_canMove) return;
|
||||
|
||||
Velocity = _movementDirection * (float)( MovementSpeed/* * delta*/);
|
||||
Velocity = _movementDirection * MovementSpeed;
|
||||
|
||||
MoveAndSlide();
|
||||
}
|
||||
|
|
|
|||
50
Scripts/Resources/Events/ControlEnemyEvent.cs
Normal file
50
Scripts/Resources/Events/ControlEnemyEvent.cs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
using Godot;
|
||||
|
||||
namespace Cirno.Scripts.Resources.Events;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class ControlEnemyEvent : EventResource
|
||||
{
|
||||
[Export]
|
||||
public NodePath Target { get; set; }
|
||||
|
||||
private Node2D _parent;
|
||||
|
||||
private GameManager _gameManager;
|
||||
private bool _isComplete = false;
|
||||
public override bool IsComplete()
|
||||
{
|
||||
return _isComplete;
|
||||
}
|
||||
|
||||
public override void Init(Node2D parent)
|
||||
{
|
||||
_gameManager = parent.GetGameManager();
|
||||
_parent = parent;
|
||||
}
|
||||
|
||||
public override void Start(Node2D parentNode)
|
||||
{
|
||||
_isComplete = false;
|
||||
|
||||
if (_parent.GetNode<Node2D>(Target) is Enemy enemy)
|
||||
{
|
||||
_gameManager.CameraTargetObject(enemy);
|
||||
_gameManager.Player.RequestMovementDisable(true);
|
||||
enemy.AssumeControl();
|
||||
if (_gameManager.Player.WingsSprite != null)
|
||||
{
|
||||
var sprite = new Sprite2D();
|
||||
sprite.SetTexture(_gameManager.Player.WingsSprite);
|
||||
//sprite.GlobalPosition = enemy.GlobalPosition;
|
||||
sprite.SetZIndex(1);
|
||||
enemy.CallDeferred("add_child", sprite);
|
||||
}
|
||||
}
|
||||
|
||||
_isComplete = true;
|
||||
}
|
||||
|
||||
public override void UpdateEvent(double delta) { }
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue