controller.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 = 2;
  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. collectible.position = rnd_pos;
  46. this.add_collectible(collectible);
  47. }
  48. }
  49. Controller.prototype.add_collectible = function(collectible){
  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 (next_cell_content && next_cell_content.physicist && event.time > 5000) this.game_over();
  145. if(this.collectibles.length < this.maximum_spawns){
  146. this.spawn_collectibles();
  147. }
  148. this.update_views();
  149. }
  150. this.check_decays();
  151. this.stage.update(event);
  152. }
  153. Controller.prototype.check_decays = function(){
  154. for(var pIndex in this.collectibles){
  155. var p = this.collectibles[pIndex];
  156. if(p.decay_time && p.decays && p.decays.length && createjs.Ticker.getTime() > p.decay_time){
  157. var offset = {x: 0, y: 0};
  158. var counter = 0;
  159. for(var daughterInd in p.decays[0]){
  160. if(counter % 2){
  161. offset.x = - offset.x;
  162. offset.y = - offset.y;
  163. } else {
  164. offset.x = Math.floor((Math.random() * this.grid_size.x / 10)) % this.grid_size.x;
  165. offset.y = Math.floor((Math.random() * this.grid_size.y / 10)) % this.grid_size.y;
  166. }
  167. while(this.is_position_occupied(offset)){
  168. offset.x++;
  169. offset.y++;
  170. }
  171. var daughter = new Particle(p.position);
  172. daughter.start_time = createjs.Ticker.getTime();
  173. daughter.target = {
  174. time: daughter.start_time + 500,
  175. x: daughter.position.x + offset.x,
  176. y: daughter.position.y + offset.y
  177. }
  178. daughter.decays = null;
  179. this.add_collectible(daughter);
  180. counter++;
  181. }
  182. this.remove_collectible(p);
  183. }
  184. }
  185. }
  186. Controller.prototype.get_next_cell_position = function(){
  187. var ph0 = this.snake.physicists[0];
  188. var next_cell = Object.create(ph0.position);
  189. next_cell.x += ph0.direction.x;
  190. next_cell.y += ph0.direction.y;
  191. if (next_cell.x < 0) next_cell.x = this.grid_size.x - 1;
  192. if (next_cell.y < 0) next_cell.y = this.grid_size.y - 1;
  193. if (next_cell.x == this.grid_size.x) next_cell.x = 0;
  194. if (next_cell.y == this.grid_size.y) next_cell.y = 0;
  195. return next_cell;
  196. }
  197. Controller.prototype.get_random_position = function(){
  198. return {x: Math.floor(Math.random()*this.grid_size.x),
  199. y: Math.floor(Math.random()*this.grid_size.y)
  200. };
  201. }
  202. Controller.prototype.is_position_occupied = function(position){
  203. var phs = this.snake.physicists;
  204. for (var ph in phs){
  205. var pos = phs[ph].position;
  206. if (pos.x == position.x && pos.y == position.y) return {physicist:phs[ph]};
  207. }
  208. for (var c in this.collectibles){
  209. var pos = this.collectibles[c].position;
  210. if (pos.x == position.x && pos.y == position.y) return {collectible:this.collectibles[c]};
  211. }
  212. return null;
  213. }
  214. Controller.prototype.hit_test = function(particle){
  215. for (var ph_i in this.snake.physicists){
  216. var ph = this.snake.physicists[ph_i];
  217. if (ph.view.hitTest(particle.position.x, particle.position.y)) {
  218. this.ph.collect(particle);
  219. }
  220. }
  221. }
  222. Controller.prototype.remove_collectible = function(collectible){
  223. var i = this.collectibles.indexOf(collectible);
  224. if (i > -1) {
  225. this.collectibles.splice(i, 1);
  226. }
  227. i = -1;
  228. i = this.views.indexOf(collectible.view);
  229. if (i > -1) {
  230. this.views.splice(i, 1);
  231. }
  232. this.stage.removeChild(collectible.view);
  233. }
  234. var get_random_element_with_probabilities = function(array){
  235. var previous_probability = 0;
  236. var rnd = Math.random();
  237. for (ind in array){
  238. var probability = array[ind].probability;
  239. if (rnd < probability + previous_probability) return array[ind];
  240. previous_probability += probability;
  241. }
  242. return null;
  243. }
  244. Controller.prototype.physicists_count = function(){
  245. if(this.counter >= 1) {
  246. this.add_physicist();
  247. this.counter = 0;
  248. }
  249. }
  250. Controller.prototype.game_over = function(){
  251. alert("Game Over!\nScore: " + this.score);
  252. this.snake = new Snake(this.initial_length);
  253. this.canTurn = true;
  254. this.counter = 0;
  255. createjs.Ticker.reset();
  256. createjs.Ticker.init();
  257. for(var view in this.views){
  258. this.stage.removeChild(this.views[view]);
  259. }
  260. for(var collectible in this.collectibles){
  261. this.stage.removeChild(this.collectibles[collectible]);
  262. }
  263. this.collectibles = [];
  264. this.views = [];
  265. this.stage.clear();
  266. this.start_game()// ...
  267. }