-
Notifications
You must be signed in to change notification settings - Fork 1
/
goodies.js
113 lines (92 loc) · 2.48 KB
/
goodies.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
function drawBall(fill, stroke, radius) {
var g_canvas = document.createElement('canvas');
g_canvas.width = radius*2 + 4;
g_canvas.height = radius*2 + 4;
var g_context = g_canvas.getContext("2d");
g_context.arc(radius+2,radius+2,radius-1.5,0,Math.PI*2,false);
g_context.fillStyle = fill;
g_context.strokeStyle = stroke;
g_context.lineWidth = 3;
g_context.stroke();
g_context.fill();
return g_canvas;
}
var gumdrops = {
'green': drawBall('#0f0', '#030', 8),
'red': drawBall('#f00', '#300', 8),
'blue': drawBall('#00f', '#003', 8),
'gold': drawBall('#ff0', '#330', 8),
};
function Goody (points, x, y, vx, vy) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.points = points;
if (points > 0) {
if (points < 100) {
this.color = 'green';
this.effect = ['#', 0];
} else {
this.color = 'gold';
this.effect = ['rgba(255,255,0,$)', 60];
}
} else if (points < 0) {
this.color = 'red';
this.effect = ['rgba(255,0,0,$)', 30];
} else {
this.color = 'blue';
this.effect = ['rgba(0,0,255,$)', 60];
}
this.radius = 8;
this.tick = function (mult, magnet, mx, my) {
if (this.points > 0 && magnet) {
var d = dist2(mx, my, this.x, this.y);
var correction = magnet / (d * d);
if (this.points >= 100) correction = -correction;
this.vx += correction * (mx - this.x);
this.vy += correction * (my - this.y);
}
this.x += mult * this.vx;
this.y += mult * this.vy;
return this.y;
}
this.draw = function (ctx, maxHeight) {
if (this.y > maxHeight) return;
ctx.drawImage(gumdrops[this.color], this.x - this.radius - 2, this.y - this.radius - 2);
}
}
function PowerUp (style, goody) {
this.__proto__ = goody;
this.style = style;
this.effect[1] = 0;
this.points = 0;
this.radius = 10;
this.countdown = 60;
this.draw = function (ctx) {
ctx.save();
ctx.fillStyle = '#fff';
ctx.strokeStyle = '#000';
ctx.lineWidth = 2;
ctx.translate(this.x, this.y);
ctx.beginPath();
ctx.arc(0, 0, this.radius, 0, Math.PI * 2, false);
if (this.style == 'x') {
ctx.moveTo(-this.radius/2.5, -this.radius/2.5);
ctx.lineTo(this.radius/2.5, this.radius/2.5);
ctx.moveTo(-this.radius/2.5, this.radius/2.5);
ctx.lineTo(this.radius/2.5, -this.radius/2.5);
} else {
ctx.stroke();
this.countdown++;
while (this.countdown >= 120) this.countdown -= 120;
if (this.countdown < 60) {
ctx.beginPath();
var r = this.radius * this.countdown / 60;
ctx.arc(0, 0, r, 0, Math.PI * 2, false);
}
}
ctx.stroke();
ctx.restore();
}
}