stick-the-quick/props/springboard.gd

45 lines
1.1 KiB
GDScript

class_name Springboard extends Area3D
@export var launch_speed: float = 20.0
@export var model: MeshInstance3D
@export var audio_player: AudioStreamPlayer3D
func _ready() -> void:
body_entered.connect(_on_body_entered)
func _set_boing(boing: float) -> void:
var success := false
if model:
var i: int = model.find_blend_shape_by_name(&'boing')
if i >= 0:
success = true
model.set_blend_shape_value(i, boing)
if !success:
scale.y = 1.0 + boing
func _get_boing() -> float:
if model:
var i: int = model.find_blend_shape_by_name(&'boing')
if i >= 0:
return model.get_blend_shape_value(i)
return scale.y - 1.0
func _on_body_entered(body: PhysicsBody3D) -> void:
var rbody := body as RigidBody3D
var ch := body as Character
if rbody:
_set_boing(1.0)
var dir := global_basis.y.normalized()
rbody.apply_central_impulse(rbody.mass*(
launch_speed*dir -
rbody.linear_velocity.project(dir)
))
if ch:
ch.ground_normal = dir
ch.soft_force_change_state(&'launch')
else:
audio_player.play()
func _physics_process(delta: float) -> void:
_set_boing(lerp(_get_boing(), 0.0, 1.0 - 0.01**delta))