-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.js
More file actions
221 lines (199 loc) · 9.3 KB
/
common.js
File metadata and controls
221 lines (199 loc) · 9.3 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
210
211
212
213
214
215
216
217
218
219
220
221
// I KNEW IT, that this "POINT" would come, heh-eh
// So I made the points into vector2-s, like a normal game engine would have
class Vector2 {
static zero = Object.freeze(new Vector2(0, 0));
static one = Object.freeze(new Vector2(1, 1));
static up = Object.freeze(new Vector2(0, 1));
static down = Object.freeze(new Vector2(0, -1));
static left = Object.freeze(new Vector2(-1, 0));
static right = Object.freeze(new Vector2(1, 0));
constructor(x, y) {
this.x = x;
this.y = y;
}
add(p) { return new Vector2(this.x + p.x, this.y + p.y); }
subtr(p) { return new Vector2(this.x - p.x, this.y - p.y); }
negate() { return new Vector2(-this.x, -this.y); }
multiply(b) { return new Vector2(this.x * b, this.y * b); }
divide(b) { return new Vector2(this.x / b, this.y / b); }
equals(p) { return this.x === p.x && this.y === p.y; }
get sqrMagnitude() { return this.x * this.x + this.y * this.y; }
get magnitude() { return Math.sqrt(this.sqrMagnitude); }
static distance(a, b) { return Math.sqrt(this.sqrDistance(a, b)); }
static sqrDistance(a, b) { return Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2); }
static angleTowards(a, b) { return Math.atan2(b.y - a.y, b.x - a.x); }
// NOTE: before it was called 'moveDirection', so replace that old knowledge with this more accurate name
static fromAngle(angle, magnitude = 1) { return new Vector2(Math.cos(angle) * magnitude, Math.sin(angle) * magnitude); }
static normalize(a) {
const mag = a.magnitude;
return mag === 0 ? Vector2.zero : a.divide(mag);
}
toString() { return `(${this.x.toFixed(2)}, ${this.y.toFixed(2)})`;}
}
class Size {
static zero = new Size(0, 0);
static one = new Size(1, 1);
get area() { return this.width * this.height; }
constructor(width, height) {
this.width = width;
this.height = height;
}
}
class Rect {
static identity = new Rect(0, 0, 0, 0);
constructor(x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
static fromPointSize(point, size) {
return new Rect(point.x, point.y, size.width, size.height);
}
}
class ClipRegion extends Rect {
constructor(x, y, width, height, atlas, scale) {
super(x, y, width, height);
this.atlas = atlas;
this.scale = scale;
}
fullSize() { return new Size(this.width * this.scale, this.height * this.scale); }
}
const UIAtlas = Object.freeze({
ButtonRedLarge: new ClipRegion(0, 0, 60, 10, 'ui_atlas', 10),
ButtonRedSmall: new ClipRegion(60, 0, 40, 10, 'ui_atlas', 10),
ButtonYellowLarge: new ClipRegion(0, 10, 60, 10, 'ui_atlas', 10),
ButtonYellowSmall: new ClipRegion(60, 10, 40, 10, 'ui_atlas', 10),
ButtonBlueLarge: new ClipRegion(0, 20, 60, 10, 'ui_atlas', 10),
ButtonBlueSmall: new ClipRegion(60, 20, 40, 10, 'ui_atlas', 10),
ButtonGreenLarge: new ClipRegion(0, 30, 60, 10, 'ui_atlas', 10),
ButtonGreenSmall: new ClipRegion(60, 30, 40, 10, 'ui_atlas', 10),
HL_ButtonRedLarge: new ClipRegion(0, 40, 60, 10, 'ui_atlas', 10),
HL_ButtonRedSmall: new ClipRegion(60, 40, 40, 10, 'ui_atlas', 10),
HL_ButtonYellowLarge: new ClipRegion(0, 50, 60, 10, 'ui_atlas', 10),
HL_ButtonYellowSmall: new ClipRegion(60, 50, 40, 10, 'ui_atlas', 10),
HL_ButtonBlueLarge: new ClipRegion(0, 60, 60, 10, 'ui_atlas', 10),
HL_ButtonBlueSmall: new ClipRegion(60, 60, 40, 10, 'ui_atlas', 10),
HL_ButtonGreenLarge: new ClipRegion(0, 70, 60, 10, 'ui_atlas', 10),
HL_ButtonGreenSmall: new ClipRegion(60, 70, 40, 10, 'ui_atlas', 10),
ButtonArrow: new ClipRegion(0, 81, 24, 17, 'ui_atlas', 5),
HL_ButtonArrow: new ClipRegion(0, 81, 24, 17, 'ui_atlas', 5),
});
const ButtonTypes = Object.freeze({
RedLarge: 'ButtonRedLarge',
RedSmall: 'ButtonRedSmall',
YellowLarge: 'ButtonYellowLarge',
YellowSmall: 'ButtonYellowSmall',
BlueLarge: 'ButtonBlueLarge',
BlueSmall: 'ButtonBlueSmall',
GreenLarge: 'ButtonGreenLarge',
GreenSmall: 'ButtonGreenSmall',
Arrow: 'ButtonArrow',
});
function getButtonSize(btntype) { return UIAtlas[btntype].fullSize(); }
class PointerType {
constructor(id, hotspot) {
this.id = id;
this.hotspot = hotspot;
}
}
const PointerTypes = Object.freeze({
POINTER: new PointerType(1, new Vector2(11, 6)),
HAND: new PointerType(2, new Vector2(17, 3)),
});
class ScaleAndRotateAnimation {
static apply = this.bind(2, 0.05235, 32); // Note rotate = 3 degrees in radians
static #handle(e, speed, rotate, scaleAmount) {
// Ensure that the xy coordinates are at the button's center
const x = e.x + e.width / 2;
const y = e.y + e.height / 2;
const scale = Math.cos(animationNow() * speed) / scaleAmount + 1 + (1 / scaleAmount);
// We set the pivot point for the rotation to the center of the button
ctx.translate(x, y);
ctx.rotate(Math.sin(animationNow() * (speed + .5)) * rotate);
ctx.scale(scale, scale);
return new Vector2(-e.width / 2, -e.height / 2);
}
static bind(speed, rotate, scaleAmount) { return (e) => { return ScaleAndRotateAnimation.#handle(e, speed, rotate, scaleAmount); } };
}
class HoverAnimation {
static apply = this.bind(2, Vector2.up.multiply(3), 0);
static #handle(e, speed, vector, offset) {
const t = Math.sin(animationNow() * speed + offset);
const vec = vector.multiply(t);
ctx.translate(-vec.x, vec.y);
return new Vector2(e.x, e.y);
}
static bind(speed, vector, offset) { return (e) => { return HoverAnimation.#handle(e, speed, vector, offset); } };
}
class HoverFlyAnimation {
static apply = this.bind(.3, Vector2.up.multiply(16));
static #handle(e, duration, vector) {
if (e.mouseOver) {
const t = Math.min(1, (animationNow() - e.hoverStart) / duration);
const vec = vector.multiply(-interpolateEaseIn(t, 3));
ctx.translate(-vec.x, vec.y);
return;
}
const hoverEnds = e.hoverEnd + duration
if (animationNow() <= hoverEnds) {
const t = Math.min(1, (hoverEnds - animationNow()) / duration);
const vec = vector.multiply(-interpolateEaseIn(t, 3));
ctx.translate(-vec.x, vec.y);
}
}
static bind(duration, vector) { return (e) => { HoverFlyAnimation.#handle(e, duration, vector); }; }
}
class StartFadeFlyUpAnimation {
static apply = this.bind(.4, Vector2.up.multiply(120));
static #handle(e, duration, vector) {
// For this we could use the canvas translating, I may modify this to return a point instead in the future to replace the canvas transformations
// TODO: Make something similar to these animation classes for easing functions, eg: linear, ease in/out, quadratic, cubic etc.
// And more complex ones
// Also for waves, like sine, cosine, but more complex, that would produce more natural-looking animations
const t = interpolateEaseOut(Math.min(1, (animationNow() - e.activeTime) / duration), 3);
const newVector = vector.multiply(1 - t);
ctx.globalAlpha = scaleAlpha(t);
ctx.translate(newVector.x, newVector.y);
}
static bind(duration, vector, shadowingUpdates = true) {
return {
duration: duration,
shadowingUpdates: shadowingUpdates,
render: (e) => { StartFadeFlyUpAnimation.#handle(e, duration, vector); }
};
}
}
function renderBackground() {
// Render backgrounds, No explaining,
// Just to know, didn't take that long, hehe ;)
// Just a handful of hours to figure it out on my own — I already did this on another project
const renderSize = 1024;
const sourceSize = 256
let startX = viewport.viewLeft - revTranslateX(viewport.viewLeft) % renderSize;
let cellX = Math.floor(revTranslateX(viewport.viewLeft) / renderSize);
let startY = viewport.viewTop - revTranslateY(viewport.viewTop) % renderSize;
let cellY = Math.floor(revTranslateY(viewport.viewTop) / renderSize);
if (scene.scrollX <= viewport.visibleWidth2)
startX -= renderSize;
if (scene.scrollY <= viewport.visibleHeight2)
startY -= renderSize;
const visibleFirstX = renderSize - (viewport.viewLeft - startX);
const amountX = Math.ceil((viewport.visibleWidth - visibleFirstX) / renderSize) + 1;
const visibleFirstY = renderSize - (viewport.viewTop - startY);
const amountY = Math.ceil((viewport.visibleHeight - visibleFirstY) / renderSize) + 1;
ctx.font = '24px "Jersey 10"';
ctx.fillStyle = 'black';
ctx.globalAlpha = 1; // Note, we don't care about the scaled alpha, as the background must be opaque!!!
for (let y = 0; y < amountY; y++) {
for (let x = 0; x < amountX; x++) {
const drawX = Math.ceil(startX + renderSize * x);
const drawY = Math.ceil(startY + renderSize * y);
const tileX = ((cellX + x) % 4 + 4) % 4;
const tileY = ((cellY + y) % 4 + 4) % 4;
ctx.drawImage(images['grid'], tileX * sourceSize, tileY * sourceSize, sourceSize, sourceSize, drawX, drawY, renderSize + 1, renderSize + 1); // Overdraw just 1 px to fix stitching issues, it's gone now
//ctx.fillText(cellX + x, drawX, startY + renderSize * (y + .5));
}
}
}
function animationNow() { return performance.now() / 1000; }