diff options
| author | Jan Tuomi <jan@jantuomi.fi> | 2025-11-27 21:34:33 +0200 |
|---|---|---|
| committer | Jan Tuomi <jan@jantuomi.fi> | 2025-11-27 21:34:33 +0200 |
| commit | cdfe41bab22af70a5cf3c7d45e0a27efc8efe854 (patch) | |
| tree | 8ce7fffa9162f77eeda5c203143003bba00b8af4 | |
| parent | 99be84042a7796fab592d46db3704f1e0058440d (diff) | |
Add curve following system
| -rw-r--r-- | player.gd | 93 | ||||
| -rw-r--r-- | player.tscn | 1 | ||||
| -rw-r--r-- | room_test1.tscn | 14 |
3 files changed, 81 insertions, 27 deletions
@@ -5,9 +5,23 @@ extends CharacterBody2D @export var jump_strength: float @export var coyote_max: int @export var jump_held_coef: float +@export var curve_speed: float ## float (-1.0 .. 1.0) or null var forced_input_x: Variant +var curve_t: float = 0 +var followed_curve: Curve2D +enum { + MOVEMENT_NORMAL, + MOVEMENT_FORCED, + MOVEMENT_CURVE, +} +var movement_type = MOVEMENT_NORMAL +var movement_handlers = { + MOVEMENT_NORMAL: _movement_normal, + MOVEMENT_FORCED: _movement_forced, + MOVEMENT_CURVE: _movement_curve, +} var gravity = ProjectSettings.get_setting("physics/2d/default_gravity") var prev_dir: float = 0.0 @@ -42,11 +56,39 @@ func _process(_delta: float) -> void: $AnimatedSprite2D.set_animation("idle") func _physics_process(delta: float): + movement_handlers[movement_type].call(delta) + +func set_forced_input(dir: Variant): + var jump_coef = 1.3 + if dir == null: + movement_type = MOVEMENT_NORMAL + forced_input_x = null + return + + movement_type = MOVEMENT_FORCED + #movement_type = MOVEMENT_CURVE + if dir == "LEFT": + forced_input_x = -1.0 + elif dir == "JUMP_LEFT": + velocity.y = -jump_coef * jump_strength + forced_input_x = -1.0 + elif dir == "RIGHT": + forced_input_x = 1.0 + elif dir == "JUMP_RIGHT": + velocity.y = -jump_coef * jump_strength + forced_input_x = 1.0 + elif dir == "JUMP": + velocity.y = -jump_strength + forced_input_x = 0.0 + gravity = 0.0 + elif dir == "DROP": + forced_input_x = 0.0 + +func _movement_normal(delta: float): var grav_coef = 1.0 if velocity.y < 0 and Input.is_action_pressed("jump"): grav_coef = 1.0 + (jump_held_coef * velocity.y / jump_strength) - #print_debug("1.0 + (", velocity.y, " / (", jump_held_coef, " * ", jump_strength, ")) = ", grav_coef) velocity.y += grav_coef * gravity * delta if velocity.y > jump_strength: velocity.y = jump_strength @@ -56,7 +98,7 @@ func _physics_process(delta: float): else: coyote += 1 - var can_jump = coyote < coyote_max and jump_was_released and forced_input_x == null + var can_jump = coyote < coyote_max and jump_was_released if Input.is_action_pressed("jump") and can_jump: velocity.y = -jump_strength # Force coyote time to expire to forbid double jumps @@ -67,9 +109,7 @@ func _physics_process(delta: float): jump_was_released = true var direction: float - if forced_input_x != null: - direction = forced_input_x - elif Input.is_action_pressed("move_left") and Input.is_action_pressed("move_right"): + if Input.is_action_pressed("move_left") and Input.is_action_pressed("move_right"): # This is a fix to the stalled movement issue # where the player stops if both x inputs are pressed at the same time. # To fix it, we store the previous direction the player was moving in, and change @@ -83,23 +123,26 @@ func _physics_process(delta: float): move_and_slide() -func set_forced_input(dir: Variant): - var jump_coef = 1.3 - if dir == null: - forced_input_x = null - elif dir == "LEFT": - forced_input_x = -1.0 - elif dir == "JUMP_LEFT": - velocity.y = -jump_coef * jump_strength - forced_input_x = -1.0 - elif dir == "RIGHT": - forced_input_x = 1.0 - elif dir == "JUMP_RIGHT": - velocity.y = -jump_coef * jump_strength - forced_input_x = 1.0 - elif dir == "JUMP": - velocity.y = -jump_strength - forced_input_x = 0.0 - gravity = 0.0 - elif dir == "DROP": - forced_input_x = 0.0 +func _movement_forced(delta: float): + velocity.y += gravity * delta + if velocity.y > jump_strength: + velocity.y = jump_strength + + velocity.x = forced_input_x * speed + + move_and_slide() + +func follow_curve(curve: Curve2D): + movement_type = MOVEMENT_CURVE + followed_curve = curve + curve_t = 0 + +func _movement_curve(delta: float): + #print_debug("t:", curve_t) + curve_t += curve_speed * delta + if curve_t >= 1.0: + movement_type = MOVEMENT_NORMAL + followed_curve = null + return + + position = followed_curve.sample_baked(curve_t * followed_curve.get_baked_length(), true) diff --git a/player.tscn b/player.tscn index df969c9..5e576e5 100644 --- a/player.tscn +++ b/player.tscn @@ -103,6 +103,7 @@ size = Vector2(6, 6) [node name="Player" type="CharacterBody2D"] script = ExtResource("2_onrkg") +curve_speed = 0.1 [node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."] position = Vector2(0, -1) diff --git a/room_test1.tscn b/room_test1.tscn index f5a1d36..55a0449 100644 --- a/room_test1.tscn +++ b/room_test1.tscn @@ -1,11 +1,18 @@ -[gd_scene load_steps=6 format=4 uid="uid://cfj21o6wsd3hv"] +[gd_scene load_steps=7 format=4 uid="uid://cfj21o6wsd3hv"] -[ext_resource type="Texture2D" uid="uid://dkek11j5dh6as" path="res://assets.bak/kenney_1-bit-platformer-pack/Tiles/Default/tile_0338.png" id="1_m3rqw"] +[ext_resource type="Texture2D" uid="uid://dkek11j5dh6as" path="res://assets.untracked/kenney_1-bit-platformer-pack/Tiles/Default/tile_0338.png" id="1_m3rqw"] [ext_resource type="PackedScene" uid="uid://coxnsharboouu" path="res://player.tscn" id="2_qglt1"] [ext_resource type="PackedScene" uid="uid://dfyvkc2qlcdac" path="res://game_camera.tscn" id="3_mrrx3"] [ext_resource type="PackedScene" uid="uid://df0loiahar23o" path="res://door.tscn" id="6_lfjh7"] [ext_resource type="TileSet" uid="uid://0cd30n67d6k5" path="res://tileset_kenney_platformer.tres" id="7_qglt1"] +[sub_resource type="Curve2D" id="Curve2D_m3rqw"] +bake_interval = 1.0 +_data = { +"points": PackedVector2Array(0, 0, 0, 0, 104, 72, 0, 0, 0, 0, 120, 3, 0, 0, 0, 0, 176, -28, 0, 0, 0, 0, 223, -43, 0, 0, 0, 0, 271, -53, 0, 0, 0, 0, 317, -83, 0, 0, 0, 0, 341, -128, 0, 0, 0, 0, 368, -159, 0, 0, 0, 0, 407, -167, 0, 0, 0, 0, 433, -152, 0, 0, 0, 0, 439, -125, 0, 0, 0, 0, 440, -65) +} +point_count = 12 + [node name="RoomTest1" type="Node2D"] process_mode = 1 @@ -64,3 +71,6 @@ scroll_scale = Vector2(1.3, 1) [node name="TileMapLayer" type="TileMapLayer" parent="FgParallax"] tile_map_data = PackedByteArray("AAAFAPv/AQATAAAAAAAFAPz/AQATAAAAAAAFAP3/AQATAAAAAAAFAP7/AQATAAAAAAAFAP//AQATAAAAAAAFAAAAAQATAAAAAAAFAAEAAQATAAAAAAAFAAIAAQATAAEAAAAKAAoAAQAPAAEAAAAKAAkAAQAPAAEAAAAKAAgAAQAPAAEAAAAKAAcAAQAPAAEAAAAKAAYAAQAPAAEAAAAKAAUAAQAPAAEAAAAKAAQAAQAPAAEAAAAKAAMAAQAPAAAAAAAJAPv/AQATAAAAAAAJAPz/AQATAAAAAAAJAP3/AQATAAAAAAAJAP7/AQATAAAAAAAJAP//AQATAAAAAAAJAAAAAQAOAAAAAAA=") tile_set = ExtResource("7_qglt1") + +[node name="Path2D" type="Path2D" parent="."] +curve = SubResource("Curve2D_m3rqw") |
