gd: added wall hugging

This commit is contained in:
2025-06-10 11:46:51 +02:00
parent 141688ef32
commit 0c015750fd
5 changed files with 135 additions and 28 deletions

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=34 format=3 uid="uid://bei4nhkf8lwdo"]
[gd_scene load_steps=35 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="Resource" uid="uid://bl5crtu1gkrtr" path="res://systems/inputs/base_mode/base_mode.tres" id="3_cresl"]
@ -29,6 +29,7 @@
[ext_resource type="Script" uid="uid://jk2jm1g6q853" path="res://addons/godot_state_charts/compound_state.gd" id="26_infe6"]
[ext_resource type="Script" uid="uid://cytafq8i1y8qm" path="res://addons/godot_state_charts/atomic_state.gd" id="27_34snm"]
[ext_resource type="Script" uid="uid://c1vp0ojjvaby1" path="res://addons/godot_state_charts/parallel_state.gd" id="27_infe6"]
[ext_resource type="Script" uid="uid://tjiji63wlom5" path="res://systems/wall_hug/WallHugSystem.cs" id="27_n7qhm"]
[ext_resource type="Script" uid="uid://cf1nsco3w0mf6" path="res://addons/godot_state_charts/transition.gd" id="28_n7qhm"]
[ext_resource type="PackedScene" uid="uid://ckm3d6k08a72u" path="res://systems/weapon/weapon.tscn" id="29_wv70j"]
@ -141,6 +142,26 @@ StraightThrowDuration = 0.07
[node name="CoyoteTime" type="Timer" parent="."]
wait_time = 0.2
[node name="WallHugSystem" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0)
script = ExtResource("27_n7qhm")
[node name="back" type="RayCast3D" parent="WallHugSystem"]
target_position = Vector3(0, 0, 1)
collision_mask = 2
[node name="front" type="RayCast3D" parent="WallHugSystem"]
target_position = Vector3(0, 0, -1)
collision_mask = 2
[node name="right" type="RayCast3D" parent="WallHugSystem"]
target_position = Vector3(1, 0, 0)
collision_mask = 2
[node name="left" type="RayCast3D" parent="WallHugSystem"]
target_position = Vector3(-1, 0, 0)
collision_mask = 2
[node name="StateChart" type="Node" parent="."]
script = ExtResource("25_wv70j")
metadata/_custom_type_script = "uid://couw105c3bde4"
@ -273,7 +294,7 @@ script = ExtResource("27_34snm")
[node name="OnJump" type="Node" parent="StateChart/Root/Movement/Hanging"]
script = ExtResource("28_n7qhm")
to = NodePath("../../Airborne/Jump")
to = NodePath("../../Airborne/JumpFromWall")
event = &"jump"
delay_in_seconds = "0.0"
@ -283,21 +304,6 @@ to = NodePath("../../Airborne/CoyoteEnabled")
event = &"drop"
delay_in_seconds = "0.0"
[node name="WallHugging" type="Node" parent="StateChart/Root/Movement"]
script = ExtResource("27_34snm")
[node name="OnJump" type="Node" parent="StateChart/Root/Movement/WallHugging"]
script = ExtResource("28_n7qhm")
to = NodePath("../../Airborne/Jump")
event = &"jump"
delay_in_seconds = "0.0"
[node name="OnDrop" type="Node" parent="StateChart/Root/Movement/WallHugging"]
script = ExtResource("28_n7qhm")
to = NodePath("../../Airborne/CoyoteEnabled")
event = &"drop"
delay_in_seconds = "0.0"
[node name="Airborne" type="Node" parent="StateChart/Root/Movement"]
script = ExtResource("26_infe6")
initial_state = NodePath("CoyoteEnabled")
@ -308,6 +314,21 @@ to = NodePath("../../Grounded")
event = &"grounded"
delay_in_seconds = "0.0"
[node name="OnWallHug" type="Node" parent="StateChart/Root/Movement/Airborne"]
script = ExtResource("28_n7qhm")
to = NodePath("../WallHugging")
event = &"wall_hug"
delay_in_seconds = "0.0"
[node name="WallHugging" type="Node" parent="StateChart/Root/Movement/Airborne"]
script = ExtResource("27_34snm")
[node name="OnJump" type="Node" parent="StateChart/Root/Movement/Airborne/WallHugging"]
script = ExtResource("28_n7qhm")
to = NodePath("../../JumpFromWall")
event = &"jump"
delay_in_seconds = "0.0"
[node name="CoyoteEnabled" type="Node" parent="StateChart/Root/Movement/Airborne"]
script = ExtResource("27_34snm")
@ -332,6 +353,15 @@ to = NodePath("../../DoubleJumpEnabled")
event = &"to_double_jump"
delay_in_seconds = "0.0"
[node name="JumpFromWall" type="Node" parent="StateChart/Root/Movement/Airborne"]
script = ExtResource("27_34snm")
[node name="ToDoubleJump" type="Node" parent="StateChart/Root/Movement/Airborne/JumpFromWall"]
script = ExtResource("28_n7qhm")
to = NodePath("../../DoubleJumpEnabled")
event = &"to_double_jump"
delay_in_seconds = "0.0"
[node name="DoubleJumpEnabled" type="Node" parent="StateChart/Root/Movement/Airborne"]
script = ExtResource("27_34snm")

View File

