41 lines
1.2 KiB
GDScript
41 lines
1.2 KiB
GDScript
class_name MotionExecutor
|
|
extends Node
|
|
|
|
signal motion_started(action: Resource)
|
|
signal motion_finished(action: Resource)
|
|
|
|
var current_action: Resource
|
|
var velocity := Vector2.ZERO
|
|
var duration := 0.0
|
|
var elapsed := 0.0
|
|
var active := false
|
|
|
|
|
|
func execute(action: Resource, direction: Vector2, beat_time: float, speed := 220.0) -> void:
|
|
current_action = action
|
|
duration = maxf(0.01, float(action.get("action_beats")) * maxf(0.01, beat_time))
|
|
elapsed = 0.0
|
|
active = true
|
|
var move_x := float(action.get("move_mult_x"))
|
|
if move_x == 0.0:
|
|
move_x = -1.0 if StringName(str(action.get("displacement"))) == &"left" else 1.0 if StringName(str(action.get("displacement"))) == &"right" else 0.0
|
|
velocity = Vector2(direction.x if direction.x != 0.0 else move_x, direction.y).normalized() * speed if move_x != 0.0 or direction != Vector2.ZERO else Vector2.ZERO
|
|
motion_started.emit(action)
|
|
|
|
|
|
func tick(delta: float) -> Vector2:
|
|
if not active:
|
|
return Vector2.ZERO
|
|
elapsed += delta
|
|
if elapsed >= duration:
|
|
active = false
|
|
velocity = Vector2.ZERO
|
|
motion_finished.emit(current_action)
|
|
return velocity
|
|
|
|
|
|
func cancel() -> void:
|
|
active = false
|
|
velocity = Vector2.ZERO
|
|
current_action = null
|