import Role from "./Role"; const {ccclass, property} = cc._decorator; @ccclass export default class Bot extends cc.Component { role:Role; target:any; onLoad(){ this.role = this.node.getComponent(Role); this.schedule(this.updateOperate,0.5); } updateOperate(){ if(this.role.saw.active){//when has saw, target a nearest other role let distance = 10000; window.controller.roles.forEach((role:Role)=>{ if(role!=this.role&&role.isValid){ let newDistance = this.role.node.position.sub(role.node.position).mag(); if(newDistance{ if(saw.isValid){ let newDistance = this.role.node.position.sub(saw.position).mag(); if(newDistance{ this.role.up = false; },0.1); } randomMove(){ if(Math.random()<0.5){ this.left(); }else{ this.right(); } if(Math.random()<0.5){ this.jump(); } } moveToTarget(){ let targetX = 0; let targetY = 0; if(this.target instanceof Role){ targetX = this.target.node.x; targetY = this.target.node.y; }else if(this.target instanceof cc.Node){ targetX = this.target.x; targetY = this.target.y; } if(targetXthis.role.node.x){ this.right(); } if(targetY - this.role.node.y > 100){ this.jump(); } } leaveTarget(){ let targetX = 0; let targetY = 0; if(this.target instanceof Role){ targetX = this.target.node.x; targetY = this.target.node.y; }else if(this.target instanceof cc.Node){ targetX = this.target.x; targetY = this.target.y; } if(targetXthis.role.node.x){ this.right(); } if(targetY - this.role.node.y > -100){ this.jump(); } } }