keebie/scripts/game_key.gd

44 lines
941 B
GDScript

class_name GameKey extends Node3D
static var _scene := preload("res://scenes/game_key.tscn")
@export var mesh: MeshInstance3D
@export var main_label: Label3D
var props: KeyProps
var _is_pressed: bool
@onready var _default_position: Vector3 = position
static func instantiate_with_props(_props: KeyProps) -> GameKey:
var node := _scene.instantiate() as GameKey
node.props = _props
return node
func _ready() -> void:
mesh.scale.x = props.width_ratio
main_label.text = props.input_event.as_text_physical_keycode()
func _process(_delta: float) -> void:
position.y = -0.1 if _is_pressed else 0.0
func _unhandled_input(event: InputEvent) -> void:
if event is not InputEventKey:
return
var event_key := event as InputEventKey
if (
event_key.physical_keycode != props.physical_keycode
or event_key.location != props.location
):
return
_is_pressed = event_key.is_pressed()
# scale.z = 0.75 if _is_pressed else 1.0