|
|
@@ -5,6 +5,19 @@ var Controller = function(){
|
|
|
this.collectibles = [];
|
|
|
this.views = [];
|
|
|
this.snake = new Snake(this.initial_length);
|
|
|
+ var pos0 = {x: -1, y: -1};
|
|
|
+
|
|
|
+ var higgs = new Particle(pos0);
|
|
|
+ higgs.mass = 125;
|
|
|
+ higgs.type = "Higgs"
|
|
|
+ var electron = new Particle(pos0);
|
|
|
+ electron.mass = .0005;
|
|
|
+ electron.type = "electron";
|
|
|
+
|
|
|
+ this.possible_collectibles = [
|
|
|
+ {collectible: higgs, probability: 3e-3},
|
|
|
+ {collectible: electron, probability: .05}
|
|
|
+ ]
|
|
|
this.stage = new createjs.Stage("demoCanvas");
|
|
|
}
|
|
|
|
|
|
@@ -14,6 +27,15 @@ Controller.prototype.update_views = function(){
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+this.prototype.spawn_collectibles = function(){
|
|
|
+ var collectible = Object.create(get_random_element_with_probabilities(this.possible_collectibles));
|
|
|
+ if (!collectible) return;
|
|
|
+ var rnd_pos = this.get_random_position();
|
|
|
+ if (this.is_position_free(rnd_pos)) {
|
|
|
+ collectible.position = rnd_pos;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
Controller.prototype.start_game = function(){
|
|
|
// this.session = new Session();
|
|
|
var c = this;
|
|
|
@@ -69,6 +91,7 @@ Controller.prototype.tick = function(event){
|
|
|
this.time = event.time;
|
|
|
var next_cell = this.get_next_cell_position();
|
|
|
this.snake.move(next_cell);
|
|
|
+ this.spawn_collectibles();
|
|
|
this.update_views();
|
|
|
}
|
|
|
this.stage.update(event);
|
|
|
@@ -85,3 +108,20 @@ Controller.prototype.get_next_cell_position = function(){
|
|
|
if (next_cell.y == this.grid_size.y) next_cell.y = 0;
|
|
|
return next_cell;
|
|
|
}
|
|
|
+
|
|
|
+Controller.prototype.get_random_position = function(){
|
|
|
+ return {x: Math.floor(Math.random()*this.grid_size.x),
|
|
|
+ y: Math.floor(Math.random()*this.grid_size.y)
|
|
|
+ };
|
|
|
+}
|
|
|
+
|
|
|
+var get_random_element_with_probabilities(array){
|
|
|
+ var previous_probability = 0;
|
|
|
+ var rnd = Math.random();
|
|
|
+ for (ind in array){
|
|
|
+ var probability = array[ind].probability;
|
|
|
+ if (rnd < probability + previous_probability) return array[ind];
|
|
|
+ previous_probability += probability;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+}
|