controller.js 7.0 KB

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