Bot.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import Boy from "./Boy";
  2. const {ccclass, property} = cc._decorator;
  3. @ccclass
  4. export default class Bot extends cc.Component {
  5. boy:Boy;
  6. target:Boy;
  7. onLoad(){
  8. this.boy = this.node.getComponent(Boy);
  9. this.schedule(this.updateOperate,0.8-0.6*window.gm.botStrength);
  10. }
  11. updateOperate(){
  12. if(window.gm.isGameOver)return;
  13. //find a nearest target
  14. let distance = 10000;
  15. window.gm.boys.forEach((boy:Boy)=>{
  16. if(boy!=this.boy&&boy.isValid){
  17. let newDistance = this.boy.node.position.sub(boy.node.position).mag();
  18. if(newDistance<distance){
  19. distance = newDistance;
  20. this.target = boy;
  21. }
  22. }
  23. });
  24. //check target whether valid
  25. if(this.target&&this.target.isValid){
  26. let key = Math.floor(Math.random()*10000)%11;
  27. if(key<10*window.gm.botStrength){
  28. this.moveToTarget();
  29. }else{
  30. this.randomMove();
  31. }
  32. }
  33. }
  34. randomMove(){
  35. // console.log("机器人随机移动");
  36. let key = Math.floor(Math.random()*10000)%10;
  37. if(key<3){
  38. this.boy.leftUp();
  39. }else if(key<6){
  40. this.boy.rightUp();
  41. }else if(key<8){
  42. this.boy.up();
  43. }
  44. }
  45. moveToTarget(){
  46. let targetX = this.target.node.x;
  47. let targetY = this.target.node.y;
  48. let targetScale = this.target.body.scaleX;
  49. if(this.node.y<0){
  50. if(this.node.y<targetY+55){
  51. if(targetScale<0){
  52. if(targetX+55>cc.winSize.width/2-30){
  53. targetX -= 55*2;
  54. }
  55. if(this.node.x<targetX+55){
  56. this.boy.rightUp();
  57. }else if(this.node.x>targetX+80){
  58. this.boy.leftUp();
  59. }else{
  60. if(this.node.y<targetY&&this.node.y>targetY-55){
  61. this.boy.leftUp();
  62. }else if(this.node.y<targetY){
  63. this.boy.up();
  64. }
  65. }
  66. }else if(targetScale>0){
  67. if(targetX-55<-cc.winSize.width/2+30){
  68. targetX += 55*2;
  69. }
  70. if(this.node.x>targetX-55){
  71. this.boy.leftUp();
  72. }else if(this.node.x<targetX-80){
  73. this.boy.rightUp();
  74. }else{
  75. if(this.node.y<targetY&&this.node.y>targetY-55){
  76. this.boy.rightUp();
  77. }else if(this.node.y<targetY){
  78. this.boy.up();
  79. }
  80. }
  81. }
  82. }
  83. }else{
  84. if(this.node.y<targetY-55){
  85. if(targetScale<0){
  86. if(this.node.x<targetX+55){
  87. this.boy.rightUp();
  88. }else if(this.node.x>targetX+80){
  89. this.boy.leftUp();
  90. }else{
  91. if(this.node.y<targetY&&this.node.y>targetY-55){
  92. this.boy.leftUp();
  93. }else if(this.node.y<targetY){
  94. this.boy.up();
  95. }
  96. }
  97. }else if(targetScale>0){
  98. if(this.node.x>targetX-55){
  99. this.boy.leftUp();
  100. }else if(this.node.x<targetX-80){
  101. this.boy.rightUp();
  102. }else{
  103. if(this.node.y<targetY&&this.node.y>targetY-55){
  104. this.boy.rightUp();
  105. }else if(this.node.y<targetY){
  106. this.boy.up();
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }