frigame3 3.0.0 → 3.0.1
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/dist/Animation.d.ts +42 -0
- package/dist/Animation.js +189 -0
- package/dist/BaseSprite.d.ts +116 -0
- package/dist/BaseSprite.js +518 -0
- package/dist/Gradient.d.ts +31 -0
- package/dist/Gradient.js +89 -0
- package/dist/Playground.d.ts +48 -0
- package/dist/Playground.js +167 -0
- package/dist/Rect.d.ts +63 -0
- package/dist/Rect.js +155 -0
- package/dist/Rectangle.d.ts +31 -0
- package/dist/Rectangle.js +69 -0
- package/dist/Renderer.d.ts +23 -0
- package/dist/Renderer.js +1 -0
- package/dist/ResourceManager.d.ts +21 -0
- package/dist/ResourceManager.js +100 -0
- package/dist/Sprite.d.ts +56 -0
- package/dist/Sprite.js +437 -0
- package/dist/SpriteGroup.d.ts +39 -0
- package/dist/SpriteGroup.js +176 -0
- package/dist/canvasRenderer/renderGroup.d.ts +15 -0
- package/dist/canvasRenderer/renderGroup.js +134 -0
- package/dist/canvasRenderer/renderPlayground.d.ts +9 -0
- package/dist/canvasRenderer/renderPlayground.js +67 -0
- package/dist/canvasRenderer/renderRectangle.d.ts +15 -0
- package/dist/canvasRenderer/renderRectangle.js +287 -0
- package/dist/canvasRenderer/renderSprite.d.ts +4 -0
- package/dist/canvasRenderer/renderSprite.js +93 -0
- package/dist/canvasRenderer.d.ts +2 -0
- package/dist/canvasRenderer.js +19 -0
- package/dist/dummyRenderer.d.ts +2 -0
- package/dist/dummyRenderer.js +41 -0
- package/dist/plugins/GamepadTracker.d.ts +15 -0
- package/dist/plugins/GamepadTracker.js +74 -0
- package/dist/plugins/Tilemap.d.ts +26 -0
- package/dist/plugins/Tilemap.js +85 -0
- package/dist/plugins/Vec2.d.ts +69 -0
- package/dist/plugins/Vec2.js +296 -0
- package/dist/plugins/fx/Shaker.d.ts +46 -0
- package/dist/plugins/fx/Shaker.js +190 -0
- package/dist/plugins/fx/Tweener.d.ts +42 -0
- package/dist/plugins/fx/Tweener.js +166 -0
- package/dist/plugins/fx/easing.d.ts +32 -0
- package/dist/plugins/fx/easing.js +192 -0
- package/dist/plugins/isometric/ISOMixin.d.ts +138 -0
- package/dist/plugins/isometric/ISOMixin.js +322 -0
- package/dist/plugins/isometric/ISORectangle.d.ts +151 -0
- package/dist/plugins/isometric/ISORectangle.js +127 -0
- package/dist/plugins/isometric/ISOSprite.d.ts +157 -0
- package/dist/plugins/isometric/ISOSprite.js +176 -0
- package/dist/plugins/isometric/ISOSpriteGroup.d.ts +151 -0
- package/dist/plugins/isometric/ISOSpriteGroup.js +116 -0
- package/dist/plugins/isometric/ISOTilemap.d.ts +24 -0
- package/dist/plugins/isometric/ISOTilemap.js +85 -0
- package/dist/plugins/isometric/utils.d.ts +2 -0
- package/dist/plugins/isometric/utils.js +10 -0
- package/dist/plugins/keyTracker.d.ts +3 -0
- package/dist/plugins/keyTracker.js +23 -0
- package/dist/plugins/mixer.d.ts +72 -0
- package/dist/plugins/mixer.js +396 -0
- package/dist/plugins/sorted/SortedAnimation.d.ts +13 -0
- package/dist/plugins/sorted/SortedAnimation.js +27 -0
- package/dist/plugins/sorted/SortedGroup.d.ts +137 -0
- package/dist/plugins/sorted/SortedGroup.js +61 -0
- package/dist/plugins/sorted/SortedMixin.d.ts +122 -0
- package/dist/plugins/sorted/SortedMixin.js +153 -0
- package/dist/plugins/sorted/SortedRectangle.d.ts +127 -0
- package/dist/plugins/sorted/SortedRectangle.js +17 -0
- package/dist/plugins/sorted/SortedSprite.d.ts +130 -0
- package/dist/plugins/sorted/SortedSprite.js +35 -0
- package/dist/utils.d.ts +11 -0
- package/dist/utils.js +33 -0
- package/package.json +6 -2
- package/CHANGELOG.md +0 -13
- package/biome.json +0 -93
- package/demo/package-lock.json +0 -515
- package/demo/package.json +0 -19
- package/demo/snake/index.html +0 -16
- package/demo/snake/index.ts +0 -110
- package/demo/tetris/index.html +0 -16
- package/demo/tetris/index.ts +0 -536
- package/demo/tsconfig.json +0 -53
- package/src/Animation.ts +0 -266
- package/src/BaseSprite.ts +0 -682
- package/src/Gradient.ts +0 -120
- package/src/Playground.ts +0 -247
- package/src/Rect.ts +0 -237
- package/src/Rectangle.ts +0 -108
- package/src/Renderer.ts +0 -28
- package/src/ResourceManager.ts +0 -145
- package/src/Sprite.ts +0 -484
- package/src/SpriteGroup.ts +0 -258
- package/src/canvasRenderer/renderGroup.ts +0 -194
- package/src/canvasRenderer/renderPlayground.ts +0 -103
- package/src/canvasRenderer/renderRectangle.ts +0 -421
- package/src/canvasRenderer/renderSprite.ts +0 -148
- package/src/canvasRenderer.ts +0 -38
- package/src/dummyRenderer.ts +0 -62
- package/src/plugins/GamepadTracker.ts +0 -97
- package/src/plugins/Tilemap.ts +0 -123
- package/src/plugins/Vec2.ts +0 -420
- package/src/plugins/fx/Shaker.ts +0 -293
- package/src/plugins/fx/Tweener.ts +0 -252
- package/src/plugins/fx/easing.ts +0 -276
- package/src/plugins/isometric/ISOMixin.ts +0 -444
- package/src/plugins/isometric/ISORectangle.ts +0 -183
- package/src/plugins/isometric/ISOSprite.ts +0 -228
- package/src/plugins/isometric/ISOSpriteGroup.ts +0 -155
- package/src/plugins/isometric/ISOTilemap.ts +0 -122
- package/src/plugins/isometric/utils.ts +0 -19
- package/src/plugins/keyTracker.ts +0 -31
- package/src/plugins/mixer.ts +0 -523
- package/src/plugins/sorted/SortedAnimation.ts +0 -53
- package/src/plugins/sorted/SortedGroup.ts +0 -94
- package/src/plugins/sorted/SortedMixin.ts +0 -203
- package/src/plugins/sorted/SortedRectangle.ts +0 -35
- package/src/plugins/sorted/SortedSprite.ts +0 -56
- package/src/utils.ts +0 -53
- package/test/animation/index.html +0 -20
- package/test/animation/index.ts +0 -253
- 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 +0 -17
- package/test/isometric1/index.ts +0 -68
- package/test/isometric1/tronco.png +0 -0
- package/test/isometric1/untitled.tmx +0 -25
- 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 +0 -16
- package/test/isometric2/index.ts +0 -129
- package/test/isometric2/knight_se.png +0 -0
- package/test/isometric2/tile.png +0 -0
- package/test/isometric2/untitled.tmx +0 -36
- 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 +0 -8
- package/test/layers/grassblock.png +0 -0
- package/test/layers/horngirl.png +0 -0
- package/test/layers/index.html +0 -17
- package/test/layers/index.ts +0 -191
- package/test/layers/pinkgirl.png +0 -0
- package/test/layers/render_ruler.py +0 -57
- 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 +0 -33
- package/test/mixer/index.ts +0 -206
- 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 +0 -515
- package/test/package.json +0 -19
- package/test/sorted/Brown Block.png +0 -0
- package/test/sorted/index.html +0 -16
- package/test/sorted/index.ts +0 -38
- package/test/transformation1/index.html +0 -17
- package/test/transformation1/index.ts +0 -51
- package/test/transformation1/sh.png +0 -0
- package/test/transformation2/index.html +0 -20
- package/test/transformation2/index.ts +0 -71
- package/test/transformation2/sh.png +0 -0
- package/test/tsconfig.json +0 -53
- package/tsconfig.json +0 -51
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Playground } from "../Playground.js";
|
|
2
|
+
export declare class GamepadTracker extends EventTarget {
|
|
3
|
+
controllers: Map<number, Gamepad>;
|
|
4
|
+
_playground: WeakRef<Playground>;
|
|
5
|
+
_callbackId: number | null;
|
|
6
|
+
constructor(playground: Playground);
|
|
7
|
+
_scanGamepads: () => void;
|
|
8
|
+
registerCallback(options?: {
|
|
9
|
+
suppressWarning?: boolean;
|
|
10
|
+
}): this;
|
|
11
|
+
removeCallback(options?: {
|
|
12
|
+
suppressWarning?: boolean;
|
|
13
|
+
}): this;
|
|
14
|
+
_onClearCallbacks: () => void;
|
|
15
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
export class GamepadTracker extends EventTarget {
|
|
2
|
+
controllers = new Map();
|
|
3
|
+
_playground;
|
|
4
|
+
_callbackId = null;
|
|
5
|
+
constructor(playground) {
|
|
6
|
+
super();
|
|
7
|
+
this._playground = new WeakRef(playground);
|
|
8
|
+
this.registerCallback();
|
|
9
|
+
playground.addEventListener("clearCallbacks", this._onClearCallbacks);
|
|
10
|
+
}
|
|
11
|
+
_scanGamepads = () => {
|
|
12
|
+
const controllers = this.controllers;
|
|
13
|
+
const gamepads = navigator.getGamepads();
|
|
14
|
+
// Step 1: Update controller status
|
|
15
|
+
for (const gamepad of gamepads) {
|
|
16
|
+
if (gamepad !== null) {
|
|
17
|
+
controllers.set(gamepad.index, gamepad);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
// Step 2: Remove disconnected controllers
|
|
21
|
+
const indexes = new Set(gamepads
|
|
22
|
+
.filter((gamepad) => gamepad !== null)
|
|
23
|
+
.map((gamepad) => gamepad.index));
|
|
24
|
+
for (const index of controllers.keys()) {
|
|
25
|
+
if (!indexes.has(index)) {
|
|
26
|
+
controllers.delete(index);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
registerCallback(options) {
|
|
31
|
+
if (this._callbackId === null) {
|
|
32
|
+
const playground = this._playground.deref();
|
|
33
|
+
if (playground !== undefined) {
|
|
34
|
+
this._callbackId = playground.registerCallback(this._scanGamepads);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
if (typeof console !== "undefined" &&
|
|
38
|
+
options?.suppressWarning === false) {
|
|
39
|
+
console.warn("playground has been garbage collected");
|
|
40
|
+
console.trace();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
if (typeof console !== "undefined" &&
|
|
46
|
+
options?.suppressWarning === false) {
|
|
47
|
+
console.warn("Callback already registered");
|
|
48
|
+
console.trace();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
removeCallback(options) {
|
|
54
|
+
const playground = this._playground.deref();
|
|
55
|
+
if (playground !== undefined) {
|
|
56
|
+
playground.removeCallback(this._callbackId, options);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
if (typeof console !== "undefined" &&
|
|
60
|
+
options?.suppressWarning === false) {
|
|
61
|
+
console.warn("playground has been garbage collected");
|
|
62
|
+
console.trace();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
this._callbackId = null;
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
_onClearCallbacks = () => {
|
|
69
|
+
if (this._callbackId !== null) {
|
|
70
|
+
this._callbackId = null;
|
|
71
|
+
this.registerCallback();
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { BaseSprite } from "../BaseSprite.js";
|
|
2
|
+
import type { RectangleOptions } from "../Rectangle.js";
|
|
3
|
+
import type { SpriteOptions } from "../Sprite.js";
|
|
4
|
+
import type { SpriteGroupOptions } from "../SpriteGroup.js";
|
|
5
|
+
import { SpriteGroup } from "../SpriteGroup.js";
|
|
6
|
+
import type { SpriteRef } from "../utils.js";
|
|
7
|
+
export type TileOptions = (Pick<SpriteOptions, "animation"> & Partial<SpriteOptions>) | (Pick<RectangleOptions, "background"> & Partial<RectangleOptions>);
|
|
8
|
+
export interface TilemapOptions {
|
|
9
|
+
sizex: number;
|
|
10
|
+
sizey: number;
|
|
11
|
+
tileWidth: number;
|
|
12
|
+
tileHeight: number;
|
|
13
|
+
data: number[];
|
|
14
|
+
animationList: Record<number, TileOptions>;
|
|
15
|
+
}
|
|
16
|
+
export declare class Tilemap extends SpriteGroup {
|
|
17
|
+
sizex: number;
|
|
18
|
+
sizey: number;
|
|
19
|
+
tileWidth: number;
|
|
20
|
+
tileHeight: number;
|
|
21
|
+
_locations: Uint32Array;
|
|
22
|
+
constructor(options: TilemapOptions & Partial<SpriteGroupOptions> & {
|
|
23
|
+
ref?: SpriteRef<Tilemap>;
|
|
24
|
+
});
|
|
25
|
+
getAt(row: number, col: number): BaseSprite | undefined;
|
|
26
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { Rectangle } from "../Rectangle.js";
|
|
2
|
+
import { Sprite } from "../Sprite.js";
|
|
3
|
+
import { SpriteGroup } from "../SpriteGroup.js";
|
|
4
|
+
export class Tilemap extends SpriteGroup {
|
|
5
|
+
sizex = 0;
|
|
6
|
+
sizey = 0;
|
|
7
|
+
tileWidth = 0;
|
|
8
|
+
tileHeight = 0;
|
|
9
|
+
_locations;
|
|
10
|
+
constructor(options) {
|
|
11
|
+
super(options);
|
|
12
|
+
const { sizex, sizey, tileWidth, tileHeight } = options;
|
|
13
|
+
this.sizex = sizex;
|
|
14
|
+
this.sizey = sizey;
|
|
15
|
+
this.tileWidth = tileWidth;
|
|
16
|
+
this.tileHeight = tileHeight;
|
|
17
|
+
const locations = new Uint32Array(sizex * sizey).fill(0xffffffff);
|
|
18
|
+
this._locations = locations;
|
|
19
|
+
const layers = this._layers;
|
|
20
|
+
if (options.width === undefined &&
|
|
21
|
+
options.halfWidth === undefined &&
|
|
22
|
+
options.radius === undefined) {
|
|
23
|
+
this.width = sizex * tileWidth;
|
|
24
|
+
}
|
|
25
|
+
if (options.height === undefined &&
|
|
26
|
+
options.halfHeight === undefined &&
|
|
27
|
+
options.radius === undefined) {
|
|
28
|
+
this.height = sizey * tileHeight;
|
|
29
|
+
}
|
|
30
|
+
const animationList = options.animationList;
|
|
31
|
+
let i_location = 0;
|
|
32
|
+
let row = 0;
|
|
33
|
+
let col = 0;
|
|
34
|
+
let left = 0;
|
|
35
|
+
let top = 0;
|
|
36
|
+
for (const data of options.data) {
|
|
37
|
+
const animation_options = animationList[data];
|
|
38
|
+
if (animation_options !== undefined && "animation" in animation_options) {
|
|
39
|
+
// Sprite
|
|
40
|
+
const sprite_options = Object.assign({}, animation_options, {
|
|
41
|
+
left,
|
|
42
|
+
top,
|
|
43
|
+
});
|
|
44
|
+
this.addChild(new Sprite(sprite_options));
|
|
45
|
+
locations[i_location] = layers.length - 1;
|
|
46
|
+
}
|
|
47
|
+
else if (animation_options !== undefined &&
|
|
48
|
+
"background" in animation_options) {
|
|
49
|
+
// Rectangle
|
|
50
|
+
const rectangle_options = Object.assign({}, animation_options, {
|
|
51
|
+
left,
|
|
52
|
+
top,
|
|
53
|
+
width: tileWidth,
|
|
54
|
+
height: tileHeight,
|
|
55
|
+
});
|
|
56
|
+
this.addChild(new Rectangle(rectangle_options));
|
|
57
|
+
locations[i_location] = layers.length - 1;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
// TODO: Invalid
|
|
61
|
+
}
|
|
62
|
+
i_location += 1;
|
|
63
|
+
left += tileWidth;
|
|
64
|
+
col += 1;
|
|
65
|
+
if (col >= sizex) {
|
|
66
|
+
col = 0;
|
|
67
|
+
left = 0;
|
|
68
|
+
row += 1;
|
|
69
|
+
top += tileHeight;
|
|
70
|
+
if (row >= sizey) {
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
getAt(row, col) {
|
|
77
|
+
const i = row * this.sizex + col;
|
|
78
|
+
if (i < this._locations.length) {
|
|
79
|
+
return this._layers[this._locations[i]];
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
export interface IVec2 {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
}
|
|
5
|
+
export declare class Vec2 implements IVec2 {
|
|
6
|
+
x: number;
|
|
7
|
+
y: number;
|
|
8
|
+
constructor(x: number, y: number);
|
|
9
|
+
static clone(a: IVec2): Vec2;
|
|
10
|
+
clone(b: IVec2): this;
|
|
11
|
+
static fromValues(x: number, y: number): Vec2;
|
|
12
|
+
fromValues(x: number, y: number): this;
|
|
13
|
+
static fromPolar(mag: number, angle: number): Vec2;
|
|
14
|
+
fromPolar(mag: number, angle: number): this;
|
|
15
|
+
static randomUnit(): Vec2;
|
|
16
|
+
randomUnit(): this;
|
|
17
|
+
static random(scalar?: number): Vec2;
|
|
18
|
+
random(scalar?: number): this;
|
|
19
|
+
static add(a: IVec2, b: IVec2): Vec2;
|
|
20
|
+
add(b: IVec2): this;
|
|
21
|
+
static sub(a: IVec2, b: IVec2): Vec2;
|
|
22
|
+
sub(b: IVec2): this;
|
|
23
|
+
static scale(a: IVec2, sx: number, sy?: number): Vec2;
|
|
24
|
+
scale(sx: number, sy?: number): this;
|
|
25
|
+
static invert(a: IVec2): Vec2;
|
|
26
|
+
invert(): this;
|
|
27
|
+
static normalize(a: IVec2): Vec2;
|
|
28
|
+
normalize(): this;
|
|
29
|
+
static rotate(a: IVec2, angle: number): Vec2;
|
|
30
|
+
rotate(angle: number): this;
|
|
31
|
+
static rotateAroundPoint(a: IVec2, axisPoint: IVec2, angle: number): Vec2;
|
|
32
|
+
rotateAroundPoint(axisPoint: IVec2, angle: number): this;
|
|
33
|
+
static scaleAndRotate(a: IVec2, axisPoint: IVec2, angle: number, sx: number, sy?: number): Vec2;
|
|
34
|
+
scaleAndRotate(axisPoint: IVec2, angle: number, sx: number, sy?: number): this;
|
|
35
|
+
static lerp(a: IVec2, b: IVec2, t: number): Vec2;
|
|
36
|
+
lerp(b: IVec2, t: number): this;
|
|
37
|
+
static perp(a: IVec2): Vec2;
|
|
38
|
+
perp(): this;
|
|
39
|
+
static magnitude(a: IVec2): number;
|
|
40
|
+
magnitude(): number;
|
|
41
|
+
static squaredMagnitude(a: IVec2): number;
|
|
42
|
+
squaredMagnitude(): number;
|
|
43
|
+
static azimuth(a: IVec2): number;
|
|
44
|
+
azimuth(): number;
|
|
45
|
+
static dot(a: IVec2, b: IVec2): number;
|
|
46
|
+
dot(b: IVec2): number;
|
|
47
|
+
static cross(a: IVec2, b: IVec2): number;
|
|
48
|
+
cross(b: IVec2): number;
|
|
49
|
+
static distance(a: IVec2, b: IVec2): number;
|
|
50
|
+
distance(b: IVec2): number;
|
|
51
|
+
static squaredDistance(a: IVec2, b: IVec2): number;
|
|
52
|
+
squaredDistance(b: IVec2): number;
|
|
53
|
+
static direction(a: IVec2, b: IVec2): Vec2;
|
|
54
|
+
direction(b: IVec2): this;
|
|
55
|
+
static project(a: IVec2, b: IVec2): Vec2;
|
|
56
|
+
project(b: IVec2): this;
|
|
57
|
+
static abs(a: IVec2): Vec2;
|
|
58
|
+
abs(): this;
|
|
59
|
+
static ceil(a: IVec2): Vec2;
|
|
60
|
+
ceil(): this;
|
|
61
|
+
static floor(a: IVec2): Vec2;
|
|
62
|
+
floor(): this;
|
|
63
|
+
static round(a: IVec2): Vec2;
|
|
64
|
+
round(): this;
|
|
65
|
+
static trunc(a: IVec2): Vec2;
|
|
66
|
+
trunc(): this;
|
|
67
|
+
static equals(a: IVec2, b: IVec2): boolean;
|
|
68
|
+
equals(b: IVec2): boolean;
|
|
69
|
+
}
|
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
export class Vec2 {
|
|
2
|
+
x;
|
|
3
|
+
y;
|
|
4
|
+
constructor(x, y) {
|
|
5
|
+
this.x = x;
|
|
6
|
+
this.y = y;
|
|
7
|
+
}
|
|
8
|
+
static clone(a) {
|
|
9
|
+
return new Vec2(a.x, a.y);
|
|
10
|
+
}
|
|
11
|
+
clone(b) {
|
|
12
|
+
this.x = b.x;
|
|
13
|
+
this.y = b.y;
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
static fromValues(x, y) {
|
|
17
|
+
return new Vec2(x, y);
|
|
18
|
+
}
|
|
19
|
+
fromValues(x, y) {
|
|
20
|
+
this.x = x;
|
|
21
|
+
this.y = y;
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
static fromPolar(mag, angle) {
|
|
25
|
+
return new Vec2(Math.cos(angle) * mag, Math.sin(angle) * mag);
|
|
26
|
+
}
|
|
27
|
+
fromPolar(mag, angle) {
|
|
28
|
+
this.x = Math.cos(angle) * mag;
|
|
29
|
+
this.y = Math.sin(angle) * mag;
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
static randomUnit() {
|
|
33
|
+
const angle = Math.random() * Math.PI * 2;
|
|
34
|
+
return new Vec2(Math.cos(angle), Math.sin(angle));
|
|
35
|
+
}
|
|
36
|
+
randomUnit() {
|
|
37
|
+
const angle = Math.random() * Math.PI * 2;
|
|
38
|
+
this.x = Math.cos(angle);
|
|
39
|
+
this.y = Math.sin(angle);
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
static random(scalar = 1) {
|
|
43
|
+
const mag = Math.random() * scalar;
|
|
44
|
+
const angle = Math.random() * Math.PI * 2;
|
|
45
|
+
return new Vec2(Math.cos(angle) * mag, Math.sin(angle) * mag);
|
|
46
|
+
}
|
|
47
|
+
random(scalar = 1) {
|
|
48
|
+
const mag = Math.random() * scalar;
|
|
49
|
+
const angle = Math.random() * Math.PI * 2;
|
|
50
|
+
this.x = Math.cos(angle) * mag;
|
|
51
|
+
this.y = Math.sin(angle) * mag;
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
static add(a, b) {
|
|
55
|
+
return new Vec2(a.x + b.x, a.y + b.y);
|
|
56
|
+
}
|
|
57
|
+
add(b) {
|
|
58
|
+
this.x += b.x;
|
|
59
|
+
this.y += b.y;
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
static sub(a, b) {
|
|
63
|
+
return new Vec2(a.x - b.x, a.y - b.y);
|
|
64
|
+
}
|
|
65
|
+
sub(b) {
|
|
66
|
+
this.x -= b.x;
|
|
67
|
+
this.y -= b.y;
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
static scale(a, sx, sy = sx) {
|
|
71
|
+
return new Vec2(a.x * sx, a.y * sy);
|
|
72
|
+
}
|
|
73
|
+
scale(sx, sy = sx) {
|
|
74
|
+
this.x *= sx;
|
|
75
|
+
this.y *= sy;
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
static invert(a) {
|
|
79
|
+
return new Vec2(-a.x, -a.y);
|
|
80
|
+
}
|
|
81
|
+
invert() {
|
|
82
|
+
this.x *= -1;
|
|
83
|
+
this.y *= -1;
|
|
84
|
+
return this;
|
|
85
|
+
}
|
|
86
|
+
static normalize(a) {
|
|
87
|
+
let mag = Math.hypot(a.x, a.y);
|
|
88
|
+
if (mag !== 0) {
|
|
89
|
+
mag = 1 / mag;
|
|
90
|
+
}
|
|
91
|
+
return new Vec2(a.x * mag, a.y * mag);
|
|
92
|
+
}
|
|
93
|
+
normalize() {
|
|
94
|
+
let mag = Math.hypot(this.x, this.y);
|
|
95
|
+
if (mag !== 0) {
|
|
96
|
+
mag = 1 / mag;
|
|
97
|
+
}
|
|
98
|
+
this.x *= mag;
|
|
99
|
+
this.y *= mag;
|
|
100
|
+
return this;
|
|
101
|
+
}
|
|
102
|
+
static rotate(a, angle) {
|
|
103
|
+
const x = a.x;
|
|
104
|
+
const y = a.y;
|
|
105
|
+
const ct = Math.cos(angle);
|
|
106
|
+
const st = Math.sin(angle);
|
|
107
|
+
return new Vec2(x * ct - y * st, x * st + y * ct);
|
|
108
|
+
}
|
|
109
|
+
rotate(angle) {
|
|
110
|
+
const x = this.x;
|
|
111
|
+
const y = this.y;
|
|
112
|
+
const ct = Math.cos(angle);
|
|
113
|
+
const st = Math.sin(angle);
|
|
114
|
+
this.x = x * ct - y * st;
|
|
115
|
+
this.y = x * st + y * ct;
|
|
116
|
+
return this;
|
|
117
|
+
}
|
|
118
|
+
static rotateAroundPoint(a, axisPoint, angle) {
|
|
119
|
+
return Vec2.clone(a).sub(axisPoint).rotate(angle).add(axisPoint);
|
|
120
|
+
}
|
|
121
|
+
rotateAroundPoint(axisPoint, angle) {
|
|
122
|
+
return this.sub(axisPoint).rotate(angle).add(axisPoint);
|
|
123
|
+
}
|
|
124
|
+
static scaleAndRotate(a, axisPoint, angle, sx, sy = sx) {
|
|
125
|
+
return Vec2.clone(a)
|
|
126
|
+
.sub(axisPoint)
|
|
127
|
+
.scale(sx, sy)
|
|
128
|
+
.rotate(angle)
|
|
129
|
+
.add(axisPoint);
|
|
130
|
+
}
|
|
131
|
+
scaleAndRotate(axisPoint, angle, sx, sy = sx) {
|
|
132
|
+
return this.sub(axisPoint).scale(sx, sy).rotate(angle).add(axisPoint);
|
|
133
|
+
}
|
|
134
|
+
static lerp(a, b, t) {
|
|
135
|
+
const ax = a.x;
|
|
136
|
+
const ay = a.y;
|
|
137
|
+
return new Vec2(ax + t * (b.x - ax), ay + t * (b.y - ay));
|
|
138
|
+
}
|
|
139
|
+
lerp(b, t) {
|
|
140
|
+
const x = this.x;
|
|
141
|
+
const y = this.y;
|
|
142
|
+
this.x = x + t * (b.x - x);
|
|
143
|
+
this.y = y + t * (b.y - y);
|
|
144
|
+
return this;
|
|
145
|
+
}
|
|
146
|
+
static perp(a) {
|
|
147
|
+
return new Vec2(-a.y, a.x);
|
|
148
|
+
}
|
|
149
|
+
perp() {
|
|
150
|
+
const x = this.x;
|
|
151
|
+
const y = this.y;
|
|
152
|
+
this.x = -y;
|
|
153
|
+
this.y = x;
|
|
154
|
+
return this;
|
|
155
|
+
}
|
|
156
|
+
static magnitude(a) {
|
|
157
|
+
return Math.hypot(a.x, a.y);
|
|
158
|
+
}
|
|
159
|
+
magnitude() {
|
|
160
|
+
return Math.hypot(this.x, this.y);
|
|
161
|
+
}
|
|
162
|
+
static squaredMagnitude(a) {
|
|
163
|
+
const x = a.x;
|
|
164
|
+
const y = a.y;
|
|
165
|
+
return x * x + y * y;
|
|
166
|
+
}
|
|
167
|
+
squaredMagnitude() {
|
|
168
|
+
const x = this.x;
|
|
169
|
+
const y = this.y;
|
|
170
|
+
return x * x + y * y;
|
|
171
|
+
}
|
|
172
|
+
static azimuth(a) {
|
|
173
|
+
return Math.atan2(a.y, a.x);
|
|
174
|
+
}
|
|
175
|
+
azimuth() {
|
|
176
|
+
return Math.atan2(this.y, this.x);
|
|
177
|
+
}
|
|
178
|
+
static dot(a, b) {
|
|
179
|
+
return a.x * b.x + a.y * b.y;
|
|
180
|
+
}
|
|
181
|
+
dot(b) {
|
|
182
|
+
return this.x * b.x + this.y * b.y;
|
|
183
|
+
}
|
|
184
|
+
static cross(a, b) {
|
|
185
|
+
return a.x * b.y - a.y * b.x;
|
|
186
|
+
}
|
|
187
|
+
cross(b) {
|
|
188
|
+
return this.x * b.y - this.y * b.x;
|
|
189
|
+
}
|
|
190
|
+
static distance(a, b) {
|
|
191
|
+
return Math.hypot(a.x - b.x, a.y - b.y);
|
|
192
|
+
}
|
|
193
|
+
distance(b) {
|
|
194
|
+
return Math.hypot(this.x - b.x, this.y - b.y);
|
|
195
|
+
}
|
|
196
|
+
static squaredDistance(a, b) {
|
|
197
|
+
const dx = a.x - b.x;
|
|
198
|
+
const dy = a.y - b.y;
|
|
199
|
+
return dx * dx + dy * dy;
|
|
200
|
+
}
|
|
201
|
+
squaredDistance(b) {
|
|
202
|
+
const dx = this.x - b.x;
|
|
203
|
+
const dy = this.y - b.y;
|
|
204
|
+
return dx * dx + dy * dy;
|
|
205
|
+
}
|
|
206
|
+
static direction(a, b) {
|
|
207
|
+
const dx = b.x - a.x;
|
|
208
|
+
const dy = b.y - a.y;
|
|
209
|
+
let mag = Math.hypot(dx, dy);
|
|
210
|
+
if (mag !== 0) {
|
|
211
|
+
mag = 1 / mag;
|
|
212
|
+
}
|
|
213
|
+
return new Vec2(dx * mag, dy * mag);
|
|
214
|
+
}
|
|
215
|
+
direction(b) {
|
|
216
|
+
const dx = b.x - this.x;
|
|
217
|
+
const dy = b.y - this.y;
|
|
218
|
+
let mag = Math.hypot(dx, dy);
|
|
219
|
+
if (mag !== 0) {
|
|
220
|
+
mag = 1 / mag;
|
|
221
|
+
}
|
|
222
|
+
this.x = dx * mag;
|
|
223
|
+
this.y = dy * mag;
|
|
224
|
+
return this;
|
|
225
|
+
}
|
|
226
|
+
static project(a, b) {
|
|
227
|
+
const bx = b.x;
|
|
228
|
+
const by = b.y;
|
|
229
|
+
const dotAB = a.x * bx + a.y * by;
|
|
230
|
+
let squaredMagB = bx * bx + by * by;
|
|
231
|
+
if (squaredMagB !== 0) {
|
|
232
|
+
squaredMagB = 1 / squaredMagB;
|
|
233
|
+
}
|
|
234
|
+
const scalar = dotAB * squaredMagB;
|
|
235
|
+
return new Vec2(bx * scalar, by * scalar);
|
|
236
|
+
}
|
|
237
|
+
project(b) {
|
|
238
|
+
const bx = b.x;
|
|
239
|
+
const by = b.y;
|
|
240
|
+
const dotAB = this.x * bx + this.y * by;
|
|
241
|
+
let squaredMagB = bx * bx + by * by;
|
|
242
|
+
if (squaredMagB !== 0) {
|
|
243
|
+
squaredMagB = 1 / squaredMagB;
|
|
244
|
+
}
|
|
245
|
+
const scalar = dotAB * squaredMagB;
|
|
246
|
+
this.x *= scalar;
|
|
247
|
+
this.y *= scalar;
|
|
248
|
+
return this;
|
|
249
|
+
}
|
|
250
|
+
static abs(a) {
|
|
251
|
+
return new Vec2(Math.abs(a.x), Math.abs(a.y));
|
|
252
|
+
}
|
|
253
|
+
abs() {
|
|
254
|
+
this.x = Math.abs(this.x);
|
|
255
|
+
this.y = Math.abs(this.y);
|
|
256
|
+
return this;
|
|
257
|
+
}
|
|
258
|
+
static ceil(a) {
|
|
259
|
+
return new Vec2(Math.ceil(a.x), Math.ceil(a.y));
|
|
260
|
+
}
|
|
261
|
+
ceil() {
|
|
262
|
+
this.x = Math.ceil(this.x);
|
|
263
|
+
this.y = Math.ceil(this.y);
|
|
264
|
+
return this;
|
|
265
|
+
}
|
|
266
|
+
static floor(a) {
|
|
267
|
+
return new Vec2(Math.floor(a.x), Math.floor(a.y));
|
|
268
|
+
}
|
|
269
|
+
floor() {
|
|
270
|
+
this.x = Math.floor(this.x);
|
|
271
|
+
this.y = Math.floor(this.y);
|
|
272
|
+
return this;
|
|
273
|
+
}
|
|
274
|
+
static round(a) {
|
|
275
|
+
return new Vec2(Math.round(a.x), Math.round(a.y));
|
|
276
|
+
}
|
|
277
|
+
round() {
|
|
278
|
+
this.x = Math.round(this.x);
|
|
279
|
+
this.y = Math.round(this.y);
|
|
280
|
+
return this;
|
|
281
|
+
}
|
|
282
|
+
static trunc(a) {
|
|
283
|
+
return new Vec2(Math.trunc(a.x), Math.trunc(a.y));
|
|
284
|
+
}
|
|
285
|
+
trunc() {
|
|
286
|
+
this.x = Math.trunc(this.x);
|
|
287
|
+
this.y = Math.trunc(this.y);
|
|
288
|
+
return this;
|
|
289
|
+
}
|
|
290
|
+
static equals(a, b) {
|
|
291
|
+
return a.x === b.x && a.y === b.y;
|
|
292
|
+
}
|
|
293
|
+
equals(b) {
|
|
294
|
+
return this.x === b.x && this.y === b.y;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Playground } from "../../Playground.js";
|
|
2
|
+
export interface ShakeOptions {
|
|
3
|
+
duration: number;
|
|
4
|
+
frequency: number;
|
|
5
|
+
decay: (t: number) => number;
|
|
6
|
+
callback: () => void;
|
|
7
|
+
suppressWarning: boolean;
|
|
8
|
+
}
|
|
9
|
+
interface ShakeProp {
|
|
10
|
+
amplitude: number;
|
|
11
|
+
start_value: number;
|
|
12
|
+
current_value: number;
|
|
13
|
+
sample_value: number;
|
|
14
|
+
}
|
|
15
|
+
interface ShakeObj<T extends object> {
|
|
16
|
+
target_obj: T;
|
|
17
|
+
current_sample: number;
|
|
18
|
+
num_sample: number;
|
|
19
|
+
current_step: number;
|
|
20
|
+
steps_per_sample: number;
|
|
21
|
+
decay: (t: number) => number;
|
|
22
|
+
callback?: () => void;
|
|
23
|
+
property_list: Map<keyof T, ShakeProp>;
|
|
24
|
+
}
|
|
25
|
+
export declare class Shaker extends EventTarget {
|
|
26
|
+
_nextId: number;
|
|
27
|
+
_shake_queue: Map<number, ShakeObj<Record<string, number>>>;
|
|
28
|
+
_shaking: boolean;
|
|
29
|
+
_playground: WeakRef<Playground>;
|
|
30
|
+
_callbackId: number | null;
|
|
31
|
+
constructor(playground: Playground);
|
|
32
|
+
_shakeStep: () => void;
|
|
33
|
+
registerCallback(options?: {
|
|
34
|
+
suppressWarning?: boolean;
|
|
35
|
+
}): this;
|
|
36
|
+
removeCallback(options?: {
|
|
37
|
+
suppressWarning?: boolean;
|
|
38
|
+
}): this;
|
|
39
|
+
_onClearCallbacks: () => void;
|
|
40
|
+
shake<T extends object>(target_obj: T, properties: Partial<Record<keyof T, number>>, options?: Partial<ShakeOptions>): number;
|
|
41
|
+
removeShake(shakeId: number | null, options?: {
|
|
42
|
+
suppressWarning?: boolean;
|
|
43
|
+
}): this;
|
|
44
|
+
clearShakes(): this;
|
|
45
|
+
}
|
|
46
|
+
export {};
|