Commit 630b55b0 authored by Nguyễn Quang Sáng's avatar Nguyễn Quang Sáng

04/10/23 commit

parent 58ede0d6
This diff is collapsed.
......@@ -22,12 +22,13 @@ export default class GameController extends cc.Component {
cellTarget: cc.Node = null;
spawnColumn: number = Utils.random(0, 4);
cachedBlocksToMerge: Array<cc.Node> = null;
currentBlockRow: number = 0;
currentBlockCol: number = 0;
protected onLoad(): void {
GameController.instance = this;
this.spawnBlock(0);
this.regEvents();
this.spawnBlock(this.blocksData.generateNumber());
}
regEvents() {
......@@ -40,6 +41,16 @@ export default class GameController extends cc.Component {
});
}
offEvents() {
let childrens = this.board.children;
childrens.forEach((child) => {
child.off(cc.Node.EventType.TOUCH_START, this.onTouchDown, this);
child.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
child.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
child.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
});
}
spawnBlock(index: number) {
if (this.blocksData.blockData.length > 0) {
const hasNonZero = this.blocksData.blockData[0].some((e) => e !== 0);
......@@ -50,6 +61,8 @@ export default class GameController extends cc.Component {
this.currentBlock.getComponent(Block).setSpriteBlock(index);
this.currentBlock.getComponent(Block).isPlace = false;
this.spawnBlockPos = this.currentBlock.parent.getPosition();
this.regEvents();
}
onTouchDown(event: cc.Event.EventTouch) {
......@@ -94,6 +107,8 @@ export default class GameController extends cc.Component {
this.spawnColumn = this.dropColumn;
this.currentBlock.getComponent(Block).currentBlockColumn = this.dropColumn;
this.onDropBlock(this.dropColumn);
this.offEvents();
}
onDropBlock(col: number) {
......@@ -130,9 +145,8 @@ export default class GameController extends cc.Component {
}
}
}
checkMergeability(row: number, col: number) {
console.log(this.board.children);
console.log(this.blocksData.blockData);
const mergedValue = this.currentBlock.getComponent(Block).value;
const neighbors = [
......@@ -182,7 +196,7 @@ export default class GameController extends cc.Component {
this.currentBlock.getComponent(Block).isPlace = true;
this.touched = false;
this.spawnBlock(0);
this.spawnBlock(this.blocksData.generateNumber());
}
}
......@@ -225,15 +239,14 @@ export default class GameController extends cc.Component {
}
this.cachedBlocksToMerge = blocksToMerge;
this.mergeBlock(nodeIndex);
}
mergeBlock(nodeIndex: number) {
let blocksToMergeCount = this.cachedBlocksToMerge.length + 1;
const cellToMerge = this.board.children[nodeIndex];
const newRow = Math.floor(nodeIndex / 5);
const newCol = nodeIndex % 5;
this.currentBlockRow = Math.floor(nodeIndex / 5);
this.currentBlockCol = nodeIndex % 5;
let newValue = 0;
if (blocksToMergeCount <= 2 || blocksToMergeCount > 3) {
......@@ -257,7 +270,12 @@ export default class GameController extends cc.Component {
cellToMerge.removeAllChildren();
cellToMerge.addChild(this.currentBlock);
this.blocksData.setBlockValue(newRow, newCol, newValue);
this.blocksData.setBlockValue(
this.currentBlockRow,
this.currentBlockCol,
newValue
);
this.mergeMotion(
cellToMerge,
this.currentBlock.getComponent(Block).value,
......@@ -266,6 +284,8 @@ export default class GameController extends cc.Component {
}
mergeMotion(cellToMerge: cc.Node, value: number, index: number) {
let hasCheckedEmptyCell = false;
for (let i = 0; i < this.cachedBlocksToMerge.length; i++) {
const block = this.cachedBlocksToMerge[i];
......@@ -275,6 +295,7 @@ export default class GameController extends cc.Component {
this.blockPrefab.data.getComponent(Block).listSpriteBlocks[
Math.log(value) / Math.log(2) - 1
];
block.parent.addChild(nodeClone);
const globalPos = cellToMerge.convertToWorldSpaceAR(cc.Vec2.ZERO);
......@@ -284,9 +305,14 @@ export default class GameController extends cc.Component {
.to(0.3, { position: new cc.Vec3(localPos.x, localPos.y, 0) })
.call(() => {
nodeClone.destroy();
nodeClone.parent.removeAllChildren();
cellToMerge.children[0].getComponent(Block).setSpriteBlock(index);
this.cachedBlocksToMerge.length = 0;
if (!hasCheckedEmptyCell) {
this.checkEmptyCell();
hasCheckedEmptyCell = true;
}
})
.start();
}
......@@ -298,11 +324,8 @@ export default class GameController extends cc.Component {
const numCols = data[0].length;
let newRow = 0;
let newCol = 0;
let currentRow = 0;
let currentCol = 0;
// detected empty cell
for (let col = 0; col < numCols; col++) {
for (let row = numRows - 2; row >= 0; row--) {
if (data[row][col] !== 0) {
......@@ -327,43 +350,8 @@ export default class GameController extends cc.Component {
}
// no empty cell detected
if (newRow == 0 && newCol == 0) {
let maxAdjacentValueCount = 0;
for (let col = 0; col < numCols; col++) {
for (let row = 0; row < numRows; row++) {
const currentValue = data[row][col];
if (currentValue !== 0) {
let adjacentValueCount = 0;
if (col > 0 && data[row][col - 1] == currentValue) {
adjacentValueCount++;
}
if (col < numCols - 1 && data[row][col + 1] == currentValue) {
adjacentValueCount++;
}
if (row < numRows - 1 && data[row + 1][col] == currentValue) {
adjacentValueCount++;
}
if (adjacentValueCount > maxAdjacentValueCount) {
maxAdjacentValueCount = adjacentValueCount;
currentRow = row;
currentCol = col;
}
}
}
}
this.currentBlock.getComponent(Block).isPlace = true;
this.touched = false;
this.spawnBlock(0);
if (currentRow == 0 && currentCol == 0) return;
this.checkMergeability(currentRow, currentCol);
return this.checkMergeability(this.currentBlockRow, this.currentBlockCol);
}
}
......@@ -384,7 +372,7 @@ export default class GameController extends cc.Component {
if (value !== 0) {
const newValueNode = this.createValueNode(value);
cell.addChild(newValueNode);
if (cell.children.length > 1) cell.children.slice(-1);
if (cell.children.length > 1) cell.children.shift();
}
}
}
......
import Utils from "../Tools/Utils";
const { ccclass, property } = cc._decorator;
@ccclass
......@@ -35,4 +37,29 @@ export default class BlockData extends cc.Component {
this.blockData[row][col] = value;
}
}
generateNumber(): number {
if (this.blockData.length == 0) return Utils.random(0, 4);
const smallNumberProbability = 0.7;
const largeNumberProbability = 0.3;
let randomNumber = 0;
const randomValue = Math.random();
if (randomValue < smallNumberProbability) {
const smallNumbers = [2, 4, 8, 16, 32];
const randomIndex = Math.floor(Math.random() * smallNumbers.length);
randomNumber = smallNumbers[randomIndex];
} else if (randomValue < smallNumberProbability + largeNumberProbability) {
const largeNumbers = [64, 128, 256, 512];
const randomIndex = Math.floor(Math.random() * largeNumbers.length);
randomNumber = largeNumbers[randomIndex];
} else {
const averageNumbers = [16, 32, 64];
const randomIndex = Math.floor(Math.random() * averageNumbers.length);
randomNumber = averageNumbers[randomIndex];
}
return Math.log(randomNumber) / Math.log(2) - 1;
}
}
......@@ -28,9 +28,8 @@ export default class Block extends cc.Component {
this.currentBlockRow,
this.currentBlockColumn
);
GameController.instance.spawnBlock(0);
}
this.node.y -= dt * 150;
this.node.y -= dt * 100;
}
setSpriteBlock(index: number) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment