Skip to content

Commit

Permalink
Add worldXYToChess
Browse files Browse the repository at this point in the history
  • Loading branch information
rexrainbow committed Nov 30, 2024
1 parent 80b9afb commit 25b4a01
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 60 deletions.
6 changes: 4 additions & 2 deletions plugins/board/miniboard/Methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import AddChess from './chess/AddChess.js';
import RemoveChess from './chess/RemoveChess.js';
import RemoveAllChess from './chess/RemoveAllChess.js';
import GetAllChess from './chess/GetAllChess.js';
import GetBounds from './chess/GetBounds.js';
import WorldXYToChess from './chess/WorldXYToChess.js';

import SetMainBoard from './mainboard/SetMainboard.js';
import CanPutOnMainBoard from './mainboard/CanPutOnMainBoard.js';
Expand All @@ -22,14 +22,15 @@ import CanRotate from './transform/CanRotate.js';
import Rotate from './transform/Rotate.js';
import CanRotateTo from './transform/CanRotateTo.js';
import RotateTo from './transform/RotateTo.js';
import GetBounds from './transform/GetBounds.js';
import SetOrigin from './transform/SetOrigin.js';

export default {
addChess: AddChess,
removeChess: RemoveChess,
removeAllChess: RemoveAllChess,
getAllChess: GetAllChess,
getBounds: GetBounds,
worldXYToChess: WorldXYToChess,

pullOutFromMainBoard: PullOutFromMainBoard,
canPutOnMainBoard: CanPutOnMainBoard,
Expand All @@ -49,5 +50,6 @@ export default {
rotate: Rotate,
canRotateTo: CanRotateTo,
rotateTo: RotateTo,
getBounds: GetBounds,
setOrigin: SetOrigin
};
4 changes: 4 additions & 0 deletions plugins/board/miniboard/chess/AddChess.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import IsUID from '../../chess/IsUID.js';
import SetSizeFromBounds from '../transform/SetSizeFromBounds.js';

var AddChess = function (gameObject, tileX, tileY, tileZ) {
var grid = this.grid;
Expand All @@ -14,6 +15,9 @@ var AddChess = function (gameObject, tileX, tileY, tileZ) {
this.add(gameObject);

grid.restoreOrigin();

SetSizeFromBounds.call(this);

return this;
}

Expand Down
3 changes: 3 additions & 0 deletions plugins/board/miniboard/chess/RemoveAllChess.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import SetSizeFromBounds from '../transform/SetSizeFromBounds.js';

var RemoveAllChess = function (destroy) {
this.board.removeAllChess(destroy);
SetSizeFromBounds.call(this);
return this;
}

Expand Down
3 changes: 3 additions & 0 deletions plugins/board/miniboard/chess/RemoveChess.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import SetSizeFromBounds from '../transform/SetSizeFromBounds.js';

var RemoveChess = function (gameObject, tileX, tileY, tileZ, destroy) {
this.board.removeChess(gameObject, tileX, tileY, tileZ, destroy);
SetSizeFromBounds.call(this);
return this;
}

Expand Down
14 changes: 14 additions & 0 deletions plugins/board/miniboard/chess/WorldXYToChess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var WorldXYToChess = function (x, y, out) {
var grid = this.grid;
grid.saveOrigin();
grid.setOriginPosition(this.x, this.y);
var tileXY = this.board.worldXYToTileXY(x, y, true);
var tileX = tileXY.x,
tileY = tileXY.y;
grid.restoreOrigin();

var gameObjects = this.board.tileXYToChessArray(tileX, tileY, out);
return gameObjects;
}

export default WorldXYToChess;
24 changes: 7 additions & 17 deletions plugins/board/miniboard/input/OnPointerDown.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,17 @@ var OnPointerDown = function (pointer) {
this.miniboardInput.pointer = pointer;
}

var hitChess = OnTouchTileStart.call(this, pointer);
if (hitChess) {
var isHit = OnTouchTileStart.call(this, pointer);
if (isHit) {
OnDragStart.call(this, pointer);
}
}

var OnTouchTileStart = function (pointer) {
// Get touched tileX, tileY
var grid = this.grid;
grid.saveOrigin();
grid.setOriginPosition(this.x, this.y);
var out = this.board.worldXYToTileXY(pointer.x, pointer.y, true);
var tileX = out.x,
tileY = out.y;
grid.restoreOrigin();
this.miniboardInput.tilePosition.x = tileX;
this.miniboardInput.tilePosition.y = tileY;

// Get touched chess
var gameObjects = this.board.tileXYToChessArray(tileX, tileY, globChessArray);
var hitChess = (gameObjects.length > 0);
if (hitChess) {
var gameObjects = this.worldXYToChess(pointer.x, pointer.y, globChessArray);
var isHit = (globChessArray.length > 0);
if (isHit) {
// Fire events
var gameObject;
for (var i = 0, cnt = gameObjects.length; i < cnt; i++) {
Expand All @@ -45,8 +34,9 @@ var OnTouchTileStart = function (pointer) {
}
this.emit('pointerdown', pointer, this);
}

globChessArray.length = 0;
return hitChess;
return isHit;
}

var globChessArray = [];
Expand Down
File renamed without changes.
13 changes: 9 additions & 4 deletions plugins/board/miniboard/transform/SetOrigin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import GetMinMaxTileXY from '../utils/GetMinMaxTileXY.js';
import Linear from '../../../utils/math/Linear.js';
import OffsetTransfer from './transferfunctions/Offset.js';
import ResetChessTileXYZ from './ResetChessTileXYZ.js';

const SetOriginBase = Phaser.GameObjects.Components.Origin.setOrigin;

var SetOrigin = function (originX, originY) {
switch (originX) {
case 'center':
Expand All @@ -21,9 +22,12 @@ var SetOrigin = function (originX, originY) {
if (originY === undefined) {
originY = originX;
}
var minMaxTileXY = GetMinMaxTileXY.call(this, undefined, true);
var offsetX = -Math.floor(Linear(minMaxTileXY.minX, minMaxTileXY.maxX, originX));
var offsetY = -Math.floor(Linear(minMaxTileXY.minY, minMaxTileXY.maxY, originY));

SetOriginBase.call(this, originX, originY);

var bounds = this.getBounds(true);
var offsetX = -Math.floor(Linear(bounds.left, bounds.right, originX));
var offsetY = -Math.floor(Linear(bounds.top, bounds.bottom, originY));

if ((offsetX !== 0) || (offsetY !== 0)) {
var newTileXYZMap = OffsetTransfer.call(this, offsetX, offsetY);
Expand All @@ -35,6 +39,7 @@ var SetOrigin = function (originX, originY) {
(this.y + (world0.y - worldOffsetXY.y))
);
}

return this;
}

Expand Down
16 changes: 16 additions & 0 deletions plugins/board/miniboard/transform/SetSizeFromBounds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const SetSizeBase = Phaser.GameObjects.Components.Size.setSize;
const SetOriginBase = Phaser.GameObjects.Components.Origin.setOrigin;

var SetSizeFromBounds = function () {
var bounds = this.getBounds(true);

SetSizeBase.call(this, bounds.width, bounds.height);

var originX = (bounds.width === 0) ? 0.5 : (this.x - bounds.left) / bounds.width;
var originY = (bounds.height === 0) ? 0.5 : (this.y - bounds.top) / bounds.height;
SetOriginBase.call(this, originX, originY);

return this;
}

export default SetSizeFromBounds;
37 changes: 0 additions & 37 deletions plugins/board/miniboard/utils/GetMinMaxTileXY.js

This file was deleted.

0 comments on commit 25b4a01

Please sign in to comment.