controller.js 7.7 KB

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