@hi-ashleyj/llama 0.1.0 → 0.2.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/Drawables.d.ts +1 -0
- package/Drawables.js +1 -0
- package/README.md +6 -0
- package/components/Drawables/Arc.svelte +23 -0
- package/components/Drawables/Arc.svelte.d.ts +19 -0
- package/components/Drawables/Circle.svelte +19 -0
- package/components/Drawables/Circle.svelte.d.ts +17 -0
- package/components/Drawables/Rectangle.svelte +17 -0
- package/components/Drawables/Rectangle.svelte.d.ts +17 -0
- package/components/Drawables/index.d.ts +3 -0
- package/components/Drawables/index.js +3 -0
- package/components/Game.svelte +75 -0
- package/components/Game.svelte.d.ts +27 -0
- package/components/GameObject.svelte +24 -0
- package/components/GameObject.svelte.d.ts +25 -0
- package/components/Layer.svelte +33 -0
- package/components/Layer.svelte.d.ts +18 -0
- package/components/motions.d.ts +25 -0
- package/components/motions.js +39 -0
- package/index.d.ts +4 -0
- package/index.js +4 -0
- package/package.json +26 -15
- package/setup.d.ts +9 -0
- package/setup.js +42 -0
- package/types/contexts.d.ts +22 -0
- package/types/index.d.ts +1 -0
- package/.parcel-cache/793d22ee560c2618 +0 -0
- package/.parcel-cache/b6694c1eefd2b32c +0 -0
- package/.parcel-cache/c0e87891cd14f32b.txt +0 -4517
- package/.parcel-cache/cbf39635cd1b2434 +0 -0
- package/.parcel-cache/data.mdb +0 -0
- package/.parcel-cache/ffdc629ccf572e1f +0 -0
- package/.parcel-cache/lock.mdb +0 -0
- package/dist/main.d.ts +0 -206
- package/dist/main.d.ts.map +0 -1
- package/dist/main.js +0 -543
- package/dist/main.js.map +0 -1
package/Drawables.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./components/Drawables/index";
|
package/Drawables.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./components/Drawables/index";
|
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
<script>import { setupDrawable } from "../../setup";
|
|
2
|
+
export let fill = null;
|
|
3
|
+
export let stroke = null;
|
|
4
|
+
export let startAngle;
|
|
5
|
+
export let endAngle;
|
|
6
|
+
$: sd = (startAngle - 90) * Math.PI / 180;
|
|
7
|
+
$: ed = (endAngle - 90) * Math.PI / 180;
|
|
8
|
+
const draw = function ({ ctx }, { x, y, w, h }) {
|
|
9
|
+
let r = (w + h) / 4;
|
|
10
|
+
ctx.beginPath();
|
|
11
|
+
ctx.arc(x + r / 2, y + r / 2, r, sd, ed);
|
|
12
|
+
ctx.lineTo(x + r / 2, y + r / 2);
|
|
13
|
+
if (fill) {
|
|
14
|
+
ctx.fillStyle = fill;
|
|
15
|
+
ctx.fill();
|
|
16
|
+
}
|
|
17
|
+
if (stroke) {
|
|
18
|
+
ctx.strokeStyle = stroke;
|
|
19
|
+
ctx.stroke();
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
setupDrawable({ draw });
|
|
23
|
+
</script>
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { SvelteComponentTyped } from "svelte";
|
|
2
|
+
declare const __propDef: {
|
|
3
|
+
props: {
|
|
4
|
+
fill?: string | null;
|
|
5
|
+
stroke?: string | null;
|
|
6
|
+
startAngle: number;
|
|
7
|
+
endAngle: number;
|
|
8
|
+
};
|
|
9
|
+
events: {
|
|
10
|
+
[evt: string]: CustomEvent<any>;
|
|
11
|
+
};
|
|
12
|
+
slots: {};
|
|
13
|
+
};
|
|
14
|
+
export declare type ArcProps = typeof __propDef.props;
|
|
15
|
+
export declare type ArcEvents = typeof __propDef.events;
|
|
16
|
+
export declare type ArcSlots = typeof __propDef.slots;
|
|
17
|
+
export default class Arc extends SvelteComponentTyped<ArcProps, ArcEvents, ArcSlots> {
|
|
18
|
+
}
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
<script>import { setupDrawable } from "../../setup";
|
|
2
|
+
export let fill = null;
|
|
3
|
+
export let stroke = null;
|
|
4
|
+
const TWO_PI = Math.PI * 2;
|
|
5
|
+
const draw = function ({ ctx }, { x, y, w, h }) {
|
|
6
|
+
let r = (w + h) / 4;
|
|
7
|
+
ctx.beginPath();
|
|
8
|
+
ctx.arc(x + r / 2, y + r / 2, r, 0, TWO_PI);
|
|
9
|
+
if (fill) {
|
|
10
|
+
ctx.fillStyle = fill;
|
|
11
|
+
ctx.fill();
|
|
12
|
+
}
|
|
13
|
+
if (stroke) {
|
|
14
|
+
ctx.strokeStyle = stroke;
|
|
15
|
+
ctx.stroke();
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
setupDrawable({ draw });
|
|
19
|
+
</script>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { SvelteComponentTyped } from "svelte";
|
|
2
|
+
declare const __propDef: {
|
|
3
|
+
props: {
|
|
4
|
+
fill?: string | null;
|
|
5
|
+
stroke?: string | null;
|
|
6
|
+
};
|
|
7
|
+
events: {
|
|
8
|
+
[evt: string]: CustomEvent<any>;
|
|
9
|
+
};
|
|
10
|
+
slots: {};
|
|
11
|
+
};
|
|
12
|
+
export declare type CircleProps = typeof __propDef.props;
|
|
13
|
+
export declare type CircleEvents = typeof __propDef.events;
|
|
14
|
+
export declare type CircleSlots = typeof __propDef.slots;
|
|
15
|
+
export default class Circle extends SvelteComponentTyped<CircleProps, CircleEvents, CircleSlots> {
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<script>import { setupDrawable } from "../../setup";
|
|
2
|
+
export let fill = null;
|
|
3
|
+
export let stroke = null;
|
|
4
|
+
const draw = function ({ ctx }, { x, y, w, h }) {
|
|
5
|
+
ctx.beginPath();
|
|
6
|
+
ctx.rect(x, y, w, h);
|
|
7
|
+
if (fill) {
|
|
8
|
+
ctx.fillStyle = fill;
|
|
9
|
+
ctx.fill();
|
|
10
|
+
}
|
|
11
|
+
if (stroke) {
|
|
12
|
+
ctx.strokeStyle = stroke;
|
|
13
|
+
ctx.stroke();
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
setupDrawable({ draw });
|
|
17
|
+
</script>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { SvelteComponentTyped } from "svelte";
|
|
2
|
+
declare const __propDef: {
|
|
3
|
+
props: {
|
|
4
|
+
fill?: string | null;
|
|
5
|
+
stroke?: string | null;
|
|
6
|
+
};
|
|
7
|
+
events: {
|
|
8
|
+
[evt: string]: CustomEvent<any>;
|
|
9
|
+
};
|
|
10
|
+
slots: {};
|
|
11
|
+
};
|
|
12
|
+
export declare type RectangleProps = typeof __propDef.props;
|
|
13
|
+
export declare type RectangleEvents = typeof __propDef.events;
|
|
14
|
+
export declare type RectangleSlots = typeof __propDef.slots;
|
|
15
|
+
export default class Rectangle extends SvelteComponentTyped<RectangleProps, RectangleEvents, RectangleSlots> {
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
<script>import { writable } from "svelte/store";
|
|
2
|
+
import { onMount, createEventDispatcher } from "svelte";
|
|
3
|
+
import { setupGame } from "../setup";
|
|
4
|
+
import { Timing } from "./motions";
|
|
5
|
+
const dispatch = createEventDispatcher();
|
|
6
|
+
export let width = 1920;
|
|
7
|
+
export let height = 1080;
|
|
8
|
+
export let background = "#000000";
|
|
9
|
+
const widthStore = writable(1920);
|
|
10
|
+
const heightStore = writable(1080);
|
|
11
|
+
const backgroundStore = writable("#000000");
|
|
12
|
+
$: {
|
|
13
|
+
$widthStore = width;
|
|
14
|
+
}
|
|
15
|
+
$: {
|
|
16
|
+
$heightStore = height;
|
|
17
|
+
}
|
|
18
|
+
$: {
|
|
19
|
+
$backgroundStore = background;
|
|
20
|
+
}
|
|
21
|
+
const layers = new Set();
|
|
22
|
+
const draw = function (delta, time) {
|
|
23
|
+
for (let layer of layers) {
|
|
24
|
+
layer(delta, time);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
const assign = function (layerDraw) {
|
|
28
|
+
layers.add(layerDraw);
|
|
29
|
+
return () => layers.delete(layerDraw);
|
|
30
|
+
};
|
|
31
|
+
const timing = new Timing();
|
|
32
|
+
export const context = {
|
|
33
|
+
width: widthStore,
|
|
34
|
+
height: heightStore,
|
|
35
|
+
background: backgroundStore,
|
|
36
|
+
assign,
|
|
37
|
+
createTimer: timing.create.bind(timing)
|
|
38
|
+
};
|
|
39
|
+
setupGame(context);
|
|
40
|
+
let last = -1;
|
|
41
|
+
const loop = function (time) {
|
|
42
|
+
if (last < 0)
|
|
43
|
+
last = time;
|
|
44
|
+
let delta = (time - last);
|
|
45
|
+
if (delta > 1000) {
|
|
46
|
+
requestAnimationFrame(loop);
|
|
47
|
+
return last = time;
|
|
48
|
+
}
|
|
49
|
+
// TODO: NEW TIMEOUT LOGIC
|
|
50
|
+
dispatch("beforeframe", { delta, time });
|
|
51
|
+
timing.update(delta);
|
|
52
|
+
dispatch("frame", { delta, time });
|
|
53
|
+
draw(delta, time);
|
|
54
|
+
dispatch("afterframe", { delta, time });
|
|
55
|
+
requestAnimationFrame(loop);
|
|
56
|
+
last = time;
|
|
57
|
+
};
|
|
58
|
+
onMount(() => {
|
|
59
|
+
requestAnimationFrame(loop);
|
|
60
|
+
});
|
|
61
|
+
</script>
|
|
62
|
+
|
|
63
|
+
<div class="game" style:background-color={background}>
|
|
64
|
+
<slot />
|
|
65
|
+
</div>
|
|
66
|
+
|
|
67
|
+
<style>
|
|
68
|
+
.game {
|
|
69
|
+
position: relative;
|
|
70
|
+
width: 100%;
|
|
71
|
+
height: 100%;
|
|
72
|
+
padding: 0;
|
|
73
|
+
margin: 0;
|
|
74
|
+
}
|
|
75
|
+
</style>
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { SvelteComponentTyped } from "svelte";
|
|
2
|
+
import type { GameContext } from "../types/contexts";
|
|
3
|
+
declare const __propDef: {
|
|
4
|
+
props: {
|
|
5
|
+
width?: number;
|
|
6
|
+
height?: number;
|
|
7
|
+
background?: string;
|
|
8
|
+
context?: GameContext;
|
|
9
|
+
};
|
|
10
|
+
events: {
|
|
11
|
+
beforeframe: CustomEvent<any>;
|
|
12
|
+
frame: CustomEvent<any>;
|
|
13
|
+
afterframe: CustomEvent<any>;
|
|
14
|
+
} & {
|
|
15
|
+
[evt: string]: CustomEvent<any>;
|
|
16
|
+
};
|
|
17
|
+
slots: {
|
|
18
|
+
default: {};
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
export declare type GameProps = typeof __propDef.props;
|
|
22
|
+
export declare type GameEvents = typeof __propDef.events;
|
|
23
|
+
export declare type GameSlots = typeof __propDef.slots;
|
|
24
|
+
export default class Game extends SvelteComponentTyped<GameProps, GameEvents, GameSlots> {
|
|
25
|
+
get context(): GameContext;
|
|
26
|
+
}
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<script>import { setupDrawable } from "../setup";
|
|
2
|
+
export let x = 0;
|
|
3
|
+
export let y = 0;
|
|
4
|
+
export let w = 0;
|
|
5
|
+
export let h = 0;
|
|
6
|
+
/**
|
|
7
|
+
* FALSE = ANCHOR TOP LEFT
|
|
8
|
+
* TRUE = ANCHOR CENTER
|
|
9
|
+
*/
|
|
10
|
+
export let centered = false;
|
|
11
|
+
$: ax = (centered) ? x - (w / 2) : x;
|
|
12
|
+
$: ay = (centered) ? y - (h / 2) : y;
|
|
13
|
+
const targets = new Set();
|
|
14
|
+
const assign = function (callable) {
|
|
15
|
+
targets.add(callable);
|
|
16
|
+
return () => { targets.delete(callable); };
|
|
17
|
+
};
|
|
18
|
+
const draw = function ({ width, height, ctx }) {
|
|
19
|
+
targets.forEach(f => f({ width, height, ctx }, { x: ax, y: ay, w, h }));
|
|
20
|
+
};
|
|
21
|
+
setupDrawable({ assign, draw });
|
|
22
|
+
</script>
|
|
23
|
+
|
|
24
|
+
<slot />
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { SvelteComponentTyped } from "svelte";
|
|
2
|
+
declare const __propDef: {
|
|
3
|
+
props: {
|
|
4
|
+
x?: number;
|
|
5
|
+
y?: number;
|
|
6
|
+
w?: number;
|
|
7
|
+
h?: number;
|
|
8
|
+
/**
|
|
9
|
+
* FALSE = ANCHOR TOP LEFT
|
|
10
|
+
* TRUE = ANCHOR CENTER
|
|
11
|
+
*/ centered?: boolean;
|
|
12
|
+
};
|
|
13
|
+
events: {
|
|
14
|
+
[evt: string]: CustomEvent<any>;
|
|
15
|
+
};
|
|
16
|
+
slots: {
|
|
17
|
+
default: {};
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
export declare type GameObjectProps = typeof __propDef.props;
|
|
21
|
+
export declare type GameObjectEvents = typeof __propDef.events;
|
|
22
|
+
export declare type GameObjectSlots = typeof __propDef.slots;
|
|
23
|
+
export default class GameObject extends SvelteComponentTyped<GameObjectProps, GameObjectEvents, GameObjectSlots> {
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
<script>import { setupLayer } from "../setup";
|
|
2
|
+
export let zIndex = 0;
|
|
3
|
+
let canvas;
|
|
4
|
+
$: ctx = (typeof canvas !== "undefined") ? canvas.getContext("2d") : null;
|
|
5
|
+
let targets = new Set();
|
|
6
|
+
let assign = function (callable) {
|
|
7
|
+
targets.add(callable);
|
|
8
|
+
return () => { targets.delete(callable); };
|
|
9
|
+
};
|
|
10
|
+
let draw = function () {
|
|
11
|
+
if (ctx === null)
|
|
12
|
+
return;
|
|
13
|
+
ctx.clearRect(0, 0, $width, $height);
|
|
14
|
+
targets.forEach(f => f({ width: $width, height: $height, ctx }));
|
|
15
|
+
};
|
|
16
|
+
let { width, height } = setupLayer({
|
|
17
|
+
assign, draw
|
|
18
|
+
}).game;
|
|
19
|
+
</script>
|
|
20
|
+
|
|
21
|
+
<canvas width={$width} height={$height} bind:this={canvas} style:z-index={zIndex}></canvas>
|
|
22
|
+
<slot />
|
|
23
|
+
|
|
24
|
+
<style>
|
|
25
|
+
canvas {
|
|
26
|
+
position: absolute;
|
|
27
|
+
top: 0;
|
|
28
|
+
left: 0;
|
|
29
|
+
width: 100%;
|
|
30
|
+
height: 100%;
|
|
31
|
+
object-fit: contain;
|
|
32
|
+
}
|
|
33
|
+
</style>
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { SvelteComponentTyped } from "svelte";
|
|
2
|
+
declare const __propDef: {
|
|
3
|
+
props: {
|
|
4
|
+
zIndex?: number;
|
|
5
|
+
};
|
|
6
|
+
events: {
|
|
7
|
+
[evt: string]: CustomEvent<any>;
|
|
8
|
+
};
|
|
9
|
+
slots: {
|
|
10
|
+
default: {};
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export declare type LayerProps = typeof __propDef.props;
|
|
14
|
+
export declare type LayerEvents = typeof __propDef.events;
|
|
15
|
+
export declare type LayerSlots = typeof __propDef.slots;
|
|
16
|
+
export default class Layer extends SvelteComponentTyped<LayerProps, LayerEvents, LayerSlots> {
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Writable } from "svelte/store";
|
|
2
|
+
export declare class Timing {
|
|
3
|
+
constructor();
|
|
4
|
+
targets: Set<{
|
|
5
|
+
current: number;
|
|
6
|
+
duration: number;
|
|
7
|
+
repeats: number;
|
|
8
|
+
store: Writable<number | null>;
|
|
9
|
+
}>;
|
|
10
|
+
/**
|
|
11
|
+
* Used to create a store whose value changes based on the total delta time.
|
|
12
|
+
* Duration is in MS
|
|
13
|
+
* Use 0 for repeats for infinite
|
|
14
|
+
*/
|
|
15
|
+
create({ duration, repeats }: {
|
|
16
|
+
duration: number;
|
|
17
|
+
repeats: number;
|
|
18
|
+
}): {
|
|
19
|
+
stop: () => boolean;
|
|
20
|
+
set(this: void, value: number): void;
|
|
21
|
+
update(this: void, updater: import("svelte/store").Updater<number>): void;
|
|
22
|
+
subscribe(this: void, run: import("svelte/store").Subscriber<number>, invalidate?: (value?: number) => void): import("svelte/store").Unsubscriber;
|
|
23
|
+
};
|
|
24
|
+
update(delta: number): void;
|
|
25
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
export class Timing {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.targets = new Set();
|
|
5
|
+
}
|
|
6
|
+
targets;
|
|
7
|
+
/**
|
|
8
|
+
* Used to create a store whose value changes based on the total delta time.
|
|
9
|
+
* Duration is in MS
|
|
10
|
+
* Use 0 for repeats for infinite
|
|
11
|
+
*/
|
|
12
|
+
create({ duration, repeats }) {
|
|
13
|
+
let store = { ...writable(0), stop: () => this.targets.delete(out) };
|
|
14
|
+
let out = {
|
|
15
|
+
current: 0,
|
|
16
|
+
duration,
|
|
17
|
+
repeats,
|
|
18
|
+
store
|
|
19
|
+
};
|
|
20
|
+
this.targets.add(out);
|
|
21
|
+
return store;
|
|
22
|
+
}
|
|
23
|
+
update(delta) {
|
|
24
|
+
this.targets.forEach((t, ignored, set) => {
|
|
25
|
+
if (t.current < 0) {
|
|
26
|
+
t.store.set(null);
|
|
27
|
+
set.delete(t);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
t.current += delta;
|
|
31
|
+
if (t.repeats > 0 && t.current > t.duration * t.repeats) {
|
|
32
|
+
t.current = -1;
|
|
33
|
+
t.store.set(1);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
t.store.set((t.current / t.duration) % 1);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
package/index.d.ts
ADDED
package/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,15 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hi-ashleyj/llama",
|
|
3
|
-
"description": "A (
|
|
4
|
-
"version": "0.1
|
|
3
|
+
"description": "A (Very Incomplete) 2D Canvas Game Engine powered by Svelte",
|
|
4
|
+
"version": "0.2.1",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"source": "src/main.ts",
|
|
7
|
-
"main": "dist/main.js",
|
|
8
|
-
"types": "dist/main.d.ts",
|
|
9
|
-
"scripts": {
|
|
10
|
-
"build": "parcel build",
|
|
11
|
-
"postversion": "npm run build"
|
|
12
|
-
},
|
|
13
6
|
"repository": {
|
|
14
7
|
"type": "git",
|
|
15
8
|
"url": "git+https://github.com/hi-ashleyj/llama-game-engine.git"
|
|
@@ -21,9 +14,27 @@
|
|
|
21
14
|
},
|
|
22
15
|
"homepage": "https://github.com/hi-ashleyj/llama-game-engine#readme",
|
|
23
16
|
"devDependencies": {
|
|
24
|
-
"@
|
|
25
|
-
"@
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
17
|
+
"@sveltejs/adapter-static": "next",
|
|
18
|
+
"@sveltejs/kit": "next",
|
|
19
|
+
"svelte": "^3.44.0",
|
|
20
|
+
"svelte-preprocess": "^4.10.7",
|
|
21
|
+
"svelte2tsx": "^0.5.12",
|
|
22
|
+
"typescript": "^4.7.4",
|
|
23
|
+
"vite": "^3.0.0"
|
|
24
|
+
},
|
|
25
|
+
"exports": {
|
|
26
|
+
"./package.json": "./package.json",
|
|
27
|
+
"./Drawables": "./Drawables.js",
|
|
28
|
+
"./components/Drawables/Arc.svelte": "./components/Drawables/Arc.svelte",
|
|
29
|
+
"./components/Drawables/Circle.svelte": "./components/Drawables/Circle.svelte",
|
|
30
|
+
"./components/Drawables/Rectangle.svelte": "./components/Drawables/Rectangle.svelte",
|
|
31
|
+
"./components/Drawables": "./components/Drawables/index.js",
|
|
32
|
+
"./components/Game.svelte": "./components/Game.svelte",
|
|
33
|
+
"./components/GameObject.svelte": "./components/GameObject.svelte",
|
|
34
|
+
"./components/Layer.svelte": "./components/Layer.svelte",
|
|
35
|
+
"./components/motions": "./components/motions.js",
|
|
36
|
+
".": "./index.js",
|
|
37
|
+
"./setup": "./setup.js"
|
|
38
|
+
},
|
|
39
|
+
"svelte": "./index.js"
|
|
40
|
+
}
|
package/setup.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { GameContext, LayerContext, DrawableContext } from "./types/contexts";
|
|
2
|
+
export declare const setupGame: (context: GameContext) => void;
|
|
3
|
+
export declare const setupLayer: (context: LayerContext) => {
|
|
4
|
+
game: GameContext;
|
|
5
|
+
};
|
|
6
|
+
export declare const setupDrawable: ({ assign, draw }: Partial<DrawableContext>) => {
|
|
7
|
+
game: GameContext;
|
|
8
|
+
};
|
|
9
|
+
export declare const getGame: () => GameContext;
|
package/setup.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { getContext, setContext, onDestroy } from 'svelte';
|
|
2
|
+
const GAME = Symbol();
|
|
3
|
+
const LAYER = Symbol();
|
|
4
|
+
const DRAWABLE = Symbol();
|
|
5
|
+
export const setupGame = function (context) {
|
|
6
|
+
if (getContext(GAME)) {
|
|
7
|
+
throw new Error("Cannot Mount Game inside a Game");
|
|
8
|
+
}
|
|
9
|
+
setContext(GAME, context);
|
|
10
|
+
};
|
|
11
|
+
export const setupLayer = function (context) {
|
|
12
|
+
if (getContext(LAYER)) {
|
|
13
|
+
throw new Error("Cannot Mount Layer inside a Layer");
|
|
14
|
+
}
|
|
15
|
+
let game = getContext(GAME);
|
|
16
|
+
if (!game)
|
|
17
|
+
throw new Error("Layers must be inside a Game");
|
|
18
|
+
let remove = game.assign(context.draw);
|
|
19
|
+
onDestroy(() => {
|
|
20
|
+
remove();
|
|
21
|
+
});
|
|
22
|
+
setContext(LAYER, context);
|
|
23
|
+
return setupDrawable({ assign: context.assign, draw: context.draw });
|
|
24
|
+
};
|
|
25
|
+
export const setupDrawable = function ({ assign, draw }) {
|
|
26
|
+
let parent = getContext(DRAWABLE);
|
|
27
|
+
if (parent && draw) {
|
|
28
|
+
let remove = parent.assign(draw);
|
|
29
|
+
onDestroy(() => {
|
|
30
|
+
remove();
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
if (assign) {
|
|
34
|
+
setContext(DRAWABLE, { assign });
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
game: getContext(GAME)
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
export const getGame = function () {
|
|
41
|
+
return getContext(GAME);
|
|
42
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Writable } from 'svelte/store';
|
|
2
|
+
import { Timing } from "../components/motions";
|
|
3
|
+
|
|
4
|
+
export type GameContext = {
|
|
5
|
+
assign: (draw: Function) => () => any,
|
|
6
|
+
width: Writable<number>,
|
|
7
|
+
height: Writable<number>,
|
|
8
|
+
background: Writable<string>,
|
|
9
|
+
createTimer: Timing["create"]
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type LayerContext = {
|
|
13
|
+
assign: (draw: Function) => () => any,
|
|
14
|
+
draw: () => any
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type DrawableFunction = ({}: { width: number, height: number, ctx: CanvasRenderingContext2D }, ...more: any[] ) => any;
|
|
18
|
+
|
|
19
|
+
export type DrawableContext = {
|
|
20
|
+
assign: (draw: Function) => () => any,
|
|
21
|
+
draw: DrawableFunction
|
|
22
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./contexts";
|
|
Binary file
|
|
Binary file
|