@hi-ashleyj/llama 0.3.1 → 0.3.2

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.
@@ -1,4 +1,5 @@
1
1
  <script>import { setupDrawable } from "../setup";
2
+ import { onMount } from "svelte";
2
3
  export let x = 0;
3
4
  export let y = 0;
4
5
  export let w = 0;
@@ -18,7 +19,10 @@ const assign = function (callable) {
18
19
  const draw = function ({ width, height, ctx }) {
19
20
  targets.forEach(f => f({ width, height, ctx }, { x: ax, y: ay, w, h }));
20
21
  };
21
- setupDrawable({ assign, draw });
22
+ let register = setupDrawable({ assign });
23
+ onMount(() => {
24
+ return register(draw);
25
+ });
22
26
  </script>
23
27
 
24
28
  <slot />
@@ -1,20 +1,25 @@
1
- <script>import { setupLayer } from "../setup";
1
+ <script>import { setupLayer, getGame } from "../setup";
2
+ import { onMount } from "svelte";
2
3
  export let zIndex = 0;
3
4
  let canvas;
4
5
  $: ctx = (typeof canvas !== "undefined") ? canvas.getContext("2d") : null;
5
6
  let targets = new Set();
6
- let { width, height } = setupLayer({
7
+ const draw = () => {
8
+ if (ctx === null)
9
+ return;
10
+ ctx.clearRect(0, 0, $width, $height);
11
+ targets.forEach(f => f({ width: $width, height: $height, ctx }));
12
+ };
13
+ let { width, height } = getGame();
14
+ let register = setupLayer({
7
15
  assign: (callable) => {
8
16
  targets.add(callable);
9
17
  return () => { targets.delete(callable); };
10
- },
11
- draw: () => {
12
- if (ctx === null)
13
- return;
14
- ctx.clearRect(0, 0, $width, $height);
15
- targets.forEach(f => f({ width: $width, height: $height, ctx }));
16
18
  }
17
- }).game;
19
+ });
20
+ onMount(() => {
21
+ return register(draw);
22
+ });
18
23
  </script>
19
24
 
20
25
  <canvas width={$width} height={$height} bind:this={canvas} style:z-index={zIndex}></canvas>
@@ -1,4 +1,5 @@
1
1
  <script>import { setupDrawable } from "../../setup";
2
+ import { onMount } from "svelte";
2
3
  export let fill = null;
3
4
  export let stroke = null;
4
5
  export let startAngle;
@@ -19,5 +20,8 @@ const draw = function ({ ctx }, { x, y, w, h }) {
19
20
  ctx.stroke();
20
21
  }
21
22
  };
22
- setupDrawable({ draw });
23
+ let register = setupDrawable({});
24
+ onMount(() => {
25
+ return register(draw);
26
+ });
23
27
  </script>
@@ -1,4 +1,5 @@
1
1
  <script>import { setupDrawable } from "../../setup";
2
+ import { onMount } from "svelte";
2
3
  export let fill = null;
3
4
  export let stroke = null;
4
5
  const TWO_PI = Math.PI * 2;
@@ -15,5 +16,8 @@ const draw = function ({ ctx }, { x, y, w, h }) {
15
16
  ctx.stroke();
16
17
  }
17
18
  };
18
- setupDrawable({ draw });
19
+ let register = setupDrawable({});
20
+ onMount(() => {
21
+ return register(draw);
22
+ });
19
23
  </script>
@@ -1,4 +1,5 @@
1
1
  <script>import { setupDrawable } from "../../setup";
2
+ import { onMount } from "svelte";
2
3
  export let image = null;
3
4
  export let crop = null;
4
5
  const draw = function ({ ctx }, { x, y, w, h }) {
@@ -9,5 +10,8 @@ const draw = function ({ ctx }, { x, y, w, h }) {
9
10
  }
10
11
  ctx.drawImage(image, x, y, w, h);
11
12
  };
12
- setupDrawable({ draw });
13
+ let register = setupDrawable({});
14
+ onMount(() => {
15
+ return register(draw);
16
+ });
13
17
  </script>
@@ -1,4 +1,5 @@
1
1
  <script>import { setupDrawable } from "../../setup";
