controller.js 4.8 KB

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