RemoteControl.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. var o0 = require('o0');
  2. var o0CC = require('o0CC');
  3. var o0Game = require('o0Game');
  4. cc.Class({
  5. extends: require('LocalControl'),
  6. properties: {
  7. remotePosition:null,
  8. },
  9. update: function (dt) {
  10. if(this.node == null || this.node.isValid == false){
  11. cc.log("Memory leak: Control");
  12. }
  13. if(this.isValid == false || this.node == null || this.node.isValid == false || this.snake == null){
  14. return;
  15. }
  16. this.updateBodySpacing();
  17. this.updateSpeed();
  18. //for(var t = 0;t<this.fixedTimer.fixedUpdateTimes(dt);++t){
  19. for(var t = 0;t<1;++t){
  20. var speedRate;
  21. if(this.snake.score <= this.minScore)
  22. speedRate = this.normalSpeedRate;
  23. else{
  24. speedRate = this.targetSpeedRate;
  25. }
  26. for(var i = 0;i<speedRate;++i){
  27. this.targetRotation = o0CC.rotationFromVector(this.targetVector);
  28. this.snake.head.rotation = o0CC.nextRotation(this.snake.head.rotation,this.targetRotation,this.turningSpeed);
  29. if(this.remotePosition!=null){
  30. //this.snake.head.x = this.remotePosition.x;
  31. //this.snake.head.y = this.remotePosition.y;
  32. var maximumSynchronizationDistance = this.speed*1.1;
  33. var targetVector = this.remotePosition.minus(new o0.Vector2(this.snake.head.x,this.snake.head.y));
  34. if(targetVector.length<=maximumSynchronizationDistance){
  35. this.snake.head.x = this.remotePosition.x;
  36. this.snake.head.y = this.remotePosition.y;
  37. }else{
  38. targetVector = targetVector.toLength(maximumSynchronizationDistance+(targetVector.length-maximumSynchronizationDistance)*0.2);
  39. this.snake.head.x += targetVector.x;
  40. this.snake.head.y += targetVector.y;
  41. }
  42. }
  43. this.updateSnakeBody();
  44. }
  45. }
  46. this.pulsatingFeedback();
  47. },
  48. onCollisionEnter:function(other,self){
  49. },
  50. onCollisionExit:function(other,self){
  51. },//必须覆盖为空白
  52. });