controller.js 4.9 KB

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