Scene services and game rules
Keep the session in a service and define the game rules separately.
In Block Puzzle, the Gameplay service stores the board, available pieces, score, and session state.
The service coordinates several operations. This excerpt shows part of the place method. BlockPuzzleGameplayService.ts
const placedCells = session.board.place(shape, request.anchor);
session.slots.use(request.slotId);
const completedLines = session.board.findCompletedLines();
const clearedCells = session.board.collectCells(completedLines);
const boardBeforeClear = session.board.snapshot();
session.board.clear(clearedCells);
const boardAfterClear = session.board.snapshot();
Before these operations, the service checks that the piece exists and calls board.canPlace. Afterward, it calculates the score, refills slots, and sends events to the display components.
GameplayServiceSession state and the order of operations
BlockPuzzleBoardCells, valid placements, and lines
BlockPuzzleSlotsWhich pieces are available or used
BlockPuzzleScoreRulesPoints for placing pieces and clearing lines
ComponentsShowing the result and playing effects
previewPlacement checks whether a piece fits. The board changes only when place runs.