1 function Particle(stage)
2 {
3     this.stage = stage;
4     this.rotater = Stage.randomRotater();
5     this.reset();
6     this.move();
7 }
9 Particle.prototype =
10 {
11     sizeMinimum: 40,
12     sizeRange: 10,
14     reset: function()
15     {
16         var randSize = Math.round(Math.pow(Pseudo.random(), 4) * this.sizeRange + this.sizeMinimum);
17         this.size = new Point(randSize, randSize);
18         this.minPosition = this.size.multiply(.5);
19         this.maxPosition = this.stage.size.subtract(this.minPosition);
20     },
22     animate: function(timeDelta)
23     {
24         this.rotater.next(timeDelta);
26         this.position = this.position.add(this.velocity.multiply(timeDelta));
27         this.velocity.y += 0.03;
29         // If particle is going to move off right side
30         if (this.position.x > this.maxPosition.x) {
31             if (this.velocity.x > 0)
32                 this.velocity.x *= -1;
33             this.position.x = this.maxPosition.x;
34         } else if (this.position.x < this.minPosition.x) {
35             // If particle is going to move off left side
36             if (this.velocity.x < 0)
37                 this.velocity.x *= -1;
38             this.position.x = this.minPosition.x;
39         }
41         // If particle is going to move off bottom side
42         if (this.position.y > this.maxPosition.y) {
43             // Adjust direction but maintain magnitude
44             var magnitude = this.velocity.length();
45             this.velocity.x *= 1.5 + .005 * this.size.x;
46             this.velocity = this.velocity.normalize().multiply(magnitude);
47             if (Math.abs(this.velocity.y) < 0.7)
48                 this.reset();
49             else {
50                 if (this.velocity.y > 0)
51                     this.velocity.y *= -0.999;
52                 this.position.y = this.maxPosition.y;
53             }
54         } else if (this.position.y < this.minPosition.y) {
55             // If particle is going to move off top side
56             var magnitude = this.velocity.length();
57             this.velocity.x *= 1.5 + .005 * this.size.x;
58             this.velocity = this.velocity.normalize().multiply(magnitude);
59             if (this.velocity.y < 0)
60                 this.velocity.y *= -0.998;
61             this.position.y = this.minPosition.y;
62         }
64         this.move();
65     },
67     move: function()
68     {
69     }
70 }
72 ParticlesStage = Utilities.createSubclass(Stage,
73     function()
74     {
75         Stage.call(this);
76         this.particles = [];
77     }, {
79     animate: function(timeDelta)
80     {
81         timeDelta /= 4;
82         this.particles.forEach(function(particle) {
83             particle.animate(timeDelta);
84         });
85     },
87     tune: function(count)
88     {
89         if (count == 0)
90             return;
92         if (count > 0) {
93             for (var i = 0; i < count; ++i)
94                 this.particles.push(this.createParticle());
95             return;
96         }
98         count = Math.min(-count, this.particles.length);
100         if (typeof(this.willRemoveParticle) == "function") {
101             for (var i = 0; i < count; ++i)
102                 this.willRemoveParticle(this.particles[i]);
103         }
105         this.particles.splice(0, count);
106     },
108     complexity: function()
109     {
110         // We add one to represent the gradient background.
111         return this.particles.length + 1;
112     }
113 });