gd,refacto: added state chart addon and namespace cleanup

This commit is contained in:
2025-06-05 14:47:51 +02:00
parent 8818e77d23
commit 5c36765a36
239 changed files with 10430 additions and 115 deletions

View File

@ -1,4 +1,4 @@
using PolarBears.PlayerControllerAddon;
namespace Movementtests.player_controller.Scripts;
public partial class AnimationPlayer : Godot.AnimationPlayer
{
@ -6,4 +6,4 @@ public partial class AnimationPlayer : Godot.AnimationPlayer
{
Play(Constants.PLAYERS_HEAD_ANIMATION_ON_DYING);
}
}
}

View File

@ -1,6 +1,6 @@
using Godot;
namespace PolarBears.PlayerControllerAddon;
namespace Movementtests.player_controller.Scripts;
public partial class Bobbing: Node3D
{

View File

@ -1,6 +1,6 @@
using Godot;
namespace PolarBears.PlayerControllerAddon;
namespace Movementtests.player_controller.Scripts;
public partial class CapsuleCollider : CollisionShape3D
{

View File

@ -1,94 +0,0 @@
using Godot;
namespace PolarBears.PlayerControllerAddon;
public record DashComputation(bool HasHit, Vector3 Location, Vector3 CollisionPoint, Vector3 CollisionNormal);
public record DashResolve(bool EndWithMantle, Vector3 DashLocation, Vector3 MantleLocation);
public partial class DashSystem: Node3D
{
[Export(PropertyHint.Range, "0,0.2,0.01,or_greater")]
public float DashSpeed { get; set; } = 0.05f;
private Node3D _head;
private ShapeCast3D _dashCast3D;
private Camera3D _camera;
private TweenQueueSystem _tweenQueueSystem;
private MantleSystem _mantleSystem;
private MeshInstance3D _dashTarget;
private DashResolve _dashResolve;
public void Init(Node3D head, Camera3D camera, TweenQueueSystem tweenQueueSystem)
{
_dashCast3D = GetNode<ShapeCast3D>("DashCast3D");
_head = head;
_camera = camera;
_tweenQueueSystem = tweenQueueSystem;
_mantleSystem = GetNode<MantleSystem>("MantleSystem");
_mantleSystem.Init(this);
_dashTarget = GetNode<MeshInstance3D>("DashTarget");
_dashTarget.SetVisible(false);
}
private DashComputation ComputeDashLocation()
{
if (!_dashCast3D.IsColliding())
{
return new DashComputation(false, _dashCast3D.ToGlobal(_dashCast3D.TargetPosition), Vector3.Zero, Vector3.Zero);
}
var collisionPoint = _dashCast3D.GetCollisionPoint(0);
var collisionNormal = _dashCast3D.GetCollisionNormal(0);
var collisionShape = (SphereShape3D) _dashCast3D.GetShape();
var centerSphereLocation = collisionPoint + collisionNormal * collisionShape.Radius;
return new DashComputation(true, centerSphereLocation, collisionPoint, collisionNormal);
}
public DashResolve PrepareDash()
{
_dashTarget.SetVisible(false);
_dashCast3D.SetRotation(new Vector3(
_camera.Rotation.X,
_head.Rotation.Y,
_camera.Rotation.Z));
var (hasHit, location, collisionPoint, collisionNormal) = ComputeDashLocation();
var shouldMantle = false;
var mantleLocation = Vector3.Zero;
if (hasHit && Mathf.Abs(collisionNormal.Y) < 0.01f)
{
var mantleResult = _mantleSystem.FindMantleLocationAtPoint(collisionPoint, collisionNormal);
shouldMantle = mantleResult.IsSome(out mantleLocation);
}
var targetColor = shouldMantle ? new Color(0.2f, 0.2f, 1f) : new Color(1f, 1f, 1f);
var targetMaterial = (StandardMaterial3D) _dashTarget.GetSurfaceOverrideMaterial(0);
targetMaterial.SetAlbedo(targetColor);
_dashTarget.SetVisible(true);
_dashTarget.SetGlobalPosition(location);
_dashResolve = new DashResolve(shouldMantle, location, mantleLocation);
return _dashResolve;
}
public void CancelDash()
{
_dashTarget.SetVisible(false);
}
public void Dash()
{
_dashTarget.SetVisible(false);
_tweenQueueSystem.QueueTween(_dashResolve.DashLocation, 0.1f);
if (_dashResolve.EndWithMantle)
{
_tweenQueueSystem.QueueTween(_dashResolve.MantleLocation, 0.1f);
}
}
}

View File

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

View File

@ -1,6 +1,6 @@
using Godot;
namespace PolarBears.PlayerControllerAddon;
namespace Movementtests.player_controller.Scripts;
public partial class FieldOfView: Node3D
{

View File

@ -1,6 +1,4 @@
using Godot;
namespace PolarBears.PlayerControllerAddon;
namespace Movementtests.player_controller.Scripts;
public class Constants
{

View File

@ -1,6 +1,6 @@
using Godot;
namespace PolarBears.PlayerControllerAddon;
namespace Movementtests.player_controller.Scripts;
public partial class Gravity: Node3D
{

View File

@ -1,7 +1,7 @@
using System;
using Godot;
namespace PolarBears.PlayerControllerAddon;
namespace Movementtests.player_controller.Scripts;
public partial class HealthSystem : Node3D
{

View File

@ -1,7 +1,7 @@
using System;
using Godot;
namespace PolarBears.PlayerControllerAddon;
namespace Movementtests.player_controller.Scripts;
public partial class Mouse : Node3D
{

View File

@ -1,9 +1,7 @@
using System.Collections.Generic;
using System.Runtime.InteropServices.JavaScript;
using Godot;
using RustyOptions;
namespace PolarBears.PlayerControllerAddon;
using GodotStateCharts;
using Movementtests.systems;
using Movementtests.player_controller.Scripts;
public partial class PlayerController : CharacterBody3D
{
@ -20,77 +18,30 @@ public partial class PlayerController : CharacterBody3D
public HealthSystem HealthSystem;
public MoveSystem MoveSystem;
public TweenQueueSystem TweenQueueSystem;
public StateChart PlayerState;
private bool _movementEnabled = true;
private bool _shouldMantle = false;
private bool _shouldMantle;
private Vector3 _dashLocation = Vector3.Zero;
private Vector3 _mantleLocation = Vector3.Zero;
private float _lastFrameWasOnFloor = -Mathf.Inf;
private const int NumOfHeadCollisionDetectors = 4;
private const int NUM_OF_HEAD_COLLISION_DETECTORS = 4;
private RayCast3D[] _headCollisionDetectors;
private Vector3 _inputMove = Vector3.Zero;
private float _inputRotateY = 0.0f;
private float _inputRotateFloorplane = 0.0f;
private float _inputRotateY;
private float _inputRotateFloorplane;
private bool _isAiming = false;
private bool _dashCanceled = false;
public void OnInputMove(Vector3 value)
{
_inputMove = value;
}
public void OnInputRotateY(float value)
{
_inputRotateY = value;
}
public void OnInputRotateFloorplane(float value)
{
_inputRotateFloorplane = value;
}
public void OnInputAimPressed()
{
if (_dashCanceled)
return;
DashSystem.PrepareDash();
}
public void OnInputAimReleased()
{
if (!_dashCanceled)
DashSystem.Dash();
_dashCanceled = false;
}
public void OnInputAimCanceled()
{
_dashCanceled = true;
DashSystem.CancelDash();
}
public void OnInputHitPressed()
{
GD.Print("OnInputHitPressed");
}
public void OnInputJumpPressed()
{
bool doesCapsuleHaveCrouchingHeight = CapsuleCollider.IsCrouchingHeight();
bool isPlayerDead = HealthSystem.IsDead();
if (!doesCapsuleHaveCrouchingHeight && !isPlayerDead)
MoveSystem.Jump(IsOnFloor());
}
private bool _isAiming;
private bool _dashCanceled;
public override void _Ready()
{
_headCollisionDetectors = new RayCast3D[NumOfHeadCollisionDetectors];
for (int i = 0; i < NumOfHeadCollisionDetectors; i++)
_headCollisionDetectors = new RayCast3D[NUM_OF_HEAD_COLLISION_DETECTORS];
for (int i = 0; i < NUM_OF_HEAD_COLLISION_DETECTORS; i++)
{
_headCollisionDetectors[i] = GetNode<RayCast3D>(
"HeadCollisionDetectors/HeadCollisionDetector" + i);
@ -99,7 +50,14 @@ public partial class PlayerController : CharacterBody3D
HeadSystem = GetNode<HeadSystem>("HeadSystem");
HeadSystem.Init();
// Getting dependencies of the components(In godot we manage this from upwards to downwards not vice versa)
// Get the state chart node and wrap it in a StateChart object like so, GetNode doesn't work
PlayerState = StateChart.Of(GetNode("%StateChart"));
// Get the poisoned state node and wrap it in a State object, so we can easily
// interact with it from C#.
// _poisonedStateChartState = StateChartState.Of(GetNode("%Poisoned"));
// Getting dependencies of the components (In godot we manage this from upwards to downwards not vice versa)
Camera3D camera = GetNode<Camera3D>("HeadSystem/CameraSmooth/Camera3D");
Node3D cameraSmooth = GetNode<Node3D>("HeadSystem/CameraSmooth");
@ -118,7 +76,7 @@ public partial class PlayerController : CharacterBody3D
ColorRect blurRect = GetNode<ColorRect>("HeadSystem/CameraSmooth/Camera3D/CLBlur(Layer_2)/BlurRect");
Node3D mapNode = GetTree().Root.FindChild("Map", true, false) as Node3D;
// Node3D mapNode = GetTree().Root.FindChild("Map", true, false) as Node3D;
// Getting components
@ -168,6 +126,56 @@ public partial class PlayerController : CharacterBody3D
HealthSystem.Init(healthSystemParams);
}
public void OnInputMove(Vector3 value)
{
_inputMove = value;
}
public void OnInputRotateY(float value)
{
_inputRotateY = value;
}
public void OnInputRotateFloorplane(float value)
{
_inputRotateFloorplane = value;
}
public void OnInputAimPressed()
{
PlayerState.SendEvent("aim_started");
if (_dashCanceled)
return;
DashSystem.PrepareDash();
}
public void OnInputAimReleased()
{
if (!_dashCanceled)
DashSystem.Dash();
_dashCanceled = false;
}
public void OnInputAimCanceled()
{
_dashCanceled = true;
DashSystem.CancelDash();
}
public void OnInputHitPressed()
{
GD.Print("OnInputHitPressed");
}
public void OnInputJumpPressed()
{
bool doesCapsuleHaveCrouchingHeight = CapsuleCollider.IsCrouchingHeight();
bool isPlayerDead = HealthSystem.IsDead();
if (!doesCapsuleHaveCrouchingHeight && !isPlayerDead)
MoveSystem.Jump(IsOnFloor());
}
public override void _PhysicsProcess(double delta)
{
TweenQueueSystem.ProcessTweens();
@ -265,7 +273,7 @@ public partial class PlayerController : CharacterBody3D
private bool IsHeadTouchingCeiling()
{
for (int i = 0; i < NumOfHeadCollisionDetectors; i++)
for (int i = 0; i < NUM_OF_HEAD_COLLISION_DETECTORS; i++)
{
if (_headCollisionDetectors[i].IsColliding())
{

View File

@ -1,6 +1,6 @@
using Godot;
namespace PolarBears.PlayerControllerAddon;
namespace Movementtests.player_controller.Scripts;
public partial class StairsSystem: Node3D
{

View File

@ -1,6 +1,6 @@
using Godot;
namespace PolarBears.PlayerControllerAddon;
namespace Movementtests.player_controller.Scripts;
public partial class Stamina : Node
{