Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
rexrainbow committed Dec 23, 2024
1 parent 2bb90d5 commit 3f4f668
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 8 deletions.
5 changes: 5 additions & 0 deletions examples/board-bejeweled/reset-board.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@echo off
set main=./examples/board-bejeweled/reset-board.js
cd ..
cd ..
npm run dev
91 changes: 91 additions & 0 deletions examples/board-bejeweled/reset-board.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import phaser from 'phaser/src/phaser.js';
import BoardPlugin from '../../plugins/board-plugin.js';
import Bejeweled from '../../templates/bejeweled/Bejeweled.js';

class Demo extends Phaser.Scene {
constructor() {
super({
key: 'examples'
})
}

preload() { }

create() {
this.bejeweled = new Bejeweled(this, {
board: {
grid: {
x: 180,
y: 50,
cellWidth: 60,
cellHeight: 60,
},
width: 8,
height: 16 // Prepared rows: upper 8 rows
},
chess: {
// pick random symbol from array, or a callback to return symbol
symbols: [0, 1, 2, 3, 4, 5],

// User-defined chess game object
create: function (board) {
var scene = board.scene;
var gameObject = scene.rexBoard.add.shape(board, 0, 0, 0, 0x0, 1, false)
.setScale(0.95)
// Initial 'symbol' value
.setData('symbol', undefined);
// Symbol is stored in gameObject's data manager (`gameObject.getData('symbol')`)
// Add data changed event to change the appearance of game object via new symbol value
gameObject.data.events.on('changedata-symbol', function (gameObject, value, previousValue) {
gameObject.setFillStyle(getColor(value));
});
return gameObject;
},

// moveTo behavior
moveTo: {
speed: 400
},
},
mask: true,

debug: true,
});
this.bejeweled.start();

this.add.text(0, 0, 'Reset', { fontSize: 40 })
.setInteractive()
.on('pointerup', function () {
this.bejeweled.mainState.goto('RESET');
}, this)
}

update() { }
}

var colorArray = Phaser.Display.Color.HSVColorWheel(0.5, 1);
var getColor = function (symbol) {
// symbols: [0, 1, 2, 3, 4, 5]
return colorArray[symbol * 60].color;
}

var config = {
type: Phaser.AUTO,
parent: 'phaser-example',
width: 768,
height: 1334,
scale: {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
},
scene: Demo,
plugins: {
scene: [{
key: 'rexBoard',
plugin: BoardPlugin,
mapping: 'rexBoard'
}]
}
};

var game = new Phaser.Game(config);
5 changes: 4 additions & 1 deletion plugins/board/moveto/MoveToTile.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,23 @@ var MoveToTile = function (tileX, tileY, direction) {
}
}

// invalid tile position
// Invalid tile position
if ((tileX == null) || (tileY == null)) {
this.lastMoveResult = false;
return this;
}

if (direction === undefined) {
globTileXYZ.x = tileX;
globTileXYZ.y = tileY
direction = board.getNeighborTileDirection(myTileXYZ, globTileXYZ);
}

if (!this.canMoveTo(tileX, tileY, direction)) {
this.lastMoveResult = false;
return this;
}

this.destinationTileX = tileX;
this.destinationTileY = tileY;
this.destinationDirection = direction;
Expand Down
12 changes: 6 additions & 6 deletions templates/bejeweled/board/PreTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ var PreTest = function () {
tileA.y = tileY;
for (var dir = 0, dirCnt = directions.length; dir < dirCnt; dir++) {
tileB = this.board.getNeighborTileXY(tileA, dir);
// swap symbol
swapSymbols(match, tileA, tileB);
// any match?
// Swap symbol
SwapSymbols(match, tileA, tileB);
// Any match?
this.preTestResult = AnyMatch.call(this, 3);
// swap symbol back
swapSymbols(match, tileA, tileB);
// Swap symbol back
SwapSymbols(match, tileA, tileB);

if (this.preTestResult) {
return true;
Expand All @@ -32,7 +32,7 @@ var PreTest = function () {
return false;
}

var swapSymbols = function (match, tileA, tileB) {
var SwapSymbols = function (match, tileA, tileB) {
var symbolA = match.getSymbol(tileA.x, tileA.y);
var symbolB = match.getSymbol(tileB.x, tileB.y);
match.setSymbol(tileA.x, tileA.y, symbolB);
Expand Down
2 changes: 1 addition & 1 deletion templates/bejeweled/states/MainState.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class State extends BaseState {
// PRETEST

// SELECT1START
enter_SELECT1() {
enter_SELECT1START() {
this.selectedChess1 = undefined;
this.selectedChess2 = undefined;

Expand Down

0 comments on commit 3f4f668

Please sign in to comment.