Bläddra i källkod

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

Colleagues 10 år sedan
förälder
incheckning
868704ad74
3 ändrade filer med 62 tillägg och 14 borttagningar
  1. 52 7
      js/controller.js
  2. 2 1
      js/models/particle.js
  3. 8 6
      js/views/particle.js

+ 52 - 7
js/controller.js

@@ -9,12 +9,15 @@ var Controller = function(){
 	this.canTurn = true;
 	var pos0 = {x: -1, y: -1};
 	
+    createjs.DisplayObject.suppressCrossDomainErrors = true;
+
 	var higgs = new Particle(pos0);
 	higgs.mass = 125;
 	higgs.type = "Higgs"
 	var electron = new Particle(pos0);
 	electron.mass = .0005;
 	electron.type = "electron";
+    electron.decays = null;
 	higgs.draw_properties = {
 		colors: ["hsl(120, 100%, 50%)", "hsl(120, 40%, 50%)"],
 		ratios: [0, 1],
@@ -42,11 +45,18 @@ Controller.prototype.spawn_collectibles = function(){
 	if (!collectible) return;
 	collectible = Object.create(collectible.collectible);
 	var rnd_pos = this.get_random_position();
-	if (! this.is_position_occupied(rnd_pos)) {
-		collectible.position = rnd_pos;
-		this.collectibles.push(collectible);
-		this.add_view(new ParticleView(collectible));
-	}
+	if (!this.is_position_occupied(rnd_pos)) {
+        this.add_collectible(collectible, rnd_pos);
+    }
+}
+
+Controller.prototype.add_collectible = function(collectible, position){
+    collectible.position = position;
+    if(collectible.halflife_time > 0){
+        collectible.decay_time = createjs.Ticker.getTime() + collectible.halflife_time;
+    }
+    this.collectibles.push(collectible);
+    this.add_view(new ParticleView(collectible));
 }
 
 Controller.prototype.start_game = function(){
@@ -140,9 +150,44 @@ Controller.prototype.tick = function(event){
         }
         this.update_views();
 	}
+    this.check_decays();
 	this.stage.update(event);
 }
 
+Controller.prototype.check_decays = function(){
+    for(var pIndex in this.collectibles){
+        var p = this.collectibles[pIndex];
+        if(p.decay_time && p.decays && p.decays.length && createjs.Ticker.getTime() > p.decay_time){
+            var offset = {x: 0, y: 0};
+            var counter = 0;
+            for(var daughterInd in p.decays[0]){
+                if(counter % 2){
+                    offset.x = Math.floor((Math.random() * this.grid_size.x / 10)) % this.grid_size.x;
+                    offset.y = Math.floor((Math.random() * this.grid_size.y / 10)) % this.grid_size.y;
+                } else {
+                    offset.x = - offset.x;
+                    offset.y = - offset.y;
+                }
+                while(this.is_position_occupied(offset)){
+                    offset.x++;
+                    offset.y++;
+                }
+                var daughter = new Particle(p.position);
+                daughter.start_time = createjs.Ticker.getTime();
+                daughter.target = {
+                    time: daughter.start_time + 500,
+                    x: daughter.position.x + offset.x,
+                    y: daughter.position.y + offset.y
+                }
+                daughter.decays = null;
+                this.add_collectible(daughter, p.position);
+                counter++;
+            }
+            this.remove_collectible(p);
+        }
+    }
+}
+
 Controller.prototype.get_next_cell_position = function(){
 	var ph0 = this.snake.physicists[0];
 	var next_cell = Object.create(ph0.position);
@@ -174,10 +219,10 @@ Controller.prototype.is_position_occupied = function(position){
 	return null;
 }
 
-Controller.prototype.hit_test = function(e, particle){
+Controller.prototype.hit_test = function(particle){
 	for (var ph_i in this.snake.physicists){
 		var ph = this.snake.physicists[ph_i];
-		if (ph.view.hitTest(e.x, e.y)) {
+		if (ph.view.hitTest(particle.position.x, particle.position.y)) {
 			this.ph.collect(particle);
 		}
 	}

+ 2 - 1
js/models/particle.js

@@ -13,9 +13,10 @@ var Particle = function(position){
 	this.charge = 0;
 	this.start_time = 1234;
 	this.decay_time = 4444;
+    this.halflife_time = 1000;
 	this.target = null;
 	this.velocity = null;
 	this.points = 125;
 	this.parent_type = "W";
-	this.decays = [{particles: ["W", "W"], probability: .3 }]
+	this.decays = [{particles: ["W", "W"], probability: 1}]
 }

+ 8 - 6
js/views/particle.js

@@ -11,9 +11,7 @@
 				dp.outer_center.x * cs, dp.outer_center.y * cs, dp.outer_radius * cs)
 			.drawCircle(0, 0, cs * dp.outer_radius);
         // this.graphics.beginFill("red").drawCircle(0, 0, window.cell_size * 0.2 / 2);
-        createjs.Tween.get(this, {loop: true})
-            .to({ scaleX: 0.9, scaleY: 1.1 }, 200)
-            .to({ scaleX: 1.1, scaleY: 0.9 }, 200);
+        this.animate();
     }
 
     ParticleView.prototype = new createjs.Shape();
@@ -37,14 +35,18 @@
                 // {guide:{ path:[0,0, 0,200,200,200, 200,0,0,0] }},
                 this.model.target.time - this.model.start_time
 				)
-			.addEventListener("change", function(e){
-				controller.hit_test(e, own_view.model);
-				})
+			.addEventListener("change", function(){
+				controller.hit_test(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;
             });
+        } else {
+            createjs.Tween.get(this, {loop: true})
+                .to({ scaleX: 0.9, scaleY: 1.1 }, 200)
+                .to({ scaleX: 1.1, scaleY: 0.9 }, 200);
         }
     }