Isometric implementation

This commit is contained in:
Marco 2025-06-10 16:33:43 +02:00
commit ed656f00bb
92 changed files with 2834 additions and 223 deletions

View file

@ -2,7 +2,7 @@
namespace Cirno.Scripts.Components.Actors;
public abstract partial class InputProvider : Node2D
public abstract partial class InputProvider : Node
{
public abstract Vector2 GetMovementInput();
public abstract Vector2 GetAimInput();

View file

@ -0,0 +1,165 @@
using Godot;
namespace Cirno.Scripts.Components.FSM._3DPlayer;
public partial class Active : BaseState<PlayerState, CharacterBody3D>
{
public override PlayerState StateId => PlayerState.Active;
private CharacterBody3D _player;
public override void Init(IStateMachine<PlayerState, CharacterBody3D> machine)
{
base.Init(machine);
//_hud = Hud.Instance;
//_damageReceiver.Death += () => { ChangeState(PlayerState.Dead); };
// _damageReceiver.HealthDecreased += (value, newValue, maxValue) =>
// {
// _animationProvider.Blink();
// };
// _damageReceiver.ShieldDecreased += (value, newValue, maxValue) =>
// {
// _animationProvider.PlayShieldAnimation();
//
// };
//_damageReceiver.Init(StateMachine);
//_damageReceiver.RefillHealth();
//_damageReceiver.RefillShield();
//_activationProvider.Init(MainObject);
//_weaponProvider.Init(MainObject);
}
public override void EnterState()
{
base.EnterState();
// enable sprite
// enable crosshair
//_crosshairProvider.Show();
//_animationProvider.ShowSprite();
//_damageReceiver.Enabled = true;
//_activationProvider.Enabled = true;
//_interactionController.Enabled = true;
}
public override void ExitState()
{
base.ExitState();
// _animationProvider.SetAnimationSpeed(Vector2.Zero);
// //_animationProvider.SetAnimation(Vector2.Zero);
// _crosshairProvider.Hide();
// _hitboxSpriteProvider.Hide();
//
// _damageReceiver.Enabled = false;
// _activationProvider.Enabled = false;
// _interactionController.Enabled = false;
}
public override void PhysicsProcessState(double delta)
{
// Reset at start of frame
//MainObject.Velocity = Vector2.Zero;
// Process modules
base.PhysicsProcessState(delta);
}
public override void ProcessState(double delta)
{
base.ProcessState(delta);
_movementDirection = _inputProvider.GetMovementInput().Normalized();
_isStrafing = _inputProvider.GetStrafePressed();
// Toggle visibility of the hitbox sprite based on strafing
_hitboxSpriteProvider.SetVisibility(_isStrafing);
var rightStickInput = _inputProvider.GetAimInput().Normalized();
// Update Facing Direction
// if (!_isStrafing)
// {
if (rightStickInput.Length() > 0.1f) // If the right stick is moved
{
FacingDirection = rightStickInput;
}
else if (_movementDirection != Vector2.Zero) // Fall back to movement direction
{
FacingDirection = _movementDirection;
}
// }
_animationProvider.SetAnimationSpeed(MainObject.Velocity);
_animationProvider.SetAnimation(FacingDirection);
HandleWeaponSwitch();
_weaponProvider.Update(delta);
//_crosshairProvider.UpdatePosition(FacingDirection);
HandleShoot();
HandleInteraction();
// FindInteractable();
// _crosshair.Position = CalculateCrosshairPosition();
if (_inputProvider.GetInventoryJustPressed())
{
GameManager.Instance.ChangeState(GameState.Inventory);
}
if (_inputProvider.GetPauseJustPressed())
{
//CallDeferred(MethodName.PauseDeferred);
PauseDeferred();
}
}
private void PauseDeferred()
{
GameManager.Instance.Pause();
}
// private void HandleShoot()
// {
// if (_inputProvider.GetReloadJustPressed())
// {
// _weaponProvider.Reload();
// return;
// }
//
// if (!_inputProvider.GetShootPressed()) return;
// _weaponProvider.Shoot(this.FacingDirection);
// }
//
// private void HandleInteraction()
// {
// _activationProvider.HandleInteraction();
// }
//
// private void HandleWeaponSwitch()
// {
// if (_inputProvider.GetWeaponNextJustPressed())
// {
// _weaponProvider.NextWeapon();
// }
// else if (_inputProvider.GetWeaponPreviousJustPressed())
// {
// _weaponProvider.PreviousWeapon();
// }
// }
}

View file

@ -0,0 +1 @@
uid://d3ydlcdm8m3tg

View file

@ -0,0 +1,37 @@
using System.Threading.Tasks;
using Godot;
namespace Cirno.Scripts.Components.FSM._3DPlayer;
public partial class Init : BaseState<PlayerState, CharacterBody3D>
{
public override PlayerState StateId => PlayerState.Init;
public override void EnterState()
{
// _storageModule.FacingDirection = ((PlayerStateMachine)StateMachine).StartingDirection;
// _animationProvider.PlayUnteleportAnimation();
_ = AutoSwitchToStart();
}
public override void ExitState()
{
}
public override void PhysicsProcessState(double delta)
{
}
public override void ProcessState(double delta)
{
}
private async Task AutoSwitchToStart()
{
await Task.Delay(500);
//StateMachine.SetState(PlayerState.Active);
}
}

View file

@ -0,0 +1 @@
uid://y35vqx1ifo52

View file

@ -0,0 +1,73 @@
using Cirno.Scripts.Components.Actors;
using Godot;
namespace Cirno.Scripts.Components.FSM._3DPlayer;
public partial class IsoMovementModule : ModuleBase<PlayerState, CharacterBody3D>
{
[Export] public IsoPlayerStorageModule PlayerStorage { get; private set; }
[Export] private InputProvider _inputProvider;
[Export] public int Speed { get; set; } = 45;
[Export] public int StrafeSpeed { get; set; } = 35;
[Export] public float Acceleration = 8f;
[Export] public float Deceleration = 8f;
private bool _isStrafing;
private float _accelerationPerSecond;
private float _decelerationPerSecond;
public int MovementSpeed => _isStrafing ? StrafeSpeed : Speed;
private IStateMachine<PlayerState, CharacterBody3D> _stateMachine;
private CharacterBody3D MainObject => _stateMachine.MainObject;
public override void EnterState(PlayerState state)
{
_accelerationPerSecond = Speed / Acceleration;
_decelerationPerSecond = Speed / Deceleration;
}
public override void ExitState(PlayerState state)
{
}
public override void Init(IStateMachine<PlayerState, CharacterBody3D> machine)
{
_stateMachine = machine;
}
public override void Process(double delta)
{
}
public override void PhysicsProcess(double delta)
{
if (_isStrafing)
{
// Instant movement at strafe speed
MainObject.Velocity = PlayerStorage.MovementDirection * StrafeSpeed;
}
else
{
Vector3 targetVelocity = PlayerStorage.MovementDirection * Speed;
if (PlayerStorage.MovementDirection != Vector3.Zero)
{
MainObject.Velocity = MainObject.Velocity.MoveToward(targetVelocity, Acceleration * (float)delta);
}
else
{
MainObject.Velocity = MainObject.Velocity.MoveToward(Vector3.Zero, Deceleration * (float)delta);
}
}
//MainObject.Velocity += _movementDirection * MovementSpeed;
MainObject.MoveAndSlide();
}
}

View file

@ -0,0 +1 @@
uid://c5brx3ail1tlh

View file

@ -0,0 +1,8 @@
using Godot;
namespace Cirno.Scripts.Components.FSM._3DPlayer;
public partial class IsoPlayerFSMProxy : CharacterBody3D
{
[Export] public IsoPlayerStateMachine PlayerFSM { get; private set; }
}

View file

@ -0,0 +1 @@
uid://88smibkin17p

View file

@ -0,0 +1,10 @@
using Godot;
namespace Cirno.Scripts.Components.FSM._3DPlayer;
public partial class IsoPlayerStateMachine : StateMachineBase<PlayerState,CharacterBody3D>
{
[Export] public override PlayerState InitialState { get; protected set; } = PlayerState.Init;
}

View file

@ -0,0 +1 @@
uid://cn3o414nvdd58

View file

@ -0,0 +1,13 @@
using Godot;
namespace Cirno.Scripts.Components.FSM._3DPlayer;
public partial class IsoPlayerStorageModule : Node
{
[Export] public IsoPlayerFSMProxy Root { get; private set; }
public Node RootAsNode => Root;
public Vector2 FacingDirection { get; set; } = Vector2.Down;
public Vector2 AimingDirection { get; set; } = Vector2.Down;
public Vector3 MovementDirection { get; set; } = Vector3.Zero;
}

View file

@ -0,0 +1 @@
uid://eop2ue3otxcs

View file

@ -89,12 +89,12 @@ public partial class TurretAnimationModule : ModuleBase<EnemyState, CharacterBod
}
}
private void DrawDebugLine(Vector2 endPoint)
{
// Request the node to redraw
QueueRedraw();
_debugLineEndPoint = endPoint;
}
// private void DrawDebugLine(Vector2 endPoint)
// {
// // Request the node to redraw
// QueueRedraw();
// _debugLineEndPoint = endPoint;
// }
private void UpdateSpriteDirection()
{
@ -103,11 +103,11 @@ public partial class TurretAnimationModule : ModuleBase<EnemyState, CharacterBod
private Vector2 _debugLineEndPoint;
public override void _Draw()
{
if (Debug)
{
DrawLine(Vector2.Zero, ToLocal(_debugLineEndPoint), Colors.Red, 2);
}
}
// public override void _Draw()
// {
// if (Debug)
// {
// DrawLine(Vector2.Zero, ToLocal(_debugLineEndPoint), Colors.Red, 2);
// }
// }
}

