moved movement code to player

This commit is contained in:
2025-07-29 16:41:31 +02:00
parent ffdb3366a5
commit 0d6d1c86de
11 changed files with 225 additions and 367 deletions

File diff suppressed because one or more lines are too long

Binary file not shown.

Before

Width:  |  Height:  |  Size: 637 B

View File

@ -1,36 +0,0 @@
[remap]
importer="texture"
type="CompressedTexture2D"
uid="uid://lp2pt8mtj7ty"
path.s3tc="res://.godot/imported/kenney-green-checkerboar-cc0.png-2ce8609a39a655125c8e037014f6f2db.s3tc.ctex"
path.etc2="res://.godot/imported/kenney-green-checkerboar-cc0.png-2ce8609a39a655125c8e037014f6f2db.etc2.ctex"
metadata={
"imported_formats": ["s3tc_bptc", "etc2_astc"],
"vram_texture": true
}
[deps]
source_file="res://player_controller/Examples/MovementTestbed/Hills/kenney-green-checkerboar-cc0.png"
dest_files=["res://.godot/imported/kenney-green-checkerboar-cc0.png-2ce8609a39a655125c8e037014f6f2db.s3tc.ctex", "res://.godot/imported/kenney-green-checkerboar-cc0.png-2ce8609a39a655125c8e037014f6f2db.etc2.ctex"]
[params]
compress/mode=2
compress/high_quality=false
compress/lossy_quality=0.7
compress/hdr_compression=1
compress/normal_map=0
compress/channel_pack=0
mipmaps/generate=true
mipmaps/limit=-1
roughness/mode=0
roughness/src_normal=""
process/fix_alpha_border=true
process/premult_alpha=false
process/normal_map_invert_y=false
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=0

View File

@ -1,33 +0,0 @@
using Godot;
// This script is an example of how you can create game systems that
// interact with PlayerController. This script applies a low gravity effect
// to any PlayerController that enters the Area3D. It does this by modifying
// the value of AdditionalGravityPower owned by the Gravity child of
// PlayerController.
namespace Movementtests.player_controller.Examples.MovementTestbed;
public partial class LowGravityArea3D : Area3D
{
[Export] public float GravityReduction { set; get; } = 0.4f;
public override void _Ready()
{
BodyEntered += (Node3D body) =>
{
if (body is PlayerController player) {
player.Gravity.AdditionalGravityPower *= GravityReduction;
GD.Print("Low Gravity Zone Entered");
}
};
BodyExited += (Node3D body) =>
{
if (body is PlayerController player) {
player.Gravity.AdditionalGravityPower /= GravityReduction;
GD.Print("Low Gravity Zone Exited");
}
};
}
}

