controller.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. var Controller = function(){
  2. this.grid_size = {x: 20, y: 20};
  3. this.initial_length = 3;
  4. this.time_step = 400;
  5. this.maximum_spawns = 100;
  6. this.collectibles = [];
  7. this.views = [];
  8. this.snake = new Snake(this.initial_length);
  9. this.canTurn = true;
  10. var pos0 = {x: -1, y: -1};
  11. var pp = possible_particles();
  12. this.possible_collectibles = [];
  13. for (var p in pp){
  14. this.possible_collectibles.push({collectible: pp[p], probability: pp[p].appearence_probabilty});
  15. }
  16. createjs.DisplayObject.suppressCrossDomainErrors = true;
  17. this.stage = new createjs.Stage("demoCanvas");
  18. }
  19. Controller.prototype.update_views = function(){
  20. for(var view in this.views){
  21. this.views[view].update();
  22. }
  23. }
  24. Controller.prototype.spawn_collectibles = function(){
  25. var collectible = get_random_element_with_probabilities(this.possible_collectibles);
  26. if (!collectible) return;
  27. collectible = Object.create(collectible.collectible);
  28. var rnd_pos = this.get_random_position();
  29. if (!this.is_position_occupied(rnd_pos)) {
  30. this.add_collectible(collectible, rnd_pos);
  31. }
  32. }
  33. Controller.prototype.add_collectible = function(collectible, position){
  34. collectible.position = position;
  35. if(collectible.halflife_time > 0){
  36. collectible.decay_time = createjs.Ticker.getTime() + collectible.halflife_time;
  37. }
  38. this.collectibles.push(collectible);
  39. this.add_view(new ParticleView(collectible));
  40. }
  41. Controller.prototype.start_game = function(){
  42. // this.session = new Session();
  43. var c = this;
  44. createjs.Ticker.on("tick", function(e){c.tick(e);});
  45. createjs.Ticker.timingMode = createjs.Ticker.RAF;
  46. this.bind_events();
  47. this.time = 0;
  48. this.score = 0;
  49. for(phModel in controller.snake.physicists){
  50. var model = controller.snake.physicists[phModel];
  51. var phView = new PhysicistView(model);
  52. this.add_view(phView);
  53. }
  54. }
  55. Controller.prototype.add_view = function(view){
  56. this.stage.addChild(view);
  57. this.views.push(view);
  58. }
  59. Controller.prototype.bind_events = function(){
  60. var c = this;
  61. window.onkeydown = function(e){
  62. var direction = null;
  63. var dir = {x: c.snake.physicists[0].direction.x, y: c.snake.physicists[0].direction.y};
  64. switch (e.keyCode){
  65. case 37:
  66. case 65:
  67. if(dir.x == 1)
  68. {
  69. break;}
  70. else {
  71. direction = {x: -1, y: 0};
  72. break;
  73. }
  74. case 38:
  75. case 87:
  76. if(dir.y == 1)
  77. {
  78. break;}
  79. else {
  80. direction = {x: 0, y: -1};
  81. break;
  82. }
  83. case 39:
  84. case 68:
  85. if(dir.x == -1)
  86. {
  87. break;}
  88. else {
  89. direction = {x: 1, y: 0};
  90. break;
  91. }
  92. case 40:
  93. case 83:
  94. if(dir.y == -1)
  95. {
  96. break;}
  97. else {
  98. direction = {x: 0, y: 1};
  99. break;
  100. }
  101. }
  102. if (direction && c.canTurn){
  103. c.turn_snake(direction);
  104. c.canTurn = false;
  105. }
  106. }
  107. }
  108. Controller.prototype.turn_snake = function(direction){
  109. this.snake.physicists[0].direction = direction;
  110. }
  111. Controller.prototype.tick = function(event){
  112. if(event.paused) return;
  113. if(event.time - this.time > this.time_step){
  114. this.time = event.time;
  115. var next_cell = this.get_next_cell_position();
  116. var next_cell_content = this.is_position_occupied(next_cell);
  117. this.snake.move(next_cell);
  118. this.canTurn = true;
  119. if (next_cell_content && next_cell_content.collectible) this.snake.physicists[0].collect(next_cell_content.collectible);
  120. if(this.collectibles.length < this.maximum_spawns){
  121. this.spawn_collectibles();
  122. }
  123. this.update_views();
  124. }
  125. this.check_decays();
  126. this.stage.update(event);
  127. }
  128. Controller.prototype.check_decays = function(){
  129. for(var pIndex in this.collectibles){
  130. var p = this.collectibles[pIndex];
  131. if(p.decay_time && p.decays && p.decays.length && createjs.Ticker.getTime() > p.decay_time){
  132. var offset = {x: 0, y: 0};
  133. var counter = 0;
  134. for(var daughterInd in p.decays[0]){
  135. if(counter % 2){
  136. offset.x = Math.floor((Math.random() * this.grid_size.x / 10)) % this.grid_size.x;
  137. offset.y = Math.floor((Math.random() * this.grid_size.y / 10)) % this.grid_size.y;
  138. } else {
  139. offset.x = - offset.x;
  140. offset.y = - offset.y;
  141. }
  142. while(this.is_position_occupied(offset)){
  143. offset.x++;
  144. offset.y++;
  145. }
  146. var daughter = new Particle(p.position);
  147. daughter.start_time = createjs.Ticker.getTime();
  148. daughter.target = {
  149. time: daughter.start_time + 500,
  150. x: daughter.position.x + offset.x,
  151. y: daughter.position.y + offset.y
  152. }
  153. daughter.decays = null;
  154. this.add_collectible(daughter, p.position);
  155. counter++;
  156. }
  157. this.remove_collectible(p);
  158. }
  159. }
  160. }
  161. Controller.prototype.get_next_cell_position = function(){
  162. var ph0 = this.snake.physicists[0];
  163. var next_cell = Object.create(ph0.position);
  164. next_cell.x += ph0.direction.x;
  165. next_cell.y += ph0.direction.y;
  166. if (next_cell.x < 0) next_cell.x = this.grid_size.x - 1;
  167. if (next_cell.y < 0) next_cell.y = this.grid_size.y - 1;
  168. if (next_cell.x == this.grid_size.x) next_cell.x = 0;
  169. if (next_cell.y == this.grid_size.y) next_cell.y = 0;
  170. return next_cell;
  171. }
  172. Controller.prototype.get_random_position = function(){
  173. return {x: Math.floor(Math.random()*this.grid_size.x),
  174. y: Math.floor(Math.random()*this.grid_size.y)
  175. };
  176. }
  177. Controller.prototype.is_position_occupied = function(position){
  178. var phs = this.snake.physicists;
  179. for (var ph in phs){
  180. var pos = phs[ph].position;
  181. if (pos.x == position.x && pos.y == position.y) return {physicist:phs[ph]};
  182. }
  183. for (var c in this.collectibles){
  184. var pos = this.collectibles[c].position;
  185. if (pos.x == position.x && pos.y == position.y) return {collectible:this.collectibles[c]};
  186. }
  187. return null;
  188. }
  189. Controller.prototype.hit_test = function(particle){
  190. for (var ph_i in this.snake.physicists){
  191. var ph = this.snake.physicists[ph_i];
  192. if (ph.view.hitTest(particle.position.x, particle.position.y)) {
  193. this.ph.collect(particle);
  194. }
  195. }
  196. }
  197. Controller.prototype.remove_collectible = function(collectible){
  198. var i = this.collectibles.indexOf(collectible);
  199. if (i > -1) {
  200. this.collectibles.splice(i, 1);
  201. }
  202. i = -1;
  203. i = this.views.indexOf(collectible.view);
  204. if (i > -1) {
  205. this.views.splice(i, 1);
  206. }
  207. this.stage.removeChild(collectible.view);
  208. }
  209. var get_random_element_with_probabilities = function(array){
  210. var previous_probability = 0;
  211. var rnd = Math.random();
  212. for (ind in array){
  213. var probability = array[ind].probability;
  214. if (rnd < probability + previous_probability) return array[ind];
  215. previous_probability += probability;
  216. }
  217. return null;
  218. }