controller.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. this.prototype.spawn_collectibles = function(){
  27. var collectible = Object.create(get_random_element_with_probabilities(this.possible_collectibles));
  28. if (!collectible) return;
  29. var rnd_pos = this.get_random_position();
  30. if (this.is_position_free(rnd_pos)) {
  31. collectible.position = rnd_pos;
  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.setFPS(20);
  39. this.bind_events();
  40. this.time = 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. switch (e.keyCode){
  56. case 37:
  57. direction = {x: -1, y: 0};
  58. break;
  59. case 38:
  60. direction = {x: 0, y: -1};
  61. break;
  62. case 39:
  63. direction = {x: 1, y: 0};
  64. break;
  65. case 40:
  66. direction = {x: 0, y: 1};
  67. break;
  68. }
  69. if (direction){
  70. c.turn_snake(direction);
  71. }
  72. }
  73. }
  74. Controller.prototype.turn_snake = function(direction){
  75. this.snake.physicists[0].direction = direction;
  76. }
  77. Controller.prototype.tick = function(event){
  78. if(event.time - this.time > this.time_step){
  79. this.time = event.time;
  80. var next_cell = this.get_next_cell_position();
  81. this.snake.move(next_cell);
  82. this.spawn_collectibles();
  83. this.update_views();
  84. }
  85. this.stage.update(event);
  86. }
  87. Controller.prototype.get_next_cell_position = function(){
  88. var ph0 = this.snake.physicists[0];
  89. var next_cell = Object.create(ph0.position);
  90. next_cell.x += ph0.direction.x;
  91. next_cell.y += ph0.direction.y;
  92. if (next_cell.x < 0) next_cell.x = this.grid_size.x - 1;
  93. if (next_cell.y < 0) next_cell.y = this.grid_size.y - 1;
  94. if (next_cell.x == this.grid_size.x) next_cell.x = 0;
  95. if (next_cell.y == this.grid_size.y) next_cell.y = 0;
  96. return next_cell;
  97. }
  98. Controller.prototype.get_random_position = function(){
  99. return {x: Math.floor(Math.random()*this.grid_size.x),
  100. y: Math.floor(Math.random()*this.grid_size.y)
  101. };
  102. }
  103. var get_random_element_with_probabilities(array){
  104. var previous_probability = 0;
  105. var rnd = Math.random();
  106. for (ind in array){
  107. var probability = array[ind].probability;
  108. if (rnd < probability + previous_probability) return array[ind];
  109. previous_probability += probability;
  110. }
  111. return null;
  112. }