controller.js 8.5 KB

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