125 lines
3.0 KiB
GDScript
125 lines
3.0 KiB
GDScript
extends KinematicBody
|
|
|
|
export var ACTIONS_PER_SECOND: float = 24
|
|
export var KB_ACTIONS_PER_SECOND: float = 6
|
|
export var DIST_PER_ACTION: float = 60
|
|
export var KB_DIST_PER_ACTION: float = 60
|
|
export var interact_brightness: float = 1.0
|
|
var time_buffer = 0.0
|
|
|
|
func _ready():
|
|
GlobalCursorState.set_cursor(self)
|
|
set_color()
|
|
|
|
func set_color():
|
|
if GlobalCursorState.is_frozen:
|
|
return
|
|
var dir = input_direction()
|
|
var color = Color(dir.x, dir.y, dir.z)
|
|
var mat = $MeshInstance.get_active_material(0)
|
|
mat.albedo_color = color
|
|
mat.emission = color
|
|
if Input.is_action_pressed("cursor_interact"):
|
|
mat.emission_energy = interact_brightness
|
|
else:
|
|
mat.emission_energy = 0.0
|
|
|
|
|
|
func keyboard_pressed():
|
|
return Input.is_action_pressed("keyboard_up") or Input.is_action_pressed("keyboard_down")
|
|
|
|
func inc_time_buffer(delta: float):
|
|
time_buffer += delta
|
|
if keyboard_pressed():
|
|
time_buffer = min(time_buffer, 1.0/KB_ACTIONS_PER_SECOND)
|
|
else:
|
|
time_buffer = min(time_buffer, 1.0/ACTIONS_PER_SECOND)
|
|
|
|
func check_time_buffer():
|
|
if keyboard_pressed():
|
|
return time_buffer >= 1.0/KB_ACTIONS_PER_SECOND
|
|
return time_buffer >= 1.0/ACTIONS_PER_SECOND
|
|
|
|
func use_time_buffer():
|
|
time_buffer = 0.0
|
|
|
|
var axes = [Vector3.UP, Vector3.BACK, Vector3.RIGHT]
|
|
var current_axis = 0
|
|
func input_direction():
|
|
if GlobalCursorState.is_frozen:
|
|
return axes[current_axis]
|
|
if Input.is_action_just_pressed("cursor_right"):
|
|
current_axis += 1
|
|
if Input.is_action_just_pressed("cursor_left"):
|
|
current_axis -= 1
|
|
current_axis = (current_axis + axes.size()) % axes.size()
|
|
return axes[current_axis]
|
|
|
|
func input_speed() -> float:
|
|
if not check_time_buffer():
|
|
return 0.0
|
|
var speed = 0.0
|
|
if Input.is_action_pressed("cursor_up") or Input.is_action_just_released("cursor_up"):
|
|
use_time_buffer()
|
|
speed += 1
|
|
if Input.is_action_pressed("cursor_down") or Input.is_action_just_released("cursor_down"):
|
|
use_time_buffer()
|
|
speed += -1
|
|
if keyboard_pressed():
|
|
speed *= KB_DIST_PER_ACTION
|
|
else:
|
|
speed *= DIST_PER_ACTION
|
|
return speed
|
|
|
|
func sound_beep():
|
|
if not $Beep.playing:
|
|
$Beep.play()
|
|
|
|
func sound_color():
|
|
if $Color.playing:
|
|
return
|
|
$Color.play()
|
|
|
|
func sound_click():
|
|
$Move.play()
|
|
|
|
func sound_bump():
|
|
if $Bump.playing:
|
|
return
|
|
$Bump.play()
|
|
|
|
func _input(_event):
|
|
if GlobalCursorState.is_frozen:
|
|
return
|
|
if Input.is_action_just_pressed("cursor_interact"):
|
|
GlobalCursorState.fire_interact()
|
|
sound_beep()
|
|
if Input.is_action_just_released("cursor_interact"):
|
|
sound_beep()
|
|
|
|
if Input.is_action_just_pressed("cursor_right") or Input.is_action_just_pressed("cursor_left"):
|
|
sound_color()
|
|
|
|
func _process(_delta):
|
|
set_color()
|
|
|
|
|
|
var locked = false
|
|
func _physics_process(delta):
|
|
inc_time_buffer(delta)
|
|
if locked:
|
|
return
|
|
var dir = input_direction()
|
|
var speed = input_speed()
|
|
var velocity = Vector3(speed, speed, speed) * dir
|
|
var result = move_and_slide(velocity, Vector3.UP)
|
|
if velocity != result:
|
|
sound_bump()
|
|
elif speed != 0.0:
|
|
sound_click()
|
|
|
|
|
|
|
|
func _on_AnimationTree_motion_status(value):
|
|
locked = value
|