like2d 2.9.0 → 2.10.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/README.md +21 -17
- package/dist/__benchmarks__/vector2.bench.d.ts +2 -0
- package/dist/__benchmarks__/vector2.bench.d.ts.map +1 -0
- package/dist/__benchmarks__/vector2.bench.js +74 -0
- package/dist/{core → audio}/audio.d.ts +12 -3
- package/dist/audio/audio.d.ts.map +1 -0
- package/dist/{core → audio}/audio.js +10 -2
- package/dist/audio/index.d.ts +2 -0
- package/dist/audio/index.d.ts.map +1 -0
- package/dist/audio/index.js +1 -0
- package/dist/engine.d.ts +12 -42
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +34 -76
- package/dist/{core/events.d.ts → events.d.ts} +27 -50
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +5 -0
- package/dist/{core → graphics}/canvas.d.ts +18 -11
- package/dist/graphics/canvas.d.ts.map +1 -0
- package/dist/{core → graphics}/canvas.js +73 -58
- package/dist/{core/graphics.d.ts → graphics/drawing.d.ts} +25 -25
- package/dist/graphics/drawing.d.ts.map +1 -0
- package/dist/{core/graphics.js → graphics/drawing.js} +59 -52
- package/dist/graphics/index.d.ts +19 -0
- package/dist/graphics/index.d.ts.map +1 -0
- package/dist/graphics/index.js +13 -0
- package/dist/index.d.ts +3 -30
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -21
- package/dist/input/controllerdb.json +1 -0
- package/dist/input/gamepad-mapping.d.ts +119 -0
- package/dist/input/gamepad-mapping.d.ts.map +1 -0
- package/dist/input/gamepad-mapping.js +114 -0
- package/dist/input/gamepad.d.ts +73 -0
- package/dist/input/gamepad.d.ts.map +1 -0
- package/dist/input/gamepad.js +291 -0
- package/dist/input/index.d.ts +6 -0
- package/dist/input/index.d.ts.map +1 -0
- package/dist/input/index.js +1 -0
- package/dist/input/input.d.ts +76 -0
- package/dist/input/input.d.ts.map +1 -0
- package/dist/input/input.js +163 -0
- package/dist/input/keyboard.d.ts +15 -0
- package/dist/input/keyboard.d.ts.map +1 -0
- package/dist/{core → input}/keyboard.js +11 -21
- package/dist/input/mouse.d.ts +108 -0
- package/dist/input/mouse.d.ts.map +1 -0
- package/dist/input/mouse.js +241 -0
- package/dist/like.d.ts +80 -0
- package/dist/like.d.ts.map +1 -0
- package/dist/like.js +5 -0
- package/dist/math/index.d.ts +16 -0
- package/dist/math/index.d.ts.map +1 -1
- package/dist/math/index.js +16 -0
- package/dist/math/rect.d.ts +24 -27
- package/dist/math/rect.d.ts.map +1 -1
- package/dist/math/rect.js +47 -73
- package/dist/math/vector2.d.ts +87 -32
- package/dist/math/vector2.d.ts.map +1 -1
- package/dist/math/vector2.js +92 -110
- package/dist/prefab-scenes/index.d.ts +1 -0
- package/dist/prefab-scenes/index.d.ts.map +1 -1
- package/dist/prefab-scenes/index.js +1 -0
- package/dist/prefab-scenes/mapGamepad.d.ts +30 -0
- package/dist/prefab-scenes/mapGamepad.d.ts.map +1 -0
- package/dist/prefab-scenes/mapGamepad.js +192 -0
- package/dist/prefab-scenes/startScreen.d.ts +2 -2
- package/dist/prefab-scenes/startScreen.js +2 -2
- package/dist/scene.d.ts +2 -2
- package/dist/scene.d.ts.map +1 -1
- package/dist/timer/index.d.ts +2 -0
- package/dist/timer/index.d.ts.map +1 -0
- package/dist/timer/index.js +1 -0
- package/dist/timer/timer.d.ts +32 -0
- package/dist/timer/timer.d.ts.map +1 -0
- package/dist/{core → timer}/timer.js +20 -3
- package/package.json +21 -12
- package/dist/core/audio.d.ts.map +0 -1
- package/dist/core/canvas.d.ts.map +0 -1
- package/dist/core/events.d.ts.map +0 -1
- package/dist/core/events.js +0 -21
- package/dist/core/gamepad-mapping.d.ts +0 -58
- package/dist/core/gamepad-mapping.d.ts.map +0 -1
- package/dist/core/gamepad-mapping.js +0 -23
- package/dist/core/gamepad.d.ts +0 -37
- package/dist/core/gamepad.d.ts.map +0 -1
- package/dist/core/gamepad.js +0 -103
- package/dist/core/graphics.d.ts.map +0 -1
- package/dist/core/input-state.d.ts +0 -14
- package/dist/core/input-state.d.ts.map +0 -1
- package/dist/core/input-state.js +0 -50
- package/dist/core/input.d.ts +0 -40
- package/dist/core/input.d.ts.map +0 -1
- package/dist/core/input.js +0 -105
- package/dist/core/keyboard.d.ts +0 -15
- package/dist/core/keyboard.d.ts.map +0 -1
- package/dist/core/like.d.ts +0 -113
- package/dist/core/like.d.ts.map +0 -1
- package/dist/core/like.js +0 -9
- package/dist/core/mouse.d.ts +0 -52
- package/dist/core/mouse.d.ts.map +0 -1
- package/dist/core/mouse.js +0 -142
- package/dist/core/timer.d.ts +0 -15
- package/dist/core/timer.d.ts.map +0 -1
package/dist/like.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module like
|
|
3
|
+
* @description A catalogue of subsystems
|
|
4
|
+
*/
|
|
5
|
+
import type { Audio } from './audio/index';
|
|
6
|
+
import type { Timer } from './timer/index';
|
|
7
|
+
import type { Input } from './input/index';
|
|
8
|
+
import type { Keyboard } from './input/keyboard';
|
|
9
|
+
import type { Mouse } from './input/mouse';
|
|
10
|
+
import type { Gamepad } from './input/gamepad';
|
|
11
|
+
import type { Canvas } from './graphics/canvas';
|
|
12
|
+
import type { BoundGraphics } from './graphics/index';
|
|
13
|
+
import { EventMap, EventType, LikeEvent } from './events';
|
|
14
|
+
import { Scene } from './scene';
|
|
15
|
+
export type TopLevelEventHandler = (event: LikeEvent) => void;
|
|
16
|
+
type Callback<K extends EventType> = (...args: EventMap[K]) => void;
|
|
17
|
+
type Callbacks = {
|
|
18
|
+
[K in EventType]?: Callback<K>;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* The main Like instance.
|
|
22
|
+
* Use this object much how you would the `love` object in Love2D.
|
|
23
|
+
* This is the interface returned by {@link createLike}.
|
|
24
|
+
*/
|
|
25
|
+
export type Like = Callbacks & {
|
|
26
|
+
/** Handle a pool of pseudo-synchronous audio sources with global volume control and more. */
|
|
27
|
+
readonly audio: Audio;
|
|
28
|
+
/** Misc. time functions, including sleeping the game. ZZZ */
|
|
29
|
+
readonly timer: Timer;
|
|
30
|
+
/** Bind inputs to actions that call `like.actionpressed`, or query them here also. */
|
|
31
|
+
readonly input: Input;
|
|
32
|
+
/** Check if scancodes are down / pressed. */
|
|
33
|
+
readonly keyboard: Keyboard;
|
|
34
|
+
/** Mouse module: Get a properly scaled mouse position, set capture and more. */
|
|
35
|
+
readonly mouse: Mouse;
|
|
36
|
+
/** Gamepad module: Map gamepads and check their buttons. */
|
|
37
|
+
readonly gamepad: Gamepad;
|
|
38
|
+
/** Get and set screen size, choosing between native and pixel perfect prescaling. Plus fullscreen control. */
|
|
39
|
+
readonly canvas: Canvas;
|
|
40
|
+
/** Graphics module: LOVE-style rendering, plus a pseudo-synchronous way to load images. */
|
|
41
|
+
gfx: BoundGraphics;
|
|
42
|
+
/** I think you meant to type `canvas`. */
|
|
43
|
+
window?: never;
|
|
44
|
+
/**
|
|
45
|
+
* Start the game loop. Call this only once.
|
|
46
|
+
* @returns Promise that resolves when the engine is ready
|
|
47
|
+
*/
|
|
48
|
+
start(): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Only call this when you're done with LIKE. Everything will stop
|
|
51
|
+
* running, and probably break if you try to use it.
|
|
52
|
+
*/
|
|
53
|
+
dispose(): void;
|
|
54
|
+
/**
|
|
55
|
+
* A simple way to set the current scene, which acts like a pluggable
|
|
56
|
+
* set of callbacks.
|
|
57
|
+
*/
|
|
58
|
+
setScene(scene?: Scene): void;
|
|
59
|
+
/**
|
|
60
|
+
* LIKE's runtime is built around calling handleEvent.
|
|
61
|
+
*
|
|
62
|
+
* This function recieves all events. If set to undefined,
|
|
63
|
+
* `like.callOwnHandlers(ev)` is the default behavior.
|
|
64
|
+
*
|
|
65
|
+
* Otherwise, you can really customize LIKE by setting this
|
|
66
|
+
* to a custom handler.
|
|
67
|
+
*
|
|
68
|
+
* For example, the scene architecture is built around
|
|
69
|
+
* setting this function.
|
|
70
|
+
*/
|
|
71
|
+
handleEvent?: TopLevelEventHandler;
|
|
72
|
+
/**
|
|
73
|
+
* Used as the default `like.handleEvent`, simply dispatches
|
|
74
|
+
* an event into LIKE callbacks.
|
|
75
|
+
* @param event
|
|
76
|
+
*/
|
|
77
|
+
callOwnHandlers(event: LikeEvent): void;
|
|
78
|
+
};
|
|
79
|
+
export {};
|
|
80
|
+
//# sourceMappingURL=like.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"like.d.ts","sourceRoot":"","sources":["../src/like.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAC1D,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,MAAM,MAAM,oBAAoB,GAAG,CAAC,KAAK,EAAE,SAAS,KAAK,IAAI,CAAC;AAE9D,KAAK,QAAQ,CAAC,CAAC,SAAS,SAAS,IAAI,CAAC,GAAG,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;AAEpE,KAAK,SAAS,GAAG;KACd,CAAC,IAAI,SAAS,CAAC,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC;CAC/B,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,IAAI,GAAG,SAAS,GAAG;IAC7B,6FAA6F;IAC7F,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,6DAA6D;IAC7D,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,sFAAsF;IACtF,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,6CAA6C;IAC7C,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,gFAAgF;IAChF,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,4DAA4D;IAC5D,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,8GAA8G;IAC9G,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,2FAA2F;IAC3F,GAAG,EAAE,aAAa,CAAC;IAEnB,0CAA0C;IAC1C,MAAM,CAAC,EAAE,KAAK,CAAC;IAEf;;;OAGG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEvB;;;OAGG;IACH,OAAO,IAAI,IAAI,CAAC;IAEhB;;;OAGG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC;IAE9B;;;;;;;;;;;OAWG;IACH,WAAW,CAAC,EAAE,oBAAoB,CAAC;IAEnC;;;;OAIG;IACH,eAAe,CAAC,KAAK,EAAE,SAAS,GAAG,IAAI,CAAC;CACzC,CAAA"}
|
package/dist/like.js
ADDED
package/dist/math/index.d.ts
CHANGED
|
@@ -1,2 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module math
|
|
3
|
+
*
|
|
4
|
+
* A purely functional math library, mostly for 2d.
|
|
5
|
+
*
|
|
6
|
+
* Check out {@link vector2} and {@link rect}
|
|
7
|
+
*
|
|
8
|
+
* Why purely functional?
|
|
9
|
+
* 1. About as fast as it can get -- the JIT will pick up on these.
|
|
10
|
+
* 2. Plays nice with stream iterators.
|
|
11
|
+
* 3. JS is likely to add a pipe operator in the future.
|
|
12
|
+
* 4. Construction is as easy as building an array.
|
|
13
|
+
*/
|
|
14
|
+
/** True modulus, conspicuously missing from JS */
|
|
1
15
|
export declare const mod: (a: number, b: number) => number;
|
|
16
|
+
export { type Vector2, Vec2 } from "./vector2";
|
|
17
|
+
export { type Rectangle, Rect } from "./rect";
|
|
2
18
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/math/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/math/index.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,GAAG,GAAI,GAAG,MAAM,EAAE,GAAG,MAAM,WAAsB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/math/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,kDAAkD;AAClD,eAAO,MAAM,GAAG,GAAI,GAAG,MAAM,EAAE,GAAG,MAAM,WAAsB,CAAC;AAE/D,OAAO,EAAE,KAAK,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,EAAE,KAAK,SAAS,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC"}
|
package/dist/math/index.js
CHANGED
|
@@ -1 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module math
|
|
3
|
+
*
|
|
4
|
+
* A purely functional math library, mostly for 2d.
|
|
5
|
+
*
|
|
6
|
+
* Check out {@link vector2} and {@link rect}
|
|
7
|
+
*
|
|
8
|
+
* Why purely functional?
|
|
9
|
+
* 1. About as fast as it can get -- the JIT will pick up on these.
|
|
10
|
+
* 2. Plays nice with stream iterators.
|
|
11
|
+
* 3. JS is likely to add a pipe operator in the future.
|
|
12
|
+
* 4. Construction is as easy as building an array.
|
|
13
|
+
*/
|
|
14
|
+
/** True modulus, conspicuously missing from JS */
|
|
1
15
|
export const mod = (a, b) => ((a % b) + b) % b;
|
|
16
|
+
export { Vec2 } from "./vector2";
|
|
17
|
+
export { Rect } from "./rect";
|
package/dist/math/rect.d.ts
CHANGED
|
@@ -41,31 +41,28 @@ import { type Vector2 } from '../math/vector2';
|
|
|
41
41
|
*
|
|
42
42
|
* */
|
|
43
43
|
export type Rectangle = [number, number, number, number];
|
|
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
|
-
function setSize(r: Rectangle, size: Vector2): Rectangle;
|
|
69
|
-
function setCenter(r: Rectangle, center: Vector2): Rectangle;
|
|
70
|
-
}
|
|
44
|
+
export declare const Rect: {
|
|
45
|
+
fromPoints(a: Vector2, b: Vector2): Rectangle;
|
|
46
|
+
fromCenter(center: Vector2, size: Vector2): Rectangle;
|
|
47
|
+
position(r: Rectangle): Vector2;
|
|
48
|
+
size(r: Rectangle): Vector2;
|
|
49
|
+
center(r: Rectangle): Vector2;
|
|
50
|
+
topLeft(r: Rectangle): Vector2;
|
|
51
|
+
topRight(r: Rectangle): Vector2;
|
|
52
|
+
bottomLeft(r: Rectangle): Vector2;
|
|
53
|
+
bottomRight(r: Rectangle): Vector2;
|
|
54
|
+
area(r: Rectangle): number;
|
|
55
|
+
isEmpty(r: Rectangle): boolean;
|
|
56
|
+
clampPoint(r: Rectangle, point: Vector2): Vector2;
|
|
57
|
+
containsPoint(r: Rectangle, point: Vector2): boolean;
|
|
58
|
+
containsRect(r: Rectangle, other: Rectangle): boolean;
|
|
59
|
+
intersects(r: Rectangle, other: Rectangle): boolean;
|
|
60
|
+
intersection(r: Rectangle, other: Rectangle): Rectangle;
|
|
61
|
+
union(r: Rectangle, other: Rectangle): Rectangle;
|
|
62
|
+
inflate(r: Rectangle, amount: number): Rectangle;
|
|
63
|
+
offset(r: Rectangle, delta: Vector2): Rectangle;
|
|
64
|
+
setPosition(r: Rectangle, pos: Vector2): Rectangle;
|
|
65
|
+
setSize(r: Rectangle, size: Vector2): Rectangle;
|
|
66
|
+
setCenter(r: Rectangle, center: Vector2): Rectangle;
|
|
67
|
+
};
|
|
71
68
|
//# sourceMappingURL=rect.d.ts.map
|
package/dist/math/rect.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rect.d.ts","sourceRoot":"","sources":["../../src/math/rect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAErD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAwCK;AACL,MAAM,MAAM,SAAS,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAEzD
|
|
1
|
+
{"version":3,"file":"rect.d.ts","sourceRoot":"","sources":["../../src/math/rect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAErD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAwCK;AACL,MAAM,MAAM,SAAS,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;AAEzD,eAAO,MAAM,IAAI;kBACD,OAAO,KAAK,OAAO,GAAG,SAAS;uBAQ1B,OAAO,QAAQ,OAAO,GAAG,SAAS;gBASzC,SAAS,GAAG,OAAO;YAIvB,SAAS,GAAG,OAAO;cAIjB,SAAS,GAAG,OAAO;eAIlB,SAAS,GAAG,OAAO;gBAIlB,SAAS,GAAG,OAAO;kBAIjB,SAAS,GAAG,OAAO;mBAIlB,SAAS,GAAG,OAAO;YAI1B,SAAS,GAAG,MAAM;eAIf,SAAS,GAAG,OAAO;kBAIhB,SAAS,SAAS,OAAO,GAAG,OAAO;qBAIhC,SAAS,SAAS,OAAO,GAAG,OAAO;oBASpC,SAAS,SAAS,SAAS,GAAG,OAAO;kBASvC,SAAS,SAAS,SAAS,GAAG,OAAO;oBASnC,SAAS,SAAS,SAAS,GAAG,SAAS;aAa9C,SAAS,SAAS,SAAS,GAAG,SAAS;eAQrC,SAAS,UAAU,MAAM,GAAG,SAAS;cAStC,SAAS,SAAS,OAAO,GAAG,SAAS;mBAIhC,SAAS,OAAO,OAAO,GAAG,SAAS;eAIvC,SAAS,QAAQ,OAAO,GAAG,SAAS;iBAIlC,SAAS,UAAU,OAAO,GAAG,SAAS;CAQpD,CAAC"}
|
package/dist/math/rect.js
CHANGED
|
@@ -1,88 +1,69 @@
|
|
|
1
1
|
import { Vec2 } from '../math/vector2';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
*/
|
|
5
|
-
export var Rect;
|
|
6
|
-
(function (Rect) {
|
|
7
|
-
function fromPoints(a, b) {
|
|
2
|
+
export const Rect = {
|
|
3
|
+
fromPoints(a, b) {
|
|
8
4
|
const minX = Math.min(a[0], b[0]);
|
|
9
5
|
const minY = Math.min(a[1], b[1]);
|
|
10
6
|
const maxX = Math.max(a[0], b[0]);
|
|
11
7
|
const maxY = Math.max(a[1], b[1]);
|
|
12
8
|
return [minX, minY, maxX - minX, maxY - minY];
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function fromCenter(center, size) {
|
|
9
|
+
},
|
|
10
|
+
fromCenter(center, size) {
|
|
16
11
|
return [
|
|
17
12
|
center[0] - size[0] / 2,
|
|
18
13
|
center[1] - size[1] / 2,
|
|
19
14
|
size[0],
|
|
20
15
|
size[1],
|
|
21
16
|
];
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function position(r) {
|
|
17
|
+
},
|
|
18
|
+
position(r) {
|
|
25
19
|
return [r[0], r[1]];
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function size(r) {
|
|
20
|
+
},
|
|
21
|
+
size(r) {
|
|
29
22
|
return [r[2], r[3]];
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function center(r) {
|
|
23
|
+
},
|
|
24
|
+
center(r) {
|
|
33
25
|
return [r[0] + r[2] / 2, r[1] + r[3] / 2];
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function topLeft(r) {
|
|
26
|
+
},
|
|
27
|
+
topLeft(r) {
|
|
37
28
|
return [r[0], r[1]];
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function topRight(r) {
|
|
29
|
+
},
|
|
30
|
+
topRight(r) {
|
|
41
31
|
return [r[0] + r[2], r[1]];
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function bottomLeft(r) {
|
|
32
|
+
},
|
|
33
|
+
bottomLeft(r) {
|
|
45
34
|
return [r[0], r[1] + r[3]];
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function bottomRight(r) {
|
|
35
|
+
},
|
|
36
|
+
bottomRight(r) {
|
|
49
37
|
return [r[0] + r[2], r[1] + r[3]];
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function area(r) {
|
|
38
|
+
},
|
|
39
|
+
area(r) {
|
|
53
40
|
return r[2] * r[3];
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function isEmpty(r) {
|
|
41
|
+
},
|
|
42
|
+
isEmpty(r) {
|
|
57
43
|
return r[2] <= 0 || r[3] <= 0;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
Rect.clampPoint = clampPoint;
|
|
64
|
-
function containsPoint(r, point) {
|
|
44
|
+
},
|
|
45
|
+
clampPoint(r, point) {
|
|
46
|
+
return Vec2.clamp(point, this.position(r), this.bottomRight(r));
|
|
47
|
+
},
|
|
48
|
+
containsPoint(r, point) {
|
|
65
49
|
return (point[0] >= r[0] &&
|
|
66
50
|
point[0] <= r[0] + r[2] &&
|
|
67
51
|
point[1] >= r[1] &&
|
|
68
52
|
point[1] <= r[1] + r[3]);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function containsRect(r, other) {
|
|
53
|
+
},
|
|
54
|
+
containsRect(r, other) {
|
|
72
55
|
return (other[0] >= r[0] &&
|
|
73
56
|
other[0] + other[2] <= r[0] + r[2] &&
|
|
74
57
|
other[1] >= r[1] &&
|
|
75
58
|
other[1] + other[3] <= r[1] + r[3]);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function intersects(r, other) {
|
|
59
|
+
},
|
|
60
|
+
intersects(r, other) {
|
|
79
61
|
return (r[0] < other[0] + other[2] &&
|
|
80
62
|
r[0] + r[2] > other[0] &&
|
|
81
63
|
r[1] < other[1] + other[3] &&
|
|
82
64
|
r[1] + r[3] > other[1]);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function intersection(r, other) {
|
|
65
|
+
},
|
|
66
|
+
intersection(r, other) {
|
|
86
67
|
const x1 = Math.max(r[0], other[0]);
|
|
87
68
|
const y1 = Math.max(r[1], other[1]);
|
|
88
69
|
const x2 = Math.min(r[0] + r[2], other[0] + other[2]);
|
|
@@ -93,44 +74,37 @@ export var Rect;
|
|
|
93
74
|
return [0, 0, 0, 0];
|
|
94
75
|
}
|
|
95
76
|
return [x1, y1, w, h];
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function union(r, other) {
|
|
77
|
+
},
|
|
78
|
+
union(r, other) {
|
|
99
79
|
const x1 = Math.min(r[0], other[0]);
|
|
100
80
|
const y1 = Math.min(r[1], other[1]);
|
|
101
81
|
const x2 = Math.max(r[0] + r[2], other[0] + other[2]);
|
|
102
82
|
const y2 = Math.max(r[1] + r[3], other[1] + other[3]);
|
|
103
83
|
return [x1, y1, x2 - x1, y2 - y1];
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function inflate(r, amount) {
|
|
84
|
+
},
|
|
85
|
+
inflate(r, amount) {
|
|
107
86
|
return [
|
|
108
87
|
r[0] - amount,
|
|
109
88
|
r[1] - amount,
|
|
110
89
|
r[2] + amount * 2,
|
|
111
90
|
r[3] + amount * 2,
|
|
112
91
|
];
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
function offset(r, delta) {
|
|
92
|
+
},
|
|
93
|
+
offset(r, delta) {
|
|
116
94
|
return [r[0] + delta[0], r[1] + delta[1], r[2], r[3]];
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
function setPosition(r, pos) {
|
|
95
|
+
},
|
|
96
|
+
setPosition(r, pos) {
|
|
120
97
|
return [pos[0], pos[1], r[2], r[3]];
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function setSize(r, size) {
|
|
98
|
+
},
|
|
99
|
+
setSize(r, size) {
|
|
124
100
|
return [r[0], r[1], size[0], size[1]];
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function setCenter(r, center) {
|
|
101
|
+
},
|
|
102
|
+
setCenter(r, center) {
|
|
128
103
|
return [
|
|
129
104
|
center[0] - r[2] / 2,
|
|
130
105
|
center[1] - r[3] / 2,
|
|
131
106
|
r[2],
|
|
132
107
|
r[3],
|
|
133
108
|
];
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
})(Rect || (Rect = {}));
|
|
109
|
+
},
|
|
110
|
+
};
|
package/dist/math/vector2.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export type Pair<T> = [T, T];
|
|
1
2
|
/** A pair of numbers `[x, y]`
|
|
2
3
|
* representing for example:
|
|
3
4
|
* - position in 2D space
|
|
@@ -6,6 +7,11 @@
|
|
|
6
7
|
*
|
|
7
8
|
* See {@link Vec2} for full library.
|
|
8
9
|
*
|
|
10
|
+
* Note that mutating Vector2 is allowed, but discouraged
|
|
11
|
+
* except for memory-demanding situations.
|
|
12
|
+
* 1. Setting `position[0]` has clunky syntax compared to the library
|
|
13
|
+
* 2. Any references to `position` will change their value.
|
|
14
|
+
*
|
|
9
15
|
* ## Examples
|
|
10
16
|
*
|
|
11
17
|
* #### Constructing a Vector2
|
|
@@ -28,8 +34,8 @@
|
|
|
28
34
|
*
|
|
29
35
|
* #### Summing an array of Vector2
|
|
30
36
|
* ```ts
|
|
31
|
-
* const
|
|
32
|
-
* const sum =
|
|
37
|
+
* const vecs: Vector2[] = [[50, 100], [-5, -5], [0, 99]];
|
|
38
|
+
* const sum = vecs.reduce(Vec2.add);
|
|
33
39
|
* ```
|
|
34
40
|
*
|
|
35
41
|
* #### Using LIKE graphics API
|
|
@@ -41,38 +47,87 @@
|
|
|
41
47
|
* )
|
|
42
48
|
* like.graphics.circle('fill', 'blue', pos, 20);
|
|
43
49
|
* ```
|
|
50
|
+
*
|
|
51
|
+
* #### Getting the bounding box of an array of `Vector2`s
|
|
52
|
+
* ```ts
|
|
53
|
+
* const upperLeft = myVector2List.reduce(Vec2.min);
|
|
54
|
+
* const lowerRight = myVector2List.reduce(Vec2.max);
|
|
55
|
+
* const boundingBox = Rect.fromPoints(upperLeft, lowerRight);
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* #### Squaring each element of a Vector2
|
|
59
|
+
* ```ts
|
|
60
|
+
* const squareVec2 = Vec2.map(a: number => a**2);
|
|
61
|
+
* squareVec2([6, 7]) // returns [36, 42]
|
|
62
|
+
* // one in one line...
|
|
63
|
+
* Vec2.map(a: number => a**2)([6, 7]);
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
44
66
|
* */
|
|
45
|
-
export type Vector2 =
|
|
46
|
-
/**
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
67
|
+
export type Vector2 = Pair<number>;
|
|
68
|
+
/**
|
|
69
|
+
* According to benchmarks, constructing funcs with these helpers is just as fast as loose coords in v8.
|
|
70
|
+
* Why not, then?
|
|
71
|
+
* If you're writing hyper-optimized Vector2 code, just use loose math anyway
|
|
72
|
+
* -- it will likely logically optimize down further, if you do the algebra.
|
|
73
|
+
*/
|
|
74
|
+
/** @see {@link Vec2.map} */
|
|
75
|
+
declare function map2x1<I, O>(op: (a: I) => O): (a: Pair<I>) => Pair<O>;
|
|
76
|
+
/** @see {@link Vec2.map2} */
|
|
77
|
+
declare function map2x2<I, O>(op: (a: I, b: I) => O): (a: Pair<I>, b: I | Pair<I>) => Pair<O>;
|
|
78
|
+
export declare const Vec2: {
|
|
79
|
+
/**
|
|
80
|
+
* Turn a unary function into a pair-wise unary function.
|
|
81
|
+
* @param op A function that takes one arg and returns something.
|
|
82
|
+
* @returns A function that takes a pair such as `[number, number]` (Vector2) and transforms it with `op`.
|
|
83
|
+
*/
|
|
84
|
+
map: typeof map2x1;
|
|
85
|
+
/**
|
|
86
|
+
* Turn a binary function into a pair-wise binary function, for example one that operates on two `Vector2`s.
|
|
87
|
+
* @param op A function that takes two values and calculates one.
|
|
88
|
+
* @returns A function that runs `op` pair-wise on both arguments, constructing a new pair.
|
|
89
|
+
*/
|
|
90
|
+
map2: typeof map2x2;
|
|
91
|
+
add: (a: Pair<number>, b: number | Pair<number>) => Pair<number>;
|
|
92
|
+
sub: (a: Pair<number>, b: number | Pair<number>) => Pair<number>;
|
|
93
|
+
mul: (a: Pair<number>, b: number | Pair<number>) => Pair<number>;
|
|
94
|
+
div: (a: Pair<number>, b: number | Pair<number>) => Pair<number>;
|
|
95
|
+
/** Returns the maximum of two coordinates,
|
|
96
|
+
* i.e. the lower right corner of a bounding box containing them. */
|
|
97
|
+
max: (a: Pair<number>, b: number | Pair<number>) => Pair<number>;
|
|
98
|
+
/** Returns the minimum of two coordinates,
|
|
99
|
+
* i.e. the upper left corner of a bounding box containing them. */
|
|
100
|
+
min: (a: Pair<number>, b: number | Pair<number>) => Pair<number>;
|
|
101
|
+
/** Apply a true modulus (i.e. where -1 % 3 == 2) to a Vector2.
|
|
102
|
+
*
|
|
103
|
+
* For JS-style modulus (who wants that?) use `Vec2.map2((a,b) => a % b)` */
|
|
104
|
+
mod: (a: Pair<number>, b: number | Pair<number>) => Pair<number>;
|
|
105
|
+
/** Apply deep (not referential) equality to a pair of Vec2s.
|
|
106
|
+
* @param epsilon Tolerance factor for inexact matches.
|
|
107
|
+
*/
|
|
108
|
+
eq(v: Vector2, other: Vector2, epsilon: 0): boolean;
|
|
109
|
+
dot(a: Vector2, b: Vector2): number;
|
|
110
|
+
cross(a: Vector2, b: Vector2): number;
|
|
111
|
+
lengthSq(v: Vector2): number;
|
|
112
|
+
length(v: Vector2): number;
|
|
113
|
+
/** Set the length of a Vector2 to 1.0 while preserving its angle. */
|
|
114
|
+
normalize(v: Vector2): Vector2;
|
|
115
|
+
distance(a: Vector2, b: Vector2): number;
|
|
116
|
+
lerp(a: Vector2, b: Vector2, t: number): Vector2;
|
|
117
|
+
toPolar(v: Vector2): {
|
|
62
118
|
r: number;
|
|
63
119
|
angle: number;
|
|
64
120
|
};
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
121
|
+
fromPolar(r: number, angle: number): Vector2;
|
|
122
|
+
angle(v: Vector2): number;
|
|
123
|
+
rotate(v: Vector2, angle: number): Vector2;
|
|
124
|
+
negate: (a: Pair<number>) => Pair<number>;
|
|
125
|
+
floor: (a: Pair<number>) => Pair<number>;
|
|
126
|
+
ceil: (a: Pair<number>) => Pair<number>;
|
|
127
|
+
round: (a: Pair<number>) => Pair<number>;
|
|
128
|
+
clamp(v: Vector2, min: Vector2, max: Vector2): Vector2;
|
|
129
|
+
fromAngle(angle: number, len?: number): Vector2;
|
|
130
|
+
zero(): Vector2;
|
|
131
|
+
};
|
|
132
|
+
export {};
|
|
78
133
|
//# sourceMappingURL=vector2.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"vector2.d.ts","sourceRoot":"","sources":["../../src/math/vector2.ts"],"names":[],"mappings":"AAEA
|
|
1
|
+
{"version":3,"file":"vector2.d.ts","sourceRoot":"","sources":["../../src/math/vector2.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAE7B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KAgEK;AAEL,MAAM,MAAM,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC;AAEnC;;;;;GAKG;AAEH,4BAA4B;AAC5B,iBAAS,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,CAE9D;AAED,6BAA6B;AAC7B,iBAAS,MAAM,CAAC,CAAC,EAAE,CAAC,EAClB,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,CAAC,GACpB,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,CAKzC;AAED,eAAO,MAAM,IAAI;IACf;;;;OAIG;;IAEH;;;;OAIG;;;;;;IAOH;wEACoE;;IAEpE;uEACmE;;IAEnE;;gFAE4E;;IAG5E;;OAEG;UACG,OAAO,SAAS,OAAO,WAAW,CAAC,GAAG,OAAO;WAM5C,OAAO,KAAK,OAAO,GAAG,MAAM;aAI1B,OAAO,KAAK,OAAO,GAAG,MAAM;gBAIzB,OAAO,GAAG,MAAM;cAIlB,OAAO,GAAG,MAAM;IAI1B,qEAAqE;iBACxD,OAAO,GAAG,OAAO;gBAMlB,OAAO,KAAK,OAAO,GAAG,MAAM;YAIhC,OAAO,KAAK,OAAO,KAAK,MAAM,GAAG,OAAO;eAIrC,OAAO,GAAG;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE;iBAOpC,MAAM,SAAS,MAAM,GAAG,OAAO;aAInC,OAAO,GAAG,MAAM;cAIf,OAAO,SAAS,MAAM,GAAG,OAAO;;;;;aAWjC,OAAO,OAAO,OAAO,OAAO,OAAO,GAAG,OAAO;qBAOrC,MAAM,QAAO,MAAM,GAAO,OAAO;YAI1C,OAAO;CAGhB,CAAC"}
|