refactor bouncing off

This commit is contained in:
2025-08-22 13:02:49 +10:00
parent 7ebc50e6fc
commit 3e308d7c4c
2 changed files with 240 additions and 31 deletions

View File

@@ -1,4 +1,4 @@
[gd_scene load_steps=67 format=3 uid="uid://cfvb33kf48sga"]
[gd_scene load_steps=93 format=3 uid="uid://cfvb33kf48sga"]
[ext_resource type="PackedScene" uid="uid://db7d5pwp4gds" path="res://scenes/player.tscn" id="1_bl13t"]
[ext_resource type="Texture2D" uid="uid://j3xt1tbjcu2r" path="res://assets/textures/reference/jumpking_0.png" id="1_iyx0m"]
@@ -48,6 +48,12 @@
[ext_resource type="Texture2D" uid="uid://2puws332f0xj" path="res://assets/textures/reference/jumpking_41.png" id="43_xsg57"]
[ext_resource type="Texture2D" uid="uid://dj38nkh6j122c" path="res://assets/textures/reference/jumpking_42.png" id="44_qx18j"]
[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_335pd"]
normal = Vector2(-1, 0)
[sub_resource type="WorldBoundaryShape2D" id="WorldBoundaryShape2D_376r3"]
normal = Vector2(1, 0)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_74lek"]
size = Vector2(481, 32)
@@ -57,12 +63,6 @@ size = Vector2(128, 176)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_lpvoh"]
size = Vector2(128, 176)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_at5dv"]
size = Vector2(20, 15503)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_jcd4p"]
size = Vector2(22, 15522)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_uacia"]
size = Vector2(112, 50)
@@ -105,6 +105,84 @@ size = Vector2(58, 16)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_si7kk"]
size = Vector2(72, 57)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_335pd"]
size = Vector2(66, 16)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_376r3"]
size = Vector2(72, 16)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_xsg57"]
size = Vector2(48, 72)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_qx18j"]
size = Vector2(16, 99)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_cgmg2"]
size = Vector2(72, 16)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_n11rl"]
size = Vector2(46, 16)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_y8o6s"]
size = Vector2(40, 80)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_1urli"]
size = Vector2(16, 138)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_35prk"]
size = Vector2(40, 16)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_eokji"]
size = Vector2(40, 16)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_8nvs4"]
size = Vector2(16, 64)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_2ixbo"]
size = Vector2(40, 16)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_uig8d"]
size = Vector2(40, 16)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_eixnv"]
size = Vector2(40, 16)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_68ewr"]
size = Vector2(32, 16)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_osnag"]
size = Vector2(176, 48)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_vqdru"]
size = Vector2(48, 16)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_tnm51"]
size = Vector2(50, 16)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_tbman"]
size = Vector2(192, 176)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_gwri7"]
size = Vector2(112, 16)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_3t5ks"]
size = Vector2(16, 56)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_elqqf"]
size = Vector2(40, 16)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_tdgre"]
size = Vector2(96, 56)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_c0b3m"]
size = Vector2(80, 176)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_80m0l"]
size = Vector2(136, 16)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_qfy4l"]
size = Vector2(56, 56)
[node name="Test" type="Node2D"]
[node name="Debugger" parent="." instance=ExtResource("1_lpvoh")]
@@ -329,6 +407,14 @@ texture = ExtResource("44_qx18j")
[node name="Geometry" type="StaticBody2D" parent="."]
position = Vector2(240, 180)
[node name="WorldBoundaryShapeR" type="CollisionShape2D" parent="Geometry"]
position = Vector2(232, -214)
shape = SubResource("WorldBoundaryShape2D_335pd")
[node name="WorldBoundaryShapeL" type="CollisionShape2D" parent="Geometry"]
position = Vector2(-232, -224)
shape = SubResource("WorldBoundaryShape2D_376r3")
[node name="CollisionShape2D" type="CollisionShape2D" parent="Geometry"]
position = Vector2(-0.5, 164)
shape = SubResource("RectangleShape2D_74lek")
@@ -341,14 +427,6 @@ shape = SubResource("RectangleShape2D_j5jx5")
position = Vector2(176, 92)
shape = SubResource("RectangleShape2D_lpvoh")
[node name="CollisionShape2D4" type="CollisionShape2D" parent="Geometry"]
position = Vector2(242, -7571.5)
shape = SubResource("RectangleShape2D_at5dv")
[node name="CollisionShape2D5" type="CollisionShape2D" parent="Geometry"]
position = Vector2(-243, -7581)
shape = SubResource("RectangleShape2D_jcd4p")
[node name="CollisionShape2D6" type="CollisionShape2D" parent="Geometry"]
position = Vector2(0, -117)
shape = SubResource("RectangleShape2D_uacia")
@@ -405,6 +483,126 @@ shape = SubResource("RectangleShape2D_n74k2")
position = Vector2(-68, -912.5)
shape = SubResource("RectangleShape2D_si7kk")
[node name="CollisionShape2D20" type="CollisionShape2D" parent="Geometry"]
position = Vector2(-209, -1036)
shape = SubResource("RectangleShape2D_335pd")
[node name="CollisionShape2D21" type="CollisionShape2D" parent="Geometry"]
position = Vector2(-68, -1036)
shape = SubResource("RectangleShape2D_376r3")
[node name="CollisionShape2D22" type="CollisionShape2D" parent="Geometry"]
position = Vector2(80, -1064)
shape = SubResource("RectangleShape2D_xsg57")
[node name="CollisionShape2D23" type="CollisionShape2D" parent="Geometry"]
position = Vector2(96, -1138.5)
shape = SubResource("RectangleShape2D_qx18j")
[node name="CollisionShape2D24" type="CollisionShape2D" parent="Geometry"]
position = Vector2(124, -1180)
shape = SubResource("RectangleShape2D_cgmg2")
[node name="CollisionShape2D25" type="CollisionShape2D" parent="Geometry"]
position = Vector2(215, -1124)
shape = SubResource("RectangleShape2D_n11rl")
[node name="CollisionShape2D26" type="CollisionShape2D" parent="Geometry"]
position = Vector2(-84, -1132)
shape = SubResource("RectangleShape2D_y8o6s")
[node name="CollisionShape2D27" type="CollisionShape2D" parent="Geometry"]
position = Vector2(-96, -1239)
shape = SubResource("RectangleShape2D_1urli")
[node name="CollisionShape2D28" type="CollisionShape2D" parent="Geometry"]
position = Vector2(-108, -1300)
shape = SubResource("RectangleShape2D_35prk")
[node name="CollisionShape2D29" type="CollisionShape2D" parent="Geometry"]
position = Vector2(108, -1300)
shape = SubResource("RectangleShape2D_eokji")
[node name="CollisionShape2D30" type="CollisionShape2D" parent="Geometry"]
position = Vector2(96, -1276)
shape = SubResource("RectangleShape2D_8nvs4")
[node name="CollisionShape2D31" type="CollisionShape2D" parent="Geometry"]
position = Vector2(108, -1371)
shape = SubResource("RectangleShape2D_2ixbo")
[node name="CollisionShape2D32" type="CollisionShape2D" parent="Geometry"]
position = Vector2(-220, -1372)
shape = SubResource("RectangleShape2D_uig8d")
[node name="CollisionShape2D33" type="CollisionShape2D" parent="Geometry"]
position = Vector2(220, -1452)
shape = SubResource("RectangleShape2D_eixnv")
[node name="CollisionShape2D34" type="CollisionShape2D" parent="Geometry"]
position = Vector2(-184, -1524)
shape = SubResource("RectangleShape2D_68ewr")
[node name="CollisionShape2D35" type="CollisionShape2D" parent="Geometry"]
position = Vector2(64, -1524)
shape = SubResource("RectangleShape2D_68ewr")
[node name="CollisionShape2D36" type="CollisionShape2D" parent="Geometry"]
position = Vector2(0, -1540)
shape = SubResource("RectangleShape2D_68ewr")
[node name="CollisionShape2D37" type="CollisionShape2D" parent="Geometry"]
position = Vector2(-64, -1556)
shape = SubResource("RectangleShape2D_68ewr")
[node name="CollisionShape2D38" type="CollisionShape2D" parent="Geometry"]
position = Vector2(0, -1628)
shape = SubResource("RectangleShape2D_osnag")
[node name="CollisionShape2D39" type="CollisionShape2D" parent="Geometry"]
position = Vector2(-88, -1732)
shape = SubResource("RectangleShape2D_vqdru")
[node name="CollisionShape2D40" type="CollisionShape2D" parent="Geometry"]
position = Vector2(-209, -1788)
shape = SubResource("RectangleShape2D_tnm51")
[node name="CollisionShape2D41" type="CollisionShape2D" parent="Geometry"]
position = Vector2(144, -1812)
shape = SubResource("RectangleShape2D_tbman")
[node name="CollisionShape2D42" type="CollisionShape2D" parent="Geometry"]
position = Vector2(-104, -1860)
shape = SubResource("RectangleShape2D_gwri7")
[node name="CollisionShape2D43" type="CollisionShape2D" parent="Geometry"]
position = Vector2(-152, -1880)
shape = SubResource("RectangleShape2D_3t5ks")
[node name="CollisionShape2D44" type="CollisionShape2D" parent="Geometry"]
position = Vector2(-164, -1900)
shape = SubResource("RectangleShape2D_elqqf")
[node name="CollisionShape2D45" type="CollisionShape2D" parent="Geometry"]
position = Vector2(136, -1968)
shape = SubResource("RectangleShape2D_tdgre")
[node name="CollisionShape2D46" type="CollisionShape2D" parent="Geometry"]
position = Vector2(88, -2044)
shape = SubResource("RectangleShape2D_c0b3m")
[node name="CollisionShape2D47" type="CollisionShape2D" parent="Geometry"]
position = Vector2(116, -2124)
shape = SubResource("RectangleShape2D_80m0l")
[node name="CollisionShape2D48" type="CollisionShape2D" parent="Geometry"]
position = Vector2(-76, -1952)
shape = SubResource("RectangleShape2D_qfy4l")
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="Geometry"]
position = Vector2(-76, -1952)
polygon = PackedVector2Array(28, -28, -60, -28, -60, -117)
[node name="Player" parent="." instance=ExtResource("1_bl13t")]
position = Vector2(240, 328)