2
+ import { onMount } from "svelte";
2
3
  export let fill = null;
3
4
  export let stroke = null;
4
5
  const draw = function ({ ctx }, { x, y, w, h }) {
@@ -13,5 +14,8 @@ const draw = function ({ ctx }, { x, y, w, h }) {
13
14
  ctx.stroke();
14
15
  }
15
16
  };
16
- setupDrawable({ draw });
17
+ let register = setupDrawable({});
18
+ onMount(() => {
19
+ return register(draw);
20
+ });
17
21
  </script>
@@ -1,4 +1,5 @@
1
1
  <script>import { setupDrawable } from "../../setup";
2
+ import { onMount } from "svelte";
2
3
  export let text;
3
4
  export let size;
4
5
  export let font;
@@ -24,5 +25,8 @@ const draw = function ({ ctx }, { x, y }) {
24
25
  ctx.strokeText(text, x, y);
25
26
  }
26
27
  };
27
- setupDrawable({ draw });
28
+ let register = setupDrawable({});
29
+ onMount(() => {
30
+ return register(draw);
31
+ });
28
32
  </script>
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hi-ashleyj/llama",
3
3
  "description": "A (Very Incomplete) 2D Canvas Game Engine powered by Svelte",
4
- "version": "0.3.1",
4
+ "version": "0.3.2",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "type": "git",
package/setup.d.ts CHANGED
@@ -1,9 +1,5 @@
1
- import type { GameContext, LayerContext, DrawableContext } from "./types/contexts";
1
+ import type { GameContext, LayerContext, DrawableContext, DrawableFunction } from "./types";
2
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
- };
3
+ export declare const setupLayer: (context: LayerContext) => (draw: () => any) => () => any;
4
+ export declare const setupDrawable: ({ assign }: Partial<DrawableContext>) => (draw: DrawableFunction) => () => any;
9
5
  export declare const getGame: () => GameContext;
package/setup.js CHANGED
@@ -1,4 +1,4 @@
1
- import { getContext, setContext, onDestroy } from 'svelte';
1
+ import { getContext, setContext } from 'svelte';
2
2
  const GAME = Symbol();
3
3
  const LAYER = Symbol();
4
4
  const DRAWABLE = Symbol();
@@ -12,29 +12,24 @@ export const setupLayer = function (context) {
12
12
  if (getContext(LAYER)) {
13
13
  throw new Error("Cannot Mount Layer inside a Layer");
14
14
  }
15
- let game = getContext(GAME);
15
+ const game = getContext(GAME);
16
16
  if (!game)
17
17
  throw new Error("Layers must be inside a Game");
18
- let remove = game.assign(context.draw);
19
- onDestroy(() => {
20
- remove();
21
- });
22
18
  setContext(LAYER, context);
23
- return setupDrawable({ assign: context.assign, draw: context.draw });
19
+ setupDrawable({ assign: context.assign });
20
+ return (draw) => {
21
+ return game.assign(draw);
22
+ };
24
23
  };
25
- export const setupDrawable = function ({ assign, draw }) {
24
+ export const setupDrawable = function ({ assign }) {
26
25
  let parent = getContext(DRAWABLE);
27
- if (parent && draw) {
28
- let remove = parent.assign(draw);
29
- onDestroy(() => {
30
- remove();
31
- });
32
- }
33
26
  if (assign) {
34
27
  setContext(DRAWABLE, { assign });
35
28
  }
36
- return {
37
- game: getContext(GAME)
29
+ return (draw) => {
30
+ if (!parent)
31
+ return () => { };
32
+ return parent.assign(draw);
38
33
  };
39
34
  };
40
35
  export const getGame = function () {
@@ -18,8 +18,7 @@ export type GameContext = {
18
18
  };
19
19
 
20
20
  export type LayerContext = {
21
- assign: (draw: Function) => () => any,
22
- draw: () => any
21
+ assign: (draw: Function) => () => any
23
22
  };
24
23
 
25
24
  export type DrawableFunction = ({}: { width: number, height: number, ctx: CanvasRenderingContext2D }, ...more: any[] ) => any;