Skip to content

Commit

Permalink
Not moveable tile is marked as invalid cost
Browse files Browse the repository at this point in the history
  • Loading branch information
rexrainbow committed Aug 22, 2023
1 parent 66f4ff9 commit a4672fb
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 15 deletions.
12 changes: 9 additions & 3 deletions dist/rexboardplugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11589,11 +11589,10 @@
if (nextTileData === null) {
break;
}
cost = this.getCost(nextTileData, curTileData);
cost = nextTileData.cost;
if (cost === STOP$1) {
cost = movingPoints;
}
nextTileData.cost = cost;
if (movingPoints >= cost) {
out.push(nextTileData);
}
Expand All @@ -11620,7 +11619,8 @@
backwardTileData = null;
var neighborTileXArray = []; // forward and other neighbors, exclude backward
var neighborTileXY,
neighborTileData = null;
neighborTileData = null,
cost;
for (var i = 0, cnt = directions.length; i < cnt; i++) {
neighborTileXY = board.getNeighborTileXY(curTileData, directions[i], true);
if (neighborTileXY === null) {
Expand All @@ -11630,6 +11630,12 @@
continue;
}
neighborTileData = CreateTileData(neighborTileXY.x, neighborTileXY.y, directions[i]);
cost = this.getCost(neighborTileData, curTileData);
if (typeof cost !== 'number') {
// Invalid cost, remove this tileData
continue;
}
neighborTileData.cost = cost;
if (directions[i] === curTileData.direction) {
forwardTileData = neighborTileData;
}
Expand Down
2 changes: 1 addition & 1 deletion dist/rexboardplugin.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/docs/board-monopoly.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ var callback = function(curTileXY, preTileXY, monopoly) {
- `monopoly` : Path finder object.
- `monopoly.board` : [Board object](board.md)
- `monopoly.gameObject` : Chess game object.
- `monopoly.STOP` : Cost of stop. Return this value means chess will stop at `curTileXY`.
- `monopoly.BLOCKER` : Cost of blocker. Return this value means that chess can not move to `curTileXY`.
- `monopoly.STOP`, or `-1` : Cost of stop. Return this value means chess will stop at `curTileXY`.
- `monopoly.BLOCKER`, or `null` : Cost of blocker. Return this value means that chess can not move to `curTileXY`.
#### Set cost function
Expand Down
4 changes: 2 additions & 2 deletions docs/site/board-monopoly/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9682,8 +9682,8 @@ <h3 id="cost-function">Cost function<a class="headerlink" href="#cost-function"
<li><code>monopoly</code> : Path finder object.<ul>
<li><code>monopoly.board</code> : <a href="../board/">Board object</a></li>
<li><code>monopoly.gameObject</code> : Chess game object.</li>
<li><code>monopoly.STOP</code> : Cost of stop. Return this value means chess will stop at <code>curTileXY</code>.</li>
<li><code>monopoly.BLOCKER</code> : Cost of blocker. Return this value means that chess can not move to <code>curTileXY</code>.</li>
<li><code>monopoly.STOP</code>, or <code>-1</code> : Cost of stop. Return this value means chess will stop at <code>curTileXY</code>.</li>
<li><code>monopoly.BLOCKER</code>, or <code>null</code> : Cost of blocker. Return this value means that chess can not move to <code>curTileXY</code>.</li>
</ul>
</li>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion docs/site/search/search_index.json

Large diffs are not rendered by default.

Binary file modified docs/site/sitemap.xml.gz
Binary file not shown.
116 changes: 113 additions & 3 deletions examples/test/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import phaser from 'phaser/src/phaser.js';
import BoardPlugin from '../../plugins/board-plugin.js';

const Between = Phaser.Math.Between;

class Demo extends Phaser.Scene {
constructor() {
Expand All @@ -8,15 +11,115 @@ class Demo extends Phaser.Scene {
}

preload() {
this.load.image('tiles', "https://raw.githubusercontent.com/pedroddomingues/morram-vermes/master/public/terrain_atlas.png");
this.load.tilemapTiledJSON('map', "https://raw.githubusercontent.com/pedroddomingues/morram-vermes/master/public/map.json");
}

create() {
}
var map = this.make.tilemap({ key: 'map' });
var tiles = map.addTilesetImage('atlas_terrain', 'tiles');
var baseLayer = map.createLayer("base", tiles);
var islandLayer = map.createLayer("island", tiles);

var board = this.rexBoard.createBoardFromTilemap(map, "island");
board.forEachTileXY(function (tileXY, board) {
var scene = board.scene;
var tile = islandLayer?.getTileAt(tileXY.x, tileXY.y);
if (tile.index === 113) {
var cost = ((tileXY.x === 2) && (tileXY.y === 5)) ? -1 : 1;
scene.rexBoard.add.shape(board, tileXY.x, tileXY.y, 2, 0xffffff, 0.4).setOrigin(0).setData('cost', cost);
} else {
scene.rexBoard.add.shape(board, tileXY.x, tileXY.y, 2, 0xffffff, 0.1).setOrigin(0).setData('cost', undefined);
}
});

var chessA = new ChessA(board, {
x: 1,
y: 8
});

this.input.on('pointerdown', function (pointer) {
chessA.moveForward(4);
});

// console.log(board.getAllChess())

update(time, delta) {
}
}

class ChessA extends RexPlugins.Board.Shape {
constructor(board, tileXY) {
var scene = board.scene;
if (tileXY === undefined) {
tileXY = board.getRandomEmptyTileXY(0);
}
// Shape(board, tileX, tileY, tileZ, fillColor, fillAlpha, addToBoard)
super(board, tileXY.x, tileXY.y, 2, 0x3f51b5);
scene.add.existing(this.setOrigin(0));

// add behaviors
this.monopoly = scene.rexBoard.add.monopoly(this, {
pathTileZ: 2,
costCallback: function (curTileXY, preTileXY, monopoly) {
var board = monopoly.board;
const tile = board.tileXYZToChess(curTileXY.x, curTileXY.y, 2);
console.log(tile.rexChess.tileXYZ)
return tile.getData('cost')
},
});
this.moveTo = scene.rexBoard.add.moveTo(this);

// private members
this.movingPathTiles = [];
}

showMovingPath(tileXYArray) {
this.hideMovingPath();
var tileXY, worldXY;
var scene = this.scene,
board = this.rexChess.board;
for (var i = 0, cnt = tileXYArray.length; i < cnt; i++) {
tileXY = tileXYArray[i];
worldXY = board.tileXYToWorldXY(tileXY.x, tileXY.y, true);
this.movingPathTiles.push(scene.add.circle(worldXY.x, worldXY.y, 8, 0xb0003a).setOrigin(-0.5));
}
}

hideMovingPath() {
for (var i = 0, cnt = this.movingPathTiles.length; i < cnt; i++) {
this.movingPathTiles[i].destroy();
}
this.movingPathTiles.length = 0;
return this;
}

moveForward(movingPoints) {
if (this.moveTo.isRunning) {
return this;
}

var path = this.monopoly.getPath(movingPoints);
this.showMovingPath(path);
this.moveAlongPath(path);
return this;
}

moveAlongPath(path) {
//console.log({path})
//console.log(this.monopoly.getPath(path.length))
if (path.length === 0) {
return;
}

this.moveTo.once('complete', function () {
this.moveAlongPath(path);
}, this);
var tileData = path.shift();
this.moveTo.moveTo(tileData);
this.monopoly.setFace(this.moveTo.destinationDirection);
return this;
}
}

var config = {
type: Phaser.AUTO,
Expand All @@ -27,7 +130,14 @@ var config = {
mode: Phaser.Scale.FIT,
autoCenter: Phaser.Scale.CENTER_BOTH,
},
scene: Demo
scene: Demo,
plugins: {
scene: [{
key: 'rexBoard',
plugin: BoardPlugin,
mapping: 'rexBoard'
}]
}
};

var game = new Phaser.Game(config);
9 changes: 8 additions & 1 deletion plugins/board/monopoly/GetNextTile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var GetNextTile = function (curTileData, preTileData) {
var forwardTileData = null,
backwardTileData = null;
var neighborTileXArray = []; // forward and other neighbors, exclude backward
var neighborTileXY, neighborTileData = null;
var neighborTileXY, neighborTileData = null, cost;
for (var i = 0, cnt = directions.length; i < cnt; i++) {
neighborTileXY = board.getNeighborTileXY(curTileData, directions[i], true);
if (neighborTileXY === null) {
Expand All @@ -18,7 +18,14 @@ var GetNextTile = function (curTileData, preTileData) {
if (!board.contains(neighborTileXY.x, neighborTileXY.y, this.pathTileZ)) {
continue;
}

neighborTileData = CreateTileData(neighborTileXY.x, neighborTileXY.y, directions[i]);
cost = this.getCost(neighborTileData, curTileData);
if (typeof (cost) !== 'number') { // Invalid cost, remove this tileData
continue;
}

neighborTileData.cost = cost;

if (directions[i] === curTileData.direction) {
forwardTileData = neighborTileData;
Expand Down
3 changes: 1 addition & 2 deletions plugins/board/monopoly/GetPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ var GetPath = function (movingPoints, out) {
if (nextTileData === null) {
break;
}
cost = this.getCost(nextTileData, curTileData);
cost = nextTileData.cost;
if (cost === STOP) {
cost = movingPoints;
}
nextTileData.cost = cost;
if (movingPoints >= cost) {
out.push(nextTileData);
}
Expand Down

0 comments on commit a4672fb

Please sign in to comment.