View File

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

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=45 format=3 uid="uid://bei4nhkf8lwdo"]
[gd_scene load_steps=43 format=3 uid="uid://bei4nhkf8lwdo"]
[ext_resource type="Script" uid="uid://bbbrf5ckydfna" path="res://player_controller/Scripts/PlayerController.cs" id="1_poq2x"]
[ext_resource type="Curve" uid="uid://c2a8soliruf35" path="res://systems/dash/dash_time_dilation.tres" id="2_2q0ik"]
@ -13,7 +13,6 @@
[ext_resource type="Resource" uid="uid://c3e0ivgaxrsyb" path="res://systems/inputs/base_mode/aim_down.tres" id="8_obsfv"]
[ext_resource type="PackedScene" uid="uid://wq1okogkhc5l" path="res://systems/mantle/mantle_system.tscn" id="8_qu4wy"]
[ext_resource type="Resource" uid="uid://7wm8ywvujwf" path="res://systems/inputs/base_mode/aim_cancel.tres" id="9_5p2qc"]
[ext_resource type="Script" uid="uid://bt0xv2q8iv1vn" path="res://player_controller/Scripts/Gravity.cs" id="9_lsueh"]
[ext_resource type="Resource" uid="uid://bebstkm608wxx" path="res://systems/inputs/base_mode/aim_pressed.tres" id="9_nob5r"]
[ext_resource type="Resource" uid="uid://bdit2jy5gbpts" path="res://systems/inputs/base_mode/jump.tres" id="10_4u7i3"]
[ext_resource type="Script" uid="uid://g8idirw62qe0" path="res://player_controller/Scripts/Bobbing.cs" id="10_7wk1w"]
@ -28,7 +27,6 @@
[ext_resource type="Script" uid="uid://b5nk6ntlps3x0" path="res://systems/inputs/input_system.gd" id="16_v31n3"]
[ext_resource type="Resource" uid="uid://htqvokm8mufq" path="res://systems/inputs/base_mode/move.tres" id="17_h6vvl"]
[ext_resource type="PackedScene" uid="uid://cqduhd4opgwvm" path="res://systems/dash/dash_system.tscn" id="18_q5h8a"]
[ext_resource type="Script" uid="uid://dyy5njw6pxoh4" path="res://systems/move/MoveSystem.cs" id="20_rxwoh"]
[ext_resource type="PackedScene" uid="uid://dbe5f0p6lvqtr" path="res://systems/tween_queue/tween_queue_system.tscn" id="22_rpwev"]
[ext_resource type="PackedScene" uid="uid://bcwkugn6v3oy7" path="res://addons/godot_state_charts/utilities/state_chart_debugger.tscn" id="24_q5h8a"]
[ext_resource type="Script" uid="uid://couw105c3bde4" path="res://addons/godot_state_charts/state_chart.gd" id="25_wv70j"]
@ -51,7 +49,11 @@ blend_mode = 1
[node name="Player" type="CharacterBody3D"]
script = ExtResource("1_poq2x")
TimeScaleAimInAir = 0.15
AccelerationSpeedFactorFloor = 3.0
Weight = 3.5
StartVelocity = 0.7
JumpFromDashSpeedFactor = 4.0
WallHugHorizontalDeceleration = 3.0
MaxJumpBoostAfterDashing = 0.7
MaxNumberOfDashActions = 2
PerfectlyTimedActionTimer = 0.3
@ -135,20 +137,6 @@ target_position = Vector3(0, 1, 0)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.296, 1.4, 0)
target_position = Vector3(0, 1, 0)
[node name="MoveSystem" type="Node3D" parent="."]
script = ExtResource("20_rxwoh")
WalkSpeed = 7.0
SprintSpeed = 7.0
AccelerationSpeedFactorFloor = 3.0
ApexHoldTime = 0.2
WallHugGravityReducingFactor = 0.2
[node name="Gravity" type="Node3D" parent="."]
script = ExtResource("9_lsueh")
Weight = 3.5
StartVelocity = 0.7
JumpFromDashSpeedFactor = 4.0
[node name="TweenQueueSystem" parent="." instance=ExtResource("22_rpwev")]
[node name="WallHugSystem" type="Node3D" parent="."]
@ -425,7 +413,7 @@ delay_in_seconds = "0.0"
[node name="OnGrounded" type="Node" parent="StateChart/Root/Movement"]
script = ExtResource("28_n7qhm")
to = NodePath("../Grounded/Standing")
to = NodePath("../Grounded")
event = &"grounded"
delay_in_seconds = "0.0"
@ -473,51 +461,26 @@ script = ExtResource("27_34snm")
script = ExtResource("27_34snm")
[node name="Grounded" type="Node" parent="StateChart/Root/Movement"]
script = ExtResource("26_infe6")
initial_state = NodePath("Standing")
[node name="Standing" type="Node" parent="StateChart/Root/Movement/Grounded"]
script = ExtResource("27_34snm")
[node name="OnJump" type="Node" parent="StateChart/Root/Movement/Grounded/Standing"]
[node name="OnJump" type="Node" parent="StateChart/Root/Movement/Grounded"]
script = ExtResource("28_n7qhm")
to = NodePath("../../../Airborne/DoubleJumpEnabled")
to = NodePath("../../Airborne/DoubleJumpEnabled")
event = &"jump"
delay_in_seconds = "0.0"
[node name="OnAirborne" type="Node" parent="StateChart/Root/Movement/Grounded/Standing"]
[node name="OnMegajump" type="Node" parent="StateChart/Root/Movement/Grounded"]
script = ExtResource("28_n7qhm")
to = NodePath("../../../Airborne/CoyoteEnabled")
to = NodePath("../../Airborne/Falling")
event = &"megajump"
delay_in_seconds = "0.0"
[node name="OnAirborne" type="Node" parent="StateChart/Root/Movement/Grounded"]
script = ExtResource("28_n7qhm")
to = NodePath("../../Airborne/CoyoteEnabled")
event = &"start_falling"
delay_in_seconds = "0.0"
[node name="OnCrouch" type="Node" parent="StateChart/Root/Movement/Grounded/Standing"]
script = ExtResource("28_n7qhm")
to = NodePath("../../Crouched")
event = &"crouch"
delay_in_seconds = "0.0"
[node name="Crouched" type="Node" parent="StateChart/Root/Movement/Grounded"]
script = ExtResource("27_34snm")
[node name="OnJump" type="Node" parent="StateChart/Root/Movement/Grounded/Crouched"]
script = ExtResource("28_n7qhm")
to = NodePath("../../../Airborne/DoubleJumpEnabled")
event = &"jump"
delay_in_seconds = "0.0"
[node name="OnAirborne" type="Node" parent="StateChart/Root/Movement/Grounded/Crouched"]
script = ExtResource("28_n7qhm")
to = NodePath("../../../Airborne/CoyoteEnabled")
event = &"start_falling"
delay_in_seconds = "0.0"
[node name="OnStandUp" type="Node" parent="StateChart/Root/Movement/Grounded/Crouched"]
script = ExtResource("28_n7qhm")
to = NodePath("../../Standing")
event = &"crouch"
delay_in_seconds = "0.0"
[node name="Airborne" type="Node" parent="StateChart/Root/Movement"]
script = ExtResource("26_infe6")
initial_state = NodePath("CoyoteEnabled")
@ -541,6 +504,12 @@ to = NodePath("../../DoubleJumpEnabled")
event = &"jump"
delay_in_seconds = "0.0"
[node name="OnMegajump" type="Node" parent="StateChart/Root/Movement/Airborne/CoyoteEnabled"]
script = ExtResource("28_n7qhm")
to = NodePath("../../Falling")
event = &"megajump"
delay_in_seconds = "0.0"
[node name="OnExpiration" type="Node" parent="StateChart/Root/Movement/Airborne/CoyoteEnabled"]
script = ExtResource("28_n7qhm")
to = NodePath("../../DoubleJumpEnabled")
@ -556,6 +525,12 @@ to = NodePath("../../Falling")
event = &"jump"
delay_in_seconds = "0.0"
[node name="OnDash" type="Node" parent="StateChart/Root/Movement/Airborne/DoubleJumpEnabled"]
script = ExtResource("28_n7qhm")
to = NodePath("../../Falling")
event = &"dash"
delay_in_seconds = "0.0"
[node name="Falling" type="Node" parent="StateChart/Root/Movement/Airborne"]
script = ExtResource("27_34snm")
@ -575,6 +550,15 @@ to = NodePath("../../Airborne/DoubleJumpEnabled")
event = &"jump"
delay_in_seconds = "0.0"
[node name="HugCanceled" type="Node" parent="StateChart/Root/Movement/OnWall"]
script = ExtResource("27_34snm")
[node name="ToHanging" type="Node" parent="StateChart/Root/Movement/OnWall/HugCanceled"]
script = ExtResource("28_n7qhm")
to = NodePath("../../Hanging")
event = &"oh_hit_wall"
delay_in_seconds = "0.0"
[node name="Hugging" type="Node" parent="StateChart/Root/Movement/OnWall"]
script = ExtResource("27_34snm")
@ -584,13 +568,19 @@ to = NodePath("../../../Airborne/CoyoteEnabled")
event = &"start_falling"
delay_in_seconds = "0.0"
[node name="OnDrop" type="Node" parent="StateChart/Root/Movement/OnWall/Hugging"]
script = ExtResource("28_n7qhm")
to = NodePath("../../HugCanceled")
event = &"dash"
delay_in_seconds = "0.0"
[node name="Hanging" type="Node" parent="StateChart/Root/Movement/OnWall"]
script = ExtResource("27_34snm")
[node name="OnDrop" type="Node" parent="StateChart/Root/Movement/OnWall/Hanging"]
script = ExtResource("28_n7qhm")
to = NodePath("../../../Airborne/CoyoteEnabled")
event = &"cancel"
to = NodePath("../../Hugging")
event = &"dash"
delay_in_seconds = "0.0"
[connection signal="input_aim_canceled" from="InputController" to="." method="OnInputAimCanceled"]

