frigame3 0.0.0-stage → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/LICENSE +373 -0
- package/README.md +80 -2
- package/biome.json +93 -0
- package/demo/package-lock.json +515 -0
- package/demo/package.json +19 -0
- package/demo/snake/index.html +16 -0
- package/demo/snake/index.ts +110 -0
- package/demo/tetris/index.html +16 -0
- package/demo/tetris/index.ts +536 -0
- package/demo/tsconfig.json +53 -0
- package/frigame.css +12 -0
- package/package.json +50 -4
- package/src/Animation.ts +266 -0
- package/src/BaseSprite.ts +682 -0
- package/src/Gradient.ts +120 -0
- package/src/Playground.ts +247 -0
- package/src/Rect.ts +237 -0
- package/src/Rectangle.ts +108 -0
- package/src/Renderer.ts +28 -0
- package/src/ResourceManager.ts +145 -0
- package/src/Sprite.ts +484 -0
- package/src/SpriteGroup.ts +258 -0
- package/src/canvasRenderer/renderGroup.ts +194 -0
- package/src/canvasRenderer/renderPlayground.ts +103 -0
- package/src/canvasRenderer/renderRectangle.ts +421 -0
- package/src/canvasRenderer/renderSprite.ts +148 -0
- package/src/canvasRenderer.ts +38 -0
- package/src/dummyRenderer.ts +62 -0
- package/src/plugins/GamepadTracker.ts +97 -0
- package/src/plugins/Tilemap.ts +123 -0
- package/src/plugins/Vec2.ts +420 -0
- package/src/plugins/fx/Shaker.ts +293 -0
- package/src/plugins/fx/Tweener.ts +252 -0
- package/src/plugins/fx/easing.ts +276 -0
- package/src/plugins/isometric/ISOMixin.ts +444 -0
- package/src/plugins/isometric/ISORectangle.ts +183 -0
- package/src/plugins/isometric/ISOSprite.ts +228 -0
- package/src/plugins/isometric/ISOSpriteGroup.ts +155 -0
- package/src/plugins/isometric/ISOTilemap.ts +122 -0
- package/src/plugins/isometric/utils.ts +19 -0
- package/src/plugins/keyTracker.ts +31 -0
- package/src/plugins/mixer.ts +523 -0
- package/src/plugins/sorted/SortedAnimation.ts +53 -0
- package/src/plugins/sorted/SortedGroup.ts +94 -0
- package/src/plugins/sorted/SortedMixin.ts +203 -0
- package/src/plugins/sorted/SortedRectangle.ts +35 -0
- package/src/plugins/sorted/SortedSprite.ts +56 -0
- package/src/utils.ts +53 -0
- package/test/animation/index.html +20 -0
- package/test/animation/index.ts +253 -0
- package/test/animation/mh.png +0 -0
- package/test/animation/moh.png +0 -0
- package/test/animation/mov.png +0 -0
- package/test/animation/mv.png +0 -0
- package/test/animation/rebound.png +0 -0
- package/test/animation/reboundm.png +0 -0
- package/test/animation/sh.png +0 -0
- package/test/animation/soh.png +0 -0
- package/test/animation/sov.png +0 -0
- package/test/animation/sv.png +0 -0
- package/test/isometric1/beach.png +0 -0
- package/test/isometric1/foglie.png +0 -0
- package/test/isometric1/grass.png +0 -0
- package/test/isometric1/index.html +17 -0
- package/test/isometric1/index.ts +68 -0
- package/test/isometric1/tronco.png +0 -0
- package/test/isometric1/untitled.tmx +25 -0
- package/test/isometric1/water.png +0 -0
- package/test/isometric2/banner.png +0 -0
- package/test/isometric2/books.png +0 -0
- package/test/isometric2/candlestick.png +0 -0
- package/test/isometric2/ice.png +0 -0
- package/test/isometric2/index.html +16 -0
- package/test/isometric2/index.ts +129 -0
- package/test/isometric2/knight_se.png +0 -0
- package/test/isometric2/tile.png +0 -0
- package/test/isometric2/untitled.tmx +36 -0
- package/test/isometric2/wall.png +0 -0
- package/test/layers/boy.png +0 -0
- package/test/layers/catgirl.png +0 -0
- package/test/layers/fri1.svg +8 -0
- package/test/layers/grassblock.png +0 -0
- package/test/layers/horngirl.png +0 -0
- package/test/layers/index.html +17 -0
- package/test/layers/index.ts +191 -0
- package/test/layers/pinkgirl.png +0 -0
- package/test/layers/render_ruler.py +57 -0
- package/test/layers/ruler.png +0 -0
- package/test/layers/sh.png +0 -0
- package/test/mixer/dieci.opus +0 -0
- package/test/mixer/dieci.wav +0 -0
- package/test/mixer/index.html +33 -0
- package/test/mixer/index.ts +206 -0
- package/test/mixer/news_intro.mp3 +0 -0
- package/test/mixer/news_intro.ogg +0 -0
- package/test/mixer/prova.mp3 +0 -0
- package/test/mixer/prova.ogg +0 -0
- package/test/package-lock.json +515 -0
- package/test/package.json +19 -0
- package/test/sorted/Brown Block.png +0 -0
- package/test/sorted/index.html +16 -0
- package/test/sorted/index.ts +38 -0
- package/test/transformation1/index.html +17 -0
- package/test/transformation1/index.ts +51 -0
- package/test/transformation1/sh.png +0 -0
- package/test/transformation2/index.html +20 -0
- package/test/transformation2/index.ts +71 -0
- package/test/transformation2/sh.png +0 -0
- package/test/tsconfig.json +53 -0
- package/tsconfig.json +51 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { Playground } from "../Playground.js";
|
|
2
|
+
|
|
3
|
+
export class GamepadTracker extends EventTarget {
|
|
4
|
+
controllers: Map<number, Gamepad> = new Map<number, Gamepad>();
|
|
5
|
+
|
|
6
|
+
_playground: WeakRef<Playground>;
|
|
7
|
+
_callbackId: number | null = null;
|
|
8
|
+
|
|
9
|
+
constructor(playground: Playground) {
|
|
10
|
+
super();
|
|
11
|
+
|
|
12
|
+
this._playground = new WeakRef(playground);
|
|
13
|
+
|
|
14
|
+
this.registerCallback();
|
|
15
|
+
|
|
16
|
+
playground.addEventListener("clearCallbacks", this._onClearCallbacks);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
_scanGamepads = (): void => {
|
|
20
|
+
const controllers = this.controllers;
|
|
21
|
+
const gamepads = navigator.getGamepads();
|
|
22
|
+
|
|
23
|
+
// Step 1: Update controller status
|
|
24
|
+
for (const gamepad of gamepads) {
|
|
25
|
+
if (gamepad !== null) {
|
|
26
|
+
controllers.set(gamepad.index, gamepad);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Step 2: Remove disconnected controllers
|
|
31
|
+
const indexes = new Set(
|
|
32
|
+
gamepads
|
|
33
|
+
.filter((gamepad: Gamepad | null): boolean => gamepad !== null)
|
|
34
|
+
.map((gamepad: Gamepad | null): number => gamepad!.index),
|
|
35
|
+
);
|
|
36
|
+
for (const index of controllers.keys()) {
|
|
37
|
+
if (!indexes.has(index)) {
|
|
38
|
+
controllers.delete(index);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
registerCallback(options?: { suppressWarning?: boolean }): this {
|
|
44
|
+
if (this._callbackId === null) {
|
|
45
|
+
const playground = this._playground.deref();
|
|
46
|
+
|
|
47
|
+
if (playground !== undefined) {
|
|
48
|
+
this._callbackId = playground.registerCallback(this._scanGamepads);
|
|
49
|
+
} else {
|
|
50
|
+
if (
|
|
51
|
+
typeof console !== "undefined" &&
|
|
52
|
+
options?.suppressWarning === false
|
|
53
|
+
) {
|
|
54
|
+
console.warn("playground has been garbage collected");
|
|
55
|
+
console.trace();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
if (
|
|
60
|
+
typeof console !== "undefined" &&
|
|
61
|
+
options?.suppressWarning === false
|
|
62
|
+
) {
|
|
63
|
+
console.warn("Callback already registered");
|
|
64
|
+
console.trace();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
removeCallback(options?: { suppressWarning?: boolean }): this {
|
|
72
|
+
const playground = this._playground.deref();
|
|
73
|
+
|
|
74
|
+
if (playground !== undefined) {
|
|
75
|
+
playground.removeCallback(this._callbackId, options);
|
|
76
|
+
} else {
|
|
77
|
+
if (
|
|
78
|
+
typeof console !== "undefined" &&
|
|
79
|
+
options?.suppressWarning === false
|
|
80
|
+
) {
|
|
81
|
+
console.warn("playground has been garbage collected");
|
|
82
|
+
console.trace();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
this._callbackId = null;
|
|
87
|
+
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
_onClearCallbacks = (): void => {
|
|
92
|
+
if (this._callbackId !== null) {
|
|
93
|
+
this._callbackId = null;
|
|
94
|
+
this.registerCallback();
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type { BaseSprite } from "../BaseSprite.js";
|
|
2
|
+
import type { RectangleOptions } from "../Rectangle.js";
|
|
3
|
+
import { Rectangle } from "../Rectangle.js";
|
|
4
|
+
import type { SpriteOptions } from "../Sprite.js";
|
|
5
|
+
import { Sprite } from "../Sprite.js";
|
|
6
|
+
import type { SpriteGroupOptions } from "../SpriteGroup.js";
|
|
7
|
+
import { SpriteGroup } from "../SpriteGroup.js";
|
|
8
|
+
import type { SpriteRef } from "../utils.js";
|
|
9
|
+
|
|
10
|
+
// animationList MUST have at least `animation` or `background`
|
|
11
|
+
export type TileOptions =
|
|
12
|
+
| (Pick<SpriteOptions, "animation"> & Partial<SpriteOptions>)
|
|
13
|
+
| (Pick<RectangleOptions, "background"> & Partial<RectangleOptions>);
|
|
14
|
+
|
|
15
|
+
export interface TilemapOptions {
|
|
16
|
+
sizex: number; // Num tiles
|
|
17
|
+
sizey: number; // Num tiles
|
|
18
|
+
tileWidth: number; // Pixel
|
|
19
|
+
tileHeight: number; // Pixel
|
|
20
|
+
data: number[]; // (sizex * sizey) members, indices of animationList
|
|
21
|
+
animationList: Record<number, TileOptions>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class Tilemap extends SpriteGroup {
|
|
25
|
+
sizex = 0;
|
|
26
|
+
sizey = 0;
|
|
27
|
+
tileWidth = 0;
|
|
28
|
+
tileHeight = 0;
|
|
29
|
+
|
|
30
|
+
_locations: Uint32Array;
|
|
31
|
+
|
|
32
|
+
constructor(
|
|
33
|
+
options: TilemapOptions &
|
|
34
|
+
Partial<SpriteGroupOptions> & { ref?: SpriteRef<Tilemap> },
|
|
35
|
+
) {
|
|
36
|
+
super(options);
|
|
37
|
+
|
|
38
|
+
const { sizex, sizey, tileWidth, tileHeight } = options;
|
|
39
|
+
|
|
40
|
+
this.sizex = sizex;
|
|
41
|
+
this.sizey = sizey;
|
|
42
|
+
this.tileWidth = tileWidth;
|
|
43
|
+
this.tileHeight = tileHeight;
|
|
44
|
+
|
|
45
|
+
const locations = new Uint32Array(sizex * sizey).fill(0xffffffff);
|
|
46
|
+
this._locations = locations;
|
|
47
|
+
const layers = this._layers;
|
|
48
|
+
|
|
49
|
+
if (
|
|
50
|
+
options.width === undefined &&
|
|
51
|
+
options.halfWidth === undefined &&
|
|
52
|
+
options.radius === undefined
|
|
53
|
+
) {
|
|
54
|
+
this.width = sizex * tileWidth;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (
|
|
58
|
+
options.height === undefined &&
|
|
59
|
+
options.halfHeight === undefined &&
|
|
60
|
+
options.radius === undefined
|
|
61
|
+
) {
|
|
62
|
+
this.height = sizey * tileHeight;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const animationList = options.animationList;
|
|
66
|
+
|
|
67
|
+
let i_location = 0;
|
|
68
|
+
let row = 0;
|
|
69
|
+
let col = 0;
|
|
70
|
+
let left = 0;
|
|
71
|
+
let top = 0;
|
|
72
|
+
for (const data of options.data) {
|
|
73
|
+
const animation_options = animationList[data];
|
|
74
|
+
if (animation_options !== undefined && "animation" in animation_options) {
|
|
75
|
+
// Sprite
|
|
76
|
+
const sprite_options = Object.assign({}, animation_options, {
|
|
77
|
+
left,
|
|
78
|
+
top,
|
|
79
|
+
});
|
|
80
|
+
this.addChild(new Sprite(sprite_options));
|
|
81
|
+
locations[i_location] = layers.length - 1;
|
|
82
|
+
} else if (
|
|
83
|
+
animation_options !== undefined &&
|
|
84
|
+
"background" in animation_options
|
|
85
|
+
) {
|
|
86
|
+
// Rectangle
|
|
87
|
+
const rectangle_options = Object.assign({}, animation_options, {
|
|
88
|
+
left,
|
|
89
|
+
top,
|
|
90
|
+
width: tileWidth,
|
|
91
|
+
height: tileHeight,
|
|
92
|
+
});
|
|
93
|
+
this.addChild(new Rectangle(rectangle_options));
|
|
94
|
+
locations[i_location] = layers.length - 1;
|
|
95
|
+
} else {
|
|
96
|
+
// TODO: Invalid
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
i_location += 1;
|
|
100
|
+
left += tileWidth;
|
|
101
|
+
col += 1;
|
|
102
|
+
if (col >= sizex) {
|
|
103
|
+
col = 0;
|
|
104
|
+
left = 0;
|
|
105
|
+
row += 1;
|
|
106
|
+
top += tileHeight;
|
|
107
|
+
if (row >= sizey) {
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
getAt(row: number, col: number): BaseSprite | undefined {
|
|
115
|
+
const i = row * this.sizex + col;
|
|
116
|
+
|
|
117
|
+
if (i < this._locations.length) {
|
|
118
|
+
return this._layers[this._locations[i]!];
|
|
119
|
+
} else {
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
export interface IVec2 {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export class Vec2 implements IVec2 {
|
|
7
|
+
x: number;
|
|
8
|
+
y: number;
|
|
9
|
+
|
|
10
|
+
constructor(x: number, y: number) {
|
|
11
|
+
this.x = x;
|
|
12
|
+
this.y = y;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static clone(a: IVec2): Vec2 {
|
|
16
|
+
return new Vec2(a.x, a.y);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
clone(b: IVec2): this {
|
|
20
|
+
this.x = b.x;
|
|
21
|
+
this.y = b.y;
|
|
22
|
+
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
static fromValues(x: number, y: number): Vec2 {
|
|
27
|
+
return new Vec2(x, y);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
fromValues(x: number, y: number): this {
|
|
31
|
+
this.x = x;
|
|
32
|
+
this.y = y;
|
|
33
|
+
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static fromPolar(mag: number, angle: number): Vec2 {
|
|
38
|
+
return new Vec2(Math.cos(angle) * mag, Math.sin(angle) * mag);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
fromPolar(mag: number, angle: number): this {
|
|
42
|
+
this.x = Math.cos(angle) * mag;
|
|
43
|
+
this.y = Math.sin(angle) * mag;
|
|
44
|
+
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
static randomUnit(): Vec2 {
|
|
49
|
+
const angle = Math.random() * Math.PI * 2;
|
|
50
|
+
|
|
51
|
+
return new Vec2(Math.cos(angle), Math.sin(angle));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
randomUnit(): this {
|
|
55
|
+
const angle = Math.random() * Math.PI * 2;
|
|
56
|
+
|
|
57
|
+
this.x = Math.cos(angle);
|
|
58
|
+
this.y = Math.sin(angle);
|
|
59
|
+
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static random(scalar = 1): Vec2 {
|
|
64
|
+
const mag = Math.random() * scalar;
|
|
65
|
+
const angle = Math.random() * Math.PI * 2;
|
|
66
|
+
|
|
67
|
+
return new Vec2(Math.cos(angle) * mag, Math.sin(angle) * mag);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
random(scalar = 1): this {
|
|
71
|
+
const mag = Math.random() * scalar;
|
|
72
|
+
const angle = Math.random() * Math.PI * 2;
|
|
73
|
+
|
|
74
|
+
this.x = Math.cos(angle) * mag;
|
|
75
|
+
this.y = Math.sin(angle) * mag;
|
|
76
|
+
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
static add(a: IVec2, b: IVec2): Vec2 {
|
|
81
|
+
return new Vec2(a.x + b.x, a.y + b.y);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
add(b: IVec2): this {
|
|
85
|
+
this.x += b.x;
|
|
86
|
+
this.y += b.y;
|
|
87
|
+
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
static sub(a: IVec2, b: IVec2): Vec2 {
|
|
92
|
+
return new Vec2(a.x - b.x, a.y - b.y);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
sub(b: IVec2): this {
|
|
96
|
+
this.x -= b.x;
|
|
97
|
+
this.y -= b.y;
|
|
98
|
+
|
|
99
|
+
return this;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
static scale(a: IVec2, sx: number, sy: number = sx): Vec2 {
|
|
103
|
+
return new Vec2(a.x * sx, a.y * sy);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
scale(sx: number, sy: number = sx): this {
|
|
107
|
+
this.x *= sx;
|
|
108
|
+
this.y *= sy;
|
|
109
|
+
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
static invert(a: IVec2): Vec2 {
|
|
114
|
+
return new Vec2(-a.x, -a.y);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
invert(): this {
|
|
118
|
+
this.x *= -1;
|
|
119
|
+
this.y *= -1;
|
|
120
|
+
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
static normalize(a: IVec2): Vec2 {
|
|
125
|
+
let mag = Math.hypot(a.x, a.y);
|
|
126
|
+
if (mag !== 0) {
|
|
127
|
+
mag = 1 / mag;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return new Vec2(a.x * mag, a.y * mag);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
normalize(): this {
|
|
134
|
+
let mag = Math.hypot(this.x, this.y);
|
|
135
|
+
if (mag !== 0) {
|
|
136
|
+
mag = 1 / mag;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
this.x *= mag;
|
|
140
|
+
this.y *= mag;
|
|
141
|
+
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
static rotate(a: IVec2, angle: number): Vec2 {
|
|
146
|
+
const x = a.x;
|
|
147
|
+
const y = a.y;
|
|
148
|
+
const ct = Math.cos(angle);
|
|
149
|
+
const st = Math.sin(angle);
|
|
150
|
+
|
|
151
|
+
return new Vec2(x * ct - y * st, x * st + y * ct);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
rotate(angle: number): this {
|
|
155
|
+
const x = this.x;
|
|
156
|
+
const y = this.y;
|
|
157
|
+
const ct = Math.cos(angle);
|
|
158
|
+
const st = Math.sin(angle);
|
|
159
|
+
|
|
160
|
+
this.x = x * ct - y * st;
|
|
161
|
+
this.y = x * st + y * ct;
|
|
162
|
+
|
|
163
|
+
return this;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
static rotateAroundPoint(a: IVec2, axisPoint: IVec2, angle: number): Vec2 {
|
|
167
|
+
return Vec2.clone(a).sub(axisPoint).rotate(angle).add(axisPoint);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
rotateAroundPoint(axisPoint: IVec2, angle: number): this {
|
|
171
|
+
return this.sub(axisPoint).rotate(angle).add(axisPoint);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
static scaleAndRotate(
|
|
175
|
+
a: IVec2,
|
|
176
|
+
axisPoint: IVec2,
|
|
177
|
+
angle: number,
|
|
178
|
+
sx: number,
|
|
179
|
+
sy: number = sx,
|
|
180
|
+
): Vec2 {
|
|
181
|
+
return Vec2.clone(a)
|
|
182
|
+
.sub(axisPoint)
|
|
183
|
+
.scale(sx, sy)
|
|
184
|
+
.rotate(angle)
|
|
185
|
+
.add(axisPoint);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
scaleAndRotate(
|
|
189
|
+
axisPoint: IVec2,
|
|
190
|
+
angle: number,
|
|
191
|
+
sx: number,
|
|
192
|
+
sy: number = sx,
|
|
193
|
+
): this {
|
|
194
|
+
return this.sub(axisPoint).scale(sx, sy).rotate(angle).add(axisPoint);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
static lerp(a: IVec2, b: IVec2, t: number): Vec2 {
|
|
198
|
+
const ax = a.x;
|
|
199
|
+
const ay = a.y;
|
|
200
|
+
|
|
201
|
+
return new Vec2(ax + t * (b.x - ax), ay + t * (b.y - ay));
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
lerp(b: IVec2, t: number): this {
|
|
205
|
+
const x = this.x;
|
|
206
|
+
const y = this.y;
|
|
207
|
+
|
|
208
|
+
this.x = x + t * (b.x - x);
|
|
209
|
+
this.y = y + t * (b.y - y);
|
|
210
|
+
|
|
211
|
+
return this;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
static perp(a: IVec2): Vec2 {
|
|
215
|
+
return new Vec2(-a.y, a.x);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
perp(): this {
|
|
219
|
+
const x = this.x;
|
|
220
|
+
const y = this.y;
|
|
221
|
+
|
|
222
|
+
this.x = -y;
|
|
223
|
+
this.y = x;
|
|
224
|
+
|
|
225
|
+
return this;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
static magnitude(a: IVec2): number {
|
|
229
|
+
return Math.hypot(a.x, a.y);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
magnitude(): number {
|
|
233
|
+
return Math.hypot(this.x, this.y);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
static squaredMagnitude(a: IVec2): number {
|
|
237
|
+
const x = a.x;
|
|
238
|
+
const y = a.y;
|
|
239
|
+
|
|
240
|
+
return x * x + y * y;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
squaredMagnitude(): number {
|
|
244
|
+
const x = this.x;
|
|
245
|
+
const y = this.y;
|
|
246
|
+
|
|
247
|
+
return x * x + y * y;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
static azimuth(a: IVec2): number {
|
|
251
|
+
return Math.atan2(a.y, a.x);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
azimuth(): number {
|
|
255
|
+
return Math.atan2(this.y, this.x);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
static dot(a: IVec2, b: IVec2): number {
|
|
259
|
+
return a.x * b.x + a.y * b.y;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
dot(b: IVec2): number {
|
|
263
|
+
return this.x * b.x + this.y * b.y;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
static cross(a: IVec2, b: IVec2): number {
|
|
267
|
+
return a.x * b.y - a.y * b.x;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
cross(b: IVec2): number {
|
|
271
|
+
return this.x * b.y - this.y * b.x;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
static distance(a: IVec2, b: IVec2): number {
|
|
275
|
+
return Math.hypot(a.x - b.x, a.y - b.y);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
distance(b: IVec2): number {
|
|
279
|
+
return Math.hypot(this.x - b.x, this.y - b.y);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
static squaredDistance(a: IVec2, b: IVec2): number {
|
|
283
|
+
const dx = a.x - b.x;
|
|
284
|
+
const dy = a.y - b.y;
|
|
285
|
+
|
|
286
|
+
return dx * dx + dy * dy;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
squaredDistance(b: IVec2): number {
|
|
290
|
+
const dx = this.x - b.x;
|
|
291
|
+
const dy = this.y - b.y;
|
|
292
|
+
|
|
293
|
+
return dx * dx + dy * dy;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
static direction(a: IVec2, b: IVec2): Vec2 {
|
|
297
|
+
const dx = b.x - a.x;
|
|
298
|
+
const dy = b.y - a.y;
|
|
299
|
+
|
|
300
|
+
let mag = Math.hypot(dx, dy);
|
|
301
|
+
if (mag !== 0) {
|
|
302
|
+
mag = 1 / mag;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return new Vec2(dx * mag, dy * mag);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
direction(b: IVec2): this {
|
|
309
|
+
const dx = b.x - this.x;
|
|
310
|
+
const dy = b.y - this.y;
|
|
311
|
+
|
|
312
|
+
let mag = Math.hypot(dx, dy);
|
|
313
|
+
if (mag !== 0) {
|
|
314
|
+
mag = 1 / mag;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
this.x = dx * mag;
|
|
318
|
+
this.y = dy * mag;
|
|
319
|
+
|
|
320
|
+
return this;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
static project(a: IVec2, b: IVec2): Vec2 {
|
|
324
|
+
const bx = b.x;
|
|
325
|
+
const by = b.y;
|
|
326
|
+
|
|
327
|
+
const dotAB = a.x * bx + a.y * by;
|
|
328
|
+
|
|
329
|
+
let squaredMagB = bx * bx + by * by;
|
|
330
|
+
if (squaredMagB !== 0) {
|
|
331
|
+
squaredMagB = 1 / squaredMagB;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const scalar = dotAB * squaredMagB;
|
|
335
|
+
|
|
336
|
+
return new Vec2(bx * scalar, by * scalar);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
project(b: IVec2): this {
|
|
340
|
+
const bx = b.x;
|
|
341
|
+
const by = b.y;
|
|
342
|
+
|
|
343
|
+
const dotAB = this.x * bx + this.y * by;
|
|
344
|
+
|
|
345
|
+
let squaredMagB = bx * bx + by * by;
|
|
346
|
+
if (squaredMagB !== 0) {
|
|
347
|
+
squaredMagB = 1 / squaredMagB;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const scalar = dotAB * squaredMagB;
|
|
351
|
+
|
|
352
|
+
this.x *= scalar;
|
|
353
|
+
this.y *= scalar;
|
|
354
|
+
|
|
355
|
+
return this;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
static abs(a: IVec2): Vec2 {
|
|
359
|
+
return new Vec2(Math.abs(a.x), Math.abs(a.y));
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
abs(): this {
|
|
363
|
+
this.x = Math.abs(this.x);
|
|
364
|
+
this.y = Math.abs(this.y);
|
|
365
|
+
|
|
366
|
+
return this;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
static ceil(a: IVec2): Vec2 {
|
|
370
|
+
return new Vec2(Math.ceil(a.x), Math.ceil(a.y));
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
ceil(): this {
|
|
374
|
+
this.x = Math.ceil(this.x);
|
|
375
|
+
this.y = Math.ceil(this.y);
|
|
376
|
+
|
|
377
|
+
return this;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
static floor(a: IVec2): Vec2 {
|
|
381
|
+
return new Vec2(Math.floor(a.x), Math.floor(a.y));
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
floor(): this {
|
|
385
|
+
this.x = Math.floor(this.x);
|
|
386
|
+
this.y = Math.floor(this.y);
|
|
387
|
+
|
|
388
|
+
return this;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
static round(a: IVec2): Vec2 {
|
|
392
|
+
return new Vec2(Math.round(a.x), Math.round(a.y));
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
round(): this {
|
|
396
|
+
this.x = Math.round(this.x);
|
|
397
|
+
this.y = Math.round(this.y);
|
|
398
|
+
|
|
399
|
+
return this;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
static trunc(a: IVec2): Vec2 {
|
|
403
|
+
return new Vec2(Math.trunc(a.x), Math.trunc(a.y));
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
trunc(): this {
|
|
407
|
+
this.x = Math.trunc(this.x);
|
|
408
|
+
this.y = Math.trunc(this.y);
|
|
409
|
+
|
|
410
|
+
return this;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
static equals(a: IVec2, b: IVec2): boolean {
|
|
414
|
+
return a.x === b.x && a.y === b.y;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
equals(b: IVec2): boolean {
|
|
418
|
+
return this.x === b.x && this.y === b.y;
|
|
419
|
+
}
|
|
420
|
+
}
|