|
|
@@ -35,13 +35,15 @@ Controller.prototype.update_views = function(){
|
|
|
|
|
|
Controller.prototype.spawn_collectibles = function(){
|
|
|
var collectible = get_random_element_with_probabilities(this.possible_collectibles);
|
|
|
- if (!collectible) return;
|
|
|
+ if (!collectible) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
collectible = Object.create(collectible.collectible);
|
|
|
var rnd_pos = this.get_random_position();
|
|
|
if (!this.is_position_occupied(rnd_pos)) {
|
|
|
collectible.position = rnd_pos;
|
|
|
this.add_collectible(collectible);
|
|
|
- }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
Controller.prototype.add_collectible = function(collectible){
|
|
|
@@ -63,24 +65,26 @@ Controller.prototype.add_physicist = function(){
|
|
|
|
|
|
Controller.prototype.start_game = function(){
|
|
|
// this.session = new Session();
|
|
|
- var c = this;
|
|
|
- createjs.Ticker.on("tick", function(e){c.tick(e);});
|
|
|
+ var c = this;
|
|
|
+ createjs.Ticker.on("tick", function(e) {
|
|
|
+ c.tick(e);
|
|
|
+ });
|
|
|
createjs.Ticker.timingMode = createjs.Ticker.RAF;
|
|
|
this.bind_events();
|
|
|
this.time = 0;
|
|
|
this.score = 0;
|
|
|
|
|
|
- this.update_interface();
|
|
|
+ this.update_interface();
|
|
|
|
|
|
- for(phModel in controller.snake.physicists){
|
|
|
- var model = controller.snake.physicists[phModel];
|
|
|
- var phView = new PhysicistView(model);
|
|
|
- this.add_view(phView);
|
|
|
- }
|
|
|
+ for(phModel in controller.snake.physicists) {
|
|
|
+ var model = controller.snake.physicists[phModel];
|
|
|
+ var phView = new PhysicistView(model);
|
|
|
+ this.add_view(phView);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
Controller.prototype.add_view = function(view){
|
|
|
- this.stage.addChild(view);
|
|
|
+ this.stage.addChild(view);
|
|
|
this.views.push(view);
|
|
|
}
|
|
|
|
|
|
@@ -90,7 +94,7 @@ Controller.prototype.bind_events = function(){
|
|
|
var direction = null;
|
|
|
var dir = {x: c.snake.physicists[0].direction.x, y: c.snake.physicists[0].direction.y};
|
|
|
|
|
|
- switch (e.keyCode){
|
|
|
+ switch (e.keyCode) {
|
|
|
case 37:
|
|
|
case 65:
|
|
|
if(dir.x == 1)
|
|
|
@@ -141,59 +145,68 @@ Controller.prototype.turn_snake = function(direction){
|
|
|
|
|
|
|
|
|
Controller.prototype.tick = function(event){
|
|
|
- if(event.paused) return;
|
|
|
- if(event.time - this.time > this.time_step){
|
|
|
+ if(event.paused) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if(event.time - this.time > this.time_step) {
|
|
|
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);
|
|
|
+ if (next_cell.is_initial !== true && next_cell_content && next_cell_content.physicist) {
|
|
|
+ this.game_over();
|
|
|
+ }
|
|
|
this.snake.move(next_cell);
|
|
|
this.canTurn = true;
|
|
|
this.physicists_count();
|
|
|
- if (next_cell_content && next_cell_content.collectible) this.snake.physicists[0].collect(next_cell_content.collectible);
|
|
|
- if (next_cell_content && next_cell_content.physicist && event.time > 5000) this.game_over();
|
|
|
- if(this.collectibles.length < this.maximum_spawns){
|
|
|
- this.spawn_collectibles();
|
|
|
- }
|
|
|
- this.update_views();
|
|
|
+ if (next_cell_content && next_cell_content.collectible) {
|
|
|
+ this.snake.physicists[0].collect(next_cell_content.collectible);
|
|
|
+ }
|
|
|
+ if(this.collectibles.length < this.maximum_spawns) {
|
|
|
+ this.spawn_collectibles();
|
|
|
+ }
|
|
|
+ this.update_views();
|
|
|
}
|
|
|
- this.check_decays();
|
|
|
+ this.check_decays();
|
|
|
this.stage.update(event);
|
|
|
}
|
|
|
|
|
|
Controller.prototype.check_decays = function(){
|
|
|
- for(var pIndex in this.collectibles){
|
|
|
- var p = this.collectibles[pIndex];
|
|
|
- if(p.half_life && p.decays && p.decays.length && createjs.Ticker.getTime() > p.decay_time){
|
|
|
- var offset = {x: 0, y: 0};
|
|
|
- var counter = 0;
|
|
|
+ for(var pIndex in this.collectibles) {
|
|
|
+ var p = this.collectibles[pIndex];
|
|
|
+ if(p.half_life && p.decays && p.decays.length
|
|
|
+ && (createjs.Ticker.getTime() > p.decay_time)) {
|
|
|
+ var offset = {x: 0, y: 0};
|
|
|
+ var counter = 0;
|
|
|
var decay = get_random_element_with_probabilities(p.decays);
|
|
|
- if (!decay) return;
|
|
|
- for(var daughterInd in decay.particles) {
|
|
|
- if(counter % 2){
|
|
|
- offset.x = - offset.x;
|
|
|
- offset.y = - offset.y;
|
|
|
- } else {
|
|
|
- offset.x = Math.floor((Math.random() * this.grid_size.x / 10)) % this.grid_size.x;
|
|
|
- offset.y = Math.floor((Math.random() * this.grid_size.y / 10)) % this.grid_size.y;
|
|
|
- }
|
|
|
- while(this.is_position_occupied(offset)){
|
|
|
- offset.x++;
|
|
|
- offset.y++;
|
|
|
- }
|
|
|
- var daughter = this.get_particle_by_type(decay.particles[counter], p.position);
|
|
|
- daughter.start_time = createjs.Ticker.getTime();
|
|
|
- daughter.target = {
|
|
|
- time: daughter.start_time + 500,
|
|
|
- x: daughter.position.x + offset.x,
|
|
|
- y: daughter.position.y + offset.y
|
|
|
- }
|
|
|
- daughter.decays = null;
|
|
|
- this.add_collectible(daughter);
|
|
|
- counter++;
|
|
|
- }
|
|
|
- this.remove_collectible(p);
|
|
|
- }
|
|
|
+ if (!decay) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for(var daughterInd in decay.particles) {
|
|
|
+ if(counter % 2){
|
|
|
+ offset.x = - offset.x;
|
|
|
+ offset.y = - offset.y;
|
|
|
+ } else {
|
|
|
+ offset.x = Math.floor((Math.random() * this.grid_size.x / 10)) % this.grid_size.x;
|
|
|
+ offset.y = Math.floor((Math.random() * this.grid_size.y / 10)) % this.grid_size.y;
|
|
|
+ }
|
|
|
+ while(this.is_position_occupied(offset)){
|
|
|
+ offset.x++;
|
|
|
+ offset.y++;
|
|
|
+ }
|
|
|
+ var daughter = this.get_particle_by_type(decay.particles[counter], p.position);
|
|
|
+ daughter.start_time = createjs.Ticker.getTime();
|
|
|
+ daughter.target = {
|
|
|
+ time: daughter.start_time + 500,
|
|
|
+ x: daughter.position.x + offset.x,
|
|
|
+ y: daughter.position.y + offset.y
|
|
|
+ }
|
|
|
+ daughter.decays = null;
|
|
|
+ this.add_collectible(daughter);
|
|
|
+ counter++;
|
|
|
+ }
|
|
|
+ this.remove_collectible(p);
|
|
|
}
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
Controller.prototype.get_particle_by_type = function(ptype, position){
|
|
|
@@ -211,19 +224,33 @@ Controller.prototype.get_particle_by_type = function(ptype, position){
|
|
|
Controller.prototype.get_next_cell_position = function(){
|
|
|
var ph0 = this.snake.physicists[0];
|
|
|
var next_cell = Object.create(ph0.position);
|
|
|
+ if (ph0.direction.x == 0 && ph0.direction.y == 0) {
|
|
|
+ next_cell.is_initial = true;
|
|
|
+ } else {
|
|
|
+ next_cell.is_initial = false;
|
|
|
+ }
|
|
|
next_cell.x += ph0.direction.x;
|
|
|
next_cell.y += ph0.direction.y;
|
|
|
- if (next_cell.x < 0) next_cell.x = this.grid_size.x - 1;
|
|
|
- if (next_cell.y < 0) next_cell.y = this.grid_size.y - 1;
|
|
|
- if (next_cell.x == this.grid_size.x) next_cell.x = 0;
|
|
|
- if (next_cell.y == this.grid_size.y) next_cell.y = 0;
|
|
|
+ if (next_cell.x < 0) {
|
|
|
+ next_cell.x = this.grid_size.x - 1;
|
|
|
+ }
|
|
|
+ if (next_cell.y < 0) {
|
|
|
+ next_cell.y = this.grid_size.y - 1;
|
|
|
+ }
|
|
|
+ if (next_cell.x == this.grid_size.x) {
|
|
|
+ next_cell.x = 0;
|
|
|
+ }
|
|
|
+ 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)
|
|
|
- };
|
|
|
+ return {
|
|
|
+ x: Math.floor(Math.random()*this.grid_size.x),
|
|
|
+ y: Math.floor(Math.random()*this.grid_size.y)
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
Controller.prototype.is_position_occupied = function(position){
|