浏览代码

Merge branch 'models' into development

Kevin Heinicke 10 年之前
父节点
当前提交
990095d4c1
共有 5 个文件被更改,包括 120 次插入0 次删除
  1. 65 0
      js/controller.js
  2. 3 0
      js/models/collectible.js
  3. 11 0
      js/models/particle.js
  4. 11 0
      js/models/physicist.js
  5. 30 0
      js/models/snake.js

+ 65 - 0
js/controller.js

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

+ 3 - 0
js/models/collectible.js

@@ -0,0 +1,3 @@
+var Collectible = function(position){
+	this.position = position;
+}

+ 11 - 0
js/models/particle.js

@@ -0,0 +1,11 @@
+var Particle = function(position){
+	Collectible.call(this, position);
+	this.type = "Higgs";
+	this.mass = 125;
+	this.charge = 0;
+	this.start_time = 1234;
+	this.target = null;
+	this.velocity = null;
+	this.points = 125;
+	this.parent_type = "W";
+}

+ 11 - 0
js/models/physicist.js

@@ -0,0 +1,11 @@
+var Physicist = function(snake, position) {
+	this.direction = 0;
+	this.position = position;
+	this.name = 'Fermi';
+	this.bonus = '';
+	this.snake = snake;
+};
+
+Physicist.prototype.catch = function(collectible) {
+
+};

+ 30 - 0
js/models/snake.js

@@ -0,0 +1,30 @@
+var Snake = function(n_physicisits){
+	this.physicists = [];
+	for (var i = 0; i < n_physicisits; i++){
+		this.physicists.push_back(new Physicist(this, position));
+	}
+	this.new_physicists = [];
+	this.bonuses = [];
+	this.speed = 1;
+}
+
+Snake.prototype.add_physicist = function(physicist){
+	this.new_physicists.push_back(new Physicist(this, this.physicists[this.physicists.length - 1].position));
+}
+
+Snake.prototype.add_bonus = function(bonus){
+	this.bonuses.push_back(bonus);
+}
+
+Snake.prototype.remove_physicist = function(index){	
+}
+
+
+Snake.prototype.move = function(next_cell){
+	this.physicist[0].position = next_cell;
+	for(var i = 1; i < this.physicist.length; i++){
+		this.physicists[i].position  = this.physicists[i-1].position;
+		this.physicists[i].direction = this.physicists[i-1].direction;
+	}
+
+}