stick-the-quick/objects/SpeedPad/SpeedPad.gd

63 lines
1.7 KiB
GDScript3
Raw Normal View History

class_name SpeedPad extends Area3D
const BASE_ANIM_SPEED: float = 0.1
@export var launch_speed: float = 30.0
@onready var mesh := $'Mesh' as MeshInstance3D
@onready var audio_player := $'AudioStreamPlayer' as AudioStreamPlayer
var turn45: int
var turn90: int
var turn135: int
var turn180: int
var anim_progress: float = 0.0
func _ready() -> void:
turn45 = mesh.find_blend_shape_by_name(&'Turn45')
turn90 = mesh.find_blend_shape_by_name(&'Turn90')
turn135 = mesh.find_blend_shape_by_name(&'Turn135')
turn180 = mesh.find_blend_shape_by_name(&'Turn180')
body_entered.connect(propel)
mesh.set_instance_shader_parameter(
&'hue_shift',
launch_speed - 30.0
)
func _process(delta: float) -> void:
anim_progress = fposmod(
anim_progress + delta*launch_speed*BASE_ANIM_SPEED,
1.0
)
var subprogress: float
var increase: int = -1
var decrease: int = -1
if anim_progress < 0.25:
subprogress = clampf(4.0*anim_progress, 0.0, 1.0)
decrease = turn180
increase = turn45
elif anim_progress < 0.5:
subprogress = clampf(4.0*(anim_progress - 0.25), 0.0, 1.0)
decrease = turn45
increase = turn90
elif anim_progress < 0.75:
subprogress = clampf(4.0*(anim_progress - 0.5), 0.0, 1.0)
decrease = turn90
increase = turn135
else:
subprogress = clampf(4.0*(anim_progress - 0.75), 0.0, 1.0)
decrease = turn135
increase = turn180
if decrease == turn180:
mesh.set_blend_shape_value(decrease, 0.0)
else:
mesh.set_blend_shape_value(
decrease,
clampf(1.0 - subprogress, 0.0, 1.0)
)
mesh.set_blend_shape_value(increase, subprogress)
func propel(body: PhysicsBody3D) -> void:
if body is RigidBody3D:
audio_player.play()
(body as RigidBody3D).linear_velocity += launch_speed*basis.z