Commit 32821f8b authored by Friends233's avatar Friends233

基本玩法实现

parent 44d16abf
This diff is collapsed.
...@@ -19,5 +19,15 @@ export const Config = { ...@@ -19,5 +19,15 @@ export const Config = {
/** 块矩阵最大列 */ /** 块矩阵最大列 */
maxCol: 10, maxCol: 10,
/** 填入方块颜色,小方块颜色 */ /** 填入方块颜色,小方块颜色 */
miniBlockColor:'#58D9B3' miniBlockColor:'#58D9B3',
/** 默认方块颜色,空白方块颜色 */
defaultColor:'#8FBABA',
}
/** 方块状态 */
export enum BLOCK_STATE {
/** 空的 */
EMPTY,
/** 非空 */
NON_EMPTY
} }
\ No newline at end of file
import Svga from "./Components/Svga/Svga"; import Svga from "./Components/Svga/Svga";
import { SvgaEvent } from "./Components/Svga/SvgaEvent"; import { SvgaEvent } from "./Components/Svga/SvgaEvent";
import { CUSTOM_EVENT, Config, GameColors } from "./Config/GameConfig"; import { BLOCK_STATE, CUSTOM_EVENT, Config, GameColors } from "./Config/GameConfig";
import exportEvent from "./exportEvent"; import exportEvent from "./exportEvent";
import propPool from "./propPool"; import propPool from "./propPool";
import { getProbability, getRandomArrayElements, loadGameResources, numToChinese, randomNum, getUrlParam } from "./utils"; import { getProbability, getRandomArrayElements, loadGameResources, numToChinese, randomNum, getUrlParam } from "./utils";
const { ccclass, property } = cc._decorator; const { ccclass, property } = cc._decorator;
@ccclass @ccclass
export default class GameScene extends cc.Component { export default class GameScene extends cc.Component {
...@@ -20,19 +19,18 @@ export default class GameScene extends cc.Component { ...@@ -20,19 +19,18 @@ export default class GameScene extends cc.Component {
/** 方块矩阵 */ /** 方块矩阵 */
blockMatrix: cc.Node = null blockMatrix: cc.Node = null
protected onLoad(): void { protected onLoad(): void {
// loadGameResources() // loadGameResources()
/** 开启碰撞检测 */ /** 开启碰撞检测 */
const cm = cc.director.getCollisionManager() const cm = cc.director.getCollisionManager()
cm.enabled = true cm.enabled = true
// cm.enabledDebugDraw = true; // cm.enabledDebugDraw = true;
} }
start() { start() {
this.blockMatrix = cc.find('blockMatrix', this.node) this.blockMatrix = cc.find('blockMatrix', this.node)
cc.propPool = propPool
this.setDefaultBlock() this.setDefaultBlock()
this.addNodeEvent() this.addNodeEvent()
} }
...@@ -41,9 +39,20 @@ export default class GameScene extends cc.Component { ...@@ -41,9 +39,20 @@ export default class GameScene extends cc.Component {
setDefaultBlock() { setDefaultBlock() {
const { maxRow, maxCol } = this.gameConfig const { maxRow, maxCol } = this.gameConfig
const parent = this.blockMatrix const parent = this.blockMatrix
const setDefaultBlock = []
for(let i = 0;i<maxCol-1;i++){
setDefaultBlock.push(...Array(6).fill('').map((_,j) => j+i*maxRow))
}
parent.removeAllChildren()
for (let i = 0; i < maxRow; i++) { for (let i = 0; i < maxRow; i++) {
for (let j = 0; j < maxCol; j++) { for (let j = 0; j < maxCol; j++) {
const block = cc.instantiate(this.defaultBlock) const block = cc.instantiate(this.defaultBlock)
block.color = cc.color(this.gameConfig.defaultColor)
if(setDefaultBlock.includes(maxRow*i+j)){
const script = block.getComponent('block')
script.changeBlockState(BLOCK_STATE.NON_EMPTY)
// block.blockState = 1
}
block.setParent(parent) block.setParent(parent)
} }
} }
...@@ -116,11 +125,6 @@ export default class GameScene extends cc.Component { ...@@ -116,11 +125,6 @@ export default class GameScene extends cc.Component {
} }
} }
/** 出钩 */
playGame() {
if (this.isGameOver || !this.isStartGame || this.clipAni) return
this.setClipState(CLIP_STATE.PLAY)
}
/** 游戏结束 */ /** 游戏结束 */
gameOver() { gameOver() {
......
...@@ -5,89 +5,36 @@ ...@@ -5,89 +5,36 @@
// Learn life-cycle callbacks: // Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
import { Config } from "./Config/GameConfig"; import { BLOCK_STATE, Config } from "./Config/GameConfig";
import propPool from "./propPool";
const { ccclass, property } = cc._decorator; const { ccclass, property } = cc._decorator;
@ccclass @ccclass
export default class Block extends cc.Component { export default class Block extends cc.Component {
/** 默认底色块 */ /** 方块状态 0空,1有方块填入 */
@property(cc.Prefab) blockState: BLOCK_STATE = BLOCK_STATE.EMPTY
defaultBlock: cc.Prefab = null
isMove = false
viewWidth = 0
viewHeight = 0
// onLoad () {} // onLoad () {}
start() { start() {
this.viewHeight = cc.view.getVisibleSize().height
this.viewWidth = cc.view.getVisibleSize().width
this.addNodeEvent()
// const matrix = cc.find('blockMatrix', this.node.parent)
// const node = matrix.children[0]
// const k = node.convertToWorldSpaceAR(node.getPosition(), cc.v2(375, 812))
// console.log('start', k.x, k.y)
} }
/** 放置block */ /** 播放清除动画 */
setBlockMatrix() { playClearAni(cb) {
const targetNodeWorldPos = this.node.parent.convertToWorldSpaceAR(this.node.getPosition()) this.changeBlockState(BLOCK_STATE.EMPTY)
const matrix = cc.find('blockMatrix', this.node.parent.parent) // propPool.push()
const { targetPos, targetNode } = this.posFindBlock(targetNodeWorldPos)
this.isMove = false
targetNode.color = cc.color(Config.miniBlockColor)
// const targetWorldPos = this.node.convertToNodeSpaceAR(targetPos)
// const blockPos = cc.v2(targetWorldPos.x + this.node.x, targetWorldPos.y + this.node.y)
// const blockTemp = cc.propPool.isEmpyt ? cc.instantiate(this.defaultBlock) : cc.propPool.pop()
// blockTemp.setPosition(blockPos)
} }
/** /**
* 根据坐标位置寻找最近的方块 * 修改当前方块状态
* @param pos 坐标 * @param type 0 清空,1 填入方块
*/ */
posFindBlock(pos: cc.Vec2) { changeBlockState(type: BLOCK_STATE) {
const matrix = cc.find('blockMatrix', this.node.parent.parent) this.blockState = type
let minX = 999999, minIdx = -1 this.node.color = cc.color(type === BLOCK_STATE.NON_EMPTY ? Config.miniBlockColor : Config.defaultColor)
const matrixPos = matrix.children.map((node, i) => {
const nodePos = matrix.convertToWorldSpaceAR(node.getPosition())
// 两点间的距离
const offsetX = Math.sqrt(Math.pow((nodePos.x - pos.x), 2) + Math.pow((nodePos.y - pos.y), 2))
if (offsetX < minX) {
minX = offsetX
minIdx = i
}
return {
nodePos,
node
}
})
return { targetPos: matrixPos[minIdx].nodePos, targetNode: matrixPos[minIdx].node }
}
addNodeEvent() {
// this.node.on(cc.Node.EventType.TOUCH_START, () => {
// this.isMove = true
// }, this)
// this.node.on(cc.Node.EventType.TOUCH_END, this.setBlockMatrix, this)
// this.node.on(cc.Node.EventType.TOUCH_MOVE, (e: cc.Event.EventTouch) => {
// if (this.isMove) {
// const pos: cc.Vec2 = e.getPreviousLocation()
// const viewW = this.viewWidth, viewH = this.viewHeight
// this.node.setPosition(pos.x - (viewW / 2), pos.y - (812 - (1624 - viewH) / 2))
// }
// }, this)
} }
update(dt) { // update (dt) {}
}
} }
{ {
"ver": "1.1.0", "ver": "1.1.0",
"uuid": "b120771b-3168-4d54-b293-1557c0abfb76", "uuid": "6e215ceb-8e04-418c-af8e-93be9dcfc80c",
"importer": "typescript", "importer": "typescript",
"isPlugin": false, "isPlugin": false,
"loadPluginInWeb": true, "loadPluginInWeb": true,
......
// Learn TypeScript:
// - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
// - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// 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 propPool from "./propPool";
const { ccclass, property } = cc._decorator;
@ccclass
export default class BlockManager extends cc.Component {
isMove = false
viewWidth = 0
viewHeight = 0
/** 方块矩阵外层容器 */
blockMatrix: cc.Node = null
/** 默认位置 */
defaultPos: cc.Vec2 = null
start() {
this.viewHeight = cc.view.getVisibleSize().height
this.viewWidth = cc.view.getVisibleSize().width
this.defaultPos = this.node.getPosition()
this.blockMatrix = cc.find('blockMatrix', this.node.parent)
this.addNodeEvent()
}
/** 设置所有的方块复原 */
setBlockMatrixAll() {
this.isMove = false
const allBlock = this.node.children
if (this.isSetBlockMartix()) {
allBlock.forEach((node: cc.Node) => {
const scripts = node.getComponent('miniBlock')
scripts.setBlockMatrix()
})
this.checkBlockMatrix()
}
this.node.setPosition(this.defaultPos)
this.node.scale = 1
}
/** 检查块矩阵是否能够消除 */
checkBlockMatrix() {
const blockMatrix = this.blockMatrix.children.map((node) => {
const script = node.getComponent('block')
return {
node,
script,
blockState: script.blockState
}
})
const { maxCol, maxRow } = Config
const eliminateBlocks = []
// 检查行
for (let i = 0; i < maxCol; i++) {
let temp = []
for (let j = 0; j < maxRow; j++) {
const block = blockMatrix[j + i * maxCol]
if (block.blockState !== BLOCK_STATE.NON_EMPTY) break
temp.push(block)
if (temp.length === maxRow) {
eliminateBlocks.push(...temp)
}
}
}
// 检查列
for (let i = 0; i < maxRow; i++) {
let temp = []
for (let j = 0; j < maxCol; j++) {
const block = blockMatrix[i + j * maxRow]
if (block.blockState !== BLOCK_STATE.NON_EMPTY) break
temp.push(block)
if (temp.length === maxCol) {
eliminateBlocks.push(...temp)
}
}
}
this.clearMatrixBlock(eliminateBlocks)
console.log('eliminateBlocks:', eliminateBlocks)
}
/** 清理矩阵块 */
clearMatrixBlock(eliminateBlocks: cc.Node[]) {
if (eliminateBlocks.length === 0) return
eliminateBlocks.forEach(({ node, script }) => {
script.playClearAni()
})
}
/**
* 判断是否能够放入
* @returns
*/
isSetBlockMartix() {
const allBlock = this.node.children
const filterBlock = allBlock.map((node: cc.Node) => {
const script = node.getComponent('miniBlock')
return { ...script.getTargetMatrix(), script }
})
// 该目标节点是否为空
const isEmpty = filterBlock.filter(({ targetNode, script }) => {
return script.blockState === BLOCK_STATE.NON_EMPTY
}).length === 0
if (!isEmpty) return false
const allTargetNodeId = filterBlock.map(({ targetNode }) => targetNode.getSiblingIndex())
// 索引是否重复
const isRepeat = allTargetNodeId.find((_, i) => allTargetNodeId.includes(_, i + 1))
return !isRepeat && isEmpty
}
addNodeEvent() {
this.node.on(cc.Node.EventType.TOUCH_START, () => {
this.isMove = true
this.node.scale = 1.7
}, this)
this.node.on(cc.Node.EventType.TOUCH_END, () => {
this.setBlockMatrixAll()
}, this)
this.node.on(cc.Node.EventType.TOUCH_MOVE, (e: cc.Event.EventTouch) => {
if (this.isMove) {
const pos: cc.Vec2 = e.getPreviousLocation()
const viewW = this.viewWidth, viewH = this.viewHeight
this.node.setPosition(pos.x - (viewW / 2), pos.y - (812 - (1624 - viewH) / 2))
}
}, this)
}
// update (dt) {}
}
{
"ver": "1.1.0",
"uuid": "15dc8ccb-64a6-461e-8380-5ab97c0af20e",
"importer": "typescript",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}
\ No newline at end of file
...@@ -5,61 +5,70 @@ ...@@ -5,61 +5,70 @@
// Learn life-cycle callbacks: // Learn life-cycle callbacks:
// - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
import { BLOCK_STATE, Config } from "./Config/GameConfig";
const { ccclass, property } = cc._decorator; const { ccclass, property } = cc._decorator;
@ccclass @ccclass
export default class NewClass extends cc.Component { export default class miniBlock extends cc.Component {
isMove = false isMove = false
viewWidth = 0 viewWidth = 0
viewHeight = 0 viewHeight = 0
/** 方块矩阵外层容器 */ /** 当前砖块的对应矩阵的目标砖块 */
blockMatrix: cc.Node = null targetNode:cc.Node = null
/** 外层容器 */ // onLoad () {}
stage: cc.Canvas = null
start() { start() {
this.viewHeight = cc.view.getVisibleSize().height this.viewHeight = cc.view.getVisibleSize().height
this.viewWidth = cc.view.getVisibleSize().width this.viewWidth = cc.view.getVisibleSize().width
this.blockMatrix = cc.find('blockMatrix', this.node.parent)
this.stage = this.node.parent
this.addNodeEvent()
} }
setBlockMatrixAll() {
this.isMove = false
const allBlock = this.node.children
allBlock.forEach((node: cc.Node) => {
const scripts = node.getComponent('block')
scripts.setBlockMatrix()
})
/** 获取目标方块 */
getTargetMatrix(){
const targetNodeWorldPos = this.node.parent.convertToWorldSpaceAR(this.node.getPosition())
const matrix = cc.find('blockMatrix', this.node.parent.parent)
const { targetPos, targetNode } = this.posFindBlock(targetNodeWorldPos)
this.targetNode = targetNode
// console.log('当前方块',this.node)
// console.log('目标方块',targetNode)
return {
targetPos,
targetNode
}
} }
addNodeEvent() { /** 放置block */
this.node.on(cc.Node.EventType.TOUCH_START, () => { setBlockMatrix() {
this.isMove = true const script = this.targetNode.getComponent('block')
this.node.scale = 1.7 console.log('s',this.targetNode)
}, this) script.changeBlockState(BLOCK_STATE.NON_EMPTY)
}
this.node.on(cc.Node.EventType.TOUCH_END, () => {
this.setBlockMatrixAll()
}, this)
this.node.on(cc.Node.EventType.TOUCH_MOVE, (e: cc.Event.EventTouch) => {
if (this.isMove) {
const pos: cc.Vec2 = e.getPreviousLocation()
const viewW = this.viewWidth, viewH = this.viewHeight
this.node.setPosition(pos.x - (viewW / 2), pos.y - (812 - (1624 - viewH) / 2)) /**
* 根据坐标位置寻找最近的方块
* @param pos 坐标
*/
posFindBlock(pos: cc.Vec2) {
const matrix = cc.find('blockMatrix', this.node.parent.parent)
let minX = 999999, minIdx = -1
const matrixPos = matrix.children.map((node, i) => {
const nodePos = matrix.convertToWorldSpaceAR(node.getPosition())
// 两点间的距离
const offsetX = Math.sqrt(Math.pow((nodePos.x - pos.x), 2) + Math.pow((nodePos.y - pos.y), 2))
if (offsetX < minX) {
minX = offsetX
minIdx = i
}
return {
nodePos,
node
} }
}, this) })
return { targetPos: matrixPos[minIdx].nodePos, targetNode: matrixPos[minIdx].node }
} }
// update (dt) {}
} }
{ {
"ver": "1.1.0", "ver": "1.1.0",
"uuid": "005cb974-4dad-45ce-acb8-fb0f1da60d28", "uuid": "b120771b-3168-4d54-b293-1557c0abfb76",
"importer": "typescript", "importer": "typescript",
"isPlugin": false, "isPlugin": false,
"loadPluginInWeb": true, "loadPluginInWeb": true,
......
...@@ -21,10 +21,13 @@ ...@@ -21,10 +21,13 @@
"_components": [ "_components": [
{ {
"__id__": 2 "__id__": 2
},
{
"__id__": 3
} }
], ],
"_prefab": { "_prefab": {
"__id__": 3 "__id__": 4
}, },
"_opacity": 255, "_opacity": 255,
"_color": { "_color": {
...@@ -105,6 +108,16 @@ ...@@ -105,6 +108,16 @@
"_atlas": null, "_atlas": null,
"_id": "" "_id": ""
}, },
{
"__type__": "6e215zrjgRBjK+Ok76dz8gM",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 1
},
"_enabled": true,
"_id": ""
},
{ {
"__type__": "cc.PrefabInfo", "__type__": "cc.PrefabInfo",
"root": { "root": {
......
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