Snake.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. //bodyParentNode: null,
  8. gameScene: null,
  9. head:null,
  10. body:[cc.Node],
  11. size:40,//大小,直径
  12. //bodySize: this.radius 子类CircleCollider的变量
  13. score: 0,
  14. color:null,
  15. bodyLength: {
  16. get: function (){
  17. return this.score / Math.pow(this.body[0].scaleX * this.body[0].scaleY,1.5);
  18. }
  19. },
  20. skin:null,
  21. nameLabel:null,
  22. },
  23. setScore:function(score){
  24. this.score = score;
  25. this.updateScale();
  26. var targetBodyLength = Math.ceil(this.bodyLength);
  27. while(targetBodyLength > this.body.length){
  28. this.addBody();
  29. }
  30. while(targetBodyLength < this.body.length && this.body.length > 1){
  31. this.removeBody();
  32. }
  33. },
  34. updateScale:function(){
  35. var newScale = Math.pow(1+(this.score - 10)/50,1.0/3);
  36. for(var i=0;i<this.body.length;++i){
  37. this.body[i].scaleX = newScale;
  38. this.body[i].scaleY = newScale;
  39. }
  40. },
  41. addEyeGraphic:function(node){
  42. var graphics = node.addComponent(cc.Graphics,10);
  43. graphics.clear();
  44. graphics.circle(12, 10, 10);
  45. graphics.circle(12, -10, -10);
  46. graphics.fillColor = cc.Color.WHITE;
  47. graphics.fill();
  48. graphics.circle(13, 9, 6);
  49. graphics.circle(13, -9, 6);
  50. graphics.fillColor = cc.Color.BLACK;
  51. graphics.fill();
  52. return graphics;
  53. },
  54. addGraphic:function(node){
  55. var graphics = node.addComponent(cc.Graphics);
  56. graphics.clear();
  57. graphics.circle(0, 0, this.radius);
  58. graphics.fillColor = cc.Color.YELLOW;
  59. graphics.fill();
  60. graphics.circle(15, 0, 5);
  61. graphics.fillColor = cc.Color.RED;
  62. graphics.fill();
  63. return graphics;
  64. },
  65. addGraphic2:function(node){
  66. var graphics = node.addComponent(cc.Graphics);
  67. graphics.clear();
  68. graphics.circle(0, 0, this.radius);
  69. graphics.fillColor = cc.Color.YELLOW;
  70. graphics.fill();
  71. graphics.circle(-15, 0, 5);
  72. graphics.fillColor = cc.Color.GRAY;
  73. graphics.fill();
  74. },
  75. addHead:function(){
  76. this.head = new cc.Node('head');
  77. this.body.push(this.head);
  78. this.head.parent = this.node;
  79. this.head.rotation = Math.random() * 360;
  80. //Graphic.
  81. var eyeNode = new cc.Node();
  82. eyeNode.parent = this.head;
  83. eyeNode.zIndex = 3;
  84. var Graphic = this.addEyeGraphic(eyeNode);
  85. this.nameLabel = o0CC.addScriptNode(this.head,'cc.Label',20);
  86. this.nameLabel.horizontalAlign = cc.Label.HorizontalAlign.CENTER;
  87. this.nameLabel.verticalAlign = cc.Label.VerticalAlign.TOP;
  88. this.nameLabel.node.color = new cc.Color(255,255,255);
  89. this.nameLabel.node.anchorX = 0;
  90. this.nameLabel.node.anchorY = 0;
  91. this.nameLabel.node.x = 0;
  92. this.nameLabel.node.y = 0;
  93. this.nameLabel.node.height = 300;
  94. this.nameLabel.fontSize = 20;
  95. this.nameLabel.node.anchorX = 0.5;
  96. this.nameLabel.node.anchorY = 0.5;
  97. var graphics = this.head.addComponent('cc.Graphics');
  98. o0CC.setGroup(graphics,o0Game.GroupIndex.Head);
  99. },
  100. addBody:function(){
  101. if(this.node.active == false)
  102. return;
  103. var i = this.body.length;
  104. this.body.push(new cc.Node('body'+i));
  105. this.node.addChild(this.body[i],-i);
  106. this.body[i].x = this.body[i-1].x;
  107. this.body[i].y = this.body[i-1].y;
  108. this.body[i].rotation = this.body[i-1].rotation;
  109. var collider = this.body[i].addComponent('cc.CircleCollider');
  110. //collider.tag = o0Game.CollisionTag.Body;
  111. collider.radius = this.radius;
  112. var sprite = o0CC.addScriptNode(this.body[this.body.length-1],'cc.Sprite',2);
  113. //var sprite = this.body[this.body.length-1].addComponent(cc.Sprite,0);
  114. sprite.trim = true;
  115. sprite.type = cc.Sprite.Type.SLICED;
  116. sprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
  117. sprite.SrcBlendFactor = cc.macro.BlendFactor.SRC_ALPHA;
  118. sprite.DstBlendFactor = cc.macro.BlendFactor.ONE_MINUS_SRC_ALPHA;
  119. sprite.node.width = this.size;
  120. sprite.node.height = this.size;
  121. cc.loader.loadRes('snake/'+this.skin[(this.body.length-1)%this.skin.length], cc.SpriteFrame, function (err, spriteFrame) {
  122. sprite.spriteFrame = spriteFrame;
  123. });
  124. var graphics = this.body[i].addComponent('cc.Graphics');
  125. o0CC.setGroup(graphics,o0Game.GroupIndex.Body);
  126. },
  127. removeBody:function(){
  128. if(this.body.length == 1)
  129. return;
  130. //this.body[this.body.length-1].getComponent('cc.CircleCollider').destroy();
  131. this.body[this.body.length-1].destroy();
  132. this.body.pop();
  133. },
  134. removeAllBody:function(){
  135. for(;this.body.length>1;){
  136. this.removeBody();
  137. }
  138. },
  139. // use this for initialization
  140. onLoad: function () {
  141. this.radius = 20;//圆碰撞体的局部变量
  142. this.color = o0CC.randomBrightColor();
  143. this.addHead();
  144. //cc.log('snake log');
  145. },
  146. start: function () {
  147. var sprite = o0CC.addScriptNode(this.head,cc.Sprite,1);
  148. //var sprite = this.head.addComponent(cc.Sprite,0);
  149. sprite.trim = true;
  150. sprite.type = cc.Sprite.Type.SLICED;
  151. sprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
  152. sprite.SrcBlendFactor = cc.macro.BlendFactor.SRC_ALPHA;
  153. sprite.DstBlendFactor = cc.macro.BlendFactor.ONE_MINUS_SRC_ALPHA;
  154. sprite.node.width = this.size;
  155. sprite.node.height = this.size;
  156. //var sprite = this.body[0].getComponent(cc.Sprite);
  157. cc.loader.loadRes('snake/'+this.skin[0], cc.SpriteFrame, function (err, spriteFrame) {
  158. sprite.spriteFrame = spriteFrame;
  159. });
  160. },
  161. lateUpdate:function(){
  162. if(this.nameLabel!=null){
  163. if(this.nameLabel.string==null||this.nameLabel.string==''){
  164. this.nameLabel.string = this.name+'\n\n';/** */
  165. }
  166. this.nameLabel.node.rotation = -this.head.rotation;
  167. }
  168. },
  169. /*
  170. // called every frame
  171. update: function (dt) {
  172. },
  173. onCollisionEnter:function(other,self){
  174. },
  175. onCollisionExit:function(other,self){
  176. },/** */
  177. _onPreDestroy:function(){
  178. //cc.log('dsada');
  179. /*
  180. while(this.body.length > 1){
  181. this.removeBody();
  182. }/** */
  183. },/*
  184. test:function(){
  185. cc.log('snake');
  186. },/** */
  187. });
  188. //module.exports = Snake;