|
@@ -3,19 +3,39 @@ 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.snake = new Snake(this.initial_length);
|
|
this.snake = new Snake(this.initial_length);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+Controller.prototype.update_views = function(){
|
|
|
|
|
+ for(view in this.views){
|
|
|
|
|
+ this.views[view].update();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
Controller.prototype.start_game = function(){
|
|
Controller.prototype.start_game = function(){
|
|
|
// this.session = new Session();
|
|
// this.session = new Session();
|
|
|
|
|
+ var c = this;
|
|
|
this.stage = new createjs.Stage("demoCanvas");
|
|
this.stage = new createjs.Stage("demoCanvas");
|
|
|
- createjs.Ticker.on("tick", this.tick);
|
|
|
|
|
|
|
+ createjs.Ticker.on("tick", function(e){c.tick(e);});
|
|
|
createjs.Ticker.setFPS(20);
|
|
createjs.Ticker.setFPS(20);
|
|
|
this.bind_events();
|
|
this.bind_events();
|
|
|
this.time = 0;
|
|
this.time = 0;
|
|
|
|
|
+
|
|
|
|
|
+ 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.views.push(view);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Controller.prototype.bind_events = function(){
|
|
Controller.prototype.bind_events = function(){
|
|
|
|
|
+ var c = this;
|
|
|
window.onkeydown = function(e){
|
|
window.onkeydown = function(e){
|
|
|
var direction = null;
|
|
var direction = null;
|
|
|
switch (e.keyCode){
|
|
switch (e.keyCode){
|
|
@@ -23,43 +43,45 @@ Controller.prototype.bind_events = function(){
|
|
|
direction = {x: -1, y: 0};
|
|
direction = {x: -1, y: 0};
|
|
|
break;
|
|
break;
|
|
|
case 38:
|
|
case 38:
|
|
|
- direction = {x: 0, y: 1};
|
|
|
|
|
|
|
+ direction = {x: 0, y: -1};
|
|
|
break;
|
|
break;
|
|
|
case 39:
|
|
case 39:
|
|
|
direction = {x: 1, y: 0};
|
|
direction = {x: 1, y: 0};
|
|
|
break;
|
|
break;
|
|
|
case 40:
|
|
case 40:
|
|
|
- direction = {x: 0, y: -1};
|
|
|
|
|
|
|
+ direction = {x: 0, y: 1};
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
if (direction){
|
|
if (direction){
|
|
|
- this.turn_snake(direction);
|
|
|
|
|
|
|
+ c.turn_snake(direction);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Controller.prototype.turn_snake = function(direction){
|
|
Controller.prototype.turn_snake = function(direction){
|
|
|
- this.snake.physicsts[0].direction = direction;
|
|
|
|
|
|
|
+ this.snake.physicists[0].direction = direction;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
Controller.prototype.tick = function(event){
|
|
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();
|
|
|
this.snake.move(next_cell);
|
|
this.snake.move(next_cell);
|
|
|
|
|
+ this.update_views();
|
|
|
}
|
|
}
|
|
|
- stage.update(event);
|
|
|
|
|
|
|
+ this.stage.update(event);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
Controller.prototype.get_next_cell_position = function(){
|
|
Controller.prototype.get_next_cell_position = function(){
|
|
|
- var ph0 = this.snake.physicsts[0];
|
|
|
|
|
|
|
+ var ph0 = this.snake.physicists[0];
|
|
|
var next_cell = ph0.position;
|
|
var next_cell = ph0.position;
|
|
|
next_cell.x += ph0.direction.x;
|
|
next_cell.x += ph0.direction.x;
|
|
|
next_cell.y += ph0.direction.y;
|
|
next_cell.y += ph0.direction.y;
|
|
|
- if (next_cell.x < 0) next_cell.x = grid_size.x - 1;
|
|
|
|
|
- if (next_cell.y < 0) next_cell.y = grid_size.y - 1;
|
|
|
|
|
- if (next_cell.x = grid_size.x) next_cell.x = 0;
|
|
|
|
|
- if (next_cell.y = 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;
|
|
return next_cell;
|
|
|
}
|
|
}
|