diff --git a/content/scenes/player.tscn b/content/scenes/player.tscn index 4904c3b..9ad4e90 100644 --- a/content/scenes/player.tscn +++ b/content/scenes/player.tscn @@ -1,14 +1,18 @@ -[gd_scene load_steps=5 format=3 uid="uid://ul8o2n823qod"] +[gd_scene load_steps=9 format=3 uid="uid://ul8o2n823qod"] [ext_resource type="Script" path="res://content/scripts/player.gd" id="1_i6r2s"] [ext_resource type="Script" path="res://content/scripts/smoother.gd" id="2_dpu6i"] [ext_resource type="Script" path="res://content/scripts/dampened_camera.gd" id="3_24qwg"] +[ext_resource type="Script" path="res://addons/godot_state_charts/state_chart.gd" id="4_erffw"] +[ext_resource type="Script" path="res://addons/godot_state_charts/compound_state.gd" id="5_mmqqh"] +[ext_resource type="Script" path="res://addons/godot_state_charts/atomic_state.gd" id="6_8xdrw"] +[ext_resource type="Script" path="res://addons/godot_state_charts/transition.gd" id="7_525mu"] [sub_resource type="CylinderShape3D" id="CylinderShape3D_tpgoe"] height = 1.8 radius = 0.25 -[node name="Player" type="StaticBody3D" node_paths=PackedStringArray("head", "body", "camera", "collision_shape")] +[node name="Player" type="StaticBody3D" node_paths=PackedStringArray("head", "body", "camera", "collision_shape", "state_chart")] script = ExtResource("1_i6r2s") max_speed = 6 slope_limit = 40.0 @@ -18,6 +22,7 @@ head = NodePath("Body/Head") body = NodePath("Body") camera = NodePath("Body/Head/Smoother/Camera3D") collision_shape = NodePath("MoveCollider") +state_chart = NodePath("StateChart") [node name="MoveCollider" type="CollisionShape3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.9, 0) @@ -35,3 +40,47 @@ target = NodePath("..") [node name="Camera3D" type="Camera3D" parent="Body/Head/Smoother" node_paths=PackedStringArray("target")] script = ExtResource("3_24qwg") target = NodePath("..") + +[node name="StateChart" type="Node" parent="."] +script = ExtResource("4_erffw") + +[node name="Root" type="Node" parent="StateChart"] +script = ExtResource("5_mmqqh") +initial_state = NodePath("Grounded") + +[node name="Grounded" type="Node" parent="StateChart/Root"] +script = ExtResource("6_8xdrw") + +[node name="On Jump" type="Node" parent="StateChart/Root/Grounded"] +script = ExtResource("7_525mu") +to = NodePath("../../Airborne/Jumping") +event = &"Jump" + +[node name="On Airborne" type="Node" parent="StateChart/Root/Grounded"] +script = ExtResource("7_525mu") +to = NodePath("../../Airborne/Falling") +event = &"Airborne" + +[node name="Airborne" type="Node" parent="StateChart/Root"] +script = ExtResource("5_mmqqh") +initial_state = NodePath("Jumping") + +[node name="On Grounded" type="Node" parent="StateChart/Root/Airborne"] +script = ExtResource("7_525mu") +to = NodePath("../../Grounded") +event = &"Grounded" + +[node name="Jumping" type="Node" parent="StateChart/Root/Airborne"] +script = ExtResource("6_8xdrw") + +[node name="On Airborne" type="Node" parent="StateChart/Root/Airborne/Jumping"] +script = ExtResource("7_525mu") +to = NodePath("../../Falling") +event = &"Airborne" + +[node name="Falling" type="Node" parent="StateChart/Root/Airborne"] +script = ExtResource("6_8xdrw") + +[connection signal="state_physics_processing" from="StateChart/Root/Grounded" to="." method="_on_grounded_state_physics_processing"] +[connection signal="state_physics_processing" from="StateChart/Root/Airborne/Jumping" to="." method="_on_jumping_state_physics_processing"] +[connection signal="state_physics_processing" from="StateChart/Root/Airborne/Falling" to="." method="_on_falling_state_physics_processing"] diff --git a/content/scripts/player.gd b/content/scripts/player.gd index 171272f..4cb6397 100644 --- a/content/scripts/player.gd +++ b/content/scripts/player.gd @@ -22,10 +22,8 @@ enum MovementType { VERTICAL, LATERAL } @export var head: Node3D @export var body: Node3D @export var camera: DampenedCamera3D - -## A reference to the collision shape this physics body is using -## (It's just a bit easier rather than aquiring the reference via code) @export var collision_shape: CollisionShape3D +@export var state_chart: StateChart @export_group("Advanced") ## Stop movement under this distance, but only if the movement touches at least 2 steep slopes @@ -137,21 +135,6 @@ func get_input() -> Vector2: return input_dir.rotated(-body.rotation.y) -func _physics_process(delta: float) -> void: - # Calculate our target velocity - var move_speed := max_speed if at_max_speed else slow_speed - var target_velocity_h := get_input() * move_speed - var target_velocity_v := 0.0 - if !grounded: - target_velocity_v += _velocity.y - elif jump_pressed: - target_velocity_v += jump_speed - target_velocity_v -= gravity * delta - - var target_velocity := Vector3(target_velocity_h.x, target_velocity_v, target_velocity_h.y) - move(target_velocity, delta) - - # Entry point to moving func move(intended_velocity: Vector3, delta: float) -> void: var start_position := position @@ -441,3 +424,37 @@ func _horz(value: Vector3) -> Vector3: func _vert(value: Vector3) -> Vector3: return Vector3(0, value.y, 0) + + +func _on_grounded_state_physics_processing(delta: float) -> void: + var move_speed := max_speed if at_max_speed else slow_speed + var target_velocity_h := get_input() * move_speed + var target_velocity_v := -gravity * delta + var target_velocity := Vector3(target_velocity_h.x, target_velocity_v, target_velocity_h.y) + move(target_velocity, delta) + + if jump_pressed: + state_chart.send_event("Jump") + elif !grounded: + state_chart.send_event("Airborne") + + +func _on_falling_state_physics_processing(delta: float) -> void: + var move_speed := max_speed if at_max_speed else slow_speed + var target_velocity_h := get_input() * move_speed + var target_velocity_v := _velocity.y - gravity * delta + var target_velocity := Vector3(target_velocity_h.x, target_velocity_v, target_velocity_h.y) + move(target_velocity, delta) + + if grounded: + state_chart.send_event("Grounded") + + +func _on_jumping_state_physics_processing(delta: float) -> void: + var move_speed := max_speed if at_max_speed else slow_speed + var target_velocity_h := get_input() * move_speed + var target_velocity_v := jump_speed + var target_velocity := Vector3(target_velocity_h.x, target_velocity_v, target_velocity_h.y) + move(target_velocity, delta) + + state_chart.send_event("Airborne")