| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- import Boy from "./Boy";
- const {ccclass, property} = cc._decorator;
- @ccclass
- export default class Bot extends cc.Component {
- boy:Boy;
- target:Boy;
- onLoad(){
- this.boy = this.node.getComponent(Boy);
-
- this.schedule(this.updateOperate,0.8-0.6*window.gm.botStrength);
- }
- updateOperate(){
- if(window.gm.isGameOver)return;
- //find a nearest target
- let distance = 10000;
- window.gm.boys.forEach((boy:Boy)=>{
- if(boy!=this.boy&&boy.isValid){
- let newDistance = this.boy.node.position.sub(boy.node.position).mag();
- if(newDistance<distance){
- distance = newDistance;
- this.target = boy;
- }
- }
- });
- //check target whether valid
- if(this.target&&this.target.isValid){
- let key = Math.floor(Math.random()*10000)%11;
- if(key<10*window.gm.botStrength){
- this.moveToTarget();
- }else{
- this.randomMove();
- }
- }
- }
- randomMove(){
- // console.log("机器人随机移动");
- let key = Math.floor(Math.random()*10000)%10;
- if(key<3){
- this.boy.leftUp();
- }else if(key<6){
- this.boy.rightUp();
- }else if(key<8){
- this.boy.up();
- }
- }
- moveToTarget(){
- let targetX = this.target.node.x;
- let targetY = this.target.node.y;
- let targetScale = this.target.body.scaleX;
- if(this.node.y<0){
- if(this.node.y<targetY+55){
- if(targetScale<0){
- if(targetX+55>cc.winSize.width/2-30){
- targetX -= 55*2;
- }
- if(this.node.x<targetX+55){
- this.boy.rightUp();
- }else if(this.node.x>targetX+80){
- this.boy.leftUp();
- }else{
- if(this.node.y<targetY&&this.node.y>targetY-55){
- this.boy.leftUp();
- }else if(this.node.y<targetY){
- this.boy.up();
- }
- }
- }else if(targetScale>0){
- if(targetX-55<-cc.winSize.width/2+30){
- targetX += 55*2;
- }
- if(this.node.x>targetX-55){
- this.boy.leftUp();
- }else if(this.node.x<targetX-80){
- this.boy.rightUp();
- }else{
- if(this.node.y<targetY&&this.node.y>targetY-55){
- this.boy.rightUp();
- }else if(this.node.y<targetY){
- this.boy.up();
- }
- }
- }
- }
- }else{
- if(this.node.y<targetY-55){
- if(targetScale<0){
- if(this.node.x<targetX+55){
- this.boy.rightUp();
- }else if(this.node.x>targetX+80){
- this.boy.leftUp();
- }else{
- if(this.node.y<targetY&&this.node.y>targetY-55){
- this.boy.leftUp();
- }else if(this.node.y<targetY){
- this.boy.up();
- }
- }
- }else if(targetScale>0){
- if(this.node.x>targetX-55){
- this.boy.leftUp();
- }else if(this.node.x<targetX-80){
- this.boy.rightUp();
- }else{
- if(this.node.y<targetY&&this.node.y>targetY-55){
- this.boy.rightUp();
- }else if(this.node.y<targetY){
- this.boy.up();
- }
- }
- }
- }
- }
- }
- }
|