jumping around
This commit is contained in:
parent
88747c868f
commit
a44bb1c14a
2 changed files with 101 additions and 12 deletions
|
@ -8,6 +8,10 @@ extends CharacterBody2D
|
||||||
@export var turn_acceleration: float
|
@export var turn_acceleration: float
|
||||||
@export var stopping_force: float
|
@export var stopping_force: float
|
||||||
|
|
||||||
|
@export_group("Air Movement")
|
||||||
|
@export var gravity: float
|
||||||
|
@export var jump_power: float
|
||||||
|
|
||||||
@export_group("Internal References")
|
@export_group("Internal References")
|
||||||
@export var state_chart: StateChart
|
@export var state_chart: StateChart
|
||||||
@export var graphics: Node2D
|
@export var graphics: Node2D
|
||||||
|
@ -26,15 +30,21 @@ func _physics_process(delta: float) -> void:
|
||||||
if velocity.x != 0.0:
|
if velocity.x != 0.0:
|
||||||
graphics.scale.x = signf(velocity.x)
|
graphics.scale.x = signf(velocity.x)
|
||||||
|
|
||||||
|
state_chart.set_expression_property(&"on_floor", is_on_floor())
|
||||||
|
|
||||||
input_dir = Input.get_vector(&"move_left", &"move_right", &"move_up", &"move_down")
|
input_dir = Input.get_vector(&"move_left", &"move_right", &"move_up", &"move_down")
|
||||||
|
|
||||||
|
|
||||||
|
func _unhandled_input(event: InputEvent) -> void:
|
||||||
|
if event.is_action_pressed(&"jump"):
|
||||||
|
state_chart.send_event(&"jump_pressed")
|
||||||
|
|
||||||
|
|
||||||
#region Idle
|
#region Idle
|
||||||
func _slow_to_stop(delta: float) -> void:
|
func _slow_to_stop(delta: float) -> void:
|
||||||
velocity.x = move_toward(velocity.x, 0.0, stopping_force * delta)
|
velocity.x = move_toward(velocity.x, 0.0, stopping_force * delta)
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Running
|
#region Running
|
||||||
func _apply_run_acceleration(delta: float) -> void:
|
func _apply_run_acceleration(delta: float) -> void:
|
||||||
if absf(velocity.x) < max_run_speed:
|
if absf(velocity.x) < max_run_speed:
|
||||||
|
@ -49,3 +59,19 @@ func _scale_run_animation(delta:float) -> void:
|
||||||
func _apply_turn_acceleration(delta: float) -> void:
|
func _apply_turn_acceleration(delta: float) -> void:
|
||||||
velocity.x += input_dir.x * turn_acceleration * delta
|
velocity.x += input_dir.x * turn_acceleration * delta
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region Falling
|
||||||
|
func _apply_gravity(delta: float) -> void:
|
||||||
|
velocity.y += gravity * delta
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region Jumping
|
||||||
|
func _start_jump() -> void:
|
||||||
|
velocity.y = -jump_power
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region Missile
|
||||||
|
#endregion
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
[gd_scene load_steps=16 format=3 uid="uid://bwtpsjpe2lf7l"]
|
[gd_scene load_steps=19 format=3 uid="uid://bwtpsjpe2lf7l"]
|
||||||
|
|
||||||
[ext_resource type="Script" uid="uid://dhyi4yn0xleoy" path="res://objects/new_player/new_player.gd" id="1_xs4s5"]
|
[ext_resource type="Script" uid="uid://dhyi4yn0xleoy" path="res://objects/new_player/new_player.gd" id="1_xs4s5"]
|
||||||
[ext_resource type="Texture2D" uid="uid://c71nqfyw4a3v4" path="res://assets/textures/player/capri.png" id="3_trcll"]
|
[ext_resource type="Texture2D" uid="uid://c71nqfyw4a3v4" path="res://assets/textures/player/capri.png" id="3_trcll"]
|
||||||
|
@ -12,6 +12,12 @@
|
||||||
[sub_resource type="CircleShape2D" id="CircleShape2D_tvyy1"]
|
[sub_resource type="CircleShape2D" id="CircleShape2D_tvyy1"]
|
||||||
radius = 6.0
|
radius = 6.0
|
||||||
|
|
||||||
|
[sub_resource type="Resource" id="Resource_sfpnl"]
|
||||||
|
resource_name = "Not On Floor"
|
||||||
|
script = ExtResource("8_8i2im")
|
||||||
|
expression = "not on_floor"
|
||||||
|
metadata/_custom_type_script = "uid://b6u7unac5srh0"
|
||||||
|
|
||||||
[sub_resource type="Resource" id="Resource_mq184"]
|
[sub_resource type="Resource" id="Resource_mq184"]
|
||||||
resource_name = "Horizontal Input"
|
resource_name = "Horizontal Input"
|
||||||
script = ExtResource("8_8i2im")
|
script = ExtResource("8_8i2im")
|
||||||
|
@ -48,15 +54,29 @@ script = ExtResource("8_8i2im")
|
||||||
expression = "input_dir.x == signf(velocity.x)"
|
expression = "input_dir.x == signf(velocity.x)"
|
||||||
metadata/_custom_type_script = "uid://b6u7unac5srh0"
|
metadata/_custom_type_script = "uid://b6u7unac5srh0"
|
||||||
|
|
||||||
|
[sub_resource type="Resource" id="Resource_163ft"]
|
||||||
|
resource_name = "On Floor"
|
||||||
|
script = ExtResource("8_8i2im")
|
||||||
|
expression = "on_floor"
|
||||||
|
metadata/_custom_type_script = "uid://b6u7unac5srh0"
|
||||||
|
|
||||||
|
[sub_resource type="Resource" id="Resource_whx0j"]
|
||||||
|
resource_name = "Falling Down"
|
||||||
|
script = ExtResource("8_8i2im")
|
||||||
|
expression = "velocity.y > 0.0"
|
||||||
|
metadata/_custom_type_script = "uid://b6u7unac5srh0"
|
||||||
|
|
||||||
[node name="Player" type="CharacterBody2D" node_paths=PackedStringArray("state_chart", "graphics", "run_animation")]
|
[node name="Player" type="CharacterBody2D" node_paths=PackedStringArray("state_chart", "graphics", "run_animation")]
|
||||||
collision_layer = 16
|
collision_layer = 16
|
||||||
collision_mask = 3
|
collision_mask = 3
|
||||||
floor_snap_length = 3.0
|
floor_snap_length = 3.0
|
||||||
script = ExtResource("1_xs4s5")
|
script = ExtResource("1_xs4s5")
|
||||||
run_acceleration = 100.0
|
run_acceleration = 150.0
|
||||||
max_run_speed = 100.0
|
max_run_speed = 100.0
|
||||||
turn_acceleration = 200.0
|
turn_acceleration = 250.0
|
||||||
stopping_force = 150.0
|
stopping_force = 200.0
|
||||||
|
gravity = 450.0
|
||||||
|
jump_power = 180.0
|
||||||
state_chart = NodePath("StateChart")
|
state_chart = NodePath("StateChart")
|
||||||
graphics = NodePath("Graphics")
|
graphics = NodePath("Graphics")
|
||||||
run_animation = NodePath("Graphics/Sprite/Run")
|
run_animation = NodePath("Graphics/Sprite/Run")
|
||||||
|
@ -109,6 +129,7 @@ script = ExtResource("5_bcjtl")
|
||||||
track_in_editor = true
|
track_in_editor = true
|
||||||
initial_expression_properties = {
|
initial_expression_properties = {
|
||||||
&"input_dir": Vector2(0, 0),
|
&"input_dir": Vector2(0, 0),
|
||||||
|
&"on_floor": false,
|
||||||
&"velocity": Vector2(0, 0)
|
&"velocity": Vector2(0, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,13 +142,26 @@ editor_description = "Player is standing on a floor."
|
||||||
script = ExtResource("6_jnxnd")
|
script = ExtResource("6_jnxnd")
|
||||||
initial_state = NodePath("Standing")
|
initial_state = NodePath("Standing")
|
||||||
|
|
||||||
|
[node name="if NotOnFloor" type="Node" parent="StateChart/Root/Grounded"]
|
||||||
|
editor_description = "Transition to falling state if not touching a floor."
|
||||||
|
script = ExtResource("7_rgjdc")
|
||||||
|
to = NodePath("../../Airborne/Falling")
|
||||||
|
guard = SubResource("Resource_sfpnl")
|
||||||
|
delay_in_seconds = "0.0"
|
||||||
|
|
||||||
|
[node name="on JumpPressed" type="Node" parent="StateChart/Root/Grounded"]
|
||||||
|
script = ExtResource("7_rgjdc")
|
||||||
|
to = NodePath("../../Airborne/Jumping")
|
||||||
|
event = &"jump_pressed"
|
||||||
|
delay_in_seconds = "0.0"
|
||||||
|
|
||||||
[node name="Standing" type="Node" parent="StateChart/Root/Grounded"]
|
[node name="Standing" type="Node" parent="StateChart/Root/Grounded"]
|
||||||
editor_description = "Player is standing still or coming to a stop."
|
editor_description = "Player is standing still or coming to a stop."
|
||||||
script = ExtResource("6_jnxnd")
|
script = ExtResource("6_jnxnd")
|
||||||
initial_state = NodePath("Idle")
|
initial_state = NodePath("Idle")
|
||||||
metadata/_custom_type_script = "uid://b11v7h3ny6kh1"
|
metadata/_custom_type_script = "uid://b11v7h3ny6kh1"
|
||||||
|
|
||||||
[node name="when WalkHeld" type="Node" parent="StateChart/Root/Grounded/Standing"]
|
[node name="if WalkHeld" type="Node" parent="StateChart/Root/Grounded/Standing"]
|
||||||
editor_description = "Transition to running state if a horizontal direction is being held."
|
editor_description = "Transition to running state if a horizontal direction is being held."
|
||||||
script = ExtResource("7_rgjdc")
|
script = ExtResource("7_rgjdc")
|
||||||
to = NodePath("../../Running")
|
to = NodePath("../../Running")
|
||||||
|
@ -138,7 +172,7 @@ delay_in_seconds = "0.0"
|
||||||
editor_description = "Player is stopped, standing still."
|
editor_description = "Player is stopped, standing still."
|
||||||
script = ExtResource("10_mvu25")
|
script = ExtResource("10_mvu25")
|
||||||
|
|
||||||
[node name="when Moving" type="Node" parent="StateChart/Root/Grounded/Standing/Idle"]
|
[node name="if Moving" type="Node" parent="StateChart/Root/Grounded/Standing/Idle"]
|
||||||
editor_description = "Transition to stopping state if the player has horizontal velocity."
|
editor_description = "Transition to stopping state if the player has horizontal velocity."
|
||||||
script = ExtResource("7_rgjdc")
|
script = ExtResource("7_rgjdc")
|
||||||
to = NodePath("../../Stopping")
|
to = NodePath("../../Stopping")
|
||||||
|
@ -149,7 +183,7 @@ delay_in_seconds = "0.0"
|
||||||
editor_description = "Player is slowing to a stop naturally."
|
editor_description = "Player is slowing to a stop naturally."
|
||||||
script = ExtResource("10_mvu25")
|
script = ExtResource("10_mvu25")
|
||||||
|
|
||||||
[node name="when Stopped" type="Node" parent="StateChart/Root/Grounded/Standing/Stopping"]
|
[node name="if Stopped" type="Node" parent="StateChart/Root/Grounded/Standing/Stopping"]
|
||||||
editor_description = "Transition to idle state when the player has come to a full stop."
|
editor_description = "Transition to idle state when the player has come to a full stop."
|
||||||
script = ExtResource("7_rgjdc")
|
script = ExtResource("7_rgjdc")
|
||||||
to = NodePath("../../Idle")
|
to = NodePath("../../Idle")
|
||||||
|
@ -160,14 +194,14 @@ delay_in_seconds = "0.0"
|
||||||
editor_description = "Player is accelerating in the held input direction."
|
editor_description = "Player is accelerating in the held input direction."
|
||||||
script = ExtResource("10_mvu25")
|
script = ExtResource("10_mvu25")
|
||||||
|
|
||||||
[node name="when WalkReleased" type="Node" parent="StateChart/Root/Grounded/Running"]
|
[node name="if WalkReleased" type="Node" parent="StateChart/Root/Grounded/Running"]
|
||||||
editor_description = "Transition to standing state if no input direction is held."
|
editor_description = "Transition to standing state if no input direction is held."
|
||||||
script = ExtResource("7_rgjdc")
|
script = ExtResource("7_rgjdc")
|
||||||
to = NodePath("../../Standing")
|
to = NodePath("../../Standing")
|
||||||
guard = SubResource("Resource_8c74o")
|
guard = SubResource("Resource_8c74o")
|
||||||
delay_in_seconds = "0.0"
|
delay_in_seconds = "0.0"
|
||||||
|
|
||||||
[node name="when OppositeInput" type="Node" parent="StateChart/Root/Grounded/Running"]
|
[node name="if OppositeInput" type="Node" parent="StateChart/Root/Grounded/Running"]
|
||||||
editor_description = "Transition to turning state if the held input dir is opposite the horizontal velocity."
|
editor_description = "Transition to turning state if the held input dir is opposite the horizontal velocity."
|
||||||
script = ExtResource("7_rgjdc")
|
script = ExtResource("7_rgjdc")
|
||||||
to = NodePath("../../Turning")
|
to = NodePath("../../Turning")
|
||||||
|
@ -178,14 +212,14 @@ delay_in_seconds = "0.0"
|
||||||
editor_description = "Player is more quickly accelerating in the held input direction to slow and turn around."
|
editor_description = "Player is more quickly accelerating in the held input direction to slow and turn around."
|
||||||
script = ExtResource("10_mvu25")
|
script = ExtResource("10_mvu25")
|
||||||
|
|
||||||
[node name="when WalkReleased" type="Node" parent="StateChart/Root/Grounded/Turning"]
|
[node name="if WalkReleased" type="Node" parent="StateChart/Root/Grounded/Turning"]
|
||||||
editor_description = "Transition to standing state if no input direction is held."
|
editor_description = "Transition to standing state if no input direction is held."
|
||||||
script = ExtResource("7_rgjdc")
|
script = ExtResource("7_rgjdc")
|
||||||
to = NodePath("../../Standing")
|
to = NodePath("../../Standing")
|
||||||
guard = SubResource("Resource_8c74o")
|
guard = SubResource("Resource_8c74o")
|
||||||
delay_in_seconds = "0.0"
|
delay_in_seconds = "0.0"
|
||||||
|
|
||||||
[node name="when AlignedInput" type="Node" parent="StateChart/Root/Grounded/Turning"]
|
[node name="if AlignedInput" type="Node" parent="StateChart/Root/Grounded/Turning"]
|
||||||
editor_description = "Transition to running state when input direction matches horizontal velocity."
|
editor_description = "Transition to running state when input direction matches horizontal velocity."
|
||||||
script = ExtResource("7_rgjdc")
|
script = ExtResource("7_rgjdc")
|
||||||
to = NodePath("../../Running")
|
to = NodePath("../../Running")
|
||||||
|
@ -193,10 +227,34 @@ guard = SubResource("Resource_e1cbd")
|
||||||
delay_in_seconds = "0.0"
|
delay_in_seconds = "0.0"
|
||||||
|
|
||||||
[node name="Airborne" type="Node" parent="StateChart/Root"]
|
[node name="Airborne" type="Node" parent="StateChart/Root"]
|
||||||
|
editor_description = "Player is in the air."
|
||||||
script = ExtResource("6_jnxnd")
|
script = ExtResource("6_jnxnd")
|
||||||
initial_state = NodePath("Falling")
|
initial_state = NodePath("Falling")
|
||||||
|
|
||||||
|
[node name="if OnFloor" type="Node" parent="StateChart/Root/Airborne"]
|
||||||
|
editor_description = "Transition to grounded state if touching a floor."
|
||||||
|
script = ExtResource("7_rgjdc")
|
||||||
|
to = NodePath("../../Grounded")
|
||||||
|
guard = SubResource("Resource_163ft")
|
||||||
|
delay_in_seconds = "0.0"
|
||||||
|
|
||||||
[node name="Falling" type="Node" parent="StateChart/Root/Airborne"]
|
[node name="Falling" type="Node" parent="StateChart/Root/Airborne"]
|
||||||
|
editor_description = "Player is falling down."
|
||||||
|
script = ExtResource("10_mvu25")
|
||||||
|
|
||||||
|
[node name="Jumping" type="Node" parent="StateChart/Root/Airborne"]
|
||||||
|
editor_description = "Player has jumped and is rising."
|
||||||
|
script = ExtResource("10_mvu25")
|
||||||
|
|
||||||
|
[node name="if DownVelocity" type="Node" parent="StateChart/Root/Airborne/Jumping"]
|
||||||
|
editor_description = "Transition to the falling state if moving downwards."
|
||||||
|
script = ExtResource("7_rgjdc")
|
||||||
|
to = NodePath("../../Falling")
|
||||||
|
guard = SubResource("Resource_whx0j")
|
||||||
|
delay_in_seconds = "0.0"
|
||||||
|
|
||||||
|
[node name="Missile" type="Node" parent="StateChart/Root/Airborne"]
|
||||||
|
editor_description = "Player is hurtling through the air and ricocheting off of surfaces."
|
||||||
script = ExtResource("10_mvu25")
|
script = ExtResource("10_mvu25")
|
||||||
|
|
||||||
[node name="Voice" type="AudioStreamPlayer" parent="."]
|
[node name="Voice" type="AudioStreamPlayer" parent="."]
|
||||||
|
@ -211,3 +269,8 @@ bus = &"Capri"
|
||||||
[connection signal="state_physics_processing" from="StateChart/Root/Grounded/Running" to="." method="_apply_run_acceleration"]
|
[connection signal="state_physics_processing" from="StateChart/Root/Grounded/Running" to="." method="_apply_run_acceleration"]
|
||||||
[connection signal="state_entered" from="StateChart/Root/Grounded/Turning" to="Graphics/Sprite/Skid" method="play"]
|
[connection signal="state_entered" from="StateChart/Root/Grounded/Turning" to="Graphics/Sprite/Skid" method="play"]
|
||||||
[connection signal="state_physics_processing" from="StateChart/Root/Grounded/Turning" to="." method="_apply_turn_acceleration"]
|
[connection signal="state_physics_processing" from="StateChart/Root/Grounded/Turning" to="." method="_apply_turn_acceleration"]
|
||||||
|
[connection signal="state_physics_processing" from="StateChart/Root/Airborne" to="." method="_apply_gravity"]
|
||||||
|
[connection signal="state_entered" from="StateChart/Root/Airborne/Falling" to="Graphics/Sprite/Fall" method="play"]
|
||||||
|
[connection signal="state_entered" from="StateChart/Root/Airborne/Jumping" to="." method="_start_jump"]
|
||||||
|
[connection signal="state_entered" from="StateChart/Root/Airborne/Jumping" to="Graphics/Sprite/Jump" method="play"]
|
||||||
|
[connection signal="state_entered" from="StateChart/Root/Airborne/Missile" to="Graphics/Sprite/Missile" method="play"]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue