Commit 4b85df29 authored by Nguyễn Quang Sáng's avatar Nguyễn Quang Sáng

05/10/23 commit

parent 630b55b0
This diff is collapsed.
......@@ -269,6 +269,7 @@ export default class GameController extends cc.Component {
this.currentBlock.setPosition(cc.Vec2.ZERO);
cellToMerge.removeAllChildren();
cellToMerge.addChild(this.currentBlock);
this.currentBlock.getComponent(Block).isPlace = true;
this.blocksData.setBlockValue(
this.currentBlockRow,
......@@ -306,7 +307,7 @@ export default class GameController extends cc.Component {
.call(() => {
nodeClone.destroy();
nodeClone.parent.removeAllChildren();
cellToMerge.children[0].getComponent(Block).setSpriteBlock(index);
this.currentBlock.getComponent(Block).setSpriteBlock(index);
this.cachedBlocksToMerge.length = 0;
if (!hasCheckedEmptyCell) {
......@@ -340,7 +341,7 @@ export default class GameController extends cc.Component {
newRow = currentRow;
newCol = col;
return this.updateBoardChildren(newRow, newCol);
return this.checkMergeability(newRow, newCol);
} else {
break;
}
......@@ -355,39 +356,35 @@ export default class GameController extends cc.Component {
}
}
updateBoardChildren(row: number, col: number) {
const data = this.blocksData.blockData;
const numRows = data.length;
const numCols = data[0].length;
const children = this.board.children;
for (let row = 0; row < numRows; row++) {
for (let col = 0; col < numCols; col++) {
const index = row * numCols + col;
const cell = children[index];
const value = data[row][col];
// updateBoardChildren() {
// const data = this.blocksData.blockData;
// const childrens = this.board.children;
cell.removeAllChildren();
// for (let row = 0; row < 6; row++) {
// for (let col = 0; col < 5; col++) {
// const index = row * 5 + col;
// const cell = childrens[index];
// const value = data[row][col];
if (value !== 0) {
const newValueNode = this.createValueNode(value);
cell.addChild(newValueNode);
if (cell.children.length > 1) cell.children.shift();
}
}
}
// cell.removeAllChildren();
this.checkMergeability(row, col);
}
// if (value !== 0) {
// const newValueNode = this.createValueNode(value);
// cell.addChild(newValueNode);
// if (cell.children.length > 1) cell.children.shift();
// }
// }
// }
// }
createValueNode(value: number) {
const newNode = new cc.Node();
// createValueNode(value: number) {
// const newNode = new cc.Node();
newNode.addComponent(cc.Sprite).spriteFrame =
this.blockPrefab.data.getComponent(Block).listSpriteBlocks[
Math.log(value) / Math.log(2) - 1
];
// newNode.addComponent(cc.Sprite).spriteFrame =
// this.blockPrefab.data.getComponent(Block).listSpriteBlocks[
// Math.log(value) / Math.log(2) - 1
// ];
return newNode;
}
// return newNode;
// }
}
import Utils from "../Tools/Utils";
const { ccclass, property } = cc._decorator;
@ccclass
......@@ -39,27 +37,35 @@ export default class BlockData extends cc.Component {
}
generateNumber(): number {
if (this.blockData.length == 0) return Utils.random(0, 4);
if (this.blockData.length == 0) return 0;
const allValues: number[] = [];
for (let row = 0; row < 6; row++) {
for (let col = 0; col < 5; col++) {
allValues.push(this.blockData[row][col]);
}
}
const smallNumberProbability = 0.7;
const largeNumberProbability = 0.3;
const higherValueCount = allValues.filter((value) => value >= 32).length;
const totalValueCount = allValues.length;
const higherValueProbability = higherValueCount / totalValueCount;
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];
const random = Math.random();
if (random < higherValueProbability) {
const minExponent = 5;
const maxExponent = 9;
const exponent =
Math.floor(Math.random() * (maxExponent - minExponent + 1)) +
minExponent;
const randomValue = Math.pow(2, exponent);
return Math.log(randomValue) / Math.log(2) - 1;
} else {
const averageNumbers = [16, 32, 64];
const randomIndex = Math.floor(Math.random() * averageNumbers.length);
randomNumber = averageNumbers[randomIndex];
const possibleValues = [2, 4, 8, 16];
const randomValue =
possibleValues[Math.floor(Math.random() * possibleValues.length)];
return Math.log(randomValue) / Math.log(2) - 1;
}
return Math.log(randomNumber) / Math.log(2) - 1;
}
}
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