View File

@@ -28,7 +28,6 @@ var _jump_charge_frames: float = 0
var _charge_strength: float = 0
var _direction: float = 0
var _freemove_enabled: bool = false
var _min_y: float = 999;
@onready var _saved_state: Vector2 = global_position
@@ -47,7 +46,11 @@ func _ready() -> void:
func _physics_process(delta: float) -> void:
if _freemove_enabled:
velocity = (
Input.get_vector("move_left", "move_right", "move_up", "move_down").normalized()
(
Input
. get_vector("move_left", "move_right", "move_up", "move_down")
. normalized()
)
* _jump_speed
* (4.0 if Input.is_action_pressed("jump") else 1.0)
)
@@ -56,6 +59,8 @@ func _physics_process(delta: float) -> void:
var new_velocity := velocity
var is_on_floor_prev := is_on_floor()
var is_on_ceiling_prev := is_on_ceiling()
var is_on_wall_prev := is_on_wall()
# falling
new_velocity.y = (
@@ -106,18 +111,20 @@ func _physics_process(delta: float) -> void:
# bounce off
if collision_occured:
var side: Side = SIDE_BOTTOM
if not is_on_floor():
if not is_on_ceiling():
velocity.x = -new_velocity.x
side = SIDE_LEFT if velocity.x > 0 else SIDE_RIGHT
else:
side = SIDE_TOP
velocity.x *= _wall_bounce_velocity_loss
elif not is_on_floor_prev:
if not is_on_floor_prev and is_on_floor():
_trajectory.visible = false
if not is_on_floor() or (not is_on_floor_prev and is_on_floor()):
collided.emit(side, new_velocity)
collided.emit(SIDE_BOTTOM, new_velocity)
if not is_on_ceiling_prev and is_on_ceiling():
velocity.x *= _wall_bounce_velocity_loss
collided.emit(SIDE_TOP, new_velocity)
if (
not is_on_wall_prev
and is_on_wall()
and not is_on_floor()
and not is_zero_approx(new_velocity.x)
):
velocity.x = -new_velocity.x * _wall_bounce_velocity_loss
collided.emit(SIDE_LEFT if velocity.x > 0 else SIDE_RIGHT, new_velocity)
queue_redraw()
@@ -127,9 +134,13 @@ func _physics_process(delta: float) -> void:
Debugger.text("is_charging_jump", _is_charging_jump)
Debugger.text("jump_charge_frames", _jump_charge_frames)
Debugger.text("charge_strength", _charge_strength)
if global_position.y < _min_y:
_min_y = global_position.y
Debugger.text("min_y", _min_y)
Debugger.text("is_on_ceiling", is_on_ceiling())
Debugger.text("is_on_ceiling_only", is_on_ceiling_only())
Debugger.text("is_on_floor", is_on_floor())
Debugger.text("is_on_floor_only", is_on_floor_only())
Debugger.text("is_on_wall", is_on_wall())
Debugger.text("is_on_wall_only", is_on_wall_only())
Debugger.text("get_slide_collision_count", get_slide_collision_count())
func _unhandled_input(event: InputEvent) -> void: