Browse Source

eating prototype

Dima Mironov 10 năm trước cách đây
mục cha
commit
ab1a41672b
3 tập tin đã thay đổi với 41 bổ sung19 xóa
  1. 32 16
      js/controller.js
  2. 4 2
      js/models/physicist.js
  3. 5 1
      js/views/particle.js

+ 32 - 16
js/controller.js

@@ -3,7 +3,7 @@ var Controller = function(){
 	this.initial_length = 3;
 	this.time_step = 400;
 	this.collectibles = [];
-    this.views = [];
+	this.views = [];
 	this.snake = new Snake(this.initial_length);
 	var pos0 = {x: -1, y: -1};
 	
@@ -22,17 +22,17 @@ var Controller = function(){
 }
 
 Controller.prototype.update_views = function(){
-    for(view in this.views){
+    for(var view in this.views){
         this.views[view].update();
     }
 }
 
 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;
 	collectible = Object.create(collectible.collectible);
 	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;
 		this.collectibles.push(collectible);
 		this.add_view(new ParticleView(collectible));
@@ -43,10 +43,10 @@ Controller.prototype.start_game = function(){
 	// this.session = new Session();
     var c = this;
 	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.time = 0;
+	this.score = 0;
 
     for(phModel in controller.snake.physicists){
         var model = controller.snake.physicists[phModel];
@@ -57,10 +57,7 @@ Controller.prototype.start_game = function(){
 
 Controller.prototype.add_view = function(view){
     this.stage.addChild(view);
-    this.views.push(view);
-    if(view.animate){
-        view.animate();
-    }
+	this.views.push(view);
 }
 
 Controller.prototype.bind_events = function(){
@@ -97,7 +94,9 @@ 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();
+		var next_cell_content = this.is_position_occupied(next_cell);
 		this.snake.move(next_cell);
+		if (next_cell_content) this.snake.physicists[0].collect(next_cell_content);
 		this.spawn_collectibles();
         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;
-	for (ph in phs){
+	for (var ph in phs){
 		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;
-		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){

+ 4 - 2
js/models/physicist.js

@@ -6,6 +6,8 @@ var Physicist = function(snake, position) {
 	this.snake = snake;
 };
 
-Physicist.prototype.catch = function(collectible) {
-
+Physicist.prototype.collect = function(collectible) {
+	this.bonus += " fat!";
+	controller.score += collectible.points;
+	controller.remove_collectible(collectible);
 };

+ 5 - 1
js/views/particle.js

@@ -25,7 +25,11 @@
             createjs.Tween.get(this).to(
                 { x: cell_size * this.model.target.x, y: cell_size * this.model.target.y },
                 this.model.target.time - this.model.start_time
-            ).call(function(){
+				)
+			.addEventListener("change", function(e){
+				controller.hit_test(e, own_view.model);
+				})
+			.call(function(){
                 own_view.update = ParticleView.prototype.update;
                 own_view.model.position.x = own_view.model.target.x;
                 own_view.model.position.y = own_view.model.target.y;