From 49d0f97e46498be8739036de72c68733fa3badb9 Mon Sep 17 00:00:00 2001 From: Malte Hegg <Malte@Maltes-MacBook-Air-2.local> Date: Fri, 29 Nov 2024 12:55:12 +0100 Subject: [PATCH 1/2] WIP wall jump improvement --- player/jump_handler.gd | 4 ++++ player/player.gd | 31 ++++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/player/jump_handler.gd b/player/jump_handler.gd index 18a1111..b65b039 100644 --- a/player/jump_handler.gd +++ b/player/jump_handler.gd @@ -47,6 +47,10 @@ func handle_jump(event : InputEvent) -> void: player.jumping = true player.bounce_jump_possible = false $SoundJump.play() + elif player.is_in_wall_slide_situation(): + pass + var jump_dir = player.if_can_wall_jump_get_direction() + else: if event.is_released() and player.jumping: # Reduce jump height if releasing the jump key before reaching the apex. diff --git a/player/player.gd b/player/player.gd index e6431f6..b7e9320 100644 --- a/player/player.gd +++ b/player/player.gd @@ -74,12 +74,29 @@ func _ready() -> void: func _process(_delta : float) -> void: - if is_in_wall_slide_situation() and current_state == $States/StandardState: - enter_wall_slide_state() - + #if is_in_wall_slide_situation() and current_state == $States/StandardState: + #enter_wall_slide_state() + pass +func is_wall_jumpable(collision): + return true +#IF true do wall slide animation and slow fall func is_in_wall_slide_situation() -> bool: - var armature_trans := ($Model/Armature as Node3D).get_transform() + #is close to wall in all directions (not roof or floor) + if is_on_floor() or velocity.y > 0: + return false + var direction = Vector3(1,1,1) + for i in range(8): + var test_collision : KinematicCollision3D = move_and_collide(direction*0.5, true) + direction = direction.rotated(Vector3.UP, PI/4) + if test_collision and is_wall_jumpable(test_collision): + return true + return false + +func if_can_wall_jump_get_direction() -> Vector3: + return Vector3(1,1,1) + + """var armature_trans := ($Model/Armature as Node3D).get_transform() var arm_facing_vec := (armature_trans.basis.get_rotation_quaternion() * Vector3(0,0,-1)).normalized() var approach_vector_wall := get_wall_normal() *-1 var angle_facing_and_wall := approach_vector_wall.angle_to(arm_facing_vec) @@ -92,7 +109,7 @@ func is_in_wall_slide_situation() -> bool: if velocity.y < 0 and horizontal_velocity.length() < 5 and ((is_on_wall_only() and angle_facing_and_wall < PI/1.5) or (test_collision and not is_on_floor() and test_col_has_angle_for_wall_slide)): return true - return false + return false""" func _physics_process(delta: float) -> void: @@ -205,7 +222,6 @@ func check_enemy_collision() -> void: var collision : KinematicCollision3D = move_and_collide(Vector3(0.0,0.0,0.0)) if collision and collision.collider.is_in_group("Enemies") and timer.is_stopped(): timer.start(1) - print("collided") lose_health(1, collision.position) @@ -223,11 +239,8 @@ func lose_health(amount: int = 1, damage_source: Vector3 = Vector3.ZERO) -> void flash_on_damage(0.1) if current_health == 0: - print("dead") get_tree().reload_current_scene() return - #Play death animation - #Die if damage_source != Vector3.ZERO: apply_knockback(damage_source) -- GitLab From e82d7e4f6abbb858cdad48258c90b7e266f75e94 Mon Sep 17 00:00:00 2001 From: Filip Berg <filbe316@student.liu.se> Date: Fri, 29 Nov 2024 15:10:31 +0100 Subject: [PATCH 2/2] new wall jump pog --- player/dash_handler.gd | 4 --- player/jump_handler.gd | 21 +++++++---- player/player.gd | 62 +++++++------------------------- player/player.tscn | 8 ++--- player/standard_state.gd | 10 ++++++ player/wall_sliding_state.gd | 70 ------------------------------------ tdde20_2024_grupp2 | 1 - 7 files changed, 39 insertions(+), 137 deletions(-) delete mode 100644 player/wall_sliding_state.gd delete mode 160000 tdde20_2024_grupp2 diff --git a/player/dash_handler.gd b/player/dash_handler.gd index 17895f2..e1cc1bd 100644 --- a/player/dash_handler.gd +++ b/player/dash_handler.gd @@ -15,13 +15,10 @@ const AIR_DASH_SPEED_DENY : float = 8.0 @onready var dash_timer : Timer = $DashTimer var is_dashing : bool = false -# Called when the node enters the scene tree for the first time. func _ready() -> void: player.floor_max_speed_lerp_factor = 10.0 - #pass # Replace with function body. -# Called every frame. 'delta' is the elapsed time since the previous frame. func _physics_process(delta: float) -> void: if player.is_on_floor(): player.accel = lerp(player.accel, player.BASE_ACCEL, delta*DASH_ACCEL_DENY) @@ -46,7 +43,6 @@ func handle_dash(event : InputEvent) -> void: player.max_speed = player.BASE_MAX_SPEED * DASH_SPEED_BOOST dash_timer.start() dash_timeout.start() - func _on_dash_timer_timeout() -> void: diff --git a/player/jump_handler.gd b/player/jump_handler.gd index b65b039..f8dda64 100644 --- a/player/jump_handler.gd +++ b/player/jump_handler.gd @@ -36,8 +36,10 @@ func pop_jump_stack() -> float: func handle_jump(event : InputEvent) -> void: + if event.is_echo(): + return #if not jumping and jump_attempt: - if event.is_pressed() and not event.is_echo() and (player.is_on_floor() or player.bounce_jump_possible): + if event.is_pressed() and (player.is_on_floor() or player.bounce_jump_possible): if not player.jumping: $LandingTimer.stop() var jump_modifier := pop_jump_stack() @@ -47,17 +49,24 @@ func handle_jump(event : InputEvent) -> void: player.jumping = true player.bounce_jump_possible = false $SoundJump.play() - elif player.is_in_wall_slide_situation(): - pass - var jump_dir = player.if_can_wall_jump_get_direction() - + # Check for wall jump + elif event.is_pressed() and player.wall_jump_time > 0.0 and \ + (player.last_wall_jump_direction.is_zero_approx() or player.last_wall_jump_direction.dot(player.last_collision_direction) < 0.1): + player.wall_jump_time = 0.0 + player.last_collision_direction.y = 0.0 + player.last_collision_direction = player.last_collision_direction.normalized() * 0.5 + if player.last_collision_direction.dot(player.control_dir) > 0.0: + player.last_collision_direction = player.control_dir + player.velocity += player.last_collision_direction * player.JUMP_VELOCITY + player.velocity.y = player.JUMP_VELOCITY + player.last_wall_jump_direction = Vector3(player.velocity.x, 0.0, player.velocity.z).normalized() else: if event.is_released() and player.jumping: # Reduce jump height if releasing the jump key before reaching the apex. player.velocity.y *= 0.5 player.jumping = false player.just_jumped = true - if event.is_pressed() and not event.is_echo(): + if event.is_pressed(): queue_jump(event) func bounce() -> void: diff --git a/player/player.gd b/player/player.gd index b7e9320..c4c8531 100644 --- a/player/player.gd +++ b/player/player.gd @@ -9,8 +9,7 @@ enum _Anim { enum States { STANDARD, - CANNONBALL, - WALL_SLIDING + CANNONBALL } const CHAR_SCALE := Vector3(1.0, 1.0, 1.0) @@ -28,6 +27,7 @@ const DAMAGE_COOLDOWN := 2.0 const KICK_ANIMATION_MIX := 0.9 const KNOCKBACK_FORCE := 5.0 const KNOCKBACK_DURATION := 0.2 +const WALL_JUMP_MARGIN := 0.25 var max_speed := 6.0 var accel := 14.0 @@ -39,8 +39,9 @@ var control_dir := Vector3() var jumping := false var just_jumped := false var bounce_jump_possible := false -var prev_shoot := false -var shoot_blend := 0.0 +var wall_jump_time := 0.0 +var last_collision_direction : Vector3 = Vector3.ZERO +var last_wall_jump_direction : Vector3 = Vector3.ZERO var queued_jump : InputEvent = null var timer := Timer.new() var can_take_damage := true @@ -63,6 +64,8 @@ var checkpoint : Checkpoint @onready var mesh_instance := $Model/Armature/Skeleton3D/Filur var flash_material: StandardMaterial3D + + func _ready() -> void: $AnimationTree.active = true @@ -73,45 +76,6 @@ func _ready() -> void: flash_material.emission_energy = 1.5 -func _process(_delta : float) -> void: - #if is_in_wall_slide_situation() and current_state == $States/StandardState: - #enter_wall_slide_state() - pass -func is_wall_jumpable(collision): - return true - -#IF true do wall slide animation and slow fall -func is_in_wall_slide_situation() -> bool: - #is close to wall in all directions (not roof or floor) - if is_on_floor() or velocity.y > 0: - return false - var direction = Vector3(1,1,1) - for i in range(8): - var test_collision : KinematicCollision3D = move_and_collide(direction*0.5, true) - direction = direction.rotated(Vector3.UP, PI/4) - if test_collision and is_wall_jumpable(test_collision): - return true - return false - -func if_can_wall_jump_get_direction() -> Vector3: - return Vector3(1,1,1) - - """var armature_trans := ($Model/Armature as Node3D).get_transform() - var arm_facing_vec := (armature_trans.basis.get_rotation_quaternion() * Vector3(0,0,-1)).normalized() - var approach_vector_wall := get_wall_normal() *-1 - var angle_facing_and_wall := approach_vector_wall.angle_to(arm_facing_vec) - var test_collision : KinematicCollision3D = move_and_collide(arm_facing_vec*0.5, true) - var test_col_has_angle_for_wall_slide := false - if test_collision: - test_collision.get_collider_shape() - test_col_has_angle_for_wall_slide = (test_collision.get_normal()*-1).angle_to(arm_facing_vec) < PI/1.5 - var horizontal_velocity := velocity * Vector3(1.0, 0.0, 1.0) - if velocity.y < 0 and horizontal_velocity.length() < 5 and ((is_on_wall_only() and angle_facing_and_wall < PI/1.5) or - (test_collision and not is_on_floor() and test_col_has_angle_for_wall_slide)): - return true - return false""" - - func _physics_process(delta: float) -> void: if global_position.y < Main.OCEAN_HEIGHT: # Player fell off the map. @@ -256,6 +220,7 @@ func gain_health(amount : int = 1) -> void: $"CanvasLayer/HeartContainer".update_health(current_health) play_healing_effect() + func play_healing_effect() -> void: var heal_tween := get_tree().create_tween() heal_tween.tween_property(self, "scale", CHAR_SCALE * 1.2, 0.2) @@ -270,6 +235,7 @@ func play_healing_effect() -> void: await get_tree().create_timer(0.3).timeout mesh_instance.set_surface_override_material(0, null) + func apply_knockback(damage_source: Vector3) -> void: is_knocked_back = true var knockback_direction := (global_position - damage_source).normalized() @@ -280,7 +246,8 @@ func apply_knockback(damage_source: Vector3) -> void: await get_tree().create_timer(KNOCKBACK_DURATION).timeout is_knocked_back = false - + + func flash_on_damage(duration: float = 0.1) -> void: # Apply the cached flash material to the body (Surface 0) mesh_instance.set_surface_override_material(0, flash_material) @@ -291,6 +258,7 @@ func flash_on_damage(duration: float = 0.1) -> void: # Remove the material override to restore the original appearance mesh_instance.set_surface_override_material(0, null) + func set_checkpoint(new_checkpoint : Checkpoint) -> void: if checkpoint == null: checkpoint = new_checkpoint @@ -305,12 +273,6 @@ func enter_standard_state() -> void: current_state = $States/StandardState -func enter_wall_slide_state() -> void: - if current_state != $States/WallSlidingState: - current_state = $States/WallSlidingState - current_state.on_state_entered() - - func enter_cannon(cannon : Cannon) -> void: current_state = $States/CannonballState $States/CannonballState.enter_cannon(cannon) diff --git a/player/player.tscn b/player/player.tscn index 919bd6d..87265ee 100644 --- a/player/player.tscn +++ b/player/player.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=39 format=3 uid="uid://cc4idhykjp5f5"] +[gd_scene load_steps=38 format=3 uid="uid://cc4idhykjp5f5"] [ext_resource type="Script" path="res://player/player.gd" id="1"] [ext_resource type="Script" path="res://player/follow_camera.gd" id="2"] @@ -13,7 +13,6 @@ [ext_resource type="PackedScene" uid="uid://c7mt7qik2dlr0" path="res://gui/heart_container.tscn" id="11_brsrk"] [ext_resource type="Script" path="res://player/dash_handler.gd" id="11_g32qe"] [ext_resource type="Script" path="res://player/standard_state.gd" id="12_7ydjt"] -[ext_resource type="Script" path="res://player/wall_sliding_state.gd" id="13_78eq3"] [ext_resource type="Script" path="res://player/cannonball_state.gd" id="13_rnw4n"] [sub_resource type="CapsuleShape3D" id="1"] @@ -277,7 +276,7 @@ wait_time = 0.164 one_shot = true [node name="QueuedJumpTimer" type="Timer" parent="JumpHandler"] -wait_time = 0.148 +wait_time = 0.15 one_shot = true [node name="DashHandler" type="Node" parent="."] @@ -302,9 +301,6 @@ script = ExtResource("12_7ydjt") [node name="CannonballState" type="Node" parent="States"] script = ExtResource("13_rnw4n") -[node name="WallSlidingState" type="Node" parent="States"] -script = ExtResource("13_78eq3") - [connection signal="area_entered" from="FeetArea" to="." method="_on_feet_touching_enemy_head"] [connection signal="timeout" from="Model/Armature/KickArea/AnimationEndTimer" to="." method="stop_kick_animation"] [connection signal="animation_finished" from="AnimationTree" to="." method="_on_animation_tree_animation_finished"] diff --git a/player/standard_state.gd b/player/standard_state.gd index 3fcb1b1..f3d930c 100644 --- a/player/standard_state.gd +++ b/player/standard_state.gd @@ -46,6 +46,16 @@ func state_physics_process(delta: float) -> void: if player.queued_jump != null: player.jump_handler.handle_jump(player.queued_jump) player.queued_jump = null + if not player.last_wall_jump_direction.is_zero_approx(): + player.last_wall_jump_direction = Vector3.ZERO + + if player.is_on_wall_only(): + player.wall_jump_time = player.WALL_JUMP_MARGIN + player.last_collision_direction = player.get_last_slide_collision().get_position().direction_to(player.global_position) + elif player.wall_jump_time > 0.0: + player.wall_jump_time -= delta + elif not player.last_collision_direction.is_zero_approx(): + player.last_collision_direction = Vector3.ZERO player.move_and_slide() player.handle_animations(anim, horizontal_speed) diff --git a/player/wall_sliding_state.gd b/player/wall_sliding_state.gd deleted file mode 100644 index 2bf7b0b..0000000 --- a/player/wall_sliding_state.gd +++ /dev/null @@ -1,70 +0,0 @@ -extends PlayerState - -const WALL_SLIDE_GRAVITY_FACTOR := 0.6 -const WALL_SLIDE_MAX_SPEED := 4.0 -const WALL_JUMP_HORIZONTAL_SPEED := 7 -const WALL_JUMP_VERTICAL_SPEED := 12.5 - - - -func state_physics_process(delta: float) -> void: - player.velocity += gravity * delta * WALL_SLIDE_GRAVITY_FACTOR - if player.velocity.y < -WALL_SLIDE_MAX_SPEED: - player.velocity.y = -WALL_SLIDE_MAX_SPEED - player.move_and_slide() - - var reverse_wall_normal := get_wall_col_normal() * -1 - #This should not happen, but it might if player is moved away from the wall without exiting this state simultaneously. - if reverse_wall_normal == Vector3.ZERO: - player.enter_standard_state() - return - else: - player.adjust_mesh_and_skeleton(reverse_wall_normal) - - var input_mov_dir := get_input_movement_direction() - if input_mov_dir!= Vector3.ZERO and input_mov_dir.angle_to(reverse_wall_normal * -1) < PI/8: - player.enter_standard_state() - player.adjust_mesh_and_skeleton(get_wall_col_normal()) - - if player.is_on_floor(): - player.enter_standard_state() - - -func state_input(event: InputEvent) -> void: - if event.is_action_pressed(&"jump"): - var horizontal_speed := get_wall_col_normal() * WALL_JUMP_HORIZONTAL_SPEED - if horizontal_speed == Vector3.ZERO: - player.enter_standard_state() - return - var vertical_speed := Vector3.UP * WALL_JUMP_VERTICAL_SPEED - player.velocity = horizontal_speed + vertical_speed - player.adjust_mesh_and_skeleton(horizontal_speed.normalized()) - player.enter_standard_state() - - -func on_state_entered() -> void: - player.velocity.y = 0 - player.velocity.z = 0 - player.velocity.x = 0 - - -func get_wall_col_normal() -> Vector3: - var no_vert_vec := Vector3(1.0, 0.0, 1.0) - var normal := player.get_wall_normal().normalized() * no_vert_vec - if normal == Vector3.ZERO: - var armature_trans := (player.get_node("Model/Armature") as Node3D).get_transform() - var arm_facing_vec := (armature_trans.basis.get_rotation_quaternion() * Vector3(0,0,-1)).normalized() - var collision := player.move_and_collide(arm_facing_vec*0.6, true) - if not collision: - return Vector3.ZERO - normal = collision.get_normal().normalized() - return normal - - -func get_input_movement_direction() -> Vector3: - var cam_basis := player._camera.get_global_transform().basis - var movement_vec2 := Input.get_vector(&"move_left", &"move_right", &"move_forward", &"move_back") - var movement_direction := cam_basis * Vector3(movement_vec2.x, 0, movement_vec2.y) - movement_direction.y = 0 - movement_direction = movement_direction.normalized() - return movement_direction diff --git a/tdde20_2024_grupp2 b/tdde20_2024_grupp2 deleted file mode 160000 index d66e717..0000000 --- a/tdde20_2024_grupp2 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit d66e717a606a483199c0e5b7123875700e01c45e -- GitLab