SnakeAIInput.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. var o0 = require('o0');
  2. var o0CC = require('o0CC');
  3. var o0Game = require('o0Game');
  4. cc.Class({
  5. extends: require('SnakePositionInput'),
  6. properties: {
  7. viewRate:10,
  8. patientTimer:null,
  9. targetFoodNode:null,
  10. },
  11. // use this for initialization
  12. onLoad: function () {
  13. this._super();
  14. this.setControllByPosition(true);
  15. this.patientTimer = new o0.Timer(3);
  16. },
  17. update: function (dt) {
  18. if(this.control == null || this.control.snake == null){
  19. return;
  20. }
  21. this.radius = this.control.snake.radius*this.viewRate;
  22. //可以修改的部分
  23. //this.setTargetSpeeding(false);//设置是否加速
  24. //this.setTargetVector();//设置目标方向
  25. if(this.targetFoodNode == null || this.patientTimer.tryNextTiming()){
  26. //this.setControllByPosition(false);
  27. this.setTargetPosition(new o0.Vector2(0,0));
  28. this.targetFoodNode = null;
  29. }
  30. this._super();
  31. },
  32. onCollisionStay:function(other,self){
  33. if(other.node == null || other.node.active == false || this.node == null || this.node.active == false){//貌似能解决两蛇对撞导致卡死的bug
  34. return;
  35. }
  36. if(this.targetFoodNode == null && other.node.groupIndex == o0Game.GroupIndex.Food){
  37. //cc.log('dsadad');
  38. this.targetFoodNode = other.node;
  39. this.setTargetPosition(new o0.Vector2(this.targetFoodNode));
  40. //this.setControllByPosition(true);
  41. this.patientTimer.nextTiming();
  42. }/* */
  43. },
  44. });