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

214 lines
5.8 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;
var _is_attaking := false;
enum Direction{
RIGHT = 1,
LEFT = -1
}
export var abilities:= {
"dash": false,
"wall_jump": false,
"attack": false
}
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_on_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:
var right_strength = min(Input.get_action_strength("direction_right") , 1)
var left_strength = min(Input.get_action_strength("direction_left"), 1)
2020-09-14 15:21:59 +00:00
return Vector2(
right_strength - left_strength if _in_air or not _is_attaking else 0,
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
var walljump__x_direction = -1 * direction.x
2020-09-15 23:17:10 +00:00
_is_wall_jumping = true
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(
$Sprite.scale.x * dash_thrust,
2020-09-15 23:17:10 +00:00
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 = $Sprite.scale.x * dash_thrust * -1
2020-09-18 19:48:52 +00:00
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
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"
if _is_attaking: return
var attack = Input.is_action_just_pressed("attack") and abilities.attack
2020-09-15 23:17:10 +00:00
if _velocity.x > .5 and not _is_wall_sliding:
$Sprite.scale = Vector2(1,1)
2020-09-14 15:21:59 +00:00
$AnimationPlayer.play("run" if is_on_floor() else air_animation)
2020-09-15 23:17:10 +00:00
elif _velocity.x < -.5 and not _is_wall_sliding:
$Sprite.scale = Vector2(-1,1)
2020-09-14 15:21:59 +00:00
$AnimationPlayer.play("run" if is_on_floor() else air_animation)
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 attack:
_is_attaking = true
var attack_animation = "attack_right" if $Sprite.scale.x > 0 else "attack_left"
$AnimationPlayer.play(attack_animation)
$AnimationPlayer.playback_speed = 2.5
$AnimationPlayer.seek(0)
yield($AnimationPlayer, "animation_finished")
$AnimationPlayer.playback_speed = 1
_is_attaking = false
return
2020-09-15 23:17:10 +00:00
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")
func setAbility(ability:String, enabled:bool=false):
if ability == 'dash':
abilities.dash = enabled
elif ability == 'wall_jump':
abilities.wall_jump = enabled
elif ability == 'attack':
abilities.attack = enabled
else:
pass
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.
func _on_SordRange_body_entered(body: Node) -> void:
if body.is_in_group("Enemies"):
body.take_damage($Sprite.scale.x)
pass # Replace with function body.