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 @onready var sounds_hit := audio_stream_randomizer_from_dir("res://sfx/hit") @onready var sounds_undo := audio_stream_randomizer_from_dir("res://sfx/undo") @onready var sounds_redo := audio_stream_randomizer_from_dir("res://sfx/redo") func _ready() -> void: add_child(board) for piece in board.pieces(): if piece.type == Piece.Type.Player: player = piece $TopLeft.aabb = board.top_left_aabb() $BottomRight.aabb = board.bottom_right_aabb() camera.position = Vector3(player.position.x, camera.position.y, player.position.z) 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): if hist.undo(): sound.stream = sounds_undo sound.play() elif event.is_action_pressed("redo", true, true): if hist.redo(): sound.stream = sounds_redo sound.play() 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() func audio_stream_randomizer_from_dir(dir: String) -> AudioStreamRandomizer: var stream := AudioStreamRandomizer.new() stream.random_pitch = 1.1 var hit_dir := DirAccess.open(dir) hit_dir.list_dir_begin() var file_name := hit_dir.get_next() while file_name != "": if file_name.ends_with("ogg"): stream.add_stream(-1, load(dir+"/"+file_name)) file_name = hit_dir.get_next() return stream