Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
DropAndMergeTheNumber
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nguyễn Quang Sáng
DropAndMergeTheNumber
Commits
4b85df29
Commit
4b85df29
authored
Oct 05, 2023
by
Nguyễn Quang Sáng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
05/10/23 commit
parent
630b55b0
Changes
3
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
130 additions
and
223 deletions
+130
-223
gameplay.fire
assets/Scenes/gameplay.fire
+70
-166
GameController.ts
assets/Scripts/Controllers/GameController.ts
+34
-37
BlockData.ts
assets/Scripts/Data/BlockData.ts
+26
-20
No files found.
assets/Scenes/gameplay.fire
View file @
4b85df29
This diff is collapsed.
Click to expand it.
assets/Scripts/Controllers/GameController.ts
View file @
4b85df29
...
...
@@ -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;
//
}
}
assets/Scripts/Data/BlockData.ts
View file @
4b85df29
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
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment