controller.js 4.9 KB

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