Refactor tune() to not return the complexity of the scene.
[WebKit-https.git] / PerformanceTests / Animometer / tests / bouncing-particles / resources / bouncing-particles.js
1 function BouncingParticle(stage)
2 {
3     this._stageSize = stage.size;
4     this.size = stage.particleSize;
5
6     this.position = Stage.randomPosition(stage.size.subtract(stage.particleSize));
7     this._angle = Stage.randomAngle();
8     this._velocity = Stage.randomVelocity(stage.maxVelocity);
9     this.rotater = Stage.randomRotater();
10 }
11
12 BouncingParticle.prototype =
13 {
14     get center()
15     {
16         return this.position.add(this.size.center);
17     },
18
19     animate: function(timeDelta)
20     {
21         this.position = this.position.move(this._angle, this._velocity, timeDelta);
22         this.rotater.next(timeDelta);
23
24         // If particle is going to move off right side
25         if (this.position.x + this.size.x > this._stageSize.x) {
26             // If direction is East-South, go West-South.
27             if (this._angle >= 0 && this._angle < Math.PI / 2)
28                 this._angle = Math.PI - this._angle;
29             // If angle is East-North, go West-North.
30             else if (this._angle > Math.PI / 2 * 3)
31                 this._angle = this._angle - (this._angle - Math.PI / 2 * 3) * 2;
32             // Make sure the particle does not go outside the stage boundaries.
33             this.position.x = this._stageSize.x - this.size.x;
34         }
35
36         // If particle is going to move off left side
37         if (this.position.x < 0) {
38             // If angle is West-South, go East-South.
39             if (this._angle > Math.PI / 2 && this._angle < Math.PI)
40                 this._angle = Math.PI - this._angle;
41             // If angle is West-North, go East-North.
42             else if (this._angle > Math.PI && this._angle < Math.PI / 2 * 3)
43                 this._angle = this._angle + (Math.PI / 2 * 3 - this._angle) * 2;
44             // Make sure the particle does not go outside the stage boundaries.
45             this.position.x = 0;
46         }
47
48         // If particle is going to move off bottom side
49         if (this.position.y + this.size.y > this._stageSize.y) {
50             // If direction is South, go North.
51             if (this._angle > 0 && this._angle < Math.PI)
52                 this._angle = Math.PI * 2 - this._angle;
53             // Make sure the particle does not go outside the stage boundaries.
54             this.position.y = this._stageSize.y - this.size.y;
55         }
56
57         // If particle is going to move off top side
58         if (this.position.y < 0) {
59             // If direction is North, go South.
60             if (this._angle > Math.PI && this._angle < Math.PI * 2)
61                 this._angle = this._angle - (this._angle - Math.PI) * 2;
62             // Make sure the particle does not go outside the stage boundaries.
63             this.position.y = 0;
64         }
65     }
66 }
67
68 BouncingParticlesStage = Utilities.createSubclass(Stage,
69     function()
70     {
71         Stage.call(this);
72         this.particles = [];
73     }, {
74
75     initialize: function(benchmark, options)
76     {
77         Stage.prototype.initialize.call(this, benchmark, options);
78         this.particleSize = new Point(parseInt(options["particleWidth"]) || 10, parseInt(options["particleHeight"]) || 10);
79         this.maxVelocity = Math.max(parseInt(options["maxVelocity"]) || 500, 100);
80     },
81
82     parseShapeParameters: function(options)
83     {
84         this.shape = options["shape"] || "circle";
85         this.fill = options["fill"] || "solid";
86         this.clip = options["clip"] || "";
87     },
88
89     animate: function(timeDelta)
90     {
91         this.particles.forEach(function(particle) {
92             particle.animate(timeDelta);
93         });
94     },
95
96     tune: function(count)
97     {
98         if (count == 0)
99             return;
100
101         if (count > 0) {
102             for (var i = 0; i < count; ++i)
103                 this.particles.push(this.createParticle());
104             return;
105         }
106
107         count = Math.min(-count, this.particles.length);
108
109         if (typeof(this.particleWillBeRemoved) == "function") {
110             for (var i = 0; i < count; ++i)
111                 this.particleWillBeRemoved(this.particles[this.particles.length - 1 - i]);
112         }
113
114         this.particles.splice(-count, count);
115     },
116
117     complexity: function()
118     {
119         return this.particles.length;
120     }
121 });