Skip to content

Commit

Permalink
Move.block -> Move.blockId
Browse files Browse the repository at this point in the history
  • Loading branch information
sspenst committed Jul 25, 2023
1 parent 1bcf76e commit 906d31d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 67 deletions.
31 changes: 12 additions & 19 deletions components/level/game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -580,30 +580,23 @@ export default function Game({
text.pop();
}

if (prevMove.block) {
const block = getBlockById(blocks, prevMove.block.id);
// undo the block push
if (prevMove.blockId !== undefined) {
const block = getBlockById(blocks, prevMove.blockId);
const direction = getDirectionFromCode(prevMove.code);

if (!block) {
if (!block || !direction) {
return prevGameState;
}

// move the block back to its original position
block.pos = prevMove.block.pos.clone();

// if it was pushed into a hole, restore the hole
// restore the hole if necessary
if (block.inHole) {
block.inHole = false;

const direction = getDirectionFromCode(prevMove.code);

if (!direction) {
return prevGameState;
}

const holePos = block.pos.add(direction);

board[holePos.y][holePos.x].tileType = TileType.Hole;
board[block.pos.y][block.pos.x].tileType = TileType.Hole;
}

// move the block back to its original position
block.pos = block.pos.sub(direction);
}

const newGameState: GameState = {
Expand Down Expand Up @@ -640,7 +633,7 @@ export default function Game({
return prevGameState;
}

move.block = block.clone();
move.blockId = block.id;
block.pos = blockPos;

// remove block if it is pushed onto a hole
Expand Down Expand Up @@ -724,7 +717,7 @@ export default function Game({
// if the board state has not changed and you're backtracking
const lastMove = moves[moves.length - 1];

return pos.equals(lastMove.pos) && !lastMove.block;
return pos.equals(lastMove.pos) && lastMove.blockId === undefined;
}

if (allowFreeUndo && checkForFreeUndo()) {
Expand Down
38 changes: 16 additions & 22 deletions helpers/checkpointHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { GameState } from '@root/components/level/game';
import TileType from '@root/constants/tileType';
import BlockState from '@root/models/blockState';
import Move from '@root/models/move';
import Position from '@root/models/position';
import Position, { getDirectionFromCode } from '@root/models/position';
import SquareState from '@root/models/squareState';

interface CheckpointSquareState {
Expand Down Expand Up @@ -76,13 +76,18 @@ export function convertToCheckpointState(gameState: GameState) {
pos: move.pos.clone(),
};

if (move.block) {
checkpointMove.block = move.block.clone();
if (move.blockId !== undefined) {
const block = blocks.find(b => b.id === move.blockId);
const direction = getDirectionFromCode(move.code);

const block = blocks.find(b => b.id === move.block?.id);
if (block && direction) {
checkpointMove.block = block.clone();
checkpointMove.block.pos = checkpointMove.block.pos.sub(direction);

if (block?.inHole) {
checkpointMove.holePos = block.pos.clone();
if (block.inHole) {
checkpointMove.block.inHole = false;
checkpointMove.holePos = block.pos.clone();
}
}
}

Expand All @@ -105,22 +110,11 @@ export function convertFromCheckpointState(checkpointState: CheckpointState) {
height: checkpointState.height,
moveCount: checkpointState.moveCount,
moves: checkpointState.moves.map(checkpointMove => {
const pos = new Position(checkpointMove.pos.x, checkpointMove.pos.y);
const move = new Move(checkpointMove.code, pos);

if (checkpointMove.block) {
const block = new BlockState(
checkpointMove.block.id,
checkpointMove.block.type,
checkpointMove.block.pos.x,
checkpointMove.block.pos.y,
checkpointMove.block.inHole,
);

move.block = block;
}

return move;
return new Move(
checkpointMove.code,
new Position(checkpointMove.pos.x, checkpointMove.pos.y),
checkpointMove.block?.id,
);
}),
pos: new Position(checkpointState.pos.x, checkpointState.pos.y),
width: checkpointState.width,
Expand Down
13 changes: 6 additions & 7 deletions models/move.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
import BlockState from './blockState';
import Position from './position';

export default class Move {
// keycode of the move direction
code: string;
// position before the move
pos: Position;
// if a block was moved, this is its position before the move
block: BlockState | undefined;
// the id of the block pushed during this move
blockId?: number;

constructor(code: string, pos: Position, block?: BlockState) {
constructor(code: string, pos: Position, blockId?: number) {
this.code = code;
this.pos = pos.clone();
this.block = block?.clone();
this.blockId = blockId;
}

static clone(move: Move) {
return new Move(
move.code,
new Position(move.pos.x, move.pos.y),
move.block ? BlockState.clone(move.block) : undefined,
move.blockId,
);
}

clone() {
return new Move(
this.code,
this.pos,
this.block,
this.blockId,
);
}
}
7 changes: 7 additions & 0 deletions models/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export default class Position {
this.y + pos.y,
);
}

sub(pos: Position) {
return new Position(
this.x - pos.x,
this.y - pos.y,
);
}
}

export function getDirectionFromCode(code: string) {
Expand Down
20 changes: 1 addition & 19 deletions tests/models/models.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('models/*.ts', () => {
expect(getDirectionFromCode('KeyB')).toBe(undefined);
});
test('Move', () => {
const move = new Move('code', new Position(1, 1));
const move = new Move('code', new Position(1, 1), 0);
const move2 = move.clone();
const move3 = Move.clone(move);

Expand All @@ -83,24 +83,6 @@ describe('models/*.ts', () => {
expect(move.pos.x).toBe(1);
move3.pos.x = 3;
expect(move.pos.x).toBe(1);

const move4 = new Move(
'code',
new Position(1, 1),
new BlockState(0, TileType.Block, 0, 0),
);

expect(move4.block?.id).toBe(0);

const move5 = Move.clone(move4);

expect(move5.block).toBeDefined();

if (move5.block) {
move5.block.pos.x = 2;
}

expect(move4.block?.pos.x).toBe(0);
});
test('SquareState', () => {
const s = new SquareState();
Expand Down

0 comments on commit 906d31d

Please sign in to comment.