platformer-game-test/src/Actors/Player.gd

183 lines
5.2 KiB
GDScript3
Raw Normal View History

2020-09-14 15:21:59 +00:00
extends KinematicBody2D
class_name Player
export var run_speed := 100.0
export var jump_power := 180.0
export var gravity: = 500.0
export var max_gravity:= 450.0
2020-09-14 15:21:59 +00:00
export var respawn_position:=Vector2.ZERO
2020-09-15 23:17:10 +00:00
export var wall_slide_friction:=.2
export var wall_jump_speed_factor := Vector2(2.5 ,.85)
export var dash_thrust = 500.0
export var max_wall_slide_gravity := 100.0
var LandingDust = load("res://src/Actors/LandingDust.tscn")
var JumpDust = load("res://src/Actors/JumpDust.tscn")
2020-09-14 15:21:59 +00:00
signal landed
signal jumping
signal died
var _velocity: Vector2 = Vector2.ZERO
var _landing_position:Vector2 = Vector2.ZERO
var _in_air = false;
2020-09-15 23:17:10 +00:00
var _is_wall_jumping := false
2020-09-14 15:21:59 +00:00
var _alive := false;
2020-09-15 23:17:10 +00:00
var _is_wall_sliding := false;
var _is_dashing := false;
var _can_dash := true;
2020-09-14 15:21:59 +00:00
func _ready() -> void:
$AnimationPlayer.play("fade_in")
respawn_position = position
func _physics_process(delta: float) -> void:
if _alive:
2020-09-15 23:17:10 +00:00
var is_jump_canceled: = Input.is_action_just_released("jump") and !_is_wall_jumping and _velocity.y < 0.0
2020-09-14 15:21:59 +00:00
var direction: = get_direction()
var prev_wall_slide_state = _is_wall_sliding
_is_wall_sliding = _velocity.y >=0 and _is_next_to_wall() and _in_air
2020-09-15 23:17:10 +00:00
_velocity = calculate_move_velocity(direction,is_jump_canceled, delta)
if _is_wall_sliding and not prev_wall_slide_state:
_velocity.y=0
2020-09-14 15:21:59 +00:00
_velocity = move_and_slide(_velocity, Vector2.UP)
update_sprite(direction)
2020-09-15 23:17:10 +00:00
if is_on_floor() and !_is_dashing: _can_dash = true;
if is_on_floor() and _in_air and !_is_dashing:
var dust = LandingDust.instance()
dust.position = position
get_parent().add_child(dust)
2020-09-14 15:21:59 +00:00
_in_air = false
2020-09-15 23:17:10 +00:00
_is_wall_jumping = false
2020-09-14 15:21:59 +00:00
emit_signal("landed", position)
2020-09-15 23:17:10 +00:00
if _is_wall_sliding:
_is_wall_jumping = false
2020-09-14 15:21:59 +00:00
func get_direction() -> Vector2:
2020-09-16 00:16:18 +00:00
var right_strength = min(Input.get_action_strength("direction_right") * 2, 1)
var left_strength = min(Input.get_action_strength("direction_left") * 2, 1)
2020-09-14 15:21:59 +00:00
return Vector2(
2020-09-16 00:16:18 +00:00
right_strength - left_strength,
2020-09-17 16:06:57 +00:00
-1.0 if Input.is_action_just_pressed("jump") and (is_on_floor() or _is_wall_sliding) else 1.0
2020-09-14 15:21:59 +00:00
)
2020-09-15 23:17:10 +00:00
func calculate_move_velocity(direction:Vector2, is_jump_canceled:bool, delta:float)->Vector2:
var output: = _velocity
var current_gravity = gravity * wall_slide_friction if _is_wall_sliding else gravity
output.x = lerp(_velocity.x, run_speed * direction.x, .09 if _in_air else .5)
output.y += current_gravity * delta
if direction.y == -1.0: # we are jumping
2020-09-14 15:21:59 +00:00
_in_air = true
2020-09-15 23:17:10 +00:00
if _is_wall_sliding:
# wall jump
2020-09-17 16:06:57 +00:00
var walljump__x_direction = 1 if _is_next_to_wall_right() else -1
2020-09-15 23:17:10 +00:00
_is_wall_jumping = true
2020-09-17 16:06:57 +00:00
var desired = -(run_speed * wall_jump_speed_factor.x * walljump__x_direction)
2020-09-15 23:17:10 +00:00
output.x = desired
output.y = jump_power * wall_jump_speed_factor.y * direction.y
else:
#jump
output.y = jump_power * direction.y
var dust = JumpDust.instance()
dust.position = position;
get_parent().add_child(dust)
2020-09-14 15:21:59 +00:00
emit_signal("jumping", position)
2020-09-15 23:17:10 +00:00
if not _is_dashing:
if is_jump_canceled:
output.y = 0
if output.y < max_gravity * -1:
output.y = max_gravity
2020-09-15 23:17:10 +00:00
if output.y > 0 and !is_on_floor():
_in_air = true;
if _is_dashing:
return _velocity
if _is_wall_sliding and output.y > max_wall_slide_gravity:
output.y = max_wall_slide_gravity
2020-09-15 23:17:10 +00:00
var dash_velocity := Vector2(
(-1 if $Sprite.flip_h else 1) * dash_thrust,
0
)
2020-09-18 19:48:52 +00:00
if Input.is_action_just_pressed("dash") and _can_dash:
2020-09-15 23:17:10 +00:00
_can_dash = false
_is_dashing = true # turn off gravity while dashing
2020-09-18 19:48:52 +00:00
# Wall dash first
if _is_wall_sliding:
dash_velocity.x = (-1 if _is_next_to_wall_right() else 1) * dash_thrust
2020-09-17 15:37:03 +00:00
if dash_velocity.x < 0 :
$DashParticlesLeft.emitting = true;
else:
$DashParticlesRight.emitting = true
2020-09-18 19:48:52 +00:00
output = dash_velocity
2020-09-15 23:17:10 +00:00
$DashTimeout.start()
2020-09-14 15:21:59 +00:00
return output;
func _respawn():
position = respawn_position
_velocity = Vector2.ZERO
$AnimationPlayer.play("fade_in")
func _revive():
_alive = true
2020-09-17 16:06:57 +00:00
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
2020-09-14 15:21:59 +00:00
2020-09-18 19:48:52 +00:00
func update_sprite(_direction:Vector2) -> void:
2020-09-14 15:21:59 +00:00
var air_animation = "jump" if _velocity.y <= 0 else "fall"
2020-09-15 23:17:10 +00:00
if _velocity.x > .5 and not _is_wall_sliding:
2020-09-14 15:21:59 +00:00
$AnimationPlayer.play("run" if is_on_floor() else air_animation)
$Sprite.flip_h = false
2020-09-15 23:17:10 +00:00
elif _velocity.x < -.5 and not _is_wall_sliding:
2020-09-14 15:21:59 +00:00
$AnimationPlayer.play("run" if is_on_floor() else air_animation)
$Sprite.flip_h = true
else:
2020-09-15 23:17:10 +00:00
if not _in_air: $AnimationPlayer.play("idle")
if _is_wall_sliding:
$AnimationPlayer.play("wall_slide")
return
if _in_air:
2020-09-14 15:21:59 +00:00
$AnimationPlayer.play(air_animation)
2020-09-15 23:17:10 +00:00
return
if _is_dashing:
$AnimationPlayer.play("jump")
2020-09-18 19:48:52 +00:00
return
2020-09-14 15:21:59 +00:00
func _on_die_animation_done():
emit_signal("died")
_respawn()
func die():
_alive = false
$AnimationPlayer.play("die")
2020-09-15 23:17:10 +00:00
func _on_DashTimeout_timeout() -> void:
2020-09-17 15:37:03 +00:00
$DashParticlesLeft.emitting = false
$DashParticlesRight.emitting = false
2020-09-15 23:17:10 +00:00
_velocity.x = run_speed * _velocity.normalized().x;
_is_dashing = false
pass # Replace with function body.