Commit f79b286f authored by Friends233's avatar Friends233

fix叠加放置、最短放置距离判定

parent 73f1ab72
......@@ -24,6 +24,8 @@ export const Config = {
defaultColor: '#8FBABA',
/** 每一个方块动画播放延时 ms*/
blockAniDelay: 35,
/** 方块拖拽判定放置的最大距离 */
blockMaxOffset:58,
}
/** 方块状态 */
......
......@@ -47,9 +47,9 @@ export default class GameScene extends cc.Component {
for (let i = 0; i < maxRow; i++) {
for (let j = 0; j < maxCol; j++) {
const block = cc.instantiate(this.defaultBlock)
block.color = cc.color(this.gameConfig.defaultColor)
const script = block.getComponent('block')
script.changeBlockState(BLOCK_STATE.EMPTY)
if(setDefaultBlock.includes(maxRow*i+j)){
const script = block.getComponent('block')
script.changeBlockState(BLOCK_STATE.NON_EMPTY)
// block.blockState = 1
}
......
......@@ -25,6 +25,7 @@ export default class Block extends cc.Component {
// onLoad () {}
start() {
// this.changeBlockState(BLOCK_STATE.EMPTY)
this.ani = this.node.getComponent(cc.Animation)
const animationState = this.ani.getAnimationState('blockBoom')
this.aniDuration = animationState.duration
......
......@@ -111,12 +111,19 @@ export default class BlockManager extends cc.Component {
*/
isSetBlockMartix() {
const allBlock = this.node.children
const filterBlock = allBlock.map((node: cc.Node) => {
const script = node.getComponent('miniBlock')
return { ...script.getTargetMatrix(), script }
})
const filterBlock = []
for (let i = 0; i < allBlock.length; i++) {
const script = allBlock[i].getComponent('miniBlock')
const { targetPos, targetNode } = script.getTargetMatrix()
// 未获取到指定最短距离内的节点
if (!targetNode) {
return false
}
filterBlock.push({ targetPos, targetNode })
}
// 该目标节点是否为空
const isEmpty = filterBlock.filter(({ targetNode, script }) => {
const isEmpty = filterBlock.filter(({ targetNode }) => {
const script = targetNode.getComponent('block')
return script.blockState === BLOCK_STATE.NON_EMPTY
}).length === 0
if (!isEmpty) return false
......
......@@ -10,7 +10,7 @@ import { BLOCK_STATE, Config } from "./Config/GameConfig";
const { ccclass, property } = cc._decorator;
@ccclass
export default class miniBlock extends cc.Component {
export default class MiniBlock extends cc.Component {
isMove = false
viewWidth = 0
......@@ -45,7 +45,6 @@ export default class miniBlock extends cc.Component {
/** 放置block */
setBlockMatrix() {
const script = this.targetNode.getComponent('block')
console.log('s',this.targetNode)
script.changeBlockState(BLOCK_STATE.NON_EMPTY)
}
......@@ -55,7 +54,7 @@ export default class miniBlock extends cc.Component {
*/
posFindBlock(pos: cc.Vec2) {
const matrix = cc.find('blockMatrix', this.node.parent.parent)
let minX = 999999, minIdx = -1
let minX = Config.blockMaxOffset, minIdx = -1
const matrixPos = matrix.children.map((node, i) => {
const nodePos = matrix.convertToWorldSpaceAR(node.getPosition())
// 两点间的距离
......@@ -69,6 +68,7 @@ export default class miniBlock extends cc.Component {
node
}
})
if(minIdx === -1) return {}
return { targetPos: matrixPos[minIdx].nodePos, targetNode: matrixPos[minIdx].node }
}
}
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