controller.js 5.0 KB

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