controller.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 = 100;
  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. this.add_collectible(collectible, rnd_pos);
  32. }
  33. }
  34. Controller.prototype.add_collectible = function(collectible, position){
  35. collectible.position = position;
  36. if(collectible.halflife_time > 0){
  37. collectible.decay_time = createjs.Ticker.getTime() + collectible.halflife_time;
  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. for(var daughterInd in p.decays[0]){
  146. if(counter % 2){
  147. offset.x = Math.floor((Math.random() * this.grid_size.x / 10)) % this.grid_size.x;
  148. offset.y = Math.floor((Math.random() * this.grid_size.y / 10)) % this.grid_size.y;
  149. } else {
  150. offset.x = - offset.x;
  151. offset.y = - offset.y;
  152. }
  153. while(this.is_position_occupied(offset)){
  154. offset.x++;
  155. offset.y++;
  156. }
  157. var daughter = new Particle(p.position);
  158. daughter.start_time = createjs.Ticker.getTime();
  159. daughter.target = {
  160. time: daughter.start_time + 500,
  161. x: daughter.position.x + offset.x,
  162. y: daughter.position.y + offset.y
  163. }
  164. daughter.decays = null;
  165. this.add_collectible(daughter, p.position);
  166. counter++;
  167. }
  168. this.remove_collectible(p);
  169. }
  170. }
  171. }
  172. Controller.prototype.get_next_cell_position = function(){
  173. var ph0 = this.snake.physicists[0];
  174. var next_cell = Object.create(ph0.position);
  175. next_cell.x += ph0.direction.x;
  176. next_cell.y += ph0.direction.y;
  177. if (next_cell.x < 0) next_cell.x = this.grid_size.x - 1;
  178. if (next_cell.y < 0) next_cell.y = this.grid_size.y - 1;
  179. if (next_cell.x == this.grid_size.x) next_cell.x = 0;
  180. if (next_cell.y == this.grid_size.y) next_cell.y = 0;
  181. return next_cell;
  182. }
  183. Controller.prototype.get_random_position = function(){
  184. return {x: Math.floor(Math.random()*this.grid_size.x),
  185. y: Math.floor(Math.random()*this.grid_size.y)
  186. };
  187. }
  188. Controller.prototype.is_position_occupied = function(position){
  189. var phs = this.snake.physicists;
  190. for (var ph in phs){
  191. var pos = phs[ph].position;
  192. if (pos.x == position.x && pos.y == position.y) return {physicist:phs[ph]};
  193. }
  194. for (var c in this.collectibles){
  195. var pos = this.collectibles[c].position;
  196. if (pos.x == position.x && pos.y == position.y) return {collectible:this.collectibles[c]};
  197. }
  198. return null;
  199. }
  200. Controller.prototype.hit_test = function(particle){
  201. for (var ph_i in this.snake.physicists){
  202. var ph = this.snake.physicists[ph_i];
  203. if (ph.view.hitTest(particle.position.x, particle.position.y)) {
  204. this.ph.collect(particle);
  205. }
  206. }
  207. }
  208. Controller.prototype.remove_collectible = function(collectible){
  209. var i = this.collectibles.indexOf(collectible);
  210. if (i > -1) {
  211. this.collectibles.splice(i, 1);
  212. }
  213. i = -1;
  214. i = this.views.indexOf(collectible.view);
  215. if (i > -1) {
  216. this.views.splice(i, 1);
  217. }
  218. this.stage.removeChild(collectible.view);
  219. }
  220. var get_random_element_with_probabilities = function(array){
  221. var previous_probability = 0;
  222. var rnd = Math.random();
  223. for (ind in array){
  224. var probability = array[ind].probability;
  225. if (rnd < probability + previous_probability) return array[ind];
  226. previous_probability += probability;
  227. }
  228. return null;
  229. }
  230. Controller.prototype.physicists_count = function(){
  231. if(this.counter >= 1) {
  232. this.add_physicist();
  233. this.counter = 0;
  234. }
  235. }
  236. Controller.prototype.game_over = function(){
  237. alert("Game Over!");
  238. // ...
  239. }