controller.js 8.5 KB

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