controller.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 = 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. 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. for(var daughterInd in p.decays[0]){
  146. if(counter % 2){
  147. offset.x = - offset.x;
  148. offset.y = - offset.y;
  149. } else {
  150. offset.x = Math.floor((Math.random() * this.grid_size.x / 10)) % this.grid_size.x;
  151. offset.y = Math.floor((Math.random() * this.grid_size.y / 10)) % this.grid_size.y;
  152. }
  153. while(this.is_position_occupied(offset)){
  154. offset.x++;
  155. offset.y++;
  156. }
  157. var daughter = this.get_particle_by_type("electron", 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);
  166. counter++;
  167. }
  168. this.remove_collectible(p);
  169. }
  170. }
  171. }
  172. Controller.prototype.get_particle_by_type = function(ptype, position){
  173. for (var pIndex in this.possible_collectibles){
  174. var cc = this.possible_collectibles[pIndex];
  175. if (cc.collectible.type == ptype){
  176. cc = Object.create(cc.collectible);
  177. var pos = Object.create(position);
  178. cc.position = pos;
  179. return cc;
  180. }
  181. }
  182. }
  183. Controller.prototype.get_next_cell_position = function(){
  184. var ph0 = this.snake.physicists[0];
  185. var next_cell = Object.create(ph0.position);
  186. next_cell.x += ph0.direction.x;
  187. next_cell.y += ph0.direction.y;
  188. if (next_cell.x < 0) next_cell.x = this.grid_size.x - 1;
  189. if (next_cell.y < 0) next_cell.y = this.grid_size.y - 1;
  190. if (next_cell.x == this.grid_size.x) next_cell.x = 0;
  191. if (next_cell.y == this.grid_size.y) next_cell.y = 0;
  192. return next_cell;
  193. }
  194. Controller.prototype.get_random_position = function(){
  195. return {x: Math.floor(Math.random()*this.grid_size.x),
  196. y: Math.floor(Math.random()*this.grid_size.y)
  197. };
  198. }
  199. Controller.prototype.is_position_occupied = function(position){
  200. var phs = this.snake.physicists;
  201. for (var ph in phs){
  202. var pos = phs[ph].position;
  203. if (pos.x == position.x && pos.y == position.y) return {physicist:phs[ph]};
  204. }
  205. for (var c in this.collectibles){
  206. var pos = this.collectibles[c].position;
  207. if (pos.x == position.x && pos.y == position.y) return {collectible:this.collectibles[c]};
  208. }
  209. return null;
  210. }
  211. Controller.prototype.hit_test = function(particle){
  212. for (var ph_i in this.snake.physicists){
  213. var ph = this.snake.physicists[ph_i];
  214. if (ph.view.hitTest(particle.position.x, particle.position.y)) {
  215. ph.collect(particle);
  216. }
  217. }
  218. }
  219. Controller.prototype.remove_collectible = function(collectible){
  220. var i = this.collectibles.indexOf(collectible);
  221. if (i > -1) {
  222. this.collectibles.splice(i, 1);
  223. }
  224. i = -1;
  225. i = this.views.indexOf(collectible.view);
  226. if (i > -1) {
  227. this.views.splice(i, 1);
  228. }
  229. this.stage.removeChild(collectible.view);
  230. }
  231. var get_random_element_with_probabilities = function(array){
  232. var previous_probability = 0;
  233. var rnd = Math.random();
  234. for (ind in array){
  235. var probability = array[ind].probability;
  236. if (rnd < probability + previous_probability) return array[ind];
  237. previous_probability += probability;
  238. }
  239. return null;
  240. }
  241. Controller.prototype.physicists_count = function(){
  242. if(this.counter >= 1) {
  243. this.add_physicist();
  244. this.counter = 0;
  245. }
  246. }
  247. Controller.prototype.game_over = function(){
  248. alert("Game Over!");
  249. // ...
  250. }