-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloatys.js
71 lines (61 loc) · 2.06 KB
/
Floatys.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*global Colors*/
/*global Floaty*/
"use strict";
function Floatys() {
this.floatys = [];
this.max = 0;
this.current = 0;
this.max_floaty = 100;
this.radii_start = 3;
this.radii_diff = 47;
this.speed_start = 0.3;
this.speed_diff = 0.3;
}
Floatys.prototype.draw = function (context) {
var i = null;
for (i = 0; i < this.floatys.length; i += 1) {
this.floatys[i].draw(context);
}
};
Floatys.prototype.update = function (low, high, left, width) {
var i = null,
x = null,
y = null,
extra = 16000,
floaty = null,
radius = null,
speed = null,
x_left = left,
x_right = left + width,
y_top = high,
y_bottom = low;
if (high > low) {
console.log("Incorrect low/high given. 'high' must be less than 'low'. 'high' represents the top y coordinate, 'low' the bottom coordinate of the screen.");
}
for (i = 0; i < this.floatys.length; i += 1) {
x = this.floatys[i].xPos();
y = this.floatys[i].yPos();
if (x < x_left - extra || x > (x_right + extra) || y > (y_bottom + extra) || y < (y_top - extra)) {
this.floatys.splice(i, 1);
}
}
radius = this.radii_start + Math.random() * this.radii_diff;
speed = this.speed_start + Math.random() * this.speed_diff;
if (this.current >= this.max) {
if (this.floatys.length < this.max_floaty) {
if (Math.random() >= 0.5) {
floaty = new Floaty(x_left - extra + Math.random() * ((x_right + extra) - (x_left - extra)), y_bottom + Math.random() * extra, radius, speed);
} else {
floaty = new Floaty(x_left - extra + Math.random() * ((x_right + extra) - (x_left - extra)), y_top - Math.random() * extra, radius, speed);
}
floaty.setColor((new Colors()).random());
this.floatys.push(floaty);
}
this.current = 0;
} else {
this.current += 1;
}
for (i = 0; i < this.floatys.length; i += 1) {
this.floatys[i].move();
}
};