cawez_puzzlebox/cubes/tile/cube_tile.gd
2024-06-27 23:25:45 -04:00

82 lines
2.1 KiB
GDScript

extends RigidBody3D
@export var recursive = false
var buffer = Vector3(0,0,0)
@export var clear_buffer = true
var death_direction = Vector3(0,0,0)
@export var mesh : Array[Node3D]
@export var colliders : Array[CollisionShape3D]
var alpha = 0.3
@export var forgive_buffer = false
@export var keyboard_input = false
var time_since_moved = 0.0
var death_timeout = 0.0
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
if keyboard_input:
time_since_moved += delta
if time_since_moved > 0.5:
if Input.get_action_strength("forward") > 0.0:
buffer = Vector3(0,3,0)
time_since_moved = 0
if Input.get_action_strength("backwards") > 0.0:
buffer = Vector3(0,-3,0)
time_since_moved = 0
if Input.get_action_strength("left") > 0.0:
buffer = Vector3(0,0,3)
time_since_moved = 0
if Input.get_action_strength("right") > 0.0:
buffer = Vector3(0,0,-3)
time_since_moved = 0
# Killed
if death_direction != Vector3.ZERO:
print ("Self-killing: " + name)
for mesh_animation in mesh:
# Melts the mesh
mesh_animation.scale = mesh_animation.scale.lerp(Vector3(0,0,0), 1 - alpha ** delta)
# Absorbs
mesh_animation.global_position = mesh_animation.global_position.lerp(mesh_animation.global_position + death_direction, 1 - alpha ** delta)
for collider_animation in colliders:
collider_animation.disabled = true
if (buffer != Vector3.ZERO):
var tryagain = !move(buffer) && forgive_buffer
if clear_buffer && !tryagain:
buffer = Vector3.ZERO
func move(direction) -> bool:
$raycast_wall.target_position = direction
$raycast_wall.force_raycast_update()
$raycast_recursive.target_position = direction
$raycast_recursive.force_raycast_update()
if !$raycast_wall.is_colliding():
translate(direction)
return true
if recursive && $raycast_recursive.is_colliding():
if ($raycast_recursive.get_collider().move(direction)):
translate(direction)
return true
return false