controller.js 5.0 KB

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