فهرست منبع

Merge branch 'models' of github.com:snaky-particles/snaky-particles into development

Kevin Heinicke 10 سال پیش
والد
کامیت
7d4ba90f02
5فایلهای تغییر یافته به همراه51 افزوده شده و 21 حذف شده
  1. 38 16
      js/controller.js
  2. 4 2
      js/models/physicist.js
  3. 2 2
      js/script.js
  4. 6 1
      js/views/particle.js
  5. 1 0
      js/views/physicist.js

+ 38 - 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 && next_cell_content.collectible) this.snake.physicists[0].collect(next_cell_content.collectible);
 		this.spawn_collectibles();
         this.update_views();
 	}
@@ -122,17 +121,40 @@ 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 {physicist: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 {collectible: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);
+	}
+	i = -1;
+	i = this.views.indexOf(collectible.view);
+	if (i > -1) {
+	    this.views.splice(i, 1);
+	}
+	this.stage.removeChild(collectible.view);
+
 }
 
 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);
 };

+ 2 - 2
js/script.js

@@ -13,7 +13,7 @@ function init() {
 
     controller.start_game();
 
-    var p2 = new Particle({x: 3, y: 3});
+    /*var p2 = new Particle({x: 3, y: 3});
     p2.start_time = createjs.Ticker.getTime();
     p2.target = {
         time: p2.start_time + 1000,
@@ -21,7 +21,7 @@ function init() {
         y: 7
     }
     var p2v = new ParticleView(p2);
-    controller.add_view(p2v);
+    controller.add_view(p2v);*/
 }
 
 function resize() {

+ 6 - 1
js/views/particle.js

@@ -1,6 +1,7 @@
 (function(window) {
     function ParticleView(modelObject){
         this.model = modelObject;
+		this.model.view = this;
 		var dp = this.model.draw_properties;
 		var cs = window.cell_size;
         this.graphics
@@ -30,7 +31,11 @@
                 // We do not want to have magnetically curved particles for now
                 // {guide:{ path:[0,0, 0,200,200,200, 200,0,0,0] }},
                 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;

+ 1 - 0
js/views/physicist.js

@@ -19,6 +19,7 @@
         this.gotoAndPlay("run");
         this.currentAnimationFrame = Math.floor(Math.random() * 7);
         this.model = modelObject;
+		this.model.view = this;
     }
 
     PhysicistView.prototype = new createjs.Sprite();