controller.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 higgs = new Particle(pos0);
  12. higgs.mass = 125;
  13. higgs.type = "Higgs"
  14. var electron = new Particle(pos0);
  15. electron.mass = .0005;
  16. electron.type = "electron";
  17. higgs.draw_properties = {
  18. colors: ["hsl(120, 100%, 50%)", "hsl(120, 40%, 50%)"],
  19. ratios: [0, 1],
  20. inner_radius: .03,
  21. outer_radius: .25,
  22. inner_center: {x: .07, y: .07},
  23. outer_center: {x: 0, y: 0}
  24. };
  25. this.possible_collectibles = [
  26. {collectible: higgs, probability: .5},
  27. {collectible: electron, probability: .5}
  28. ]
  29. this.stage = new createjs.Stage("demoCanvas");
  30. }
  31. Controller.prototype.update_views = function(){
  32. for(var view in this.views){
  33. this.views[view].update();
  34. }
  35. }
  36. Controller.prototype.spawn_collectibles = function(){
  37. var collectible = get_random_element_with_probabilities(this.possible_collectibles);
  38. if (!collectible) return;
  39. collectible = Object.create(collectible.collectible);
  40. var rnd_pos = this.get_random_position();
  41. if (! this.is_position_occupied(rnd_pos)) {
  42. collectible.position = rnd_pos;
  43. this.collectibles.push(collectible);
  44. this.add_view(new ParticleView(collectible));
  45. }
  46. }
  47. Controller.prototype.start_game = function(){
  48. // this.session = new Session();
  49. var c = this;
  50. createjs.Ticker.on("tick", function(e){c.tick(e);});
  51. createjs.Ticker.timingMode = createjs.Ticker.RAF;
  52. this.bind_events();
  53. this.time = 0;
  54. this.score = 0;
  55. for(phModel in controller.snake.physicists){
  56. var model = controller.snake.physicists[phModel];
  57. var phView = new PhysicistView(model);
  58. this.add_view(phView);
  59. }
  60. }
  61. Controller.prototype.add_view = function(view){
  62. this.stage.addChild(view);
  63. this.views.push(view);
  64. }
  65. Controller.prototype.bind_events = function(){
  66. var c = this;
  67. window.onkeydown = function(e){
  68. var direction = null;
  69. var dir = {x: c.snake.physicists[0].direction.x, y: c.snake.physicists[0].direction.y};
  70. switch (e.keyCode){
  71. case 37:
  72. case 65:
  73. if(dir.x == 1)
  74. {
  75. break;}
  76. else {
  77. direction = {x: -1, y: 0};
  78. break;
  79. }
  80. case 38:
  81. case 87:
  82. if(dir.y == 1)
  83. {
  84. break;}
  85. else {
  86. direction = {x: 0, y: -1};
  87. break;
  88. }
  89. case 39:
  90. case 68:
  91. if(dir.x == -1)
  92. {
  93. break;}
  94. else {
  95. direction = {x: 1, y: 0};
  96. break;
  97. }
  98. case 40:
  99. case 83:
  100. if(dir.y == -1)
  101. {
  102. break;}
  103. else {
  104. direction = {x: 0, y: 1};
  105. break;
  106. }
  107. }
  108. if (direction && c.canTurn){
  109. c.turn_snake(direction);
  110. c.canTurn = false;
  111. }
  112. }
  113. }
  114. Controller.prototype.turn_snake = function(direction){
  115. this.snake.physicists[0].direction = direction;
  116. }
  117. Controller.prototype.tick = function(event){
  118. if(event.paused) return;
  119. if(event.time - this.time > this.time_step){
  120. this.time = event.time;
  121. var next_cell = this.get_next_cell_position();
  122. var next_cell_content = this.is_position_occupied(next_cell);
  123. this.snake.move(next_cell);
  124. this.canTurn = true;
  125. if (next_cell_content && next_cell_content.collectible) this.snake.physicists[0].collect(next_cell_content.collectible);
  126. if(this.collectibles.length < this.maximum_spawns){
  127. this.spawn_collectibles();
  128. }
  129. this.update_views();
  130. }
  131. this.stage.update(event);
  132. }
  133. Controller.prototype.get_next_cell_position = function(){
  134. var ph0 = this.snake.physicists[0];
  135. var next_cell = Object.create(ph0.position);
  136. next_cell.x += ph0.direction.x;
  137. next_cell.y += ph0.direction.y;
  138. if (next_cell.x < 0) next_cell.x = this.grid_size.x - 1;
  139. if (next_cell.y < 0) next_cell.y = this.grid_size.y - 1;
  140. if (next_cell.x == this.grid_size.x) next_cell.x = 0;
  141. if (next_cell.y == this.grid_size.y) next_cell.y = 0;
  142. return next_cell;
  143. }
  144. Controller.prototype.get_random_position = function(){
  145. return {x: Math.floor(Math.random()*this.grid_size.x),
  146. y: Math.floor(Math.random()*this.grid_size.y)
  147. };
  148. }
  149. Controller.prototype.is_position_occupied = function(position){
  150. var phs = this.snake.physicists;
  151. for (var ph in phs){
  152. var pos = phs[ph].position;
  153. if (pos.x == position.x && pos.y == position.y) return {physicist:phs[ph]};
  154. }
  155. for (var c in this.collectibles){
  156. var pos = this.collectibles[c].position;
  157. if (pos.x == position.x && pos.y == position.y) return {collectible:this.collectibles[c]};
  158. }
  159. return null;
  160. }
  161. Controller.prototype.hit_test = function(e, particle){
  162. for (var ph_i in this.snake.physicists){
  163. var ph = this.snake.physicists[ph_i];
  164. if (ph.view.hitTest(e.x, e.y)) {
  165. this.ph.collect(particle);
  166. }
  167. }
  168. }
  169. Controller.prototype.remove_collectible = function(collectible){
  170. var i = this.collectibles.indexOf(collectible);
  171. if (i > -1) {
  172. this.collectibles.splice(i, 1);
  173. }
  174. i = -1;
  175. i = this.views.indexOf(collectible.view);
  176. if (i > -1) {
  177. this.views.splice(i, 1);
  178. }
  179. this.stage.removeChild(collectible.view);
  180. }
  181. var get_random_element_with_probabilities = function(array){
  182. var previous_probability = 0;
  183. var rnd = Math.random();
  184. for (ind in array){
  185. var probability = array[ind].probability;
  186. if (rnd < probability + previous_probability) return array[ind];
  187. previous_probability += probability;
  188. }
  189. return null;
  190. }