like2d 2.7.4 → 2.9.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/README.md +34 -35
- package/dist/core/audio.d.ts +12 -9
- package/dist/core/audio.d.ts.map +1 -1
- package/dist/core/audio.js +7 -4
- package/dist/core/canvas.d.ts +58 -0
- package/dist/core/canvas.d.ts.map +1 -0
- package/dist/core/canvas.js +209 -0
- package/dist/core/events.d.ts +92 -7
- package/dist/core/events.d.ts.map +1 -1
- package/dist/core/events.js +20 -0
- package/dist/core/gamepad-mapping.d.ts +57 -18
- package/dist/core/gamepad-mapping.d.ts.map +1 -1
- package/dist/core/gamepad-mapping.js +23 -223
- package/dist/core/gamepad.d.ts +34 -58
- package/dist/core/gamepad.d.ts.map +1 -1
- package/dist/core/gamepad.js +79 -213
- package/dist/core/graphics.d.ts +175 -64
- package/dist/core/graphics.d.ts.map +1 -1
- package/dist/core/graphics.js +294 -198
- package/dist/core/input-state.d.ts +2 -2
- package/dist/core/input-state.d.ts.map +1 -1
- package/dist/core/input.d.ts +22 -15
- package/dist/core/input.d.ts.map +1 -1
- package/dist/core/input.js +25 -37
- package/dist/core/keyboard.d.ts +7 -7
- package/dist/core/keyboard.d.ts.map +1 -1
- package/dist/core/keyboard.js +24 -31
- package/dist/core/like.d.ts +98 -44
- package/dist/core/like.d.ts.map +1 -1
- package/dist/core/like.js +8 -0
- package/dist/core/mouse.d.ts +45 -22
- package/dist/core/mouse.d.ts.map +1 -1
- package/dist/core/mouse.js +90 -78
- package/dist/core/timer.d.ts +2 -5
- package/dist/core/timer.d.ts.map +1 -1
- package/dist/core/timer.js +2 -14
- package/dist/engine.d.ts +61 -11
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +119 -102
- package/dist/index.d.ts +42 -21
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +35 -14
- package/dist/math/index.d.ts +2 -0
- package/dist/math/index.d.ts.map +1 -0
- package/dist/math/index.js +1 -0
- package/dist/math/rect.d.ts +71 -0
- package/dist/math/rect.d.ts.map +1 -0
- package/dist/{core → math}/rect.js +8 -0
- package/dist/math/vector2.d.ts +78 -0
- package/dist/math/vector2.d.ts.map +1 -0
- package/dist/{core → math}/vector2.js +24 -0
- package/dist/prefab-scenes/index.d.ts +7 -0
- package/dist/prefab-scenes/index.d.ts.map +1 -0
- package/dist/prefab-scenes/index.js +6 -0
- package/dist/prefab-scenes/startScreen.d.ts +59 -0
- package/dist/prefab-scenes/startScreen.d.ts.map +1 -0
- package/dist/{scenes/startup.js → prefab-scenes/startScreen.js} +47 -7
- package/dist/scene.d.ts +103 -5
- package/dist/scene.d.ts.map +1 -1
- package/dist/scene.js +28 -1
- package/package.json +18 -2
- package/dist/core/canvas-config.d.ts +0 -22
- package/dist/core/canvas-config.d.ts.map +0 -1
- package/dist/core/canvas-config.js +0 -14
- package/dist/core/canvas-manager.d.ts +0 -25
- package/dist/core/canvas-manager.d.ts.map +0 -1
- package/dist/core/canvas-manager.js +0 -178
- package/dist/core/gamepad-buttons.d.ts +0 -23
- package/dist/core/gamepad-buttons.d.ts.map +0 -1
- package/dist/core/gamepad-buttons.js +0 -36
- package/dist/core/rect.d.ts +0 -26
- package/dist/core/rect.d.ts.map +0 -1
- package/dist/core/vector2.d.ts +0 -26
- package/dist/core/vector2.d.ts.map +0 -1
- package/dist/gamecontrollerdb.txt +0 -2221
- package/dist/scenes/startup.d.ts +0 -18
- package/dist/scenes/startup.d.ts.map +0 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,25 +1,46 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @module like2d
|
|
3
|
+
* @description A cozy web-native 2D game framework.
|
|
4
|
+
*
|
|
5
|
+
* See main like/README.md file an for overview of Like2D.
|
|
6
|
+
*/
|
|
7
|
+
import type { LikeEvent } from './core/events';
|
|
2
8
|
import type { Like } from './core/like';
|
|
3
9
|
export type { Like } from './core/like';
|
|
4
|
-
export type {
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
7
|
-
export type { Like2DEvent, EventType, EventMap } from './core/events';
|
|
8
|
-
export type { CanvasMode, PartialCanvasMode } from './core/canvas-config';
|
|
9
|
-
export type { Color, Quad, ShapeProps, DrawProps, PrintProps } from './core/graphics';
|
|
10
|
+
export type { LikeEvent, EventType, EventMap } from './core/events';
|
|
11
|
+
export type { CanvasSize, CanvasModeOptions as CanvasModeFlags } from './core/canvas';
|
|
12
|
+
export type { Color, ShapeProps, DrawProps, PrintProps } from './core/graphics';
|
|
10
13
|
export { ImageHandle } from './core/graphics';
|
|
11
|
-
export type {
|
|
12
|
-
export type
|
|
13
|
-
export
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
export
|
|
14
|
+
export type { AudioSource, AudioSourceOptions } from './core/audio';
|
|
15
|
+
export { type LikeButton } from './core/gamepad';
|
|
16
|
+
export type TopLevelEventHandler = (event: LikeEvent) => void;
|
|
17
|
+
/**
|
|
18
|
+
* Create a new Like2D game instance attached to a DOM container.
|
|
19
|
+
*
|
|
20
|
+
* This is the entry point for all Like2D games. It creates a canvas element,
|
|
21
|
+
* initializes all subsystems (graphics, audio, input), and returns an object
|
|
22
|
+
* where you can assign game callbacks.
|
|
23
|
+
*
|
|
24
|
+
* ### How to bind callbacks
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* export const like = createLike();
|
|
28
|
+
*
|
|
29
|
+
* like.start();
|
|
30
|
+
*
|
|
31
|
+
* like.draw = () => { like.gfx.clear('yellow') }
|
|
32
|
+
*
|
|
33
|
+
* like.update = function (dt: number) {
|
|
34
|
+
* if (dt === Math.random()) {
|
|
35
|
+
* console.log("You just won the Powerball!")
|
|
36
|
+
* }
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @param container - The HTML element to attach the game canvas to.
|
|
41
|
+
* Must be in the DOM.
|
|
42
|
+
* @returns A {@link Like} instance ready for callback assignment
|
|
43
|
+
*
|
|
44
|
+
*/
|
|
45
|
+
export declare function createLike(container: HTMLElement): Like;
|
|
25
46
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAExC,YAAY,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AACxC,YAAY,EAAE,SAAS,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACpE,YAAY,EAAE,UAAU,EAAE,iBAAiB,IAAI,eAAe,EAAE,MAAM,eAAe,CAAC;AACtF,YAAY,EAAE,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAChF,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC9C,YAAY,EAAE,WAAW,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AACpE,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAEjD,MAAM,MAAM,oBAAoB,GAAG,CAAC,KAAK,EAAE,SAAS,KAAK,IAAI,CAAC;AAE9D;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,wBAAgB,UAAU,CAAC,SAAS,EAAE,WAAW,GAAG,IAAI,CAGvD"}
|
package/dist/index.js
CHANGED
|
@@ -1,19 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module like2d
|
|
3
|
+
* @description A cozy web-native 2D game framework.
|
|
4
|
+
*
|
|
5
|
+
* See main like/README.md file an for overview of Like2D.
|
|
6
|
+
*/
|
|
1
7
|
import { Engine } from './engine';
|
|
2
|
-
export { Vec2 } from './core/vector2';
|
|
3
|
-
export { Rect } from './core/rect';
|
|
4
8
|
export { ImageHandle } from './core/graphics';
|
|
5
|
-
|
|
6
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Create a new Like2D game instance attached to a DOM container.
|
|
11
|
+
*
|
|
12
|
+
* This is the entry point for all Like2D games. It creates a canvas element,
|
|
13
|
+
* initializes all subsystems (graphics, audio, input), and returns an object
|
|
14
|
+
* where you can assign game callbacks.
|
|
15
|
+
*
|
|
16
|
+
* ### How to bind callbacks
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* export const like = createLike();
|
|
20
|
+
*
|
|
21
|
+
* like.start();
|
|
22
|
+
*
|
|
23
|
+
* like.draw = () => { like.gfx.clear('yellow') }
|
|
24
|
+
*
|
|
25
|
+
* like.update = function (dt: number) {
|
|
26
|
+
* if (dt === Math.random()) {
|
|
27
|
+
* console.log("You just won the Powerball!")
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @param container - The HTML element to attach the game canvas to.
|
|
33
|
+
* Must be in the DOM.
|
|
34
|
+
* @returns A {@link Like} instance ready for callback assignment
|
|
35
|
+
*
|
|
36
|
+
*/
|
|
7
37
|
export function createLike(container) {
|
|
8
38
|
const engine = new Engine(container);
|
|
9
|
-
|
|
10
|
-
const handleEvent = (event) => {
|
|
11
|
-
const cb = callbacks[event.type];
|
|
12
|
-
if (cb)
|
|
13
|
-
cb(...event.args);
|
|
14
|
-
};
|
|
15
|
-
return Object.assign(engine.like, callbacks, {
|
|
16
|
-
start: () => engine.start(handleEvent),
|
|
17
|
-
dispose: () => engine.dispose(),
|
|
18
|
-
});
|
|
39
|
+
return engine.like;
|
|
19
40
|
}
|
|
@@ -0,0 +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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const mod = (a, b) => ((a % b) + b) % b;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { type Vector2 } from '../math/vector2';
|
|
2
|
+
/**
|
|
3
|
+
* A four-coordinate type `[x, y, width, height]`.
|
|
4
|
+
* Full reference {@link Rect}.
|
|
5
|
+
*
|
|
6
|
+
* ## Examples
|
|
7
|
+
*
|
|
8
|
+
* ### Construct a rectangle
|
|
9
|
+
* ```ts
|
|
10
|
+
* const beastPen: Rectangle = [20, 25, 40, 50];
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* ### Construct around a center point and size
|
|
14
|
+
* ```ts
|
|
15
|
+
* const beastPen: Rectangle = Rect.setCenter(
|
|
16
|
+
* [0, 0, ...penSize],
|
|
17
|
+
* beastPos
|
|
18
|
+
* );
|
|
19
|
+
* ```
|
|
20
|
+
* ### Deconstruct a rect
|
|
21
|
+
* ```ts
|
|
22
|
+
* [x, y, w, h] = beastPen;
|
|
23
|
+
* ```
|
|
24
|
+
* ### Deconstruct into points
|
|
25
|
+
* ```ts
|
|
26
|
+
* const penPos = Rect.position(beastPen);
|
|
27
|
+
* const penSize = Rect.size(beastPen);
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* ### Check if beast is in his pen
|
|
31
|
+
* ```ts
|
|
32
|
+
* const isInPen = Rect.containsPoint(
|
|
33
|
+
* beastPen,
|
|
34
|
+
* beast.pos,
|
|
35
|
+
* )
|
|
36
|
+
* ```
|
|
37
|
+
* ### Put the beast back in his pen
|
|
38
|
+
* ```ts
|
|
39
|
+
* beast.pos = Rect.clampPoint(beast.pos, beastPen)
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* */
|
|
43
|
+
export type Rectangle = [number, number, number, number];
|
|
44
|
+
/**
|
|
45
|
+
* A set of
|
|
46
|
+
*/
|
|
47
|
+
export declare namespace Rect {
|
|
48
|
+
function fromPoints(a: Vector2, b: Vector2): Rectangle;
|
|
49
|
+
function fromCenter(center: Vector2, size: Vector2): Rectangle;
|
|
50
|
+
function position(r: Rectangle): Vector2;
|
|
51
|
+
function size(r: Rectangle): Vector2;
|
|
52
|
+
function center(r: Rectangle): Vector2;
|
|
53
|
+
function topLeft(r: Rectangle): Vector2;
|
|
54
|
+
function topRight(r: Rectangle): Vector2;
|
|
55
|
+
function bottomLeft(r: Rectangle): Vector2;
|
|
56
|
+
function bottomRight(r: Rectangle): Vector2;
|
|
57
|
+
function area(r: Rectangle): number;
|
|
58
|
+
function isEmpty(r: Rectangle): boolean;
|
|
59
|
+
function clampPoint(r: Rectangle, point: Vector2): Vector2;
|
|
60
|
+
function containsPoint(r: Rectangle, point: Vector2): boolean;
|
|
61
|
+
function containsRect(r: Rectangle, other: Rectangle): boolean;
|
|
62
|
+
function intersects(r: Rectangle, other: Rectangle): boolean;
|
|
63
|
+
function intersection(r: Rectangle, other: Rectangle): Rectangle;
|
|
64
|
+
function union(r: Rectangle, other: Rectangle): Rectangle;
|
|
65
|
+
function inflate(r: Rectangle, amount: number): Rectangle;
|
|
66
|
+
function offset(r: Rectangle, delta: Vector2): Rectangle;
|
|
67
|
+
function setPosition(r: Rectangle, pos: Vector2): Rectangle;
|
|
68
|
+
function setSize(r: Rectangle, size: Vector2): Rectangle;
|
|
69
|
+
function setCenter(r: Rectangle, center: Vector2): Rectangle;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=rect.d.ts.map
|
|
@@ -0,0 +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;;;;;;;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AAO,CAOlE;IAED,SAAgB,YAAY,CAAC,CAAC,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,GAAG,SAAS,CAWtE;IAED,SAAgB,KAAK,CAAC,CAAC,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,GAAG,SAAS,CAM/D;IAED,SAAgB,OAAO,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,GAAG,SAAS,CAO/D;IAED,SAAgB,MAAM,CAAC,CAAC,EAAE,SAAS,EAAE,KAAK,EAAE,OAAO,GAAG,SAAS,CAE9D;IAED,SAAgB,WAAW,CAAC,CAAC,EAAE,SAAS,EAAE,GAAG,EAAE,OAAO,GAAG,SAAS,CAEjE;IAED,SAAgB,OAAO,CAAC,CAAC,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,GAAG,SAAS,CAE9D;IAED,SAAgB,SAAS,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,GAAG,SAAS,CAOlE;CACF"}
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import { Vec2 } from '../math/vector2';
|
|
2
|
+
/**
|
|
3
|
+
* A set of
|
|
4
|
+
*/
|
|
1
5
|
export var Rect;
|
|
2
6
|
(function (Rect) {
|
|
3
7
|
function fromPoints(a, b) {
|
|
@@ -53,6 +57,10 @@ export var Rect;
|
|
|
53
57
|
return r[2] <= 0 || r[3] <= 0;
|
|
54
58
|
}
|
|
55
59
|
Rect.isEmpty = isEmpty;
|
|
60
|
+
function clampPoint(r, point) {
|
|
61
|
+
return Vec2.clamp(point, Rect.position(r), Rect.bottomRight(r));
|
|
62
|
+
}
|
|
63
|
+
Rect.clampPoint = clampPoint;
|
|
56
64
|
function containsPoint(r, point) {
|
|
57
65
|
return (point[0] >= r[0] &&
|
|
58
66
|
point[0] <= r[0] + r[2] &&
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/** A pair of numbers `[x, y]`
|
|
2
|
+
* representing for example:
|
|
3
|
+
* - position in 2D space
|
|
4
|
+
* - width and height
|
|
5
|
+
* - velocity
|
|
6
|
+
*
|
|
7
|
+
* See {@link Vec2} for full library.
|
|
8
|
+
*
|
|
9
|
+
* ## Examples
|
|
10
|
+
*
|
|
11
|
+
* #### Constructing a Vector2
|
|
12
|
+
* ```ts
|
|
13
|
+
* const onionSize: Vector2 = [width, height];
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* #### Deconstructing a Vector2
|
|
17
|
+
* ```ts
|
|
18
|
+
* const [width, height] = onionSize;
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* #### Making math less repetitive.
|
|
22
|
+
* ```ts
|
|
23
|
+
* x += dx * speed;
|
|
24
|
+
* y += dy * speed;
|
|
25
|
+
* // becomes...
|
|
26
|
+
* pos = Vec2.add(pos, Vec2.mul(delta, speed))
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* #### Summing an array of Vector2
|
|
30
|
+
* ```ts
|
|
31
|
+
* const nums: Vector2[] = [[50, 100], [-5, -5], [0, 99]];
|
|
32
|
+
* const sum = nums.reduce(Vec2.add);
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* #### Using LIKE graphics API
|
|
36
|
+
* ```ts
|
|
37
|
+
* // Draw a circle in the center of the canvas.
|
|
38
|
+
* const pos = Vec2.div(
|
|
39
|
+
* like.canvas.getSize(),
|
|
40
|
+
* 2,
|
|
41
|
+
* )
|
|
42
|
+
* like.graphics.circle('fill', 'blue', pos, 20);
|
|
43
|
+
* ```
|
|
44
|
+
* */
|
|
45
|
+
export type Vector2 = [number, number];
|
|
46
|
+
/** Library of Vec2 functions. See {@link Vector2} for overview. */
|
|
47
|
+
export declare namespace Vec2 {
|
|
48
|
+
function add(a: Vector2, b: Vector2): Vector2;
|
|
49
|
+
function sub(a: Vector2, b: Vector2): Vector2;
|
|
50
|
+
function mul(v: Vector2, other: Vector2 | number): Vector2;
|
|
51
|
+
function div(v: Vector2, other: Vector2 | number): Vector2;
|
|
52
|
+
function mod(v: Vector2, other: Vector2 | number): Vector2;
|
|
53
|
+
function eq(v: Vector2, other: Vector2): boolean;
|
|
54
|
+
function dot(a: Vector2, b: Vector2): number;
|
|
55
|
+
function cross(a: Vector2, b: Vector2): number;
|
|
56
|
+
function lengthSq(v: Vector2): number;
|
|
57
|
+
function length(v: Vector2): number;
|
|
58
|
+
function normalize(v: Vector2): Vector2;
|
|
59
|
+
function distance(a: Vector2, b: Vector2): number;
|
|
60
|
+
function lerp(a: Vector2, b: Vector2, t: number): Vector2;
|
|
61
|
+
function toPolar(v: Vector2): {
|
|
62
|
+
r: number;
|
|
63
|
+
angle: number;
|
|
64
|
+
};
|
|
65
|
+
function fromPolar(r: number, angle: number): Vector2;
|
|
66
|
+
function angle(v: Vector2): number;
|
|
67
|
+
function rotate(v: Vector2, angle: number): Vector2;
|
|
68
|
+
function negate(v: Vector2): Vector2;
|
|
69
|
+
function floor(v: Vector2): Vector2;
|
|
70
|
+
function ceil(v: Vector2): Vector2;
|
|
71
|
+
function round(v: Vector2): Vector2;
|
|
72
|
+
function min(a: Vector2, b: Vector2): Vector2;
|
|
73
|
+
function max(a: Vector2, b: Vector2): Vector2;
|
|
74
|
+
function clamp(v: Vector2, min: Vector2, max: Vector2): Vector2;
|
|
75
|
+
function fromAngle(angle: number, len?: number): Vector2;
|
|
76
|
+
function zero(): Vector2;
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=vector2.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vector2.d.ts","sourceRoot":"","sources":["../../src/math/vector2.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KA2CK;AACL,MAAM,MAAM,OAAO,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAEvC,mEAAmE;AACnE,yBAAiB,IAAI,CAAC;IACpB,SAAgB,GAAG,CAAC,CAAC,EAAE,OAAO,EAA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AE,MAAM,GAAG,OAAO,CAE3D;IAED,SAAgB,KAAK,CAAC,CAAC,EAAE,OAAO,GAAG,MAAM,CAExC;IAED,SAAgB,MAAM,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAIzD;IAED,SAAgB,MAAM,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAE1C;IAED,SAAgB,KAAK,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAEzC;IAED,SAAgB,IAAI,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAExC;IAED,SAAgB,KAAK,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAEzC;IAED,SAAgB,GAAG,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAEnD;IAED,SAAgB,GAAG,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAEnD;IAED,SAAgB,KAAK,CAAC,CAAC,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,GAAG,OAAO,CAKrE;IAED,SAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,GAAE,MAAU,GAAG,OAAO,CAEjE;IAED,SAAgB,IAAI,IAAI,OAAO,CAE9B;CACF"}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { mod as mmod } from ".";
|
|
2
|
+
/** Library of Vec2 functions. See {@link Vector2} for overview. */
|
|
1
3
|
export var Vec2;
|
|
2
4
|
(function (Vec2) {
|
|
3
5
|
function add(a, b) {
|
|
@@ -22,6 +24,17 @@ export var Vec2;
|
|
|
22
24
|
return [v[0] / other[0], v[1] / other[1]];
|
|
23
25
|
}
|
|
24
26
|
Vec2.div = div;
|
|
27
|
+
function mod(v, other) {
|
|
28
|
+
if (typeof other === 'number') {
|
|
29
|
+
return [mmod(v[0], other), mmod(v[1], other)];
|
|
30
|
+
}
|
|
31
|
+
return [mmod(v[0], other[0]), mmod(v[1], other[1])];
|
|
32
|
+
}
|
|
33
|
+
Vec2.mod = mod;
|
|
34
|
+
function eq(v, other) {
|
|
35
|
+
return v[0] == other[0] && v[1] == other[1];
|
|
36
|
+
}
|
|
37
|
+
Vec2.eq = eq;
|
|
25
38
|
function dot(a, b) {
|
|
26
39
|
return a[0] * b[0] + a[1] * b[1];
|
|
27
40
|
}
|
|
@@ -53,6 +66,17 @@ export var Vec2;
|
|
|
53
66
|
return [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t];
|
|
54
67
|
}
|
|
55
68
|
Vec2.lerp = lerp;
|
|
69
|
+
function toPolar(v) {
|
|
70
|
+
return {
|
|
71
|
+
r: Vec2.length(v),
|
|
72
|
+
angle: Vec2.angle(v),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
Vec2.toPolar = toPolar;
|
|
76
|
+
function fromPolar(r, angle) {
|
|
77
|
+
return [r * Math.cos(angle), r * Math.sin(angle)];
|
|
78
|
+
}
|
|
79
|
+
Vec2.fromPolar = fromPolar;
|
|
56
80
|
function angle(v) {
|
|
57
81
|
return Math.atan2(v[1], v[0]);
|
|
58
82
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/prefab-scenes/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type Scene } from '../scene';
|
|
2
|
+
import { Like } from '..';
|
|
3
|
+
/**
|
|
4
|
+
* ## Why
|
|
5
|
+
*
|
|
6
|
+
* 1. Because the LIKE logo looks awesome.
|
|
7
|
+
* 2. Autoplay restriction, doesn't let you play audio until the page is clicked.
|
|
8
|
+
* 3. You have to click on the game in order to send inputs, anyway.
|
|
9
|
+
* 4. It's polite.
|
|
10
|
+
*
|
|
11
|
+
* ## Usage
|
|
12
|
+
*
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { createLike, StartScreen } from 'like';
|
|
15
|
+
* import { GameScene } from './game';
|
|
16
|
+
*
|
|
17
|
+
* const container = document.getElementById("myGame");
|
|
18
|
+
* const like = createLike(container);
|
|
19
|
+
*
|
|
20
|
+
* // these callbacks will be ignored until the scene is clicked
|
|
21
|
+
* like.update = function () { ... }
|
|
22
|
+
* like.draw = function () { ... }
|
|
23
|
+
*
|
|
24
|
+
* // Set up the start screen
|
|
25
|
+
* like.setScene(new StartScreen())
|
|
26
|
+
* like.start();
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* Alternatively, copy-paste this code into your own project and modify it freely.
|
|
30
|
+
* Update imports:
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { type Scene } from 'like/scene';
|
|
34
|
+
* import type { ImageHandle } from 'like/core/graphics';
|
|
35
|
+
* import { Vec2 } from 'like/math/vector2';
|
|
36
|
+
* import { Like } from 'like';
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* ## Custom Rendering
|
|
40
|
+
*
|
|
41
|
+
* Pass a custom draw function to replace the default logo:
|
|
42
|
+
*
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const startup = new StartupScene(gameScene, (like) => {
|
|
45
|
+
* like.gfx.clear([0, 0, 0, 1]);
|
|
46
|
+
* like.gfx.print([1, 1, 1], 'Click to Start', [100, 100]);
|
|
47
|
+
* });
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export declare class StartScreen implements Scene {
|
|
51
|
+
private next;
|
|
52
|
+
private onDraw?;
|
|
53
|
+
private logo;
|
|
54
|
+
constructor(next: Scene, onDraw?: ((like: Like) => void) | undefined);
|
|
55
|
+
load(like: Like): void;
|
|
56
|
+
draw(like: Like): void;
|
|
57
|
+
mousepressed(like: Like): void;
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=startScreen.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"startScreen.d.ts","sourceRoot":"","sources":["../../src/prefab-scenes/startScreen.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,UAAU,CAAC;AAGtC,OAAO,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAI1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,qBAAa,WAAY,YAAW,KAAK;IAIrC,OAAO,CAAC,IAAI;IACZ,OAAO,CAAC,MAAM,CAAC;IAJjB,OAAO,CAAC,IAAI,CAAe;gBAGjB,IAAI,EAAE,KAAK,EACX,MAAM,CAAC,GAAE,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,aAAA;IAGvC,IAAI,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI;IAItB,IAAI,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI;IAWtB,YAAY,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI;CAG/B"}
|
|
@@ -1,13 +1,53 @@
|
|
|
1
|
-
import { Vec2 } from '../
|
|
1
|
+
import { Vec2 } from '../math/vector2';
|
|
2
2
|
const LOGO = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPCEtLSBDcmVhdGVkIHdpdGggSW5rc2NhcGUgKGh0dHA6Ly93d3cuaW5rc2NhcGUub3JnLykgLS0+Cjxzdmcgd2lkdGg9IjMwMG1tIiBoZWlnaHQ9IjEwNW1tIiB2ZXJzaW9uPSIxLjEiIHZpZXdCb3g9IjAgMCAzMDAgMTA1IiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgogPHJlY3QgeD0iMTAiIHk9IjExLjIzIiB3aWR0aD0iMjgwIiBoZWlnaHQ9IjgzLjU0NCIgZmlsbD0iI2U0ODA4MCIgc3Ryb2tlPSIjMDAwIiBzdHJva2UtbGluZWpvaW49InJvdW5kI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Nmb3JtPSJyb3RhdGUoMTM1KSIgY3g9IjIuNjIzOCIgY3k9Ii02OS42MzIiIHI9IjIzLjYxMiIvPgogIDxjaXJjbGUgY3g9IjkxLjA2MiIgY3k9IjcxLjE2MSIgcj0iMjMuNjEyIi8+CiAgPGNpcmNsZSBjeD0iMzcuMDkzIiBjeT0iNzEuMTYxIiByPSIyMy42MTIiLz4KIDwvZz4KPC9zdmc+Cg==';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* ## Why
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
6
|
+
* 1. Because the LIKE logo looks awesome.
|
|
7
|
+
* 2. Autoplay restriction, doesn't let you play audio until the page is clicked.
|
|
8
|
+
* 3. You have to click on the game in order to send inputs, anyway.
|
|
9
|
+
* 4. It's polite.
|
|
10
|
+
*
|
|
11
|
+
* ## Usage
|
|
12
|
+
*
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { createLike, StartScreen } from 'like';
|
|
15
|
+
* import { GameScene } from './game';
|
|
16
|
+
*
|
|
17
|
+
* const container = document.getElementById("myGame");
|
|
18
|
+
* const like = createLike(container);
|
|
19
|
+
*
|
|
20
|
+
* // these callbacks will be ignored until the scene is clicked
|
|
21
|
+
* like.update = function () { ... }
|
|
22
|
+
* like.draw = function () { ... }
|
|
23
|
+
*
|
|
24
|
+
* // Set up the start screen
|
|
25
|
+
* like.setScene(new StartScreen())
|
|
26
|
+
* like.start();
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* Alternatively, copy-paste this code into your own project and modify it freely.
|
|
30
|
+
* Update imports:
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { type Scene } from 'like/scene';
|
|
34
|
+
* import type { ImageHandle } from 'like/core/graphics';
|
|
35
|
+
* import { Vec2 } from 'like/math/vector2';
|
|
36
|
+
* import { Like } from 'like';
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* ## Custom Rendering
|
|
40
|
+
*
|
|
41
|
+
* Pass a custom draw function to replace the default logo:
|
|
42
|
+
*
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const startup = new StartupScene(gameScene, (like) => {
|
|
45
|
+
* like.gfx.clear([0, 0, 0, 1]);
|
|
46
|
+
* like.gfx.print([1, 1, 1], 'Click to Start', [100, 100]);
|
|
47
|
+
* });
|
|
48
|
+
* ```
|
|
9
49
|
*/
|
|
10
|
-
export class
|
|
50
|
+
export class StartScreen {
|
|
11
51
|
constructor(next, onDraw) {
|
|
12
52
|
Object.defineProperty(this, "next", {
|
|
13
53
|
enumerable: true,
|
|
@@ -37,7 +77,7 @@ export class StartupScene {
|
|
|
37
77
|
}
|
|
38
78
|
else if (this.logo.isReady()) {
|
|
39
79
|
like.gfx.clear([0.05, 0.05, 0.08, 1]);
|
|
40
|
-
const winSize = like.
|
|
80
|
+
const winSize = like.canvas.getSize();
|
|
41
81
|
const scale = (winSize[0] * 0.5) / this.logo.size[0];
|
|
42
82
|
like.gfx.draw(this.logo, Vec2.div(winSize, 2), { scale, origin: Vec2.div(this.logo.size, 2) });
|
|
43
83
|
}
|
package/dist/scene.d.ts
CHANGED
|
@@ -1,10 +1,108 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @module scene
|
|
3
|
+
* @description A helpful callback / state management layer, plus utility scenes.
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
import type { LikeEvent, EventMap } from './core/events';
|
|
2
7
|
import type { Like } from './core/like';
|
|
3
|
-
|
|
8
|
+
/**
|
|
9
|
+
* An interface for creating scenes.
|
|
10
|
+
*
|
|
11
|
+
* ## Why Scenes?
|
|
12
|
+
*
|
|
13
|
+
* For any game with more than one scene, we have to either:
|
|
14
|
+
* - switch-case on game state in every single callback
|
|
15
|
+
* - rebind all of the callbacks ourselves
|
|
16
|
+
* - wrap handleEvent (hint: that's what this does)
|
|
17
|
+
*
|
|
18
|
+
* Also, some no need to pass around a `like` object.
|
|
19
|
+
* Here, `like` instead piggybacks on a closure that follows around
|
|
20
|
+
* your running scene and shows up as an additional first argument
|
|
21
|
+
* to every callback.
|
|
22
|
+
*
|
|
23
|
+
* ## Quick Start
|
|
24
|
+
*
|
|
25
|
+
* Have a scene handle all the callbacks, disabling global
|
|
26
|
+
* callbacks.
|
|
27
|
+
* ```typescript
|
|
28
|
+
* // set up a scene
|
|
29
|
+
* class MagicalGrowingRectangle extends Scene {
|
|
30
|
+
* rectangleSize = 10;
|
|
31
|
+
* constructor() {}
|
|
32
|
+
*
|
|
33
|
+
* keypressed(_like: Like) {
|
|
34
|
+
* this.rectangleSize += 10;
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* draw(like: Like) {
|
|
38
|
+
* like.gfx.rectangle('fill', 'green',
|
|
39
|
+
* [10, 10, this.rectangleSize, this.rectangleSize])
|
|
40
|
+
* }
|
|
41
|
+
* }
|
|
42
|
+
*
|
|
43
|
+
* like.setScene(new MagicalGrowingRectangle());
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* To get back to global callbacks, just use `like.handleEvent = undefined`
|
|
47
|
+
*
|
|
48
|
+
* ## Scene Lifecycle
|
|
49
|
+
*
|
|
50
|
+
* Works a lot like global callbacks.
|
|
51
|
+
*
|
|
52
|
+
* 1. `like.setScene(scene)` is called
|
|
53
|
+
* 2. Scene's `load` callback fires immediately
|
|
54
|
+
* 3. `update` and `draw` begin on next frame
|
|
55
|
+
* 4. Scene receives input events as they occur
|
|
56
|
+
*
|
|
57
|
+
* ## Composing scenes
|
|
58
|
+
*
|
|
59
|
+
* Thought you'd never ask.
|
|
60
|
+
* Just like the `like` object, scenes have handleEvent on them.
|
|
61
|
+
* So, you could layer them like this, for example:
|
|
62
|
+
*
|
|
63
|
+
* ```typescript
|
|
64
|
+
* class UI implements Scene {
|
|
65
|
+
* constructor(public game: Game) {}
|
|
66
|
+
*
|
|
67
|
+
* handleEvent(like: Like, event: Like2DEvent) {
|
|
68
|
+
* // Block mouse events in order to create a top bar.
|
|
69
|
+
* const mouseY = like.mouse.getPosition()[1];
|
|
70
|
+
* if (!event.type.startsWith('mouse') || mouseY > 100) {
|
|
71
|
+
* sceneDispatch(this.game, like, event);
|
|
72
|
+
* }
|
|
73
|
+
*
|
|
74
|
+
* // Then, call my own callbacks.
|
|
75
|
+
* // By calling it here, the UI draws on top.
|
|
76
|
+
* callSceneHandlers(this, like, event);
|
|
77
|
+
* }
|
|
78
|
+
* ...
|
|
79
|
+
* }
|
|
80
|
+
*
|
|
81
|
+
* class Game implements Scene {
|
|
82
|
+
* ...
|
|
83
|
+
* }
|
|
84
|
+
*
|
|
85
|
+
* like.setScene(new UI(new Game()))
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* Composing scenes lets you filter events, layer game elements,
|
|
89
|
+
* and more. Don't sleep on it.
|
|
90
|
+
*/
|
|
4
91
|
export type Scene = {
|
|
5
|
-
[K in
|
|
92
|
+
[K in keyof EventMap]?: (like: Like, ...args: EventMap[K]) => void;
|
|
6
93
|
} & {
|
|
7
|
-
handleEvent?(like: Like, event:
|
|
94
|
+
handleEvent?(like: Like, event: LikeEvent): void;
|
|
8
95
|
};
|
|
9
|
-
|
|
96
|
+
/**
|
|
97
|
+
* Used to call a scene's own handlers like `update` or `draw`,
|
|
98
|
+
* typically at the end of handleEvent
|
|
99
|
+
* after modifying the event stream or composing sub-scenes.
|
|
100
|
+
*/
|
|
101
|
+
export declare const callSceneHandlers: (scene: Scene, like: Like, event: LikeEvent) => void;
|
|
102
|
+
/**
|
|
103
|
+
* Used to call sub scenes while respecting potential `handleEvent` within them.
|
|
104
|
+
* The main scene is similar to a sub-scene of the root (like) object in this
|
|
105
|
+
* regard.
|
|
106
|
+
*/
|
|
107
|
+
export declare const sceneDispatch: (scene: Scene, like: Like, event: LikeEvent) => void;
|
|
10
108
|
//# sourceMappingURL=scene.d.ts.map
|
package/dist/scene.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scene.d.ts","sourceRoot":"","sources":["../src/scene.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"scene.d.ts","sourceRoot":"","sources":["../src/scene.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAExC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkFG;AAEH,MAAM,MAAM,KAAK,GAAG;KACjB,CAAC,IAAI,MAAM,QAAQ,CAAC,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,GAAG,IAAI,EAAE,QAAQ,CAAC,CAAC,CAAC,KAAK,IAAI;CACnE,GAAG;IACF,WAAW,CAAC,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,GAAG,IAAI,CAAC;CAClD,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,GAAI,OAAO,KAAK,EAAE,MAAM,IAAI,EAAE,OAAO,SAAS,SAI3E,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,aAAa,GAAI,OAAO,KAAK,EAAE,MAAM,IAAI,EAAE,OAAO,SAAS,SAMvE,CAAA"}
|