controller.js 5.2 KB

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