laya 武器旋转砍怪判断
typescript代码:
export interface IMonster extends Laya.Node { id: number; isDie:boolean; getPoint(): Laya.Point; getHitRect():Laya.Rectangle; } export class Monster extends Laya.Sprite implements IMonster { private _isDie: boolean; private _id: number; constructor(id: number) { this._id = id; } public get isDie(): boolean { return this._isDie; } getPoint(): Laya.Point { return new Laya.Point(this.x, this.y); } getHitRect(): Laya.Rectangle { // 固定为原始矩形 //let x = this.x - this.width * this.anchorX; //let y = this.y - this.height * this.anchorY; //return new Laya.Rectangle(x , y, this.width, this.height); // 动态的边界 return this.getBounds(); } } export class Player { let monsterIds: number[] = []; // 检查圆圈是否击中怪物(武器绕着玩家旋转, 空心圆圈) checkCircleHitMonsters() { for (let monsterId in monsterList) { let monster: IMonster = monsterList[monsterId]; let pos: Laya.Point = monster.getPoint(); // 计算玩家中心点与怪物中心点之间的夹角 let hitAngle = Math.atan2(this.y - pos.y, this.x - pos.x); let halfWidth = this.width * 0.5; let halfHeight = this.height * 0.5; // 玩家中心点与怪物中心点之间的连线与玩家的边框矩形相交点(武器转圈, 击中怪物的点) let hitPoint = new Laya.Point(); hitPoint.x = this.x - halfWidth * Math.cos(hitAngle); hitPoint.y = this.y - halfHeight * Math.sin(hitAngle); if (!monster.isDie) { if (monster.getHitRect().contains(hitPoint.x, hitPoint.y)) { // if (this.hitTestPoint(hitPoint.x, hitPoint.y)) { monsterIds.push(monster.id); //所有的的被攻击到的id //播放受击动画什么的 this._monsterCtrl.hitMonster(monster.id, this, hitPoint, hitAngle); } else { //停止播放受击动画 this._monsterCtrl.outHitMonster(monster.id); } } } } // 如果是实心圆圈的符文, 只要使用符文hitTestPoint(怪物坐标)来检测 // 如果是激光, 一根或多根时, 可以额外放对应数量的条状图对应相应的激活, 条隐藏, 不显示(如果激光旋转, 可以在onframe里面把条跟随旋转), 然后使用这些条来检测与怪物的碰撞 // 子弹类就是每帧检测判断有没击中怪 }
浙公网安备 33010602011771号