求大佬给一个2D游戏,人物在地图上与物体之前的遮挡算法,不是用格子计算的
试试坐标算,x越大z越小,y越大z越大
2d游戏的物体也可以有z轴,根据z轴大小判断遮挡
我搞定了,自己写了个算法,人物的层级会根据物体的层级进行变化,建筑设置2个点构成一个线段,如果人物在线段上方,人物的层级就比物体的层级低反之就比物体层级高,就OK了
发出来观摩一下,dddd
let zIndexOut = { out: 0, dir: 0, wid: this.comState.node.height };
let _boundingBox = this.comState.node.getBoundingBoxToWorld();
let isInBuild = _boundingBox.contains(pos);
if (isInBuild) {
if (this._buildConfig.zIndexPos.length > 0) {
let pos1 = cc.v2(this._buildConfig.zIndexPos[0][0], this._buildConfig.zIndexPos[0][1]), pos2 = cc.v2(this._buildConfig.zIndexPos[1][0], this._buildConfig.zIndexPos[1][1]);
if (pos1 && pos2) {
let dis = ((pos2.y - pos1.y) * pospre.x + (pos1.x - pos2.x) * pospre.y + (pos2.x * pos1.y - pos1.x * pos2.y));
if (dis > 0) {
zIndexOut.out = this.node.zIndex + 1;
zIndexOut.dir = 0;
} else if (dis < 0) {
zIndexOut.out = this.node.zIndex - 1;
zIndexOut.dir = 1;
}
}
}
}
2赞
没哪么复杂,把人物&物体的zindex设置成自身坐标的Y轴值(竖方向)就可以了。