View file

@ -0,0 +1,55 @@
using System.Collections.Generic;
using Godot;
namespace Cirno.Scripts.Components.FSM;
public abstract partial class IsoStateMachineBase<TKey, TType> : Node, IStateMachine<TKey, TType>
where TKey : notnull
where TType : Node
{
public Dictionary<TKey, IState<TKey, TType>> States { get; set; } = new();
public TKey CurrentStateIndex { get; set; }
public IState<TKey, TType> CurrentState => States[CurrentStateIndex];
public abstract TKey InitialState { get; protected set; }
private TType _mainObject;
public TType MainObject => _mainObject;
public override void _Ready()
{
_mainObject = this.GetParent<TType>();
var children = GetChildren();
foreach (var child in children)
{
if (child is IState<TKey, TType> state)
{
States.Add(state.StateId, state);
state.Init(this);
}
}
SetState(InitialState);
}
public void SetState(TKey stateId)
{
if (CurrentStateIndex is not null)
{
CurrentState.ExitState();
}
CurrentStateIndex = stateId;
CurrentState.EnterState();
}
public override void _Process(double delta)
{
if (CurrentStateIndex is null) return;
CurrentState.ProcessState(delta);
}
public override void _PhysicsProcess(double delta)
{
if (CurrentStateIndex is null) return;
CurrentState.PhysicsProcessState(delta);
}
}

