|
@@ -3,7 +3,7 @@ var Controller = function(){
|
|
|
this.initial_length = 3;
|
|
this.initial_length = 3;
|
|
|
this.time_step = 400;
|
|
this.time_step = 400;
|
|
|
this.collectibles = [];
|
|
this.collectibles = [];
|
|
|
- this.views = [];
|
|
|
|
|
|
|
+ this.views = [];
|
|
|
this.snake = new Snake(this.initial_length);
|
|
this.snake = new Snake(this.initial_length);
|
|
|
var pos0 = {x: -1, y: -1};
|
|
var pos0 = {x: -1, y: -1};
|
|
|
|
|
|
|
@@ -22,17 +22,17 @@ var Controller = function(){
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Controller.prototype.update_views = function(){
|
|
Controller.prototype.update_views = function(){
|
|
|
- for(view in this.views){
|
|
|
|
|
|
|
+ for(var view in this.views){
|
|
|
this.views[view].update();
|
|
this.views[view].update();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Controller.prototype.spawn_collectibles = function(){
|
|
Controller.prototype.spawn_collectibles = function(){
|
|
|
- var collectible =get_random_element_with_probabilities(this.possible_collectibles);
|
|
|
|
|
|
|
+ var collectible = get_random_element_with_probabilities(this.possible_collectibles);
|
|
|
if (!collectible) return;
|
|
if (!collectible) return;
|
|
|
collectible = Object.create(collectible.collectible);
|
|
collectible = Object.create(collectible.collectible);
|
|
|
var rnd_pos = this.get_random_position();
|
|
var rnd_pos = this.get_random_position();
|
|
|
- if (this.is_position_free(rnd_pos)) {
|
|
|
|
|
|
|
+ if (! this.is_position_occupied(rnd_pos)) {
|
|
|
collectible.position = rnd_pos;
|
|
collectible.position = rnd_pos;
|
|
|
this.collectibles.push(collectible);
|
|
this.collectibles.push(collectible);
|
|
|
this.add_view(new ParticleView(collectible));
|
|
this.add_view(new ParticleView(collectible));
|
|
@@ -43,10 +43,10 @@ Controller.prototype.start_game = function(){
|
|
|
// this.session = new Session();
|
|
// this.session = new Session();
|
|
|
var c = this;
|
|
var c = this;
|
|
|
createjs.Ticker.on("tick", function(e){c.tick(e);});
|
|
createjs.Ticker.on("tick", function(e){c.tick(e);});
|
|
|
- createjs.Ticker.addEventListener("tick", this.stage);
|
|
|
|
|
- createjs.Ticker.timingMode = createjs.Ticker.RAF;
|
|
|
|
|
|
|
+ createjs.Ticker.setFPS(20);
|
|
|
this.bind_events();
|
|
this.bind_events();
|
|
|
this.time = 0;
|
|
this.time = 0;
|
|
|
|
|
+ this.score = 0;
|
|
|
|
|
|
|
|
for(phModel in controller.snake.physicists){
|
|
for(phModel in controller.snake.physicists){
|
|
|
var model = controller.snake.physicists[phModel];
|
|
var model = controller.snake.physicists[phModel];
|
|
@@ -57,10 +57,7 @@ Controller.prototype.start_game = function(){
|
|
|
|
|
|
|
|
Controller.prototype.add_view = function(view){
|
|
Controller.prototype.add_view = function(view){
|
|
|
this.stage.addChild(view);
|
|
this.stage.addChild(view);
|
|
|
- this.views.push(view);
|
|
|
|
|
- if(view.animate){
|
|
|
|
|
- view.animate();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ this.views.push(view);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Controller.prototype.bind_events = function(){
|
|
Controller.prototype.bind_events = function(){
|
|
@@ -97,7 +94,9 @@ Controller.prototype.tick = function(event){
|
|
|
if(event.time - this.time > this.time_step){
|
|
if(event.time - this.time > this.time_step){
|
|
|
this.time = event.time;
|
|
this.time = event.time;
|
|
|
var next_cell = this.get_next_cell_position();
|
|
var next_cell = this.get_next_cell_position();
|
|
|
|
|
+ var next_cell_content = this.is_position_occupied(next_cell);
|
|
|
this.snake.move(next_cell);
|
|
this.snake.move(next_cell);
|
|
|
|
|
+ if (next_cell_content) this.snake.physicists[0].collect(next_cell_content);
|
|
|
this.spawn_collectibles();
|
|
this.spawn_collectibles();
|
|
|
this.update_views();
|
|
this.update_views();
|
|
|
}
|
|
}
|
|
@@ -122,17 +121,34 @@ Controller.prototype.get_random_position = function(){
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-Controller.prototype.is_position_free = function(position){
|
|
|
|
|
|
|
+Controller.prototype.is_position_occupied = function(position){
|
|
|
var phs = this.snake.physicists;
|
|
var phs = this.snake.physicists;
|
|
|
- for (ph in phs){
|
|
|
|
|
|
|
+ for (var ph in phs){
|
|
|
var pos = phs[ph].position;
|
|
var pos = phs[ph].position;
|
|
|
- if (pos.x == position.x && pos.y == position.y) return false;
|
|
|
|
|
|
|
+ if (pos.x == position.x && pos.y == position.y) return phs[ph];
|
|
|
}
|
|
}
|
|
|
- for (c in this.collectibles){
|
|
|
|
|
|
|
+ for (var c in this.collectibles){
|
|
|
var pos = this.collectibles[c].position;
|
|
var pos = this.collectibles[c].position;
|
|
|
- if (pos.x == position.x && pos.y == position.y) return false;
|
|
|
|
|
|
|
+ if (pos.x == position.x && pos.y == position.y) return this.collectibles[c];
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Controller.prototype.hit_test = function(e, particle){
|
|
|
|
|
+ for (var ph_i in this.snake.physicists){
|
|
|
|
|
+ var ph = this.snake.physicists[ph_i];
|
|
|
|
|
+ if (ph.view.hitTest(e.x, e.y)) {
|
|
|
|
|
+ this.ph.collect(particle);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- return true;
|
|
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+Controller.prototype.remove_collectible = function(collectible){
|
|
|
|
|
+ var i = this.collectibles.indexOf(collectible);
|
|
|
|
|
+ if (i > -1) {
|
|
|
|
|
+ this.collectibles.splice(i, 1);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
var get_random_element_with_probabilities = function(array){
|
|
var get_random_element_with_probabilities = function(array){
|