LocalControl.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. var o0 = require('o0');
  2. var o0CC = require('o0CC');
  3. var o0Game = require('o0Game');
  4. cc.Class({
  5. extends: cc.CircleCollider,
  6. properties: {
  7. speed: 1,//per frame
  8. turningSpeed: 6,//degree per frame
  9. normalSpeedRate: 1,//加速时的速度比率
  10. speedingSpeedRate: 3,//加速时的速度比率
  11. targetSpeedRate: 1,//当前速度比率
  12. isSpeeding: {
  13. get: function (){
  14. return this.targetSpeedRate != this.normalSpeedRate;
  15. }
  16. },
  17. targetVector:null,
  18. bodySpacingRate: 0.4,//rate of size
  19. bodySpacing: 0,
  20. pulledScore: 0,
  21. pullScoreThreshold: {
  22. get: function (){
  23. return Math.pow(this.snake.body[0].scaleX * this.snake.body[0].scaleY,1) * 2;
  24. }
  25. },
  26. pullScoreRate:0.7,//the rate means remove 1 score from snake, and 0.5 score will come out to be food.
  27. speedingScoreCost: 0.1,//score cost per frame speeding
  28. minScore: 5,
  29. snake:null,
  30. fixedTimer:null,
  31. },
  32. updateSpeed:function(){
  33. this.speed = 5 * Math.pow(this.snake.body[0].scaleX * this.snake.body[0].scaleY,1.0/3);
  34. },
  35. updateBodySpacing:function(){
  36. this.bodySpacing = this.radius*2 * this.bodySpacingRate * Math.sqrt(this.snake.body[0].scaleX * this.snake.body[0].scaleY);
  37. },
  38. setTargetSpeeding:function(bool){
  39. if(!bool)
  40. this.targetSpeedRate = this.normalSpeedRate;
  41. else
  42. this.targetSpeedRate = this.speedingSpeedRate;
  43. },
  44. setTargetVector:function(vector){
  45. this.targetVector = vector;
  46. },
  47. removeTargetVector:function(){
  48. this.setTargetVector(o0CC.vectorFromRotation(this.snake.head.rotation));
  49. },
  50. addScore: function (score) {
  51. if(this.snake == null){
  52. return;
  53. }
  54. this.snake.setScore(this.snake.score + score);
  55. //this.snake.score += score;
  56. //this.snake.updateScale();
  57. this.updateBodySpacing();
  58. this.updateSpeed();
  59. /*var targetBodyLength = Math.ceil(this.snake.bodyLength);
  60. while(targetBodyLength > this.snake.body.length){
  61. this.snake.addBody();
  62. }/** */
  63. },
  64. pullFood: function (score) {
  65. var newFood = o0CC.addScriptNode(this.snake.gameScene.foodNode,'Food',-100);
  66. var lastBody = this.snake.body[this.snake.body.length-1];
  67. var foodPosition = o0.randomInCircle2(lastBody,(this.radius-newFood.radius)*2);
  68. newFood.node.x = foodPosition.x;
  69. newFood.node.y = foodPosition.y;
  70. newFood.score = score;
  71. newFood.node.name = ''+(this.snake.gameScene.foodIDcounter++);
  72. },
  73. pullAllFood: function () {
  74. var pullScoreThreshold = this.pullScoreThreshold;
  75. while(this.snake.score >= 1){
  76. this.pullScore(1,pullScoreThreshold);
  77. }
  78. },
  79. pullScore:function(score){
  80. this.snake.setScore(this.snake.score - score);
  81. //this.snake.score -= score;
  82. //this.snake.updateScale();
  83. this.updateBodySpacing();
  84. this.updateSpeed();
  85. this.pulledScore += score * this.pullScoreRate;
  86. var pullScoreThreshold;
  87. if(arguments.length < 2){
  88. pullScoreThreshold = this.pullScoreThreshold;
  89. }else{
  90. pullScoreThreshold = arguments[1];
  91. }
  92. if(this.pulledScore >= pullScoreThreshold){
  93. this.pulledScore -= pullScoreThreshold;
  94. this.pullFood(pullScoreThreshold);
  95. }/*
  96. var targetBodyLength = Math.ceil(this.snake.bodyLength);
  97. while(targetBodyLength < this.snake.body.length && this.snake.body.length > 1){
  98. this.snake.removeBody();
  99. }/** */
  100. },
  101. updateSnakeBody:function(){
  102. //cc.log(this.getComponent('Snake').body.length);
  103. for(var i = 0, j = 1;j<this.snake.body.length;++i, ++j){
  104. var targetVector = new o0.Vector2(this.snake.body[i].x-this.snake.body[j].x,this.snake.body[i].y-this.snake.body[j].y);
  105. var moveDistance = targetVector.length - this.bodySpacing;
  106. if(j==this.snake.body.length-1)
  107. moveDistance += this.bodySpacing * (Math.ceil(this.snake.bodyLength)-this.snake.bodyLength)
  108. targetVector = targetVector.mod;
  109. this.snake.body[j].rotation = o0CC.rotationFromVector(targetVector);
  110. if(moveDistance <= 0)
  111. continue;
  112. this.snake.body[j].x += targetVector.x * moveDistance;
  113. this.snake.body[j].y += targetVector.y * moveDistance;/** */
  114. }
  115. },/** */
  116. /*
  117. destroySnake: function(){
  118. this.node.active = false;//防止destory之前body再增长
  119. while(this.snake.score >= 1){
  120. this.pullScore(1);
  121. }
  122. while(this.snake.body.length > 1){
  123. this.removeBody();
  124. }
  125. //cc.log(this.snake.score);
  126. //this.destroy();
  127. },/** */
  128. pulsatingFeedback:function(){
  129. if(this.isSpeeding && this.snake.score > this.minScore){
  130. for(var i =0;i<this.snake.body.length;++i){
  131. var graphics = this.snake.body[i].getComponent(cc.Graphics);
  132. if(graphics==null){
  133. return;
  134. }
  135. graphics.clear();
  136. var timeRemainder = Math.abs(((new Date()).getTime()/10 - i*2)%40-20);
  137. for(var j = 0;j<timeRemainder;++j){
  138. graphics.circle(0, 0, this.radius+j);
  139. graphics.strokeColor = new cc.Color(255,255,255,(timeRemainder-j)*5);
  140. graphics.stroke();
  141. }
  142. }
  143. }else{
  144. for(var i =0;i<this.snake.body.length;++i){
  145. var graphics = this.snake.body[i].getComponent(cc.Graphics);
  146. if(graphics==null){
  147. return;
  148. }
  149. graphics.clear();
  150. }
  151. }
  152. },
  153. // use this for initialization
  154. onLoad: function () {
  155. this.snake = this.node.parent.getComponent('Snake');
  156. this.radius = this.snake.size/2;
  157. this.removeTargetVector();
  158. this.snake.updateScale();
  159. this.updateBodySpacing();
  160. this.updateSpeed();
  161. },
  162. start: function () {
  163. this.addScore(10);
  164. //cc.log(this.snake.body.length);
  165. this.fixedTimer = new o0.FixedTimerDT(60);
  166. },
  167. // called every frame
  168. update: function (dt) {
  169. if(this.node == null || this.node.isValid == false){
  170. cc.log("Memory leak: Control");
  171. }
  172. if(this.isValid == false || this.node == null || this.node.isValid == false || this.snake == null){
  173. return;
  174. }
  175. //for(var t = 0;t<this.fixedTimer.fixedUpdateTimes(dt);++t){
  176. for(var t = 0;t<1;++t){
  177. var speedRate;
  178. if(this.snake.score <= this.minScore)
  179. speedRate = this.normalSpeedRate;
  180. else{
  181. speedRate = this.targetSpeedRate;
  182. if(speedRate != this.normalSpeedRate && speedRate == this.speedingSpeedRate)
  183. this.pullScore(this.speedingScoreCost);
  184. }
  185. for(var i = 0;i<speedRate;++i){
  186. //移动蛇头
  187. this.targetRotation = o0CC.rotationFromVector(this.targetVector);
  188. this.snake.head.rotation = o0CC.nextRotation(this.snake.head.rotation,this.targetRotation,this.turningSpeed);
  189. var moveVector = o0CC.vectorFromRotation(this.snake.head.rotation);
  190. this.snake.body[0].x += moveVector.x*this.speed;
  191. this.snake.body[0].y += moveVector.y*this.speed;/** */
  192. //////////////下面是移动蛇身
  193. this.updateSnakeBody();
  194. }
  195. }
  196. this.pulsatingFeedback();
  197. },
  198. onCollisionEnter:function(other,self){
  199. if(other.node == null || other.node.active == false || this.node == null || this.node.active == false){//貌似能解决两蛇对撞导致卡死的bug
  200. return;
  201. }
  202. if(self != this){
  203. return;
  204. }
  205. if(other.node.groupIndex == o0Game.GroupIndex.Body){
  206. //cc.log("aaaaaaaaaaaaaaaaaaa");
  207. var selfBody = false;
  208. if(this.snake.body.length > 1)
  209. for(var i = 1; i<this.snake.body.length;++i){
  210. if(other.node == this.snake.body[i]){
  211. selfBody = true;
  212. break;
  213. }
  214. }
  215. if(selfBody == false){
  216. this.pullAllFood();
  217. this.snake.node.destroy();
  218. }
  219. }
  220. if(other.node.groupIndex == o0Game.GroupIndex.Food){
  221. this.addScore(other.score);
  222. other.node.destroy();
  223. //other.destroy();
  224. }/* */
  225. },
  226. onCollisionExit:function(other,self){
  227. if(other.node == null || other.node.active == false || this.node == null || this.node.active == false){//貌似能解决两蛇对撞导致卡死的bug
  228. return;
  229. }
  230. if(self != this){
  231. return;
  232. }
  233. if(other.node.groupIndex == o0Game.GroupIndex.PlayGround){
  234. //this.o0Destroy();
  235. this.pullAllFood();
  236. this.snake.node.destroy();
  237. }/* */
  238. },
  239. /*
  240. o0Destroy:function(){
  241. this.node.active = false;
  242. //this.node.removeComponent(this);
  243. this.destroy();
  244. this.node.destroy();
  245. },/** */
  246. _onPreDestroy:function(){
  247. },
  248. });