|
|
|
@@ -0,0 +1,147 @@
|
|
|
|
|
class_name CharacterController extends Node
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@export_category("Movement Setting")
|
|
|
|
|
@export var body: CharacterBody2D
|
|
|
|
|
@export var move_res:MovementResouce = MovementResouce.new()
|
|
|
|
|
# @export var SPEED = 500.0
|
|
|
|
|
# @export var JUMP_VELOCITY = -900.0
|
|
|
|
|
# @export var gravity = 1900
|
|
|
|
|
var gravity_multiplier = 1
|
|
|
|
|
|
|
|
|
|
@export_category("Buffer Settings")
|
|
|
|
|
var buffer_timer:Timer
|
|
|
|
|
@export var buffer_delay:float = 0.2
|
|
|
|
|
|
|
|
|
|
@export_category("Cayote Settings")
|
|
|
|
|
var cayote_timer:Timer
|
|
|
|
|
@export var cayote_delay:float = 0.1
|
|
|
|
|
|
|
|
|
|
@export_category("Debug")
|
|
|
|
|
@export var label:Label
|
|
|
|
|
|
|
|
|
|
var jump_loaded:bool = false
|
|
|
|
|
|
|
|
|
|
enum FloorState{
|
|
|
|
|
ON_FLOOR,
|
|
|
|
|
JUST_LEFT_FLOOR,
|
|
|
|
|
IN_AIR,
|
|
|
|
|
}
|
|
|
|
|
var current_floor_state:FloorState = FloorState.IN_AIR
|
|
|
|
|
# var has_released_jump:bool = true
|
|
|
|
|
|
|
|
|
|
func _ready() -> void:
|
|
|
|
|
create_timers()
|
|
|
|
|
if body == null:
|
|
|
|
|
if get_parent() is CharacterBody2D:
|
|
|
|
|
body = get_parent()
|
|
|
|
|
|
|
|
|
|
func set_movment_resource(movement_resource:MovementResouce):
|
|
|
|
|
move_res = movement_resource
|
|
|
|
|
|
|
|
|
|
func create_timers():
|
|
|
|
|
cayote_timer = Timer.new()
|
|
|
|
|
cayote_timer.wait_time = cayote_delay
|
|
|
|
|
cayote_timer.one_shot = true
|
|
|
|
|
add_child(cayote_timer)
|
|
|
|
|
|
|
|
|
|
buffer_timer = Timer.new()
|
|
|
|
|
buffer_timer.wait_time = buffer_delay
|
|
|
|
|
buffer_timer.one_shot = true
|
|
|
|
|
add_child(buffer_timer)
|
|
|
|
|
|
|
|
|
|
func _process(_delta: float) -> void:
|
|
|
|
|
if label:
|
|
|
|
|
label.text = "Buff TimeLeft: {buff_timeleft}\nCayote TimeLeft: {cayote_timeleft} \nFloor State: {floor_state}".format({
|
|
|
|
|
"buff_timeleft": "%0.2f" % buffer_timer.time_left,
|
|
|
|
|
"cayote_timeleft": "%0.2f" % cayote_timer.time_left,
|
|
|
|
|
"floor_state": FloorState.keys()[current_floor_state]
|
|
|
|
|
})
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
func handle_gravity(delta: float):
|
|
|
|
|
match current_floor_state:
|
|
|
|
|
FloorState.ON_FLOOR:
|
|
|
|
|
if not body.is_on_floor():
|
|
|
|
|
current_floor_state = FloorState.JUST_LEFT_FLOOR
|
|
|
|
|
cayote_timer.start()
|
|
|
|
|
FloorState.JUST_LEFT_FLOOR:
|
|
|
|
|
if cayote_timer.is_stopped():
|
|
|
|
|
current_floor_state = FloorState.IN_AIR
|
|
|
|
|
body.velocity.y += move_res.gravity * delta * gravity_multiplier
|
|
|
|
|
FloorState.IN_AIR:
|
|
|
|
|
if not cayote_timer.is_stopped():
|
|
|
|
|
cayote_timer.stop()
|
|
|
|
|
body.velocity.y += move_res.gravity * delta * gravity_multiplier
|
|
|
|
|
if body.is_on_floor():
|
|
|
|
|
current_floor_state = FloorState.ON_FLOOR
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
# if left_floor_flag:
|
|
|
|
|
# else:
|
|
|
|
|
# left_floor_flag = true
|
|
|
|
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
|
|
|
# Add the gravity.
|
|
|
|
|
|
|
|
|
|
handle_gravity(delta)
|
|
|
|
|
handle_jump()
|
|
|
|
|
|
|
|
|
|
# Handle jump.
|
|
|
|
|
# if Input.is_action_pressed("up") and has_released_jump:
|
|
|
|
|
# if Input.is_action_pressed("up"):
|
|
|
|
|
# gravity_multiplier = 1
|
|
|
|
|
# if body.is_on_floor():
|
|
|
|
|
# jump()
|
|
|
|
|
# else:
|
|
|
|
|
# if buffer_timer.is_stopped():
|
|
|
|
|
# buffer_timer.start()
|
|
|
|
|
|
|
|
|
|
# if Input.is_action_just_released("up"):
|
|
|
|
|
# has_released_jump = true
|
|
|
|
|
# gravity_multiplier = 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var direction := Input.get_axis("left", "right")
|
|
|
|
|
if direction:
|
|
|
|
|
body.velocity.x = direction * move_res.speed
|
|
|
|
|
else:
|
|
|
|
|
body.velocity.x = move_toward(body.velocity.x, 0, move_res.speed)
|
|
|
|
|
|
|
|
|
|
# if jump_loaded:
|
|
|
|
|
# jump_loaded = false
|
|
|
|
|
# has_released_jump = false
|
|
|
|
|
# body.velocity.y = JUMP_VELOCITY
|
|
|
|
|
# move_and_slide()
|
|
|
|
|
|
|
|
|
|
# func jump():
|
|
|
|
|
# jump_loaded = true
|
|
|
|
|
|
|
|
|
|
# func handle_buffer_jump():
|
|
|
|
|
# if not buffer_timer.is_stopped() and Input.is_action_pressed("up"):
|
|
|
|
|
# jump()
|
|
|
|
|
# buffer_timer.stop()
|
|
|
|
|
|
|
|
|
|
func handle_jump():
|
|
|
|
|
if Input.is_action_just_pressed("up"):
|
|
|
|
|
# Body is on floor
|
|
|
|
|
if current_floor_state != FloorState.IN_AIR :
|
|
|
|
|
jump_loaded = true
|
|
|
|
|
else:
|
|
|
|
|
buffer_timer.start()
|
|
|
|
|
|
|
|
|
|
if not buffer_timer.is_stopped() and body.is_on_floor():
|
|
|
|
|
buffer_timer.stop()
|
|
|
|
|
jump_loaded = true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if jump_loaded:
|
|
|
|
|
body.velocity.y = move_res.jump_velocity
|
|
|
|
|
current_floor_state = FloorState.IN_AIR
|
|
|
|
|
jump_loaded = false
|
|
|
|
|
|
|
|
|
|
func _on_buffer_timer_timeout() -> void:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
func _on_cayote_timer_timeout() -> void:
|
|
|
|
|
pass # Replace with function body.
|