@ -21,6 +21,7 @@ public partial class PlayerController : CharacterBody3D
public TweenQueueSystem TweenQueueSystem;
public Node3D WeaponRoot;
public WeaponSystem WeaponSystem;
public WallHugSystem WallHugSystem;
private bool _movementEnabled = true;
@ -57,6 +58,7 @@ public partial class PlayerController : CharacterBody3D
private StateChartState _airborne;
private StateChartState _coyoteEnabled;
private StateChartState _jump;
private StateChartState _jumpFromWall;
private StateChartState _doubleJumpEnabled;
private StateChartState _doubleJump;
private StateChartState _falling;
@ -92,6 +94,7 @@ public partial class PlayerController : CharacterBody3D
MoveSystem = GetNode<MoveSystem>("MoveSystem");
DashSystem = GetNode<DashSystem>("DashSystem");
StairsSystem = GetNode<StairsSystem>("StairsSystem");
WallHugSystem = GetNode<WallHugSystem>("WallHugSystem");
RayCast3D stairsBelowRayCast3D = GetNode<RayCast3D>("StairsBelowRayCast3D");
RayCast3D stairsAheadRayCast3D = GetNode<RayCast3D>("StairsAheadRayCast3D");
_headCollisionDetectors = new RayCast3D[NUM_OF_HEAD_COLLISION_DETECTORS];
@ -117,10 +120,11 @@ public partial class PlayerController : CharacterBody3D
_grounded = StateChartState.Of(GetNode("StateChart/Root/Movement/Grounded"));
_mantling = StateChartState.Of(GetNode("StateChart/Root/Movement/Mantling"));
_movHanging = StateChartState.Of(GetNode("StateChart/Root/Movement/Hanging"));
_wallHugging = StateChartState.Of(GetNode("StateChart/Root/Movement/WallHugging"));
_airborne = StateChartState.Of(GetNode("StateChart/Root/Movement/Airborne"));
_wallHugging = StateChartState.Of(GetNode("StateChart/Root/Movement/Airborne/WallHugging"));
_coyoteEnabled = StateChartState.Of(GetNode("StateChart/Root/Movement/Airborne/CoyoteEnabled"));
_jump = StateChartState.Of(GetNode("StateChart/Root/Movement/Airborne/Jump"));
_jumpFromWall = StateChartState.Of(GetNode("StateChart/Root/Movement/Airborne/JumpFromWall"));
_doubleJumpEnabled = StateChartState.Of(GetNode("StateChart/Root/Movement/Airborne/DoubleJumpEnabled"));
_doubleJump = StateChartState.Of(GetNode("StateChart/Root/Movement/Airborne/DoubleJump"));
_falling = StateChartState.Of(GetNode("StateChart/Root/Movement/Airborne/Falling"));
@ -150,6 +154,7 @@ public partial class PlayerController : CharacterBody3D
StairsSystem.Init(stairsBelowRayCast3D, stairsAheadRayCast3D, cameraSmooth);
DashSystem.Init(HeadSystem, camera, TweenQueueSystem);
WeaponSystem.Init(HeadSystem, camera);
WallHugSystem.Init();
// RPG Stuff
HealthSystem.HealthSystemInitParams healthSystemParams = new HealthSystem.HealthSystemInitParams()
@ -179,6 +184,7 @@ public partial class PlayerController : CharacterBody3D
_coyoteEnabled.StateEntered += StartCoyoteTime;
_coyoteTimer.Timeout += CoyoteExpired;
_jump.StateEntered += Jump;
_jumpFromWall.StateEntered += JumpFromWall;
_doubleJump.StateEntered += DoubleJump;
_mantling.StateEntered += Mantle;
@ -252,17 +258,27 @@ public partial class PlayerController : CharacterBody3D
_playerState.SendEvent("to_double_jump");
PerformJump(false);
}
public void JumpFromWall()
{
_playerState.SendEvent("to_double_jump");
var wallNormal = WallHugSystem.GetWallNormal();
if (wallNormal.IsSome(out var normal))
PerformJump(false, normal);
PerformJump(false);
}
public void DoubleJump()
{
_playerState.SendEvent("to_falling");
PerformJump(true);
}
private void PerformJump(bool isDoubleJump)
private void PerformJump(bool isDoubleJump, Vector3? jumpDirection = null)
{
var effectiveJumpDirection = jumpDirection ?? Vector3.Up;
var jumpVector = (effectiveJumpDirection.Normalized() + Vector3.Up).Normalized();
bool doesCapsuleHaveCrouchingHeight = CapsuleCollider.IsCrouchingHeight();
bool isPlayerDead = HealthSystem.IsDead();
if (!doesCapsuleHaveCrouchingHeight && !isPlayerDead)
MoveSystem.Jump(isDoubleJump);
MoveSystem.Jump(isDoubleJump, jumpVector);
}
// Mantling
@ -363,6 +379,8 @@ public partial class PlayerController : CharacterBody3D
{
if (isOnFloorCustom())
_playerState.SendEvent("grounded");
if (WallHugSystem.IsWallHugging() && Velocity.Y < 0)
_playerState.SendEvent("wall_hug");
}
///////////////////////////
@ -382,7 +400,8 @@ public partial class PlayerController : CharacterBody3D
isOnFloorCustom(),
HealthSystem.IsDead(),
IsHeadTouchingCeiling(),
_actionHanging.Active);
_actionHanging.Active,
_wallHugging.Active);
MoveSystem.MoveAround(moveAroundParams);
}
@ -502,4 +521,5 @@ public partial class PlayerController : CharacterBody3D
CameraModifications((float) delta);
HandleStairs((float) delta);
}
}