45 lines
1.0 KiB
GDScript
45 lines
1.0 KiB
GDScript
class_name KeyboardAnimator extends Resource
|
|
|
|
@export_group("Idle")
|
|
@export var _idle_frequency: float = 0.5
|
|
@export var _idle_amplitude: float = 0.1
|
|
@export var _idle_offset: float = 1
|
|
|
|
@export_group("Pressing")
|
|
@export var _pressing_lean_pitch: float = 0.05
|
|
@export var _pressing_lean_roll: float = 0.01
|
|
|
|
var _time: float
|
|
|
|
|
|
func animate_keyboard(
|
|
delta: float, position: Vector3, pressed_positions: Dictionary[Vector3, bool]
|
|
) -> Transform3D:
|
|
_time += delta
|
|
|
|
var rotation := Vector3.ZERO
|
|
for pos: Vector3 in pressed_positions.keys():
|
|
var pos_pressed := pressed_positions[pos]
|
|
if not pos_pressed:
|
|
continue
|
|
rotation.z -= pos.x * _pressing_lean_roll
|
|
rotation.x += pos.z * _pressing_lean_pitch
|
|
|
|
return Transform3D(Basis.from_euler(rotation), position)
|
|
|
|
|
|
func animate_key(_delta: float, position: Vector3) -> Transform3D:
|
|
position.y += (
|
|
sin(
|
|
(
|
|
_time * _idle_frequency
|
|
+ position.x * _idle_offset
|
|
+ position.y * _idle_offset
|
|
)
|
|
)
|
|
* _idle_amplitude
|
|
)
|
|
|
|
return Transform3D(Basis.IDENTITY, position)
|
|
|