goodnight_jellybean/godot/3DCursor.gd

83 lines
2.2 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():
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
func _input(_event):
if Input.is_action_just_pressed("cursor_interact"):
GlobalCursorState.fire_interact()
func _process(_delta):
set_color()
func _physics_process(delta):
inc_time_buffer(delta)
var dir = input_direction()
var speed = input_speed()
var _slide_vel = move_and_slide(Vector3(speed, speed, speed) * dir, Vector3.UP)