godot-puzzle/main.gd
2025-05-01 16:19:21 -04:00

63 lines
1.4 KiB
GDScript

extends Node3D
var microban_1 := "
####
# .#
# ###
#*@ #
# $ #
# ###
####
"
var hist := UndoRedo.new()
@onready var board := Board.from_string(microban_1)
var player: Piece
func _ready() -> void:
add_child(board)
for piece in board.pieces():
if piece.type == Piece.Type.Player:
player = piece
break
func _input(event: InputEvent) -> void:
if event.is_action_pressed("u"):
step(Vector2i.UP)
elif event.is_action_pressed("d"):
step(Vector2i.DOWN)
elif event.is_action_pressed("l"):
step(Vector2i.LEFT)
elif event.is_action_pressed("r"):
step(Vector2i.RIGHT)
# because most redo keybinds are a superset of undo, we check them first
# e.g. if C-Z, then C-z is pressed
elif event.is_action_pressed("redo"):
hist.redo()
elif event.is_action_pressed("undo"):
hist.undo()
elif event.is_action_pressed("restart"):
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()