View File

@ -107,7 +107,6 @@ public partial class HealthSystem : Node3D
private float _currentHealthInPrevFrame;
private float _currentVelocityYInAir;
private Gravity _gravity;
private CharacterBody3D _characterBody3D;
@ -144,7 +143,6 @@ public partial class HealthSystem : Node3D
public struct HealthSystemInitParams
{
public Gravity Gravity;
public CharacterBody3D Parent;
public Camera3D Camera;
public Node3D Head;
@ -160,7 +158,6 @@ public partial class HealthSystem : Node3D
_currentSpeed = SpeedMin;
_gravity = initParams.Gravity;
_characterBody3D = initParams.Parent;
_camera = initParams.Camera;

View File

@ -5,6 +5,14 @@ using Movementtests.systems;
using Movementtests.player_controller.Scripts;
using RustyOptions;
public enum JumpTypes
{
SimpleJump,
DoubleJump,
JumpFromDash,
JumpFromWall
}
public partial class PlayerController : CharacterBody3D
{
// User API to important child nodes.
@ -16,9 +24,7 @@ public partial class PlayerController : CharacterBody3D
public MantleSystem MantleSystem;
public DashSystem DashSystem;
public CapsuleCollider CapsuleCollider;
public Gravity Gravity;
public HealthSystem HealthSystem;
public MoveSystem MoveSystem;
public TweenQueueSystem TweenQueueSystem;
public Node3D WeaponRoot;
public WeaponSystem WeaponSystem;
@ -49,8 +55,40 @@ public partial class PlayerController : CharacterBody3D
private Timer _dashCooldownTimer;
private Timer _empowerTimeDownscale;
[ExportCategory("Movement")]
[ExportGroup("Ground")]
[Export(PropertyHint.Range, "0,20,0.1,or_greater")]
public float WalkSpeed { get; set; } = 7.0f;
[Export(PropertyHint.Range, "0,10,0.1,or_greater")]
public float AccelerationSpeedFactorFloor = 5.0f;
[Export(PropertyHint.Range, "0,10,0.1,or_greater")]
public float DecelerationSpeedFactorFloor = 5.0f;
[ExportGroup("Air")]
[Export(PropertyHint.Range, "0,10,0.1,or_greater")]
public float DecelerationSpeedFactorAir = 1.0f;
[Export(PropertyHint.Range, "0,10,0.01,or_greater")]
public float Weight { get; set; } = 3.0f;
[ExportGroup("Jump")]
[Export(PropertyHint.Range, "0,2,0.01,or_greater")]
public float StartVelocity { get; set; } = 1.0f;
[Export(PropertyHint.Range, "0.1,10,0.1,or_greater")]
public float DoubleJumpSpeedFactor { get; set; } = 2f;
[Export(PropertyHint.Range, "0.1,10,0.1,or_greater")]
public float JumpFromDashSpeedFactor { get; set; } = 2f;
[Export(PropertyHint.Range, "0.1,10,0.1,or_greater")]
public float JumpFromWallSpeedFactor { get; set; } = 2f;
[ExportGroup("WallHug")]
[Export(PropertyHint.Range, "0.1,10,0.1,or_greater")]
public float WallHugDownwardSpeed { get; set; } = 2f;
[Export(PropertyHint.Range, "0.1,10,0.1,or_greater")]
public float WallHugHorizontalDeceleration { get; set; } = 5f;
private float _targetSpeed;
private float _gravity;
[ExportCategory("Other")]
[Export(PropertyHint.Range, "0,1,0.01,or_greater")]
public float TimeScaleAimInAir { get; set; } = 0.2f;
public float TimeScaleAimInAir { get; set; } = 0.05f;
[Export(PropertyHint.Range, "0,5,0.1,or_greater")]
public float MaxJumpBoostAfterDashing { get; set; } = 1f;
@ -96,14 +134,13 @@ public partial class PlayerController : CharacterBody3D
private StateChartState _powerFull;
private StateChartState _grounded;
private StateChartState _crouched;
private StateChartState _standing;
private StateChartState _mantling;
private StateChartState _movHanging;
private StateChartState _airborne;
private StateChartState _coyoteEnabled;
private StateChartState _doubleJumpEnabled;
private StateChartState _onWall;
private StateChartState _onWallHugCanceled;
private StateChartState _onWallHugging;
private StateChartState _onWallHanging;
private StateChartState _falling;
@ -121,6 +158,7 @@ public partial class PlayerController : CharacterBody3D
DashCooldownIndicator = GetNode<ColorRect>("%DashCooldownIndicator");
DashCooldownIndicator.Visible = false;
EmpoweredActionsLeft = MaxNumberOfDashActions;
_targetSpeed = WalkSpeed;
// Node3D mapNode = GetTree().Root.FindChild("Map", true, false) as Node3D;
@ -141,8 +179,6 @@ public partial class PlayerController : CharacterBody3D
WeaponSystem = GetNode<WeaponSystem>("WeaponRoot/WeaponSystem");
MantleSystem = GetNode<MantleSystem>("MantleSystem");
CapsuleCollider = GetNode<CapsuleCollider>("CapsuleCollider");
Gravity = GetNode<Gravity>("Gravity");
MoveSystem = GetNode<MoveSystem>("MoveSystem");
DashSystem = GetNode<DashSystem>("DashSystem");
StairsSystem = GetNode<StairsSystem>("StairsSystem");
WallHugSystem = GetNode<WallHugSystem>("WallHugSystem");
@ -174,8 +210,6 @@ public partial class PlayerController : CharacterBody3D
_powerFull = StateChartState.Of(GetNode("StateChart/Root/PowerReserve/Full"));
_grounded = StateChartState.Of(GetNode("StateChart/Root/Movement/Grounded"));
_standing = StateChartState.Of(GetNode("StateChart/Root/Movement/Grounded/Standing"));
_crouched = StateChartState.Of(GetNode("StateChart/Root/Movement/Grounded/Crouched"));
_mantling = StateChartState.Of(GetNode("StateChart/Root/Movement/Mantling"));
_movHanging = StateChartState.Of(GetNode("StateChart/Root/Movement/OnWall/Hanging"));
_airborne = StateChartState.Of(GetNode("StateChart/Root/Movement/Airborne"));
@ -183,6 +217,7 @@ public partial class PlayerController : CharacterBody3D
_doubleJumpEnabled = StateChartState.Of(GetNode("StateChart/Root/Movement/Airborne/DoubleJumpEnabled"));
_onWall = StateChartState.Of(GetNode("StateChart/Root/Movement/OnWall"));
_onWallHugging = StateChartState.Of(GetNode("StateChart/Root/Movement/OnWall/Hugging"));
_onWallHugCanceled = StateChartState.Of(GetNode("StateChart/Root/Movement/OnWall/HugCanceled"));
_onWallHanging = StateChartState.Of(GetNode("StateChart/Root/Movement/OnWall/Hanging"));
_falling = StateChartState.Of(GetNode("StateChart/Root/Movement/Airborne/Falling"));
// State timers
@ -206,12 +241,8 @@ public partial class PlayerController : CharacterBody3D
// Movement stuff
// Getting universal setting from GODOT editor to be in sync
float gravitySetting = (float)ProjectSettings.GetSetting("physics/3d/default_gravity");
Gravity.Init(gravitySetting);
_gravity = (float)ProjectSettings.GetSetting("physics/3d/default_gravity");
MantleSystem.Init(HeadSystem);
var moveSystemParams = new MoveSystem.MoveSystemParameters(this, Gravity, MantleSystem, TweenQueueSystem,
HeadSystem, CapsuleCollider);
MoveSystem.Init(moveSystemParams);
StairsSystem.Init(stairsBelowRayCast3D, stairsAheadRayCast3D, cameraSmooth);
DashSystem.Init(HeadSystem, camera, TweenQueueSystem);
WeaponSystem.Init(HeadSystem, camera);
@ -220,7 +251,6 @@ public partial class PlayerController : CharacterBody3D
// RPG Stuff
HealthSystem.HealthSystemInitParams healthSystemParams = new HealthSystem.HealthSystemInitParams()
{
Gravity = Gravity,
Parent = this,
Camera = camera,
Head = HeadSystem,
@ -229,7 +259,7 @@ public partial class PlayerController : CharacterBody3D
BlurRect = blurRect,
};
HealthSystem.Init(healthSystemParams);
Stamina.SetSpeeds(MoveSystem.WalkSpeed, MoveSystem.SprintSpeed);
Stamina.SetSpeeds(WalkSpeed, WalkSpeed);
EmpoweredActionsLeft = MaxNumberOfDashActions;
@ -249,7 +279,9 @@ public partial class PlayerController : CharacterBody3D
_grounded.StateEntered += OnGrounded;
_grounded.StatePhysicsProcessing += HandleGrounded;
_airborne.StatePhysicsProcessing += HandleAirborne;
_onWallHugCanceled.StatePhysicsProcessing += HandleAirborne;
_onWallHugging.StatePhysicsProcessing += HandleWallHugging;
_onWallHanging.StatePhysicsProcessing += HandleWallHanging;
_coyoteEnabled.StateEntered += StartCoyoteTime;
_coyoteTimer.Timeout += CoyoteExpired;
@ -342,27 +374,30 @@ public partial class PlayerController : CharacterBody3D
}
public void OnInputJumpPressed()
{
if (MoveSystem.CanMantle())
if (CanMantle())
{
Mantle();
return;
}
if (_grounded.Active || _coyoteEnabled.Active)
if (_empowerOn.Active && CanPerformEmpoweredAction())
{
PerformEmpoweredAction();
PerformJump(MoveSystem.JumpTypes.JumpFromDash);
PerformJump(JumpTypes.JumpFromDash);
_playerState.SendEvent("megajump");
}
else
PerformJump(MoveSystem.JumpTypes.SimpleJump);
PerformJump(JumpTypes.SimpleJump);
else if (_doubleJumpEnabled.Active)
if (_empowerOn.Active && CanPerformEmpoweredAction())
{
PerformEmpoweredAction();
PerformJump(MoveSystem.JumpTypes.JumpFromDash);
PerformJump(JumpTypes.JumpFromDash);
_playerState.SendEvent("megajump");
}
else
PerformJump(MoveSystem.JumpTypes.DoubleJump);
PerformJump(JumpTypes.DoubleJump);
else if (_onWall.Active)
JumpFromWall(_empowerOn.Active);
@ -462,15 +497,17 @@ public partial class PlayerController : CharacterBody3D
if (isEmpowered && CanPerformEmpoweredAction())
{
PerformEmpoweredAction();
PerformJump(MoveSystem.JumpTypes.JumpFromDash, jumpDirection);
PerformJump(JumpTypes.JumpFromDash, jumpDirection);
return;
}
PerformJump(MoveSystem.JumpTypes.JumpFromWall, jumpDirection);
PerformJump(JumpTypes.JumpFromWall, jumpDirection);
}
private void PerformJump(MoveSystem.JumpTypes jumpType, Vector3? jumpDirection = null)
private void PerformJump(JumpTypes jumpType, Vector3? jumpDirection = null)
{
var effectiveJumpDirection = jumpDirection ?? Vector3.Up;
var jumpVector = (effectiveJumpDirection.Normalized() + Vector3.Up).Normalized();
if (jumpType == JumpTypes.DoubleJump)
_canDash = false;
// var proportionOfTimeGone = _timeAfterDashingTimer.TimeLeft / _timeAfterDashingTimer.WaitTime;
// var actualBoost = 1 + MaxJumpBoostAfterDashing * proportionOfTimeGone;
@ -482,17 +519,65 @@ public partial class PlayerController : CharacterBody3D
bool doesCapsuleHaveCrouchingHeight = CapsuleCollider.IsCrouchingHeight();
bool isPlayerDead = HealthSystem.IsDead();
if (!doesCapsuleHaveCrouchingHeight && !isPlayerDead)
MoveSystem.Jump(jumpType, jumpVector);
Jump(jumpType, jumpVector);
}
public void Jump(JumpTypes jumpType, Vector3? jumpDirection = null, float boost = 1.0f)
{
var effectiveJumpDirection = jumpDirection ?? Vector3.Up;
var jumpForce = 0.0f;
switch (jumpType)
{
case JumpTypes.DoubleJump:
jumpForce = CalculateDoubleJumpForce();
break;
case JumpTypes.SimpleJump:
jumpForce = CalculateJumpForce();
break;
case JumpTypes.JumpFromDash:
jumpForce = CalculateJumpFromDashForce();
break;
case JumpTypes.JumpFromWall:
jumpForce = CalculateJumpFromWallForce();
break;
default:
jumpForce = CalculateJumpForce();
break;
}
var currentHorizontalVelocity = new Vector3(Velocity.X, 0, Velocity.Z);
var jumpVelocity = jumpForce * effectiveJumpDirection * boost;
Velocity = currentHorizontalVelocity + jumpVelocity;
}
// Mantling
public void Mantle()
{
_playerState.SendEvent("mantle");
var optionTween = MoveSystem.Mantle();
var optionTween = FindMantle();
if (optionTween.IsSome(out var tween))
tween.Finished += MantleFinished;
}
public bool CanMantle()
{
var mantleLocationResult = MantleSystem.FindMantleInFrontOfPlayer();
return mantleLocationResult.IsSome(out _);
}
public Option<Tween> FindMantle()
{
var mantleLocationResult = MantleSystem.FindMantleInFrontOfPlayer();
if (mantleLocationResult.IsSome(out var mantleLocation))
{
var duration = 0.1f * mantleLocation.DistanceTo(Position);
var tween = TweenQueueSystem.TweenToLocation(new TweenQueueSystem.TweenInputs(mantleLocation, duration));
return tween.Some();
}
return Option<Tween>.None;
}
public void MantleFinished()
{
_playerState.SendEvent("grounded");
@ -635,22 +720,14 @@ public partial class PlayerController : CharacterBody3D
// Physics processes
public void HandleGrounded(float delta)
{
MoveOnGround(delta);
_canDash = true;
if (!isOnFloorCustom())
_playerState.SendEvent("start_falling");
}
public void HandleGroundedStanding(float delta)
{
CapsuleCollider.UndoCrouching(delta, 1);
HeadSystem.SetHeight(CapsuleCollider.GetCurrentHeight());
}
public void HandleGroundedCrouched(float delta)
{
CapsuleCollider.Crouch(delta, 1);
HeadSystem.SetHeight(CapsuleCollider.GetCurrentHeight());
}
public void HandleAirborne(float delta)
{
MoveInAir(delta);
if (isOnFloorCustom())
_playerState.SendEvent("grounded");
if (WallHugSystem.IsWallHugging() && Velocity.Y < 0)
@ -658,12 +735,19 @@ public partial class PlayerController : CharacterBody3D
}
public void HandleWallHugging(float delta)
{
WallHug(delta);
if (isOnFloorCustom())
_playerState.SendEvent("grounded");
if (!WallHugSystem.IsWallHugging())
_playerState.SendEvent("start_falling");
}
public void HandleWallHanging(float delta)
{
WallHang(delta);
}
///////////////////////////
// Stateless logic ////////
///////////////////////////
@ -673,27 +757,62 @@ public partial class PlayerController : CharacterBody3D
HeadSystem.LookAround(inputLookDir);
}
private void MoveAround(double delta)
public void MoveOnGround(double delta)
{
var moveAroundParams = new MoveSystem.MoveAroundParameters(
delta,
_inputMove,
isOnFloorCustom(),
HealthSystem.IsDead(),
IsHeadTouchingCeiling(),
_onWallHanging.Active,
_onWallHugging.Active);
MoveSystem.MoveAround(moveAroundParams);
Vector3 direction = HeadSystem.Transform.Basis * _inputMove;
var accelerationFactor = direction.Length() > 0 ? AccelerationSpeedFactorFloor : DecelerationSpeedFactorFloor;
float xAcceleration = Mathf.Lerp(Velocity.X, direction.X * _targetSpeed,
(float)delta * accelerationFactor);
float zAcceleration = Mathf.Lerp(Velocity.Z, direction.Z * _targetSpeed,
(float)delta * accelerationFactor);
Velocity = new Vector3(xAcceleration, Velocity.Y, zAcceleration);
}
public void MoveInAir(double delta)
{
Velocity = new Vector3(
x: Velocity.X,
y: Velocity.Y - (CalculateGravityForce() * (float)delta),
z: Velocity.Z);
// The code below is required to quickly adjust player's position on Y-axis when there's a ceiling on the
// trajectory of player's jump and player is standing
if (IsHeadTouchingCeiling())
{
Velocity = new Vector3(
x: Velocity.X,
y: Velocity.Y - 2.0f,
z: Velocity.Z);
}
}
public void WallHug(float delta)
{
float xAcceleration = Mathf.Lerp(Velocity.X, 0,
(float)delta * WallHugHorizontalDeceleration);
float zAcceleration = Mathf.Lerp(Velocity.Z, 0,
(float)delta * WallHugHorizontalDeceleration);
Velocity = new Vector3(
x: xAcceleration,
y: -WallHugDownwardSpeed,
z: zAcceleration);
}
public void WallHang(float delta)
{
Velocity = Vector3.Zero;
MoveAndSlide();
}
private void HandleStairs(float delta)
{
StairsSystem.UpStairsCheckParams upStairsCheckParams = new StairsSystem.UpStairsCheckParams
{
IsOnFloorCustom = isOnFloorCustom(),
IsCapsuleHeightLessThanNormal = CapsuleCollider.IsCapsuleHeightLessThanNormal(),
CurrentSpeedGreaterThanWalkSpeed = MoveSystem._currentSpeed > MoveSystem.WalkSpeed,
CurrentSpeedGreaterThanWalkSpeed = false,
IsCrouchingHeight = CapsuleCollider.IsCrouchingHeight(),
Delta = (float)delta,
FloorMaxAngle = FloorMaxAngle,
@ -714,7 +833,7 @@ public partial class PlayerController : CharacterBody3D
StairsSystem.DownStairsCheckParams downStairsCheckParams = new StairsSystem.DownStairsCheckParams
{
IsOnFloor = IsOnFloor(), // TODO: replace on IsOnFloor Custom
IsOnFloor = IsOnFloor(),
IsCrouchingHeight = CapsuleCollider.IsCrouchingHeight(),
LastFrameWasOnFloor = _lastFrameWasOnFloor,
CapsuleDefaultHeight = CapsuleCollider.GetDefaultHeight(),
@ -737,7 +856,7 @@ public partial class PlayerController : CharacterBody3D
StairsSystem.SlideCameraParams slideCameraParams = new StairsSystem.SlideCameraParams
{
IsCapsuleHeightLessThanNormal = CapsuleCollider.IsCapsuleHeightLessThanNormal(),
CurrentSpeedGreaterThanWalkSpeed = MoveSystem._currentSpeed > MoveSystem.WalkSpeed,
CurrentSpeedGreaterThanWalkSpeed = false,
BetweenCrouchingAndNormalHeight = CapsuleCollider.IsBetweenCrouchingAndNormalHeight(),
Delta = (float)delta
};
@ -758,7 +877,7 @@ public partial class PlayerController : CharacterBody3D
{
IsCrouchingHeight = CapsuleCollider.IsCrouchingHeight(),
Delta = (float)delta,
SprintSpeed = MoveSystem.SprintSpeed,
SprintSpeed = WalkSpeed,
Velocity = Velocity
};
FieldOfView.PerformFovAdjustment(fovParams);
@ -767,6 +886,13 @@ public partial class PlayerController : CharacterBody3D
///////////////////////////
// Helpers ////////////////
///////////////////////////
public float CalculateJumpForce() => _gravity * StartVelocity;
public float CalculateJumpFromDashForce() => CalculateJumpForce() * JumpFromDashSpeedFactor;
public float CalculateJumpFromWallForce() => CalculateJumpForce() * JumpFromWallSpeedFactor;
public float CalculateDoubleJumpForce() => CalculateJumpForce() * DoubleJumpSpeedFactor;
public float CalculateGravityForce() => _gravity * Weight;
public void ReduceTimeScaleWhileAiming()
{
Engine.SetTimeScale(TimeScaleAimInAir);
@ -808,7 +934,7 @@ public partial class PlayerController : CharacterBody3D
TweenQueueSystem.ProcessTweens();
LookAround();
MoveAround(delta);
// MoveAround(delta);
CameraModifications((float) delta);
HandleStairs((float) delta);
}