@hi-ashleyj/llama 0.3.1 → 0.3.3

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.
@@ -24,12 +24,12 @@ $: {
24
24
  const layers = new Set();
25
25
  const draw = function (delta, time) {
26
26
  for (let layer of layers) {
27
- layer(delta, time);
27
+ layer.draw(delta, time);
28
28
  }
29
29
  };
30
- const assign = function (layerDraw) {
31
- layers.add(layerDraw);
32
- return () => layers.delete(layerDraw);
30
+ const assign = function (ctx) {
31
+ layers.add(ctx);
32
+ return () => layers.delete(ctx);
33
33
  };
34
34
  const timing = new Timing();
35
35
  const controller = new Controller();
@@ -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;
@@ -11,14 +12,20 @@ export let centered = false;
11
12
  $: ax = (centered) ? x - (w / 2) : x;
12
13
  $: ay = (centered) ? y - (h / 2) : y;
13
14
  const targets = new Set();
14
- const assign = function (callable) {
15
- targets.add(callable);
16
- return () => { targets.delete(callable); };
17
- };
18
15
  const draw = function ({ width, height, ctx }) {
19
- targets.forEach(f => f({ width, height, ctx }, { x: ax, y: ay, w, h }));
16
+ targets.forEach(f => f.draw({ width, height, ctx }, { x: ax, y: ay, w, h }));
20
17
  };
21
- setupDrawable({ assign, draw });
18
+ let register = setupDrawable({
19
+ assign: (ctx) => {
20
+ targets.add(ctx);
21
+ return () => {
22
+ targets.delete(ctx);
23
+ };
24
+ },
25
+ });
26
+ onMount(() => {
27
+ return register({ draw });
28
+ });
22
29
  </script>
23
30
 
24
- <slot />
31
+ <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
- assign: (callable) => {
8
- targets.add(callable);
9
- 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 }));
7
+ const draw = () => {
8
+ if (ctx === null)
9
+ return;
10
+ ctx.clearRect(0, 0, $width, $height);
11
+ targets.forEach(f => f.draw({ width: $width, height: $height, ctx }));
12
+ };
13
+ let { width, height } = getGame();
14
+ let register = setupLayer({
15
+ assign: (ctx) => {
16
+ targets.add(ctx);
17
+ return () => { targets.delete(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.3",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "type": "git",
package/setup.d.ts CHANGED
@@ -1,9 +1,9 @@
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 }: {
4
+ draw: () => any;
5
+ }) => () => any;
6
+ export declare const setupDrawable: ({ assign }: Partial<DrawableContext>) => ({ draw }: {
7
+ draw: () => DrawableFunction;
8
+ }) => () => any;
9
9
  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 (ctx) => {
21
+ return game.assign(ctx);
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 (ctx) => {
30
+ if (!parent)
31
+ return () => { };
32
+ return parent.assign(ctx);
38
33
  };
39
34
  };
40
35
  export const getGame = function () {
@@ -3,7 +3,7 @@ import { Timing } from "../components/motions";
3
3
  import { Controller } from "../components/controller";
4
4
 
5
5
  export type GameContext = {
6
- assign: (draw: Function) => () => any,
6
+ assign: (context: { draw: Function }) => () => any,
7
7
  width: Writable<number>,
8
8
  height: Writable<number>,
9
9
  background: Writable<string>,
@@ -17,14 +17,12 @@ export type GameContext = {
17
17
 
18
18
  };
19
19
 
20
- export type LayerContext = {
21
- assign: (draw: Function) => () => any,
22
- draw: () => any
20
+ export type LayerContext = {
21
+ assign: (context: { draw: Function }) => () => any
23
22
  };
24
23
 
25
24
  export type DrawableFunction = ({}: { width: number, height: number, ctx: CanvasRenderingContext2D }, ...more: any[] ) => any;
26
25
 
27
- export type DrawableContext = {
28
- assign: (draw: Function) => () => any,
29
- draw: DrawableFunction
26
+ export type DrawableContext = {
27
+ assign: (context: { draw: Function }) => () => any
30
28
  }