Commit bc669b04 authored by Friends233's avatar Friends233

随机生成方块组

parent f79b286f
This diff is collapsed.
......@@ -10,7 +10,9 @@ export const CUSTOM_EVENT = {
/** 初始化游戏数据 */
GAME_INIT: 'GAME_INIT',
/** 销毁游戏场景 */
GAME_DESTROY: 'GAME_DESTROY'
GAME_DESTROY: 'GAME_DESTROY',
/** 加分 */
ADD_SCORE:'ADD_SCORE',
}
export const Config = {
......@@ -25,7 +27,204 @@ export const Config = {
/** 每一个方块动画播放延时 ms*/
blockAniDelay: 35,
/** 方块拖拽判定放置的最大距离 */
blockMaxOffset:58,
blockMaxOffset: 58,
/** 下方小方块间隔 */
miniBlockOffset: 2,
/** 小方块大小 */
miniBlockW:33.5,
/** 小方块生成矩阵 5*5 */
miniBlockMatrix:[
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
],
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 1, 1, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
],
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
],
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 1, 1, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
],
[
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
],
[
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0],
],
[
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
],
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0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, 1, 0, 0],
[0, 0, 0, 0, 0],
],
[
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 1, 0],
[0, 0, 0, 1, 0],
[0, 0, 0, 0, 0],
],
[
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 1, 1, 1, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0],
],
[
[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0],
],
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 1, 1, 0],
[0, 1, 1, 0, 0],
[0, 0, 0, 0, 0],
],
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 1, 1, 0, 0],
[0, 0, 1, 1, 0],
[0, 0, 0, 0, 0],
],
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[1, 1, 1, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
],
[
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
]
],
}
/** 方块状态 */
......
import Svga from "./Components/Svga/Svga";
import { SvgaEvent } from "./Components/Svga/SvgaEvent";
import { BLOCK_STATE, CUSTOM_EVENT, Config, GameColors } from "./Config/GameConfig";
import { BLOCK_STATE, CUSTOM_EVENT, Config, GameColors } from "./Config/GameConfig";
import exportEvent from "./exportEvent";
import propPool from "./propPool";
import { getProbability, getRandomArrayElements, loadGameResources, numToChinese, randomNum, getUrlParam } from "./utils";
......@@ -19,39 +19,43 @@ export default class GameScene extends cc.Component {
/** 方块矩阵 */
blockMatrix: cc.Node = null
/** 当前得分 */
score = 0
protected onLoad(): void {
// loadGameResources()
/** 开启碰撞检测 */
const cm = cc.director.getCollisionManager()
cm.enabled = true
// cm.enabledDebugDraw = true;
}
start() {
this.blockMatrix = cc.find('blockMatrix', this.node)
this.setDefaultBlock()
const { maxRow, maxCol } = this.gameConfig
const testBlock = []
// for (let i = 0; i < maxCol - 2; i++) {
// testBlock.push(...Array(8).fill('').map((_, j) => j + 1 + (i + 1) * maxRow))
// }
this.setDefaultBlock(testBlock)
this.addNodeEvent()
}
/** 设置默认方块底色块 */
setDefaultBlock() {
setDefaultBlock(setDefaultBlock = []) {
setDefaultBlock = Array.isArray(setDefaultBlock) ? setDefaultBlock : []
const { maxRow, maxCol } = this.gameConfig
const parent = this.blockMatrix
const setDefaultBlock = []
for(let i = 0;i<maxCol-1;i++){
setDefaultBlock.push(...Array(9).fill('').map((_,j) => j+i*maxRow))
}
parent.removeAllChildren()
for (let i = 0; i < maxRow; i++) {
for (let j = 0; j < maxCol; j++) {
const block = cc.instantiate(this.defaultBlock)
const script = block.getComponent('block')
script.changeBlockState(BLOCK_STATE.EMPTY)
if(setDefaultBlock.includes(maxRow*i+j)){
if (setDefaultBlock.includes(maxRow * i + j)) {
script.changeBlockState(BLOCK_STATE.NON_EMPTY)
// block.blockState = 1
}
block.setParent(parent)
}
......@@ -68,7 +72,7 @@ export default class GameScene extends cc.Component {
// this.countDown = Math.max(0, this.countDown - Math.ceil(difference / 1000))
// this.dateNow = 0
// })
this.node.on(CUSTOM_EVENT.ADD_SCORE, this.addScore, this)
exportEvent.on(CUSTOM_EVENT.NEXT_LEVEL, this.nextLevel, this)
exportEvent.on(CUSTOM_EVENT.GAME_START, this.startGame, this)
exportEvent.on(CUSTOM_EVENT.GAME_INIT, ({ detail = {} }) => {
......@@ -84,10 +88,10 @@ export default class GameScene extends cc.Component {
testNode.active = isTest
if (isTest && (href?.includes('duibadev') || href?.includes('duibatest')) || href.includes('localhost')) {
const testEventMap = {
next: 'nextLevel',
refreshProp: 'refreshStageProps',
start: 'testStartGame',
init: 'testInit',
clear: 'setDefaultBlock'
}
testNode.children.forEach((n) => {
const event = this?.[testEventMap[n.name]]
......@@ -107,6 +111,12 @@ export default class GameScene extends cc.Component {
})
}
/** 加分 */
addScore() {
this.score += 1
this.setLable('score', this.score)
}
/**
* 开始游戏
* @param config config
......@@ -123,6 +133,8 @@ export default class GameScene extends cc.Component {
...Config,
...con
}
this.setLable('score', 0)
this.setDefaultBlock()
}
......@@ -156,6 +168,11 @@ export default class GameScene extends cc.Component {
/** 刷新舞台道具 */
refreshStageProps() {
['miniBlockWrpLeft', 'miniBlockWrpMid', 'miniBlockWrpRight']
.forEach((key) => {
const blockManager = cc.find(key, this.node).getComponent('blockManager')
blockManager.refreshMiniBlock()
})
}
......
......@@ -5,15 +5,14 @@
// Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
import { BLOCK_STATE, Config } from "./Config/GameConfig";
import { BLOCK_STATE, CUSTOM_EVENT, Config } from "./Config/GameConfig";
import propPool from "./propPool";
import { waitTime } from "./utils";
import { getRandomArrayElements, waitTime } from "./utils";
const { ccclass, property } = cc._decorator;
@ccclass
export default class BlockManager extends cc.Component {
isMove = false
viewWidth = 0
......@@ -26,14 +25,62 @@ export default class BlockManager extends cc.Component {
/** 默认位置 */
defaultPos: cc.Vec2 = null
/** 加分事件 */
addScoreEvents: cc.Event.EventCustom = null
/** 小方块 */
@property(cc.Prefab)
miniBlockPre: cc.Prefab = null
start() {
this.viewHeight = cc.view.getVisibleSize().height
this.viewWidth = cc.view.getVisibleSize().width
this.defaultPos = this.node.getPosition()
this.addScoreEvents = new cc.Event.EventCustom(CUSTOM_EVENT.ADD_SCORE, true)
this.blockMatrix = cc.find('blockMatrix', this.node.parent)
this.refreshMiniBlock()
this.addNodeEvent()
}
/** 刷新小方块的生成 */
refreshMiniBlock() {
const miniBlockMatrix = getRandomArrayElements(Config.miniBlockMatrix, 1)[0]
this.node.removeAllChildren()
const { miniBlockOffset, miniBlockW } = Config
const len = miniBlockMatrix.length, maxRowNum = 0
const maxColNumAry = Array(len).fill(0)
miniBlockMatrix.forEach((colBlocks, i) => {
let maxX = 0
colBlocks.map((miniBlock, j) => {
if (miniBlock == 1) {
maxColNumAry[j]++
maxX++
const node = propPool.isEmpyt ? cc.instantiate(this.miniBlockPre) : propPool.pop()
const miniBlockSize = miniBlockOffset + miniBlockW
node.x = j * miniBlockSize - this.node.width / 2 + miniBlockOffset + miniBlockW / 2
node.y = (len - i - 1) * miniBlockSize - this.node.width / 2 + miniBlockOffset + miniBlockW / 2
node.setParent(this.node)
}
})
if (maxX > maxRowNum) {
maxRowNum = maxX
}
})
// 处理偶数方块x轴偏移
if (maxRowNum % 2 === 0) {
this.node.children.forEach((n) => {
n.x -= miniBlockW / 2
})
}
// 处理偶数方块y轴偏移
if (Math.max(...maxColNumAry) % 2 === 0) {
this.node.children.forEach((n) => {
n.y += miniBlockW / 2
})
}
}
/** 设置所有的方块复原 */
setBlockMatrixAll() {
this.isMove = false
......@@ -44,11 +91,22 @@ export default class BlockManager extends cc.Component {
scripts.setBlockMatrix()
})
this.checkBlockMatrix()
this.clearAllChildren()
// this.refreshMiniBlock()
}
this.node.setPosition(this.defaultPos)
this.node.scale = 1
}
/** 清除所有自己的子元素 */
clearAllChildren() {
this.node.children.forEach((node) => {
propPool.push(node)
})
this.node.removeAllChildren()
}
/** 检查块矩阵是否能够消除 */
checkBlockMatrix() {
const blockMatrix = this.blockMatrix.children.map((node) => {
......@@ -56,7 +114,8 @@ export default class BlockManager extends cc.Component {
return {
node,
script,
blockState: script.blockState
blockState: script.blockState,
idx: node.getSiblingIndex()
}
})
const { maxCol, maxRow } = Config
......@@ -69,6 +128,7 @@ export default class BlockManager extends cc.Component {
if (block.blockState !== BLOCK_STATE.NON_EMPTY) break
temp.push(block)
if (temp.length === maxRow) {
// console.log('最后一个方块',block.idx)
eliminateBlocks.push(temp)
}
}
......@@ -82,21 +142,27 @@ export default class BlockManager extends cc.Component {
if (block.blockState !== BLOCK_STATE.NON_EMPTY) break
temp.push(block)
if (temp.length === maxCol) {
// console.log('最后一个方块',block.idx)
eliminateBlocks.push(temp)
}
}
}
this.clearMatrixBlock(eliminateBlocks)
console.log('eliminateBlocks:', eliminateBlocks)
// console.log('eliminateBlocks:', eliminateBlocks)
}
/** 清理矩阵块 */
/**
* 清理矩阵块
* @param eliminateBlocks 需要清理的方块组
*/
clearMatrixBlock(eliminateBlocks: cc.Node[]) {
const recursionPlay = async (els) => {
if (els.length === 0) return
const { script } = els.pop()
script.playClearAni()
script.playClearAni(() => {
cc.find('Canvas').dispatchEvent(this.addScoreEvents)
})
await waitTime(Config.blockAniDelay)
recursionPlay(els)
}
......
......@@ -14,9 +14,9 @@
"frame": 0,
"value": {
"__type__": "cc.Color",
"r": 222,
"g": 70,
"b": 91,
"r": 255,
"g": 0,
"b": 130,
"a": 255
}
}
......
[
{
"__type__": "cc.Prefab",
"_name": "",
"_objFlags": 0,
"_native": "",
"data": {
"__id__": 1
},
"optimizationPolicy": 0,
"asyncLoadAssets": false,
"readonly": false
},
{
"__type__": "cc.Node",
"_name": "miniBlock",
"_objFlags": 0,
"_parent": null,
"_children": [],
"_active": true,
"_components": [
{
"__id__": 2
},
{
"__id__": 3
}
],
"_prefab": {
"__id__": 4
},
"_opacity": 255,
"_color": {
"__type__": "cc.Color",
"r": 88,
"g": 217,
"b": 179,
"a": 255
},
"_contentSize": {
"__type__": "cc.Size",
"width": 33.5,
"height": 33.5
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 0.5
},
"_trs": {
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
0,
0,
0,
0,
0,
0,
1,
1,
1,
1
]
},
"_eulerAngles": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_skewX": 0,
"_skewY": 0,
"_is3DNode": false,
"_groupIndex": 0,
"groupIndex": 0,
"_id": ""
},
{
"__type__": "cc.Sprite",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"_materials": [
{
"__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432"
}
],
"_srcBlendFactor": 770,
"_dstBlendFactor": 771,
"_spriteFrame": {
"__uuid__": "a23235d1-15db-4b95-8439-a2e005bfff91"
},
"_type": 0,
"_sizeMode": 0,
"_fillType": 0,
"_fillCenter": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0
},
"_fillStart": 0,
"_fillRange": 0,
"_isTrimmedMode": true,
"_atlas": null,
"_id": ""
},
{
"__type__": "b1207cbMWhNVLKTFVfAq/t2",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"_id": ""
},
{
"__type__": "cc.PrefabInfo",
"root": {
"__id__": 1
},
"asset": {
"__id__": 0
},
"fileId": "",
"sync": false
}
]
\ No newline at end of file
{
"ver": "1.3.2",
"uuid": "b73e2b92-c61e-42a6-84d4-532e39360a8d",
"importer": "prefab",
"optimizationPolicy": "AUTO",
"asyncLoadAssets": false,
"readonly": false,
"subMetas": {}
}
\ No newline at end of file
[
{
"__type__": "cc.Prefab",
"_name": "",
"_objFlags": 0,
"_native": "",
"data": {
"__id__": 1
},
"optimizationPolicy": 0,
"asyncLoadAssets": false,
"readonly": false
},
{
"__type__": "cc.Node",
"_name": "sp",
"_objFlags": 0,
"_parent": null,
"_children": [],
"_active": true,
"_components": [
{
"__id__": 2
}
],
"_prefab": {
"__id__": 3
},
"_opacity": 255,
"_color": {
"__type__": "cc.Color",
"r": 12,
"g": 36,
"b": 249,
"a": 255
},
"_contentSize": {
"__type__": "cc.Size",
"width": 33.5,
"height": 33.5
},
"_anchorPoint": {
"__type__": "cc.Vec2",
"x": 0.5,
"y": 0.5
},
"_trs": {
"__type__": "TypedArray",
"ctor": "Float64Array",
"array": [
0,
0,
0,
0,
0,
0,
1,
1,
1,
1
]
},
"_eulerAngles": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_skewX": 0,
"_skewY": 0,
"_is3DNode": false,
"_groupIndex": 0,
"groupIndex": 0,
"_id": ""
},
{
"__type__": "cc.Sprite",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"_materials": [
{
"__uuid__": "eca5d2f2-8ef6-41c2-bbe6-f9c79d09c432"
}
],
"_srcBlendFactor": 770,
"_dstBlendFactor": 771,
"_spriteFrame": {
"__uuid__": "a23235d1-15db-4b95-8439-a2e005bfff91"
},
"_type": 0,
"_sizeMode": 0,
"_fillType": 0,
"_fillCenter": {
"__type__": "cc.Vec2",
"x": 0,
"y": 0
},
"_fillStart": 0,
"_fillRange": 0,
"_isTrimmedMode": true,
"_atlas": null,
"_id": ""
},
{
"__type__": "cc.PrefabInfo",
"root": {
"__id__": 1
},
"asset": {
"__id__": 0
},
"fileId": "",
"sync": false
}
]
\ No newline at end of file
{
"ver": "1.3.2",
"uuid": "6f2fd9a1-818e-4371-8db5-0aaf8d6122b2",
"importer": "prefab",
"optimizationPolicy": "AUTO",
"asyncLoadAssets": false,
"readonly": false,
"subMetas": {}
}
\ No newline at end of file
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