godot-puzzle/main.gd
2025-05-01 18:00:27 -04:00

93 lines
2.4 KiB
GDScript

extends Node3D
var microban_1 := "
####
# .#
# ###
#*@ #
# $ #
# ###
####
"
@onready var camera: Camera3D = $Camera
var hist := UndoRedo.new()
@onready var board := Board.from_string(microban_1)
var player: Piece
@onready var sound: AudioStreamPlayer = $Sound
var sounds_hit := AudioStreamRandomizer.new()
func _ready() -> void:
add_child(board)
var wall_count := 0
var wall_pos_sum := Vector2.ZERO
for piece in board.pieces():
if piece.type == Piece.Type.Player:
player = piece
elif piece.type == Piece.Type.Wall:
wall_count += 1
wall_pos_sum += Vector2(piece.position.x, piece.position.z)
var wall_pos_mean := wall_pos_sum / wall_count
camera.position = Vector3(wall_pos_mean.x, camera.position.y, wall_pos_mean.y)
sounds_hit.random_pitch = 1.1
var hit_dir := DirAccess.open("res://sfx/hit")
hit_dir.list_dir_begin()
var file_name := hit_dir.get_next()
while file_name != "":
if file_name.ends_with("ogg"):
sounds_hit.add_stream(-1, load("res://sfx/hit/"+file_name))
file_name = hit_dir.get_next()
$TopLeft.aabb = board.top_left_aabb()
$BottomRight.aabb = board.bottom_right_aabb()
print($TopLeft.position)
print(board.bottom_right_aabb())
func _process(delta: float) -> void:
if $TopLeft.is_on_screen() and $BottomRight.is_on_screen():
return
camera.position.y += delta
func _input(event: InputEvent) -> void:
if event.is_action_pressed("u", true, true):
step(Vector2i.UP)
elif event.is_action_pressed("d", true, true):
step(Vector2i.DOWN)
elif event.is_action_pressed("l", true, true):
step(Vector2i.LEFT)
elif event.is_action_pressed("r", true, true):
step(Vector2i.RIGHT)
elif event.is_action_pressed("undo", true, true):
hist.undo()
elif event.is_action_pressed("redo", true, true):
hist.redo()
elif event.is_action_pressed("restart", true, true):
restart()
# TODO:
func restart():
pass
func step(move: Vector2i):
var pos := player.lpos
var box := board.find_piece_at(pos + move, func(p): return p.type == Piece.Type.Box)
if board.passable_at(pos + move):
hist.create_action("move")
hist.add_do_method(player.do_move(move))
hist.add_undo_method(player.undo_move())
hist.commit_action()
elif box != null and board.passable_at(pos + move * 2):
hist.create_action("push")
hist.add_do_method(player.do_move(move))
hist.add_do_method(box.do_move(move))
hist.add_undo_method(player.undo_move())
hist.add_undo_method(box.undo_move())
hist.commit_action()
else:
sound.stream = sounds_hit
sound.play()