-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.js
More file actions
209 lines (190 loc) · 5.85 KB
/
util.js
File metadata and controls
209 lines (190 loc) · 5.85 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
class Vector {
constructor(vec_or_x, y=null) {
if (y != null) {
this.x = vec_or_x;
this.y = y;
} else {
this.x = vec_or_x.x;
this.y = vec_or_x.y;
}
this.length = Math.sqrt(this.x * this.x + this.y * this.y);
}
add(vec_or_x, y=null) {
if (y != null) {
this.x += vec_or_x;
this.y += y;
} else {
this.x += vec_or_x.x;
this.y += vec_or_x.y;
}
this.length = Math.sqrt(this.x * this.x + this.y * this.y);
}
subtract(vec_or_x, y=null) {
if (y != null) {
this.x -= vec_or_x;
this.y -= y;
} else {
this.x -= vec_or_x.x;
this.y -= vec_or_x.y;
}
this.length = Math.sqrt(this.x * this.x + this.y * this.y);
}
scale(s) {
this.x *= s;
this.y *= s;
this.length = Math.sqrt(this.x * this.x + this.y * this.y);
}
normalize() {
this.x /= this.length;
this.x = Math.round(this.x);
this.y /= this.length;
this.y = Math.round(this.y);
this.length = Math.sqrt(this.x * this.x + this.y * this.y);
}
isInside(array){
for (var i=0; i<array.length; i++) {
if(array[i].x == this.x && array[i].y == this.y) {
return true;
}
}
return false;
}
isEqual(vec) {
return this.x == vec.x && this.y == vec.y;
}
}
class Rect {
// A Rectangle, facilitate geometry calculations.
constructor(x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.left = x;
this.right = x + width;
this.top = y;
this.bottom = y + height;
this.centerx = x + width / 2;
this.centery = y + height / 2;
}
hasPoint(vec) {
// True if the given point in inside the rect
return vec.x > this.left && vec.x < this.right && vec.y > this.top && vec.y < this.bottom;
}
asPointArray() {
// Return every point inside this rect
var points = [];
for (var y=this.top; y<this.bottom; y++) {
for (var x=this.left; x<this.right; x++) {
points.push(new Vector(x, y));
}
}
return points;
}
randomPoint() {
// Return a random point inside this rect
var x = randint(this.left, this.right),
y = randint(this.top, this.bottom);
return new Vector(x, y);
}
}
function randint(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
class Button extends Rect {
constructor(x, y, width, height, text, fontsize, color, callback) {
super(x, y, width, height);
this.text = text;
this.fontsize = fontsize;
this.color = color;
this.callback = callback;
}
draw() {
ctx.font = this.fontsize + "px 'Courier New'";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.strokeStyle = this.color;
ctx.strokeRect(this.x, this.y, this.width, this.height);
ctx.fillStyle = this.color;
ctx.fillText(this.text, this.x + (this.width/2), this.y + (this.height/2));
}
}
function getCursorPosition(canvas, event) {
// Get the mouse position relative to the canvas.
var rect = canvas.getBoundingClientRect();
var x = event.clientX - rect.left;
var y = event.clientY - rect.top;
return new Vector(x, y);
}
function clearCanvas() {
ctx.fillStyle = "rgb(25,25,25)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
class Animation extends Rect {
constructor(x, y, width, height, rgbArray, time, itensity) {
super(x, y, width, height);
this.start = delta;
this.destroy = false;
this.time = time;
this.itensity = itensity;
this.color = rgbArray;
}
draw() {
var alpha = (delta - this.start) / (1000 * this.time);
if (!this.destroy){
ctx.fillStyle = "rgba(" + this.color + "," + alpha + ")";
ctx.fillRect(this.x, this.y, this.width, this.height);
}
this.destroy = alpha > this.itensity;
}
}
function playAnimation(x, y, size, color, time=0.1, itensity=0.6) {
control.ani.push( new Animation(x, y, size, size, color, time, itensity) );
}
class FloatingText {
constructor(x, y, text, fontsize, color) {
this.x = x;
this.start_y = y;
this.y = y;
this.text = text;
this.fontsize = fontsize;
this.color = color;
this.destroy = false;
}
draw() {
if (!this.destroy) {
ctx.font = this.fontsize + "px 'Courier New'";
ctx.fillStyle = this.color;
ctx.fillText(this.text, this.x, this.y);
}
this.y--;
if (this.y < this.start_y-20) { this.destroy = true; }
}
}
function playFloatText(x, y, text, color='yellow', fontsize=10) {
control.ani.push( new FloatingText(x*TILESIZE + TILESIZE/2, y*TILESIZE, text, fontsize, color) );
}
class Bullet {
constructor(x, y, radius, rgbArray, time) {
this.x = x + randint(-5, 5);
this.y = y + randint(-5, 5);
this.radius = radius;
this.start = delta;
this.color = rgbArray;
this.time = time;
this.destroy = false;
}
draw() {
var alpha = 1 - (delta - this.start) / (1000 * this.time);
if (!this.destroy) {
ctx.fillStyle = "rgba(" + this.color + "," + alpha + ")";
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, 2*Math.PI);
ctx.fill();
}
this.destroy = alpha < 0;
}
}
function playBullet(x, y, radius=5, rgbArray=[255,0,0], time=0.5) {
control.ani.push( new Bullet(x*TILESIZE + TILESIZE/2, y*TILESIZE + TILESIZE/2, radius, rgbArray, time));
}