View file

@ -0,0 +1 @@
uid://dgdsp7fdmycl7

View file

@ -2,7 +2,7 @@
namespace Cirno.Scripts.Components.FSM;
public abstract partial class ModuleBase<TKey, TType> : Node2D, IModule<TKey, TType>
public abstract partial class ModuleBase<TKey, TType> : Node, IModule<TKey, TType>
where TKey : notnull
where TType : Node
{

View file

@ -23,6 +23,8 @@ public partial class FreezeModule : ModuleBase<PlayerState, CharacterBody2D>
public bool Enabled { get; set; } = false;
private double _cooldownTimer = 0;
private IStateMachine<PlayerState, CharacterBody2D> _machine;
public override void EnterState(PlayerState state)
{
@ -33,9 +35,10 @@ public partial class FreezeModule : ModuleBase<PlayerState, CharacterBody2D>
{
Enabled = false;
}
public override void Init(IStateMachine<PlayerState, CharacterBody2D> machine)
{
_machine = machine;
}
public override void Process(double delta)
@ -77,7 +80,7 @@ public partial class FreezeModule : ModuleBase<PlayerState, CharacterBody2D>
where child.BulletOwner is not BulletOwner.Player
where !child.IsFrozen
where child.BulletInfo.Freezable
let distance = GlobalPosition.DistanceTo(child.GlobalPosition)
let distance = _machine.MainObject.GlobalPosition.DistanceTo(child.GlobalPosition)
where distance <= FreezeRadius
select child).ToList();
}

View file

@ -3,7 +3,7 @@ using Godot;
namespace Cirno.Scripts.Components.FSM;
public abstract partial class StateMachineBase<TKey, TType> : Node2D, IStateMachine<TKey, TType>
public abstract partial class StateMachineBase<TKey, TType> : Node, IStateMachine<TKey, TType>
where TKey : notnull
where TType : Node
{