2020-09-14 15:21:59 +00:00
|
|
|
extends KinematicBody2D
|
|
|
|
class_name Player
|
|
|
|
|
2020-09-27 03:18:34 +00:00
|
|
|
|
|
|
|
|
2020-09-17 17:44:48 +00:00
|
|
|
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
|
2020-10-04 02:50:07 +00:00
|
|
|
export var wall_jump_speed_factor := Vector2(1 ,.8)
|
2020-09-17 17:44:48 +00:00
|
|
|
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-10-04 02:50:07 +00:00
|
|
|
var LightBeam = load("res://src/Actors/LightBeam.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
|
2020-09-15 23:17:10 +00:00
|
|
|
var _is_wall_jumping := false
|
2020-10-04 02:50:07 +00:00
|
|
|
var _alive := false
|
|
|
|
var _can_dash := true
|
|
|
|
var _is_jump_canceled := false
|
|
|
|
var _direction := Vector2.ZERO
|
|
|
|
|
|
|
|
enum States {
|
|
|
|
IDLE,
|
|
|
|
IN_AIR,
|
|
|
|
DASHING,
|
|
|
|
WALL_SLIDING,
|
|
|
|
ATTACKING
|
|
|
|
}
|
|
|
|
|
|
|
|
var _state = States.IDLE
|
2020-09-27 03:18:34 +00:00
|
|
|
|
|
|
|
enum Direction{
|
|
|
|
RIGHT = 1,
|
|
|
|
LEFT = -1
|
|
|
|
}
|
|
|
|
|
|
|
|
export var abilities:= {
|
|
|
|
"dash": false,
|
|
|
|
"wall_jump": false,
|
2020-10-04 02:50:07 +00:00
|
|
|
"attack": false,
|
|
|
|
"light_beam": false
|
2020-09-27 03:18:34 +00:00
|
|
|
}
|
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-10-04 02:50:07 +00:00
|
|
|
_update_state()
|
|
|
|
_input_check()
|
|
|
|
_velocity = calculate_move_velocity(_direction,_is_jump_canceled, delta)
|
2020-09-14 15:21:59 +00:00
|
|
|
_velocity = move_and_slide(_velocity, Vector2.UP)
|
2020-10-04 02:50:07 +00:00
|
|
|
update_sprite(_direction)
|
|
|
|
|
|
|
|
|
|
|
|
func _input_check() -> void:
|
|
|
|
if GameState.get_state() == GameState.States.GAME:
|
|
|
|
_is_jump_canceled = Input.is_action_just_released("jump") and !_is_wall_jumping and _velocity.y < 0.0;
|
|
|
|
_get_direction()
|
|
|
|
_check_dash()
|
|
|
|
if Input.is_action_just_pressed("attack") and abilities.attack:
|
|
|
|
_attack()
|
|
|
|
else:
|
|
|
|
_direction = Vector2.ZERO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _update_state(new_state:int = -1):
|
|
|
|
var auto_update = new_state == -1;
|
|
|
|
if new_state == -1:
|
|
|
|
new_state = _state
|
|
|
|
if _state == States.DASHING: return
|
|
|
|
if _state == States.ATTACKING: return
|
|
|
|
if _state == States.IDLE:
|
|
|
|
if _velocity.y > 0 and not is_on_floor():
|
|
|
|
# Falling from ground
|
|
|
|
new_state = States.IN_AIR
|
|
|
|
elif _state == States.IN_AIR:
|
|
|
|
if is_on_floor():
|
|
|
|
_on_landed()
|
|
|
|
new_state = States.IDLE
|
|
|
|
elif _velocity.y >=0 and is_on_wall():
|
|
|
|
if _state != States.WALL_SLIDING:
|
|
|
|
_velocity.y=0
|
|
|
|
new_state = States.WALL_SLIDING
|
|
|
|
elif _state == States.WALL_SLIDING:
|
|
|
|
if is_on_floor() or not is_on_wall():
|
|
|
|
new_state = States.IDLE
|
|
|
|
else: new_state = States.IDLE
|
|
|
|
_transition_state(_state, new_state, auto_update)
|
|
|
|
|
|
|
|
func _transition_state(old_state, new_state, auto_update):
|
|
|
|
if new_state != old_state:
|
|
|
|
print("[Player]: State Update: [%s] -> [%s] | Automatic: %s" % [old_state, new_state, auto_update])
|
|
|
|
_state = new_state
|
|
|
|
if old_state == States.IDLE:
|
|
|
|
# idle -> attak = ground attack - stop _velocity.x
|
|
|
|
if new_state == States.ATTACKING:
|
|
|
|
_direction.x = 0;
|
|
|
|
_velocity.x = 0;
|
|
|
|
if old_state == States.DASHING:
|
|
|
|
if new_state == States.IDLE:
|
|
|
|
_can_dash = true
|
|
|
|
if old_state == States.WALL_SLIDING:
|
|
|
|
if new_state == States.IDLE:
|
|
|
|
_on_landed()
|
|
|
|
|
|
|
|
func _on_landed():
|
|
|
|
_can_dash = true
|
|
|
|
var dust = LandingDust.instance()
|
|
|
|
dust.position = position
|
|
|
|
get_parent().add_child(dust)
|
|
|
|
emit_signal("landed", position)
|
|
|
|
|
|
|
|
func _on_jump(wall_jump:bool = false):
|
|
|
|
var dust:Node = JumpDust.instance()
|
|
|
|
dust.position = position;
|
|
|
|
get_parent().add_child(dust)
|
|
|
|
if wall_jump:
|
|
|
|
dust.position.x += 2 * -$Sprite.scale.x
|
|
|
|
dust.get_node("Sprite").rotate(deg2rad(90) * -$Sprite.scale.x)
|
|
|
|
emit_signal("jumping", position)
|
|
|
|
|
|
|
|
func _get_direction() -> void:
|
|
|
|
var right_strength = round(Input.get_action_strength("direction_right"))
|
|
|
|
var left_strength = round(Input.get_action_strength("direction_left"))
|
|
|
|
var vertical_movement = right_strength - left_strength if not _state == States.DASHING else 0
|
|
|
|
var is_in_jumpable_state = _state == States.IDLE or _state == States.WALL_SLIDING
|
|
|
|
_direction = Vector2(
|
|
|
|
vertical_movement,
|
|
|
|
-1.0 if Input.is_action_just_pressed("jump") and is_in_jumpable_state else 1.0
|
2020-09-14 15:21:59 +00:00
|
|
|
)
|
2020-10-04 02:50:07 +00:00
|
|
|
func _check_dash():
|
|
|
|
if Input.is_action_just_pressed("dash") and _can_dash and abilities.dash:
|
|
|
|
var dash_velocity := Vector2($Sprite.scale.x * dash_thrust,0)
|
|
|
|
_can_dash = false
|
|
|
|
# Wall dash first
|
|
|
|
if _state == States.WALL_SLIDING:
|
|
|
|
dash_velocity.x = $Sprite.scale.x * dash_thrust * -1
|
|
|
|
|
|
|
|
if dash_velocity.x < 0 :
|
|
|
|
$DashParticlesLeft.emitting = true;
|
|
|
|
else:
|
|
|
|
$DashParticlesRight.emitting = true
|
|
|
|
_velocity = dash_velocity
|
|
|
|
$DashTimeout.start()
|
|
|
|
_update_state(States.DASHING) # turn off gravity while dashing
|
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
|
2020-10-04 02:50:07 +00:00
|
|
|
var current_gravity = gravity * wall_slide_friction if _state == States.WALL_SLIDING else gravity
|
2020-09-15 23:17:10 +00:00
|
|
|
|
2020-10-04 02:50:07 +00:00
|
|
|
output.x = lerp(_velocity.x, run_speed * direction.x, .1 if _state == States.IN_AIR else .5)
|
2020-09-15 23:17:10 +00:00
|
|
|
|
|
|
|
output.y += current_gravity * delta
|
|
|
|
|
2020-10-04 02:50:07 +00:00
|
|
|
if _state == States.DASHING:
|
|
|
|
return _velocity
|
|
|
|
|
2020-09-15 23:17:10 +00:00
|
|
|
if direction.y == -1.0: # we are jumping
|
2020-10-04 02:50:07 +00:00
|
|
|
var wall_jump := false
|
|
|
|
if _state == States.WALL_SLIDING and abilities.wall_jump:
|
2020-09-15 23:17:10 +00:00
|
|
|
# wall jump
|
2020-10-04 02:50:07 +00:00
|
|
|
wall_jump = true
|
2020-09-27 03:18:34 +00:00
|
|
|
var walljump__x_direction = -1 * direction.x
|
|
|
|
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
|
2020-10-04 02:50:07 +00:00
|
|
|
_update_state(States.IN_AIR)
|
|
|
|
_on_jump(wall_jump)
|
|
|
|
else:
|
2020-09-15 23:17:10 +00:00
|
|
|
if is_jump_canceled:
|
|
|
|
output.y = 0
|
2020-09-17 17:44:48 +00:00
|
|
|
if output.y < max_gravity * -1:
|
|
|
|
output.y = max_gravity
|
2020-10-04 02:50:07 +00:00
|
|
|
|
2020-09-15 23:17:10 +00:00
|
|
|
|
2020-10-04 02:50:07 +00:00
|
|
|
if _state == States.WALL_SLIDING and output.y > max_wall_slide_gravity:
|
2020-09-17 17:44:48 +00:00
|
|
|
output.y = max_wall_slide_gravity
|
|
|
|
|
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"
|
2020-10-04 02:50:07 +00:00
|
|
|
if _state == States.ATTACKING: return
|
2020-09-27 03:18:34 +00:00
|
|
|
var attack = Input.is_action_just_pressed("attack") and abilities.attack
|
2020-10-04 02:50:07 +00:00
|
|
|
if _velocity.x > .5 and not _state == States.WALL_SLIDING:
|
2020-09-27 03:18:34 +00:00
|
|
|
$Sprite.scale = Vector2(1,1)
|
2020-09-14 15:21:59 +00:00
|
|
|
$AnimationPlayer.play("run" if is_on_floor() else air_animation)
|
2020-10-04 02:50:07 +00:00
|
|
|
elif _velocity.x < -.5 and not _state == States.WALL_SLIDING:
|
2020-09-27 03:18:34 +00:00
|
|
|
$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-10-04 02:50:07 +00:00
|
|
|
if not _state == States.IN_AIR: $AnimationPlayer.play("idle")
|
2020-09-15 23:17:10 +00:00
|
|
|
|
2020-10-04 02:50:07 +00:00
|
|
|
if _state == States.WALL_SLIDING:
|
2020-09-15 23:17:10 +00:00
|
|
|
$AnimationPlayer.play("wall_slide")
|
|
|
|
return
|
2020-10-04 02:50:07 +00:00
|
|
|
if _state == States.IN_AIR:
|
2020-09-14 15:21:59 +00:00
|
|
|
$AnimationPlayer.play(air_animation)
|
2020-09-15 23:17:10 +00:00
|
|
|
return
|
|
|
|
|
2020-10-04 02:50:07 +00:00
|
|
|
if _state == States.DASHING:
|
2020-09-15 23:17:10 +00:00
|
|
|
$AnimationPlayer.play("jump")
|
2020-09-18 19:48:52 +00:00
|
|
|
return
|
2020-09-27 03:18:34 +00:00
|
|
|
|
2020-10-04 02:50:07 +00:00
|
|
|
func _attack():
|
|
|
|
if _state == States.DASHING:
|
|
|
|
var beam = LightBeam.instance()
|
|
|
|
beam.direction = Vector2($Sprite.scale.x, 0)
|
|
|
|
beam.position = $Sprite/LightBeamInitPoint.get_global_transform().get_origin()
|
|
|
|
get_parent().add_child(beam)
|
|
|
|
$DashTimeout.stop()
|
|
|
|
_on_DashTimeout_timeout(false)
|
|
|
|
_update_state(States.ATTACKING)
|
|
|
|
AudioManager.play_sfx(AudioManager.Sfx.PLAYER_ATTACK)
|
|
|
|
var attack_animation = "attack_right" if $Sprite.scale.x > 0 else "attack_left"
|
|
|
|
$AnimationPlayer.play(attack_animation)
|
|
|
|
$AnimationPlayer.playback_speed = 2.5
|
|
|
|
yield($AnimationPlayer, "animation_finished")
|
|
|
|
$AnimationPlayer.seek(0)
|
|
|
|
$AnimationPlayer.playback_speed = 1
|
|
|
|
_update_state(States.IDLE)
|
2020-09-14 15:21:59 +00:00
|
|
|
|
|
|
|
func _on_die_animation_done():
|
|
|
|
emit_signal("died")
|
|
|
|
_respawn()
|
|
|
|
|
|
|
|
func die():
|
|
|
|
_alive = false
|
2020-09-29 01:36:57 +00:00
|
|
|
GameState.player_died()
|
2020-09-14 15:21:59 +00:00
|
|
|
$AnimationPlayer.play("die")
|
2020-09-27 03:18:34 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-10-04 02:50:07 +00:00
|
|
|
func _on_DashTimeout_timeout(timed_out=true) -> void:
|
|
|
|
$AnimationPlayer.playback_speed = 1
|
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;
|
2020-10-04 02:50:07 +00:00
|
|
|
if timed_out: _update_state(States.IN_AIR)
|
2020-09-15 23:17:10 +00:00
|
|
|
pass # Replace with function body.
|
2020-09-27 03:18:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
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.
|