Ver código fonte

eat as many particles as you can

Dima Mironov 10 anos atrás
pai
commit
29346cbce1
2 arquivos alterados com 11 adições e 11 exclusões
  1. 10 10
      js/controller.js
  2. 1 1
      js/models/snake.js

+ 10 - 10
js/controller.js

@@ -145,15 +145,14 @@ Controller.prototype.tick = function(event){
 	if(event.time - this.time > this.time_step){
 		this.time = event.time;
         var next_cell = this.get_next_cell_position();
-		this.snake.move(next_cell);
+		var next_cell_content;
 		this.canTurn = true;
 		this.physicists_count();
-		var next_cell_content  = this.is_position_occupied(next_cell);
-		while (next_cell_content){
-			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();
-			next_cell_content = this.is_position_occupied(next_cell);
+		while (next_cell_content = this.is_position_occupied(next_cell)){
+			if (next_cell_content.collectible) this.snake.physicists[0].collect(next_cell_content.collectible);
+			if (next_cell_content.physicist && event.time > 5000) this.game_over();
 		}
+		this.snake.move(next_cell);
         if(this.collectibles.length < this.maximum_spawns){
 		    this.spawn_collectibles();
         }
@@ -230,15 +229,16 @@ Controller.prototype.get_random_position = function(){
 }
 
 Controller.prototype.is_position_occupied = function(position){
+
+	for (var c in this.collectibles){
+		var pos = this.collectibles[c].position;
+		if (pos.x == position.x && pos.y == position.y) return {collectible:this.collectibles[c]};
+	}
 	var phs = this.snake.physicists;
 	for (var ph in phs){
 		var pos = phs[ph].position;
 		if (pos.x == position.x && pos.y == position.y) return {physicist:phs[ph]};
 	}
-	for (var c in this.collectibles){
-		var pos = this.collectibles[c].position;
-		if (pos.x == position.x && pos.y == position.y) return {collectible:this.collectibles[c]};
-	}
 	return null;
 }
 

+ 1 - 1
js/models/snake.js

@@ -3,7 +3,7 @@ var Snake = function(n_physicisits){
 	for (var i = 0; i < n_physicisits; i++){
 		this.physicists.push(new Physicist(this, {x: 0, y: 0}));
 	}
-    this.physicists[0].direction = {x: 0, y: 0};
+    this.physicists[0].direction = {x: 1, y: 0};
 	this.new_physicists = [];
 	this.bonuses = [];
 	this.speed = 1;