goodnight_jellybean/godot/3DCursor.gd

113 lines
2.7 KiB
GDScript3
Raw Normal View History

2022-12-31 01:19:40 -07:00
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():
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 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
2023-01-01 02:23:56 -07:00
func sound_beep():
if not $Beep.playing:
$Beep.play()
func sound_color():
if $Color.playing:
return
$Color.play()
2022-12-31 01:19:40 -07:00
func _input(_event):
if Input.is_action_just_pressed("cursor_interact"):
GlobalCursorState.fire_interact()
2023-01-01 02:23:56 -07:00
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()
2022-12-31 01:19:40 -07:00
func _process(_delta):
set_color()
2023-01-01 02:23:56 -07:00
func bump():
if $Bump.playing:
return
$Bump.play()
var locked = false
2022-12-31 01:19:40 -07:00
func _physics_process(delta):
inc_time_buffer(delta)
2023-01-01 02:23:56 -07:00
if locked:
return
2022-12-31 01:19:40 -07:00
var dir = input_direction()
var speed = input_speed()
2023-01-01 02:23:56 -07:00
var velocity = Vector3(speed, speed, speed) * dir
var result = move_and_slide(velocity, Vector3.UP)
if velocity != result:
bump()
2022-12-31 01:19:40 -07:00
2023-01-01 02:23:56 -07:00
func _on_AnimationTree_motion_status(value):
locked = value