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
630b55b0
Commit
630b55b0
authored
Oct 04, 2023
by
Nguyễn Quang Sáng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
04/10/23 commit
parent
58ede0d6
Changes
4
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
238 additions
and
874 deletions
+238
-874
gameplay.fire
assets/Scenes/gameplay.fire
+172
-822
GameController.ts
assets/Scripts/Controllers/GameController.ts
+38
-50
BlockData.ts
assets/Scripts/Data/BlockData.ts
+27
-0
Block.ts
assets/Scripts/Objects/Block.ts
+1
-2
No files found.
assets/Scenes/gameplay.fire
View file @
630b55b0
This diff is collapsed.
Click to expand it.
assets/Scripts/Controllers/GameController.ts
View file @
630b55b0
...
...
@@ -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
new
Row
=
Math
.
floor
(
nodeIndex
/
5
);
const
new
Col
=
nodeIndex
%
5
;
this
.
currentBlock
Row
=
Math
.
floor
(
nodeIndex
/
5
);
this
.
currentBlock
Col
=
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
.
s
lice
(
-
1
);
if
(
cell
.
children
.
length
>
1
)
cell
.
children
.
s
hift
(
);
}
}
}
...
...
assets/Scripts/Data/BlockData.ts
View file @
630b55b0
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
;
}
}
assets/Scripts/Objects/Block.ts
View file @
630b55b0
...
...
@@ -28,9 +28,8 @@ export default class Block extends cc.Component {
this
.
currentBlockRow
,
this
.
currentBlockColumn
);
GameController
.
instance
.
spawnBlock
(
0
);
}
this
.
node
.
y
-=
dt
*
1
5
0
;
this
.
node
.
y
-=
dt
*
1
0
0
;
}
setSpriteBlock
(
index
:
number
)
{
...
...
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