godot 4.7, more player controller

This commit is contained in:
Lilith Schier 2026-06-21 16:00:05 +02:00
parent f774f8e8a4
commit eea6830534
9 changed files with 220 additions and 36 deletions

View file

@ -1,9 +1,13 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using Chickensoft.LogicBlocks;
using Chickensoft.LogicBlocks.Auto;
namespace Dancer;
[Meta(typeof(IAutoNode))]
public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner, IProvide<IHitboxOwner>
public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
IProvide<IHitboxOwner>
{
public override void _Notification(int what) => this.Notify(what);
@ -20,13 +24,16 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
public float GroundedSpeedStopDecay { get; set; } = 0.005f;
[Export(PropertyHint.None, "suffix:px/s")]
public float WalkMaxSpeed { get; set; } = 130.0f;
public float WalkMaxSpeed { get; set; } = 110.0f;
[Export(PropertyHint.None, "suffix:px/s²")]
public float WalkAccel { get; set; } = 900.0f;
public float WalkAccel { get; set; } = 800.0f;
[Export(PropertyHint.None, "suffix:px/s²")]
public float AirAccel { get; set; } = 210.0f;
public float AirAccel { get; set; } = 180.0f;
[Export(PropertyHint.None, "suffix:px/s²")]
public float WaterAccel { get; set; } = 1200.0f;
[Export(PropertyHint.None, "suffix:s")]
public float JumpAscentMaxTime { get; set; } = 0.2f;
@ -52,6 +59,8 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
[Export] public float Health { get; set; }
[Export] public bool CanTurn { get; set; } = true;
[Export] public TileMapLayer WaterTileMap { get; set; } = null!;
private ulong MaxInputBufferTimeMicroseconds => (ulong)(MaxInputBufferTime * 1_000_000);
private float _jumpAscentRemainingTime;
private bool _isJumping;
@ -60,8 +69,11 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
private bool _isFacingRight = true;
private AnimationNodeStateMachinePlayback _playback = null!;
private List<BufferedInput> _inputBuffer = [];
private bool _isInWater;
private float PlatformIgnoreTime { get; set; }
[Node] private AnimationTree AnimationTree { get; set; } = null!;
[Node] private Area2D SubmersedSensor { get; set; } = null!;
public void OnReady()
{
@ -109,15 +121,21 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
if (CanTurn) AdjustFacing();
// Walking on the floor
if (IsOnFloor())
// Water movement
if (_isInWater)
{
if (Mathf.Sign(velocity.X) != Mathf.Sign(_currentDirection.X))
velocity += _currentDirection * WaterAccel * deltaF;
}
// Walking on the floor
else if (IsOnFloor())
{
var walkInput = IsInputVecSide(_currentDirection) ? _currentDirection.X : 0f;
if (Mathf.Sign(velocity.X) != Mathf.Sign(walkInput))
velocity.X *= Mathf.Pow(GroundedSpeedStopDecay, deltaF);
else
velocity.X *= Mathf.Pow(GroundedSpeedDecay, deltaF);
var targetVelX = Mathf.Sign(_currentDirection.X) * WalkMaxSpeed;
var targetVelX = Mathf.Sign(walkInput) * WalkMaxSpeed;
var isWalking = targetVelX != 0;
if (!isWalking || Mathf.Abs(velocity.X) < WalkMaxSpeed)
@ -139,14 +157,21 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
if (_jumpAscentRemainingTime < 0)
velocity += GetGravity() * -_jumpAscentRemainingTime;
}
else if (!IsOnFloor())
else if (!IsOnFloor() && !_isInWater)
{
velocity += GetGravity() * deltaF;
}
// Drag in water
if (_isInWater)
{
velocity += Physics.DragForce(-velocity, 0.02f) * deltaF;
}
// Drag while in air
if (!IsOnFloor() && _jumpAscentRemainingTime <= 0)
velocity += Physics.DragForce(-velocity) * deltaF;
else if (!IsOnFloor() && _jumpAscentRemainingTime <= 0)
{
velocity += Physics.DragForce(-velocity, 0.009f) * deltaF;
}
if (IsHoldingWall())
{
@ -155,6 +180,15 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
}
Velocity = velocity;
if (PlatformIgnoreTime > 0f)
{
CollisionMask = 0b1;
PlatformIgnoreTime = Mathf.Max(0f, PlatformIgnoreTime - deltaF);
}
else
{
CollisionMask = 0b0100_0001;
}
MoveAndSlide();
_jumpAllowedTime = Mathf.Max(0, _jumpAllowedTime - deltaF);
@ -168,6 +202,20 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
}
}
public void EnteredWater(Node2D body)
{
if (_isInWater) return;
_isInWater = true;
GD.Print("Entered water!");
}
public void LeftWater(Node2D body)
{
if (!_isInWater || SubmersedSensor.HasOverlappingBodies()) return;
GD.Print("Left water!");
_isInWater = false;
}
private void AdjustFacing()
{
if (_currentDirection.X < 0)
@ -211,7 +259,12 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
if (input.Event.IsAction("jump"))
{
if (
if (input.Event.IsPressed() && IsInputVecDown(input.Direction) && IsOnFloor())
{
inputFound = true;
DropDown();
}
else if (
input.Event.IsPressed()
&& _jumpAllowedTime > 0
&& (!IsOnFloor() || !IsInputVecDown(input.Direction)))
@ -243,6 +296,11 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
}
}
private void DropDown()
{
PlatformIgnoreTime = 0.1f;
}
private void GroundJump()
{
AdjustFacing();
@ -279,6 +337,12 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
{
return Mathf.Abs(vec.AngleTo(Vector2.Down)) < DownInputAngle && vec.Length() > 0.1f;
}
private bool IsInputVecSide(Vector2 vec)
{
return vec.Length() > 0.1f
&& (Mathf.Abs(vec.AngleTo(Vector2.Left)) < SideInputAngle
|| Mathf.Abs(vec.AngleTo(Vector2.Right)) < SideInputAngle) ;
}
private void OnEnemyHit(Hitbox hitbox, Node2D enemy)
{
@ -306,4 +370,41 @@ public sealed partial class Player : CharacterBody2D, IAttackable, IHitboxOwner,
public void OnHitboxAdded(Hitbox hitbox) => hitbox.BodyHit += OnEnemyHit;
public void OnHitboxRemoved(Hitbox hitbox) => hitbox.BodyHit -= OnEnemyHit;
IHitboxOwner IProvide<IHitboxOwner>.Value() => this;
[Meta]
public sealed partial class Logic : AutoBlock
{
public sealed record Data
{
}
public Logic()
{
Set(new Data());
Preallocate<Root>();
}
public static class Input
{
public readonly record struct OnPhysicsProcess(double Delta);
}
[Meta, StateDiagram]
public abstract partial record Root : LogicBlockState
{
[Meta]
public sealed partial record Grounded : Root
{
}
[Meta]
public sealed partial record Airborne : Root
{
}
[Meta]
public sealed partial record Diving : Root
{
}
}
}
}