2020-10-03 16:46:14 +00:00
|
|
|
extends ExtendedKinematicBody2D
|
|
|
|
class_name Player
|
|
|
|
|
2020-10-03 17:34:58 +00:00
|
|
|
export(Vector2) var gravity: Vector2 = Vector2(0, 10)
|
2020-10-03 16:46:14 +00:00
|
|
|
var speed: Vector2 = Vector2.ZERO
|
|
|
|
|
2020-10-03 17:34:58 +00:00
|
|
|
export(float) var move_speed: float = 300
|
2020-10-03 22:33:54 +00:00
|
|
|
export(float) var jump_speed: float = 425
|
|
|
|
export(float) var jump_buffer_msec: float = 80
|
2020-10-03 17:34:58 +00:00
|
|
|
export(float) var quick_fall_gravity_multiplier: float = 4
|
2020-10-03 22:33:54 +00:00
|
|
|
export(bool) var stop_jump_on_bonk: bool = true
|
2020-10-03 16:46:14 +00:00
|
|
|
|
2020-10-03 17:34:58 +00:00
|
|
|
var can_jump: bool = false
|
|
|
|
var jump_buffer: int = 0
|
|
|
|
|
|
|
|
var is_quick_falling: bool = false
|
|
|
|
var quick_fall_buffer: int = 0
|
2020-10-03 16:46:14 +00:00
|
|
|
|
2020-10-03 18:27:59 +00:00
|
|
|
func up_normal():
|
|
|
|
return -gravity.normalized()
|
2020-10-03 16:46:14 +00:00
|
|
|
|
|
|
|
func _physics_process(delta):
|
2020-10-03 18:27:59 +00:00
|
|
|
var up_normal = up_normal()
|
2020-10-03 16:46:14 +00:00
|
|
|
var floor_normal = get_floor_normal()
|
|
|
|
|
|
|
|
if is_on_floor():
|
|
|
|
var gravity_speed = speed * up_normal
|
|
|
|
speed -= gravity_speed * floor_normal
|
|
|
|
can_jump = true
|
2020-10-03 17:34:58 +00:00
|
|
|
is_quick_falling = false
|
2020-10-03 16:46:14 +00:00
|
|
|
|
2020-10-03 22:33:54 +00:00
|
|
|
if stop_jump_on_bonk and is_on_ceiling():
|
|
|
|
speed.y = 0
|
|
|
|
|
2020-10-03 17:34:58 +00:00
|
|
|
if is_quick_falling:
|
|
|
|
speed += gravity * quick_fall_gravity_multiplier
|
|
|
|
else:
|
|
|
|
speed += gravity
|
2020-10-03 16:46:14 +00:00
|
|
|
|
|
|
|
var current_time = OS.get_ticks_msec()
|
|
|
|
|
|
|
|
if Input.is_action_just_pressed("plr_up"):
|
|
|
|
jump_buffer = current_time
|
|
|
|
|
2020-10-03 17:34:58 +00:00
|
|
|
if Input.is_action_just_released("plr_up"):
|
|
|
|
quick_fall_buffer = OS.get_ticks_msec()
|
|
|
|
|
2020-10-03 16:46:14 +00:00
|
|
|
if can_jump and current_time - jump_buffer <= jump_buffer_msec:
|
|
|
|
speed += up_normal * jump_speed
|
|
|
|
can_jump = false
|
|
|
|
|
2020-10-03 17:34:58 +00:00
|
|
|
if quick_fall_buffer > jump_buffer:
|
|
|
|
is_quick_falling = true
|
|
|
|
|
2020-10-03 16:46:14 +00:00
|
|
|
var movement = speed
|
|
|
|
|
|
|
|
if is_on_floor():
|
|
|
|
var current_floor = get_floor()
|
2020-10-04 13:01:51 +00:00
|
|
|
if current_floor.has_method("get_relative_cb_speed"):
|
|
|
|
movement += floor_normal.rotated(- PI / 2) * current_floor.get_relative_cb_speed(position)
|
2020-10-03 16:46:14 +00:00
|
|
|
|
|
|
|
if Input.is_action_pressed("plr_left"):
|
|
|
|
movement += Vector2.LEFT * move_speed
|
|
|
|
if Input.is_action_pressed("plr_right"):
|
|
|
|
movement += Vector2.RIGHT * move_speed
|
|
|
|
|
2020-10-03 18:53:58 +00:00
|
|
|
move_and_slide(movement, up_normal)
|