controller.js 8.4 KB

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