controller.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. var pp = possible_particles();
  13. this.possible_collectibles = [];
  14. for (var p in pp){
  15. this.possible_collectibles.push({collectible: pp[p], probability: pp[p].appearence_probabilty});
  16. }
  17. createjs.DisplayObject.suppressCrossDomainErrors = true;
  18. this.stage = new createjs.Stage("demoCanvas");
  19. }
  20. Controller.prototype.update_views = function(){
  21. for(var view in this.views){
  22. this.views[view].update();
  23. }
  24. }
  25. Controller.prototype.spawn_collectibles = function(){
  26. var collectible = get_random_element_with_probabilities(this.possible_collectibles);
  27. if (!collectible) return;
  28. collectible = Object.create(collectible.collectible);
  29. var rnd_pos = this.get_random_position();
  30. if (!this.is_position_occupied(rnd_pos)) {
  31. collectible.position = rnd_pos;
  32. this.add_collectible(collectible);
  33. }
  34. }
  35. Controller.prototype.add_collectible = function(collectible){
  36. if(collectible.half_life > 0){
  37. collectible.decay_time = createjs.Ticker.getTime() + collectible.half_life;
  38. }
  39. this.collectibles.push(collectible);
  40. this.add_view(new ParticleView(collectible));
  41. }
  42. Controller.prototype.add_physicist = function(){
  43. //var new_physicist = Object.create(this.snake.physicists[this.snake.physicists.length - 1]);
  44. var new_physicist = new Physicist(this, {x: 0, y: 0});//Object.create(physicist.physicist);
  45. new_physicist.position = this.snake.physicists[this.snake.physicists.length - 1].position;
  46. new_physicist.direction = this.snake.physicists[this.snake.physicists.length - 1].direction;
  47. this.snake.physicists.push(new_physicist);
  48. this.add_view(new PhysicistView(new_physicist));
  49. }
  50. Controller.prototype.start_game = function(){
  51. // this.session = new Session();
  52. var c = this;
  53. createjs.Ticker.on("tick", function(e){c.tick(e);});
  54. createjs.Ticker.timingMode = createjs.Ticker.RAF;
  55. this.bind_events();
  56. this.time = 0;
  57. this.score = 0;
  58. for(phModel in controller.snake.physicists){
  59. var model = controller.snake.physicists[phModel];
  60. var phView = new PhysicistView(model);
  61. this.add_view(phView);
  62. }
  63. }
  64. Controller.prototype.add_view = function(view){
  65. this.stage.addChild(view);
  66. this.views.push(view);
  67. }
  68. Controller.prototype.bind_events = function(){
  69. var c = this;
  70. window.onkeydown = function(e){
  71. var direction = null;
  72. var dir = {x: c.snake.physicists[0].direction.x, y: c.snake.physicists[0].direction.y};
  73. switch (e.keyCode){
  74. case 37:
  75. case 65:
  76. if(dir.x == 1)
  77. {
  78. break;}
  79. else {
  80. direction = {x: -1, y: 0};
  81. break;
  82. }
  83. case 38:
  84. case 87:
  85. if(dir.y == 1)
  86. {
  87. break;}
  88. else {
  89. direction = {x: 0, y: -1};
  90. break;
  91. }
  92. case 39:
  93. case 68:
  94. if(dir.x == -1)
  95. {
  96. break;}
  97. else {
  98. direction = {x: 1, y: 0};
  99. break;
  100. }
  101. case 40:
  102. case 83:
  103. if(dir.y == -1)
  104. {
  105. break;}
  106. else {
  107. direction = {x: 0, y: 1};
  108. break;
  109. }
  110. }
  111. if (direction && c.canTurn){
  112. c.turn_snake(direction);
  113. c.canTurn = false;
  114. }
  115. }
  116. }
  117. Controller.prototype.turn_snake = function(direction){
  118. this.snake.physicists[0].direction = direction;
  119. }
  120. Controller.prototype.tick = function(event){
  121. if(event.paused) return;
  122. if(event.time - this.time > this.time_step){
  123. this.time = event.time;
  124. var next_cell = this.get_next_cell_position();
  125. var next_cell_content = this.is_position_occupied(next_cell);
  126. this.snake.move(next_cell);
  127. this.canTurn = true;
  128. this.physicists_count();
  129. if (next_cell_content && next_cell_content.collectible) this.snake.physicists[0].collect(next_cell_content.collectible);
  130. if (next_cell_content && next_cell_content.physicist && event.time > 5000) this.game_over();
  131. if(this.collectibles.length < this.maximum_spawns){
  132. this.spawn_collectibles();
  133. }
  134. this.update_views();
  135. }
  136. this.check_decays();
  137. this.stage.update(event);
  138. }
  139. Controller.prototype.check_decays = function(){
  140. for(var pIndex in this.collectibles){
  141. var p = this.collectibles[pIndex];
  142. if(p.decay_time && p.decays && p.decays.length && createjs.Ticker.getTime() > p.decay_time){
  143. var offset = {x: 0, y: 0};
  144. var counter = 0;
  145. var decay = get_random_element_with_probabilities(p.decays);
  146. if (!decay) return;
  147. for(var daughterInd in decay.particles) {
  148. if(counter % 2){
  149. offset.x = - offset.x;
  150. offset.y = - offset.y;
  151. } else {
  152. offset.x = Math.floor((Math.random() * this.grid_size.x / 10)) % this.grid_size.x;
  153. offset.y = Math.floor((Math.random() * this.grid_size.y / 10)) % this.grid_size.y;
  154. }
  155. while(this.is_position_occupied(offset)){
  156. offset.x++;
  157. offset.y++;
  158. }
  159. var daughter = this.get_particle_by_type(decay.particles[counter], p.position);
  160. daughter.start_time = createjs.Ticker.getTime();
  161. daughter.target = {
  162. time: daughter.start_time + 500,
  163. x: daughter.position.x + offset.x,
  164. y: daughter.position.y + offset.y
  165. }
  166. daughter.decays = null;
  167. this.add_collectible(daughter);
  168. counter++;
  169. }
  170. this.remove_collectible(p);
  171. }
  172. }
  173. }
  174. Controller.prototype.get_particle_by_type = function(ptype, position){
  175. for (var pIndex in this.possible_collectibles){
  176. var cc = this.possible_collectibles[pIndex];
  177. if (cc.collectible.type == ptype){
  178. cc = Object.create(cc.collectible);
  179. var pos = Object.create(position);
  180. cc.position = pos;
  181. return cc;
  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. 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 >= 1) {
  245. this.add_physicist();
  246. this.counter = 0;
  247. }
  248. }
  249. Controller.prototype.game_over = function(){
  250. alert("Game Over!");
  251. // ...
  252. }