the-crypt/script/player.gd

83 lines
1.9 KiB
GDScript3
Raw Normal View History

2020-07-17 09:51:46 -05:00
extends KinematicBody2D
2020-07-17 00:27:22 -05:00
2020-07-17 11:03:22 -05:00
signal grounded_updated(is_grounded)
var velocity = Vector2()
var input_direction: int = 0
var speed = 10 * 16
var gravity
var max_jump_velocity
var min_jump_velocity
var walljump_velocity
var max_jump_height = 2.75 * 16
var min_jump_height = 0.6 * 16
var walljump_height = 2.25 * 16
var jump_duration = 0.35
var is_grounded
var touching_wall = 0
onready var Spr: Sprite = $Sprite
onready var Occluder: LightOccluder2D = $Sprite/LightOccluder2D
onready var Anim: AnimationPlayer = $AnimationPlayer
onready var CoyoteTimer: Timer = $CoyoteTimer
onready var StateMachine: Node = $PlayerStateMachine
func _ready():
gravity = 2 * max_jump_height / pow(jump_duration, 2)
max_jump_velocity = -sqrt(2 * gravity * max_jump_height)
min_jump_velocity = -sqrt(2 * gravity * min_jump_height)
walljump_velocity = -sqrt(2 * gravity * walljump_height)
2020-07-17 00:27:22 -05:00
2020-07-17 09:51:46 -05:00
func _physics_process(delta):
2020-07-17 11:03:22 -05:00
emit_signal("grounded_updated", is_on_floor())
func handle_move_input():
input_direction = int(Input.is_action_pressed("right")) - int(Input.is_action_pressed("left"))
velocity.x = lerp(velocity.x, speed * input_direction, get_movement_weight())
if input_direction > 0:
Spr.flip_h = false
Occluder.scale.x = 1
if input_direction < 0:
Spr.flip_h = true
Occluder.scale.x = -1
func jump():
velocity.y = max_jump_velocity
CoyoteTimer.stop()
func apply_gravity(delta, modifier = 1):
velocity.y += gravity * delta * modifier
func apply_movement():
if velocity.x > -1 and velocity.x < 1:
velocity.x = 0
var was_on_floor = is_on_floor()
velocity = move_and_slide(velocity, Vector2.UP)
if !is_on_floor() and was_on_floor and velocity.y >= 0:
CoyoteTimer.start()
var was_grounded = is_grounded
is_grounded = is_on_floor()
if was_grounded == null or is_grounded != was_grounded:
emit_signal("grounded_updated", is_grounded)
func get_movement_weight():
2020-07-17 09:51:46 -05:00
if is_on_floor():
2020-07-17 11:03:22 -05:00
return 0.2
else:
return 0.07
2020-07-17 00:27:22 -05:00
2020-07-17 09:51:46 -05:00