Added better wall jump. (RayCats2D)

This commit is contained in:
Sagi Dayan 2020-09-17 12:06:57 -04:00
parent 5884c3294e
commit b38662cab7
2 changed files with 34 additions and 4 deletions

View file

@ -31,7 +31,7 @@ func _physics_process(delta: float) -> void:
if _alive: if _alive:
var is_jump_canceled: = Input.is_action_just_released("jump") and !_is_wall_jumping and _velocity.y < 0.0 var is_jump_canceled: = Input.is_action_just_released("jump") and !_is_wall_jumping and _velocity.y < 0.0
var direction: = get_direction() var direction: = get_direction()
_is_wall_sliding = _velocity.y >=0 and is_on_wall() and !is_on_floor() _is_wall_sliding = _velocity.y >=0 and _is_next_to_wall() and !is_on_floor()
_velocity = calculate_move_velocity(direction,is_jump_canceled, delta) _velocity = calculate_move_velocity(direction,is_jump_canceled, delta)
_velocity = move_and_slide(_velocity, Vector2.UP) _velocity = move_and_slide(_velocity, Vector2.UP)
update_sprite(direction) update_sprite(direction)
@ -48,7 +48,7 @@ func get_direction() -> Vector2:
var left_strength = min(Input.get_action_strength("direction_left") * 2, 1) var left_strength = min(Input.get_action_strength("direction_left") * 2, 1)
return Vector2( return Vector2(
right_strength - left_strength, right_strength - left_strength,
-1.0 if Input.is_action_just_pressed("jump") and (is_on_floor() or is_on_wall()) else 1.0 -1.0 if Input.is_action_just_pressed("jump") and (is_on_floor() or _is_wall_sliding) else 1.0
) )
func calculate_move_velocity(direction:Vector2, is_jump_canceled:bool, delta:float)->Vector2: func calculate_move_velocity(direction:Vector2, is_jump_canceled:bool, delta:float)->Vector2:
@ -64,8 +64,9 @@ func calculate_move_velocity(direction:Vector2, is_jump_canceled:bool, delta:fl
_in_air = true _in_air = true
if _is_wall_sliding: if _is_wall_sliding:
# wall jump # wall jump
var walljump__x_direction = 1 if _is_next_to_wall_right() else -1
_is_wall_jumping = true _is_wall_jumping = true
var desired = -(run_speed * wall_jump_speed_factor.x * direction.x) var desired = -(run_speed * wall_jump_speed_factor.x * walljump__x_direction)
output.x = desired output.x = desired
output.y = jump_power * wall_jump_speed_factor.y * direction.y output.y = jump_power * wall_jump_speed_factor.y * direction.y
pass pass
@ -109,6 +110,20 @@ func _respawn():
func _revive(): func _revive():
_alive = true _alive = true
func _is_next_to_wall():
return _is_next_to_wall_right() || _is_next_to_wall_left()
func _is_next_to_wall_right():
if $RayCastWallRight.is_colliding():
var colider = $RayCastWallRight.get_collider();
return colider.name == "SolidsTileMap"
return false
func _is_next_to_wall_left():
if $RayCastWallLeft.is_colliding():
var colider = $RayCastWallLeft.get_collider();
return colider.name == "SolidsTileMap"
return false
func update_sprite(direction:Vector2)->void: func update_sprite(direction:Vector2)->void:
var air_animation = "jump" if _velocity.y <= 0 else "fall" var air_animation = "jump" if _velocity.y <= 0 else "fall"

View file

@ -269,6 +269,9 @@ tracks/1/keys = {
[node name="Player" type="KinematicBody2D"] [node name="Player" type="KinematicBody2D"]
collision_mask = 30 collision_mask = 30
script = ExtResource( 2 ) script = ExtResource( 2 )
__meta__ = {
"_edit_vertical_guides_": [ 5.41509 ]
}
[node name="CollisionShape2D" type="CollisionShape2D" parent="."] [node name="CollisionShape2D" type="CollisionShape2D" parent="."]
position = Vector2( 0, -7 ) position = Vector2( 0, -7 )
@ -292,7 +295,7 @@ position = Vector2( 0, -8 )
texture = ExtResource( 8 ) texture = ExtResource( 8 )
vframes = 13 vframes = 13
hframes = 8 hframes = 8
frame = 48 frame = 32
[node name="AnimationPlayer" type="AnimationPlayer" parent="."] [node name="AnimationPlayer" type="AnimationPlayer" parent="."]
autoplay = "idle" autoplay = "idle"
@ -323,4 +326,16 @@ one_shot = true
[node name="DashParticlesLeft" parent="." instance=ExtResource( 3 )] [node name="DashParticlesLeft" parent="." instance=ExtResource( 3 )]
[node name="DashParticlesRight" parent="." instance=ExtResource( 1 )] [node name="DashParticlesRight" parent="." instance=ExtResource( 1 )]
[node name="RayCastWallRight" type="RayCast2D" parent="."]
position = Vector2( 0, -8.09363 )
enabled = true
cast_to = Vector2( 5.2, 0 )
collision_mask = 8
[node name="RayCastWallLeft" type="RayCast2D" parent="."]
position = Vector2( 0, -8.04107 )
enabled = true
cast_to = Vector2( -5.2, 0 )
collision_mask = 8
[connection signal="timeout" from="DashTimeout" to="." method="_on_DashTimeout_timeout"] [connection signal="timeout" from="DashTimeout" to="." method="_on_DashTimeout_timeout"]