controller.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. var Controller = function(){
  2. this.grid_size = {x: 20, y: 20};
  3. this.initial_length = 3;
  4. this.time_step = 400;
  5. this.collectibles = [];
  6. this.views = [];
  7. this.snake = new Snake(this.initial_length);
  8. var pos0 = {x: -1, y: -1};
  9. var higgs = new Particle(pos0);
  10. higgs.mass = 125;
  11. higgs.type = "Higgs"
  12. var electron = new Particle(pos0);
  13. electron.mass = .0005;
  14. electron.type = "electron";
  15. this.possible_collectibles = [
  16. {collectible: higgs, probability: 3e-3},
  17. {collectible: electron, probability: .05}
  18. ]
  19. this.stage = new createjs.Stage("demoCanvas");
  20. }
  21. Controller.prototype.update_views = function(){
  22. for(view in this.views){
  23. this.views[view].update();
  24. }
  25. }
  26. Controller.prototype.spawn_collectibles = function(){
  27. var collectible =get_random_element_with_probabilities(this.possible_collectibles);
  28. if (!collectible) return;
  29. collectible = Object.create(collectible);
  30. var rnd_pos = this.get_random_position();
  31. if (this.is_position_free(rnd_pos)) {
  32. collectible.position = rnd_pos;
  33. }
  34. this.collectibles.push(collectible);
  35. }
  36. Controller.prototype.start_game = function(){
  37. // this.session = new Session();
  38. var c = this;
  39. createjs.Ticker.on("tick", function(e){c.tick(e);});
  40. createjs.Ticker.setFPS(20);
  41. this.bind_events();
  42. this.time = 0;
  43. for(phModel in controller.snake.physicists){
  44. var model = controller.snake.physicists[phModel];
  45. var phView = new PhysicistView(model);
  46. this.add_view(phView);
  47. }
  48. }
  49. Controller.prototype.add_view = function(view){
  50. this.stage.addChild(view);
  51. this.views.push(view);
  52. }
  53. Controller.prototype.bind_events = function(){
  54. var c = this;
  55. window.onkeydown = function(e){
  56. var direction = null;
  57. switch (e.keyCode){
  58. case 37:
  59. direction = {x: -1, y: 0};
  60. break;
  61. case 38:
  62. direction = {x: 0, y: -1};
  63. break;
  64. case 39:
  65. direction = {x: 1, y: 0};
  66. break;
  67. case 40:
  68. direction = {x: 0, y: 1};
  69. break;
  70. }
  71. if (direction){
  72. c.turn_snake(direction);
  73. }
  74. }
  75. }
  76. Controller.prototype.turn_snake = function(direction){
  77. this.snake.physicists[0].direction = direction;
  78. }
  79. Controller.prototype.tick = function(event){
  80. if(event.time - this.time > this.time_step){
  81. this.time = event.time;
  82. var next_cell = this.get_next_cell_position();
  83. this.snake.move(next_cell);
  84. this.spawn_collectibles();
  85. this.update_views();
  86. }
  87. this.stage.update(event);
  88. }
  89. Controller.prototype.get_next_cell_position = function(){
  90. var ph0 = this.snake.physicists[0];
  91. var next_cell = Object.create(ph0.position);
  92. next_cell.x += ph0.direction.x;
  93. next_cell.y += ph0.direction.y;
  94. if (next_cell.x < 0) next_cell.x = this.grid_size.x - 1;
  95. if (next_cell.y < 0) next_cell.y = this.grid_size.y - 1;
  96. if (next_cell.x == this.grid_size.x) next_cell.x = 0;
  97. if (next_cell.y == this.grid_size.y) next_cell.y = 0;
  98. return next_cell;
  99. }
  100. Controller.prototype.get_random_position = function(){
  101. return {x: Math.floor(Math.random()*this.grid_size.x),
  102. y: Math.floor(Math.random()*this.grid_size.y)
  103. };
  104. }
  105. Controller.prototype.is_position_free = function(position){
  106. var phs = this.snake.physicists;
  107. for (ph in phs){
  108. var pos = phs[ph].position;
  109. if (pos.x == position.x && pos.y == position.y) return false;
  110. }
  111. for (c in this.collectibles){
  112. var pos = this.collectibles[c].position;
  113. if (pos.x == position.x && pos.y == position.y) return false;
  114. }
  115. return true;
  116. }
  117. var get_random_element_with_probabilities = function(array){
  118. var previous_probability = 0;
  119. var rnd = Math.random();
  120. for (ind in array){
  121. var probability = array[ind].probability;
  122. if (rnd < probability + previous_probability) return array[ind];
  123. previous_probability += probability;
  124. }
  125. return null;
  126. }