@hi-ashleyj/llama 0.5.0 → 0.7.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/components/Game.svelte +16 -1
- package/components/Game.svelte.d.ts +3 -3
- package/components/GameObject.svelte.d.ts +3 -3
- package/components/Layer.svelte.d.ts +3 -3
- package/components/drawables/Arc.svelte.d.ts +3 -3
- package/components/drawables/Circle.svelte.d.ts +3 -3
- package/components/drawables/DisplayImage.svelte.d.ts +3 -3
- package/components/drawables/MultiLineText.svelte.d.ts +3 -3
- package/components/drawables/Rectangle.svelte.d.ts +3 -3
- package/components/drawables/Text.svelte.d.ts +3 -3
- package/components/motions.d.ts +16 -1
- package/components/motions.js +19 -1
- package/components/mouse/MouseClickable.svelte +50 -0
- package/components/mouse/MouseClickable.svelte.d.ts +21 -0
- package/components/mouse/MouseEventArea.svelte +55 -0
- package/components/mouse/MouseEventArea.svelte.d.ts +23 -0
- package/components/mouse/MouseLeftClick.svelte +33 -0
- package/components/mouse/MouseLeftClick.svelte.d.ts +16 -0
- package/components/mouse.d.ts +33 -0
- package/components/mouse.js +143 -0
- package/index.d.ts +6 -0
- package/index.js +5 -0
- package/package.json +10 -5
- package/setup.js +1 -0
- package/types/contexts.d.ts +7 -1
package/components/Game.svelte
CHANGED
|
@@ -3,6 +3,7 @@ import { onMount } from "svelte";
|
|
|
3
3
|
import { setupGame } from "../setup";
|
|
4
4
|
import { Timing } from "./motions";
|
|
5
5
|
import { Controller } from "./controller";
|
|
6
|
+
import { Mouse } from "./mouse";
|
|
6
7
|
export let width = 1920;
|
|
7
8
|
export let height = 1080;
|
|
8
9
|
export let background = "#000000";
|
|
@@ -33,6 +34,7 @@ const assign = function (ctx) {
|
|
|
33
34
|
};
|
|
34
35
|
const timing = new Timing();
|
|
35
36
|
const controller = new Controller();
|
|
37
|
+
const mouse = new Mouse();
|
|
36
38
|
const frameEvents = new Set();
|
|
37
39
|
const frameBeforeEvents = new Set();
|
|
38
40
|
const frameAfterEvents = new Set();
|
|
@@ -42,10 +44,15 @@ export const context = {
|
|
|
42
44
|
height: heightStore,
|
|
43
45
|
background: backgroundStore,
|
|
44
46
|
assign,
|
|
45
|
-
createTimer: timing.
|
|
47
|
+
createTimer: timing.createTimer.bind(timing),
|
|
48
|
+
createBurst: timing.createBurst.bind(timing),
|
|
46
49
|
onKeyboardEvent: controller.on.bind(controller),
|
|
47
50
|
isKeyboardPressed: controller.isPressed.bind(controller),
|
|
48
51
|
getKeyboardStore: controller.getStore.bind(controller),
|
|
52
|
+
onMouseEvent: mouse.on.bind(mouse),
|
|
53
|
+
isMousePressed: mouse.isPressed.bind(mouse),
|
|
54
|
+
getMousePosition: mouse.getPosition.bind(mouse),
|
|
55
|
+
getMouseStore: mouse.getStore.bind(mouse),
|
|
49
56
|
onFrame: (callback) => {
|
|
50
57
|
frameEvents.add(callback);
|
|
51
58
|
return () => frameEvents.delete(callback);
|
|
@@ -81,13 +88,21 @@ const loop = function (time) {
|
|
|
81
88
|
onMount(() => {
|
|
82
89
|
requestAnimationFrame(loop);
|
|
83
90
|
controller.start();
|
|
91
|
+
mouse.start();
|
|
84
92
|
});
|
|
93
|
+
let wiw = 0;
|
|
94
|
+
let wih = 0;
|
|
95
|
+
$: {
|
|
96
|
+
mouse.changeWindowDimensions(wiw, wih);
|
|
97
|
+
}
|
|
85
98
|
</script>
|
|
86
99
|
|
|
87
100
|
<div class="game" style:background-color={background}>
|
|
88
101
|
<slot />
|
|
89
102
|
</div>
|
|
90
103
|
|
|
104
|
+
<svelte:window bind:innerHeight={wih} bind:innerWidth={wiw}></svelte:window>
|
|
105
|
+
|
|
91
106
|
<style>
|
|
92
107
|
.game {
|
|
93
108
|
position: relative;
|
|
@@ -14,9 +14,9 @@ declare const __propDef: {
|
|
|
14
14
|
default: {};
|
|
15
15
|
};
|
|
16
16
|
};
|
|
17
|
-
export
|
|
18
|
-
export
|
|
19
|
-
export
|
|
17
|
+
export type GameProps = typeof __propDef.props;
|
|
18
|
+
export type GameEvents = typeof __propDef.events;
|
|
19
|
+
export type GameSlots = typeof __propDef.slots;
|
|
20
20
|
export default class Game extends SvelteComponentTyped<GameProps, GameEvents, GameSlots> {
|
|
21
21
|
get context(): GameContext;
|
|
22
22
|
}
|
|
@@ -18,9 +18,9 @@ declare const __propDef: {
|
|
|
18
18
|
default: {};
|
|
19
19
|
};
|
|
20
20
|
};
|
|
21
|
-
export
|
|
22
|
-
export
|
|
23
|
-
export
|
|
21
|
+
export type GameObjectProps = typeof __propDef.props;
|
|
22
|
+
export type GameObjectEvents = typeof __propDef.events;
|
|
23
|
+
export type GameObjectSlots = typeof __propDef.slots;
|
|
24
24
|
export default class GameObject extends SvelteComponentTyped<GameObjectProps, GameObjectEvents, GameObjectSlots> {
|
|
25
25
|
}
|
|
26
26
|
export {};
|
|
@@ -10,9 +10,9 @@ declare const __propDef: {
|
|
|
10
10
|
default: {};
|
|
11
11
|
};
|
|
12
12
|
};
|
|
13
|
-
export
|
|
14
|
-
export
|
|
15
|
-
export
|
|
13
|
+
export type LayerProps = typeof __propDef.props;
|
|
14
|
+
export type LayerEvents = typeof __propDef.events;
|
|
15
|
+
export type LayerSlots = typeof __propDef.slots;
|
|
16
16
|
export default class Layer extends SvelteComponentTyped<LayerProps, LayerEvents, LayerSlots> {
|
|
17
17
|
}
|
|
18
18
|
export {};
|
|
@@ -11,9 +11,9 @@ declare const __propDef: {
|
|
|
11
11
|
};
|
|
12
12
|
slots: {};
|
|
13
13
|
};
|
|
14
|
-
export
|
|
15
|
-
export
|
|
16
|
-
export
|
|
14
|
+
export type ArcProps = typeof __propDef.props;
|
|
15
|
+
export type ArcEvents = typeof __propDef.events;
|
|
16
|
+
export type ArcSlots = typeof __propDef.slots;
|
|
17
17
|
export default class Arc extends SvelteComponentTyped<ArcProps, ArcEvents, ArcSlots> {
|
|
18
18
|
}
|
|
19
19
|
export {};
|
|
@@ -9,9 +9,9 @@ declare const __propDef: {
|
|
|
9
9
|
};
|
|
10
10
|
slots: {};
|
|
11
11
|
};
|
|
12
|
-
export
|
|
13
|
-
export
|
|
14
|
-
export
|
|
12
|
+
export type CircleProps = typeof __propDef.props;
|
|
13
|
+
export type CircleEvents = typeof __propDef.events;
|
|
14
|
+
export type CircleSlots = typeof __propDef.slots;
|
|
15
15
|
export default class Circle extends SvelteComponentTyped<CircleProps, CircleEvents, CircleSlots> {
|
|
16
16
|
}
|
|
17
17
|
export {};
|
|
@@ -14,9 +14,9 @@ declare const __propDef: {
|
|
|
14
14
|
};
|
|
15
15
|
slots: {};
|
|
16
16
|
};
|
|
17
|
-
export
|
|
18
|
-
export
|
|
19
|
-
export
|
|
17
|
+
export type DisplayImageProps = typeof __propDef.props;
|
|
18
|
+
export type DisplayImageEvents = typeof __propDef.events;
|
|
19
|
+
export type DisplayImageSlots = typeof __propDef.slots;
|
|
20
20
|
export default class DisplayImage extends SvelteComponentTyped<DisplayImageProps, DisplayImageEvents, DisplayImageSlots> {
|
|
21
21
|
}
|
|
22
22
|
export {};
|
|
@@ -16,9 +16,9 @@ declare const __propDef: {
|
|
|
16
16
|
};
|
|
17
17
|
slots: {};
|
|
18
18
|
};
|
|
19
|
-
export
|
|
20
|
-
export
|
|
21
|
-
export
|
|
19
|
+
export type MultiLineTextProps = typeof __propDef.props;
|
|
20
|
+
export type MultiLineTextEvents = typeof __propDef.events;
|
|
21
|
+
export type MultiLineTextSlots = typeof __propDef.slots;
|
|
22
22
|
export default class MultiLineText extends SvelteComponentTyped<MultiLineTextProps, MultiLineTextEvents, MultiLineTextSlots> {
|
|
23
23
|
}
|
|
24
24
|
export {};
|
|
@@ -9,9 +9,9 @@ declare const __propDef: {
|
|
|
9
9
|
};
|
|
10
10
|
slots: {};
|
|
11
11
|
};
|
|
12
|
-
export
|
|
13
|
-
export
|
|
14
|
-
export
|
|
12
|
+
export type RectangleProps = typeof __propDef.props;
|
|
13
|
+
export type RectangleEvents = typeof __propDef.events;
|
|
14
|
+
export type RectangleSlots = typeof __propDef.slots;
|
|
15
15
|
export default class Rectangle extends SvelteComponentTyped<RectangleProps, RectangleEvents, RectangleSlots> {
|
|
16
16
|
}
|
|
17
17
|
export {};
|
|
@@ -15,9 +15,9 @@ declare const __propDef: {
|
|
|
15
15
|
};
|
|
16
16
|
slots: {};
|
|
17
17
|
};
|
|
18
|
-
export
|
|
19
|
-
export
|
|
20
|
-
export
|
|
18
|
+
export type TextProps = typeof __propDef.props;
|
|
19
|
+
export type TextEvents = typeof __propDef.events;
|
|
20
|
+
export type TextSlots = typeof __propDef.slots;
|
|
21
21
|
export default class Text extends SvelteComponentTyped<TextProps, TextEvents, TextSlots> {
|
|
22
22
|
}
|
|
23
23
|
export {};
|
package/components/motions.d.ts
CHANGED
|
@@ -6,13 +6,14 @@ export declare class Timing {
|
|
|
6
6
|
duration: number;
|
|
7
7
|
repeats: number;
|
|
8
8
|
store: Writable<number | null>;
|
|
9
|
+
burst?: boolean;
|
|
9
10
|
}>;
|
|
10
11
|
/**
|
|
11
12
|
* Used to create a store whose value changes based on the total delta time.
|
|
12
13
|
* Duration is in MS
|
|
13
14
|
* Use 0 for repeats for infinite
|
|
14
15
|
*/
|
|
15
|
-
|
|
16
|
+
createTimer({ duration, repeats }: {
|
|
16
17
|
duration: number;
|
|
17
18
|
repeats: number;
|
|
18
19
|
}): {
|
|
@@ -21,5 +22,19 @@ export declare class Timing {
|
|
|
21
22
|
update(this: void, updater: import("svelte/store").Updater<number>): void;
|
|
22
23
|
subscribe(this: void, run: import("svelte/store").Subscriber<number>, invalidate?: ((value?: number | undefined) => void) | undefined): import("svelte/store").Unsubscriber;
|
|
23
24
|
};
|
|
25
|
+
/**
|
|
26
|
+
* Used to create a store whose value adjusts based on bursts.
|
|
27
|
+
* Duration is in MS
|
|
28
|
+
*/
|
|
29
|
+
createBurst({ duration, initialTrigger }: {
|
|
30
|
+
duration: number;
|
|
31
|
+
initialTrigger?: boolean;
|
|
32
|
+
}): {
|
|
33
|
+
stop: () => boolean;
|
|
34
|
+
trigger: () => void;
|
|
35
|
+
set(this: void, value: number): void;
|
|
36
|
+
update(this: void, updater: import("svelte/store").Updater<number>): void;
|
|
37
|
+
subscribe(this: void, run: import("svelte/store").Subscriber<number>, invalidate?: ((value?: number | undefined) => void) | undefined): import("svelte/store").Unsubscriber;
|
|
38
|
+
};
|
|
24
39
|
update(delta: number): void;
|
|
25
40
|
}
|
package/components/motions.js
CHANGED
|
@@ -9,7 +9,7 @@ export class Timing {
|
|
|
9
9
|
* Duration is in MS
|
|
10
10
|
* Use 0 for repeats for infinite
|
|
11
11
|
*/
|
|
12
|
-
|
|
12
|
+
createTimer({ duration, repeats }) {
|
|
13
13
|
let store = { ...writable(0), stop: () => this.targets.delete(out) };
|
|
14
14
|
let out = {
|
|
15
15
|
current: 0,
|
|
@@ -20,9 +20,27 @@ export class Timing {
|
|
|
20
20
|
this.targets.add(out);
|
|
21
21
|
return store;
|
|
22
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Used to create a store whose value adjusts based on bursts.
|
|
25
|
+
* Duration is in MS
|
|
26
|
+
*/
|
|
27
|
+
createBurst({ duration, initialTrigger }) {
|
|
28
|
+
let store = { ...writable(initialTrigger ? 0 : 1), stop: () => this.targets.delete(out), trigger: () => { store.set(0); out.current = 0; } };
|
|
29
|
+
let out = {
|
|
30
|
+
current: initialTrigger ? 0 : -1,
|
|
31
|
+
duration,
|
|
32
|
+
repeats: 1,
|
|
33
|
+
store,
|
|
34
|
+
burst: true
|
|
35
|
+
};
|
|
36
|
+
this.targets.add(out);
|
|
37
|
+
return store;
|
|
38
|
+
}
|
|
23
39
|
update(delta) {
|
|
24
40
|
this.targets.forEach((t, ignored, set) => {
|
|
25
41
|
if (t.current < 0) {
|
|
42
|
+
if (t.burst)
|
|
43
|
+
return; // do not destroy stores for bursts
|
|
26
44
|
t.store.set(null);
|
|
27
45
|
set.delete(t);
|
|
28
46
|
return;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
<script>import { setupDrawable, getGame } from "../../setup";
|
|
2
|
+
import { onMount, createEventDispatcher } from "svelte";
|
|
3
|
+
import { MOUSE_ACTION } from "../mouse";
|
|
4
|
+
let tx = 0;
|
|
5
|
+
let ty = 0;
|
|
6
|
+
let tw = 0;
|
|
7
|
+
let th = 0;
|
|
8
|
+
let dispatch = createEventDispatcher();
|
|
9
|
+
let context = getGame();
|
|
10
|
+
const draw = function (_info, { x, y, w, h }) {
|
|
11
|
+
tx = x;
|
|
12
|
+
ty = y;
|
|
13
|
+
tw = w;
|
|
14
|
+
th = h;
|
|
15
|
+
};
|
|
16
|
+
let register = setupDrawable({});
|
|
17
|
+
onMount(() => {
|
|
18
|
+
let event = context.onMouseEvent(null, MOUSE_ACTION.DOWN, ({ key }) => {
|
|
19
|
+
let x = context.getMousePosition("mouse_x");
|
|
20
|
+
if (x < tx || x > tx + tw)
|
|
21
|
+
return;
|
|
22
|
+
let y = context.getMousePosition("mouse_y");
|
|
23
|
+
if (y < ty || y > ty + th)
|
|
24
|
+
return;
|
|
25
|
+
console.log(key);
|
|
26
|
+
dispatch("click");
|
|
27
|
+
switch (key) {
|
|
28
|
+
case ("mouse_left"): {
|
|
29
|
+
dispatch("left");
|
|
30
|
+
dispatch("leftorright");
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
case ("mouse_right"): {
|
|
34
|
+
dispatch("right");
|
|
35
|
+
dispatch("leftorright");
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
case ("mouse_middle"): {
|
|
39
|
+
dispatch("middle");
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
dispatch("other");
|
|
43
|
+
});
|
|
44
|
+
let deregister = register({ draw });
|
|
45
|
+
return () => {
|
|
46
|
+
event();
|
|
47
|
+
deregister();
|
|
48
|
+
};
|
|
49
|
+
});
|
|
50
|
+
</script>
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { SvelteComponentTyped } from "svelte";
|
|
2
|
+
declare const __propDef: {
|
|
3
|
+
props: Record<string, never>;
|
|
4
|
+
events: {
|
|
5
|
+
click: CustomEvent<any>;
|
|
6
|
+
left: CustomEvent<any>;
|
|
7
|
+
leftorright: CustomEvent<any>;
|
|
8
|
+
right: CustomEvent<any>;
|
|
9
|
+
middle: CustomEvent<any>;
|
|
10
|
+
other: CustomEvent<any>;
|
|
11
|
+
} & {
|
|
12
|
+
[evt: string]: CustomEvent<any>;
|
|
13
|
+
};
|
|
14
|
+
slots: {};
|
|
15
|
+
};
|
|
16
|
+
export type MouseClickableProps = typeof __propDef.props;
|
|
17
|
+
export type MouseClickableEvents = typeof __propDef.events;
|
|
18
|
+
export type MouseClickableSlots = typeof __propDef.slots;
|
|
19
|
+
export default class MouseClickable extends SvelteComponentTyped<MouseClickableProps, MouseClickableEvents, MouseClickableSlots> {
|
|
20
|
+
}
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
<script>import { setupDrawable, getGame } from "../../setup";
|
|
2
|
+
import { onMount, createEventDispatcher } from "svelte";
|
|
3
|
+
import { MOUSE_ACTION } from "../mouse";
|
|
4
|
+
let tx = 0;
|
|
5
|
+
let ty = 0;
|
|
6
|
+
let tw = 0;
|
|
7
|
+
let th = 0;
|
|
8
|
+
let dispatch = createEventDispatcher();
|
|
9
|
+
let context = getGame();
|
|
10
|
+
let mouseX = context.getMouseStore("mouse_x");
|
|
11
|
+
let mouseY = context.getMouseStore("mouse_y");
|
|
12
|
+
export let hover = false;
|
|
13
|
+
const draw = function (_info, { x, y, w, h }) {
|
|
14
|
+
tx = x;
|
|
15
|
+
ty = y;
|
|
16
|
+
tw = w;
|
|
17
|
+
th = h;
|
|
18
|
+
if ($mouseX < tx || $mouseX > tx + tw)
|
|
19
|
+
return hover = false;
|
|
20
|
+
if ($mouseY < ty || $mouseY > ty + th)
|
|
21
|
+
return hover = false;
|
|
22
|
+
hover = true;
|
|
23
|
+
};
|
|
24
|
+
let register = setupDrawable({});
|
|
25
|
+
onMount(() => {
|
|
26
|
+
let event = context.onMouseEvent(null, MOUSE_ACTION.DOWN, ({ key }) => {
|
|
27
|
+
if ($mouseX < tx || $mouseX > tx + tw)
|
|
28
|
+
return;
|
|
29
|
+
if ($mouseY < ty || $mouseY > ty + th)
|
|
30
|
+
return;
|
|
31
|
+
dispatch("click");
|
|
32
|
+
switch (key) {
|
|
33
|
+
case ("mouse_left"): {
|
|
34
|
+
dispatch("left");
|
|
35
|
+
dispatch("leftorright");
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
case ("mouse_right"): {
|
|
39
|
+
dispatch("right");
|
|
40
|
+
dispatch("leftorright");
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
case ("mouse_middle"): {
|
|
44
|
+
dispatch("middle");
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
dispatch("other");
|
|
48
|
+
});
|
|
49
|
+
let deregister = register({ draw });
|
|
50
|
+
return () => {
|
|
51
|
+
event();
|
|
52
|
+
deregister();
|
|
53
|
+
};
|
|
54
|
+
});
|
|
55
|
+
</script>
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { SvelteComponentTyped } from "svelte";
|
|
2
|
+
declare const __propDef: {
|
|
3
|
+
props: {
|
|
4
|
+
hover?: boolean | undefined;
|
|
5
|
+
};
|
|
6
|
+
events: {
|
|
7
|
+
click: CustomEvent<any>;
|
|
8
|
+
left: CustomEvent<any>;
|
|
9
|
+
leftorright: CustomEvent<any>;
|
|
10
|
+
right: CustomEvent<any>;
|
|
11
|
+
middle: CustomEvent<any>;
|
|
12
|
+
other: CustomEvent<any>;
|
|
13
|
+
} & {
|
|
14
|
+
[evt: string]: CustomEvent<any>;
|
|
15
|
+
};
|
|
16
|
+
slots: {};
|
|
17
|
+
};
|
|
18
|
+
export type MouseEventAreaProps = typeof __propDef.props;
|
|
19
|
+
export type MouseEventAreaEvents = typeof __propDef.events;
|
|
20
|
+
export type MouseEventAreaSlots = typeof __propDef.slots;
|
|
21
|
+
export default class MouseEventArea extends SvelteComponentTyped<MouseEventAreaProps, MouseEventAreaEvents, MouseEventAreaSlots> {
|
|
22
|
+
}
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
<script>import { setupDrawable, getGame } from "../../setup";
|
|
2
|
+
import { onMount, createEventDispatcher } from "svelte";
|
|
3
|
+
import { MOUSE_ACTION } from "../mouse";
|
|
4
|
+
let tx = 0;
|
|
5
|
+
let ty = 0;
|
|
6
|
+
let tw = 0;
|
|
7
|
+
let th = 0;
|
|
8
|
+
let dispatch = createEventDispatcher();
|
|
9
|
+
let context = getGame();
|
|
10
|
+
const draw = function (_info, { x, y, w, h }) {
|
|
11
|
+
tx = x;
|
|
12
|
+
ty = y;
|
|
13
|
+
tw = w;
|
|
14
|
+
th = h;
|
|
15
|
+
};
|
|
16
|
+
let register = setupDrawable({});
|
|
17
|
+
onMount(() => {
|
|
18
|
+
let event = context.onMouseEvent("mouse_left", MOUSE_ACTION.DOWN, () => {
|
|
19
|
+
let x = context.getMousePosition("mouse_x");
|
|
20
|
+
if (x < tx || x > tx + tw)
|
|
21
|
+
return;
|
|
22
|
+
let y = context.getMousePosition("mouse_y");
|
|
23
|
+
if (y < ty || y > ty + th)
|
|
24
|
+
return;
|
|
25
|
+
dispatch("click");
|
|
26
|
+
});
|
|
27
|
+
let deregister = register({ draw });
|
|
28
|
+
return () => {
|
|
29
|
+
event();
|
|
30
|
+
deregister();
|
|
31
|
+
};
|
|
32
|
+
});
|
|
33
|
+
</script>
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { SvelteComponentTyped } from "svelte";
|
|
2
|
+
declare const __propDef: {
|
|
3
|
+
props: Record<string, never>;
|
|
4
|
+
events: {
|
|
5
|
+
click: CustomEvent<any>;
|
|
6
|
+
} & {
|
|
7
|
+
[evt: string]: CustomEvent<any>;
|
|
8
|
+
};
|
|
9
|
+
slots: {};
|
|
10
|
+
};
|
|
11
|
+
export type MouseLeftClickProps = typeof __propDef.props;
|
|
12
|
+
export type MouseLeftClickEvents = typeof __propDef.events;
|
|
13
|
+
export type MouseLeftClickSlots = typeof __propDef.slots;
|
|
14
|
+
export default class MouseLeftClick extends SvelteComponentTyped<MouseLeftClickProps, MouseLeftClickEvents, MouseLeftClickSlots> {
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Writable } from "svelte/store";
|
|
2
|
+
export declare enum MOUSE_ACTION {
|
|
3
|
+
DOWN = "down",
|
|
4
|
+
UP = "up",
|
|
5
|
+
MOVE = "move"
|
|
6
|
+
}
|
|
7
|
+
type MouseEventBoolean = "mouse_left" | "mouse_middle" | "mouse_right";
|
|
8
|
+
type MouseEventNumber = "mouse_x" | "mouse_y";
|
|
9
|
+
export declare class Mouse {
|
|
10
|
+
constructor();
|
|
11
|
+
events: Set<{
|
|
12
|
+
key: string | null;
|
|
13
|
+
action: MOUSE_ACTION;
|
|
14
|
+
call: (e: {
|
|
15
|
+
key: string | null;
|
|
16
|
+
action: MOUSE_ACTION;
|
|
17
|
+
}) => any | void;
|
|
18
|
+
}>;
|
|
19
|
+
mouseState: Map<MouseEventBoolean | MouseEventNumber, boolean | number>;
|
|
20
|
+
mouseStores: Map<MouseEventBoolean | MouseEventNumber, Writable<boolean | number>>;
|
|
21
|
+
innerWidth: number;
|
|
22
|
+
innerHeight: number;
|
|
23
|
+
start(): void;
|
|
24
|
+
on(key: MouseEventBoolean | MouseEventNumber | null, action: MOUSE_ACTION, callback: (e: {
|
|
25
|
+
key: string | null;
|
|
26
|
+
action: MOUSE_ACTION;
|
|
27
|
+
}) => any | void): () => any;
|
|
28
|
+
getStore<T extends (MouseEventBoolean | MouseEventNumber)>(key: T): Writable<T extends MouseEventNumber ? number : boolean>;
|
|
29
|
+
isPressed(key: MouseEventBoolean): boolean;
|
|
30
|
+
getPosition(key: MouseEventNumber): number;
|
|
31
|
+
changeWindowDimensions(width: number, height: number): void;
|
|
32
|
+
}
|
|
33
|
+
export {};
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { writable } from "svelte/store";
|
|
2
|
+
export var MOUSE_ACTION;
|
|
3
|
+
(function (MOUSE_ACTION) {
|
|
4
|
+
MOUSE_ACTION["DOWN"] = "down";
|
|
5
|
+
MOUSE_ACTION["UP"] = "up";
|
|
6
|
+
// CLICK = "click",
|
|
7
|
+
MOUSE_ACTION["MOVE"] = "move";
|
|
8
|
+
})(MOUSE_ACTION || (MOUSE_ACTION = {}));
|
|
9
|
+
export class Mouse {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.events = new Set();
|
|
12
|
+
this.mouseState = new Map();
|
|
13
|
+
this.mouseStores = new Map();
|
|
14
|
+
}
|
|
15
|
+
events;
|
|
16
|
+
mouseState;
|
|
17
|
+
mouseStores;
|
|
18
|
+
innerWidth = 0;
|
|
19
|
+
innerHeight = 0;
|
|
20
|
+
start() {
|
|
21
|
+
window.addEventListener("pointerdown", (e) => {
|
|
22
|
+
e.preventDefault();
|
|
23
|
+
let eKeyLowCase;
|
|
24
|
+
switch (e.button) {
|
|
25
|
+
case (0): {
|
|
26
|
+
eKeyLowCase = "mouse_left";
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
case (2): {
|
|
30
|
+
eKeyLowCase = "mouse_right";
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
case (1): {
|
|
34
|
+
eKeyLowCase = "mouse_middle";
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
default: return;
|
|
38
|
+
}
|
|
39
|
+
this.mouseState.set(eKeyLowCase, true);
|
|
40
|
+
this.events.forEach(({ key, action, call }) => {
|
|
41
|
+
if ((key === eKeyLowCase || key === null) && action === MOUSE_ACTION.DOWN)
|
|
42
|
+
call({ key: eKeyLowCase, action });
|
|
43
|
+
});
|
|
44
|
+
if (this.mouseStores.has(eKeyLowCase)) {
|
|
45
|
+
this.mouseStores.get(eKeyLowCase)?.set(true);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
window.addEventListener("pointerup", (e) => {
|
|
49
|
+
e.preventDefault();
|
|
50
|
+
let eKeyLowCase;
|
|
51
|
+
switch (e.button) {
|
|
52
|
+
case (0): {
|
|
53
|
+
eKeyLowCase = "mouse_left";
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
case (1): {
|
|
57
|
+
eKeyLowCase = "mouse_right";
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
case (2): {
|
|
61
|
+
eKeyLowCase = "mouse_middle";
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
default: return;
|
|
65
|
+
}
|
|
66
|
+
this.mouseState.set(eKeyLowCase, false);
|
|
67
|
+
this.events.forEach(({ key, action, call }) => {
|
|
68
|
+
if ((key === eKeyLowCase || key === null) && action === MOUSE_ACTION.UP)
|
|
69
|
+
call({ key: eKeyLowCase, action });
|
|
70
|
+
});
|
|
71
|
+
if (this.mouseStores.has(eKeyLowCase)) {
|
|
72
|
+
this.mouseStores.get(eKeyLowCase)?.set(false);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
window.addEventListener("pointermove", (e) => {
|
|
76
|
+
let rawX = e.offsetX;
|
|
77
|
+
let rawY = e.offsetY;
|
|
78
|
+
let adjustedX;
|
|
79
|
+
let adjustedY;
|
|
80
|
+
if (this.innerWidth / this.innerHeight > 16 / 9) {
|
|
81
|
+
// TODO: De Hard-code this
|
|
82
|
+
const scaleFactor = 1080 / this.innerHeight;
|
|
83
|
+
const realCanvasSize = 1920 / scaleFactor;
|
|
84
|
+
adjustedY = rawY * scaleFactor;
|
|
85
|
+
adjustedX = (rawX - ((this.innerWidth - realCanvasSize) / 2)) * scaleFactor;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
// TODO: De Hard-code this
|
|
89
|
+
const scaleFactor = 1920 / this.innerWidth;
|
|
90
|
+
const realCanvasSize = 1080 / scaleFactor;
|
|
91
|
+
adjustedX = rawX * scaleFactor;
|
|
92
|
+
adjustedY = (rawY - ((this.innerHeight - realCanvasSize) / 2)) * scaleFactor;
|
|
93
|
+
}
|
|
94
|
+
this.mouseState.set("mouse_x", adjustedX);
|
|
95
|
+
this.mouseState.set("mouse_y", adjustedY);
|
|
96
|
+
this.events.forEach(({ key, action, call }) => {
|
|
97
|
+
if ((key === "mouse_x" || key === null) && action === MOUSE_ACTION.MOVE)
|
|
98
|
+
call({ key: "mouse_x", action });
|
|
99
|
+
if ((key === "mouse_y" || key === null) && action === MOUSE_ACTION.MOVE)
|
|
100
|
+
call({ key: "mouse_y", action });
|
|
101
|
+
});
|
|
102
|
+
if (this.mouseStores.has("mouse_x")) {
|
|
103
|
+
this.mouseStores.get("mouse_x")?.set(adjustedX);
|
|
104
|
+
}
|
|
105
|
+
if (this.mouseStores.has("mouse_y")) {
|
|
106
|
+
this.mouseStores.get("mouse_y")?.set(adjustedY);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
window.addEventListener("contextmenu", (e) => {
|
|
110
|
+
e.preventDefault();
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
on(key, action, callback) {
|
|
114
|
+
let obj = { key, action, call: callback };
|
|
115
|
+
this.events.add(obj);
|
|
116
|
+
return () => {
|
|
117
|
+
this.events.delete(obj);
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
getStore(key) {
|
|
121
|
+
if (this.mouseStores.has(key)) {
|
|
122
|
+
return this.mouseStores.get(key);
|
|
123
|
+
}
|
|
124
|
+
if (key === "mouse_x" || key === "mouse_y") {
|
|
125
|
+
let wr = writable(this.mouseState.get(key) ?? 0);
|
|
126
|
+
this.mouseStores.set(key, wr);
|
|
127
|
+
return wr;
|
|
128
|
+
}
|
|
129
|
+
let wr = writable(this.mouseState.get(key) ?? false);
|
|
130
|
+
this.mouseStores.set(key, wr);
|
|
131
|
+
return wr;
|
|
132
|
+
}
|
|
133
|
+
isPressed(key) {
|
|
134
|
+
return this.mouseState.get(key) ?? false;
|
|
135
|
+
}
|
|
136
|
+
getPosition(key) {
|
|
137
|
+
return this.mouseState.get(key) ?? 0;
|
|
138
|
+
}
|
|
139
|
+
changeWindowDimensions(width, height) {
|
|
140
|
+
this.innerHeight = height;
|
|
141
|
+
this.innerWidth = width;
|
|
142
|
+
}
|
|
143
|
+
}
|
package/index.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
export { default as Game } from "./components/Game.svelte";
|
|
2
2
|
export { default as Layer } from "./components/Layer.svelte";
|
|
3
3
|
export { default as GameObject } from "./components/GameObject.svelte";
|
|
4
|
+
export { default as MouseLeftClick } from "./components/mouse/MouseLeftClick.svelte";
|
|
5
|
+
export { default as MouseClickable } from "./components/mouse/MouseClickable.svelte";
|
|
6
|
+
export { default as MouseEventArea } from "./components/mouse/MouseEventArea.svelte";
|
|
4
7
|
export * as drawables from "./components/drawables/index";
|
|
5
8
|
export { setupDrawable, getGame } from "./setup";
|
|
6
9
|
export { resolve as resolveImage } from "./components/resources/images";
|
|
10
|
+
export { CONTROLLER_ACTION } from "./components/controller";
|
|
11
|
+
export { MOUSE_ACTION } from "./components/mouse";
|
|
12
|
+
export type { GameContext, DrawableContext, DrawableFunction, LayerContext } from "./types/index";
|
package/index.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
export { default as Game } from "./components/Game.svelte";
|
|
2
2
|
export { default as Layer } from "./components/Layer.svelte";
|
|
3
3
|
export { default as GameObject } from "./components/GameObject.svelte";
|
|
4
|
+
export { default as MouseLeftClick } from "./components/mouse/MouseLeftClick.svelte";
|
|
5
|
+
export { default as MouseClickable } from "./components/mouse/MouseClickable.svelte";
|
|
6
|
+
export { default as MouseEventArea } from "./components/mouse/MouseEventArea.svelte";
|
|
4
7
|
export * as drawables from "./components/drawables/index";
|
|
5
8
|
export { setupDrawable, getGame } from "./setup";
|
|
6
9
|
export { resolve as resolveImage } from "./components/resources/images";
|
|
10
|
+
export { CONTROLLER_ACTION } from "./components/controller";
|
|
11
|
+
export { MOUSE_ACTION } from "./components/mouse";
|
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.
|
|
4
|
+
"version": "0.7.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -14,14 +14,15 @@
|
|
|
14
14
|
},
|
|
15
15
|
"homepage": "https://github.com/hi-ashleyj/llama-game-engine#readme",
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"@sveltejs/adapter-static": "
|
|
18
|
-
"@sveltejs/kit": "
|
|
19
|
-
"@sveltejs/package": "
|
|
17
|
+
"@sveltejs/adapter-static": "latest",
|
|
18
|
+
"@sveltejs/kit": "latest",
|
|
19
|
+
"@sveltejs/package": "latest",
|
|
20
|
+
"sass": "^1.58.0",
|
|
20
21
|
"svelte": "^3.44.0",
|
|
21
22
|
"svelte-preprocess": "^4.10.7",
|
|
22
23
|
"svelte2tsx": "^0.5.12",
|
|
23
24
|
"typescript": "^4.7.4",
|
|
24
|
-
"vite": "^
|
|
25
|
+
"vite": "^4.0.0"
|
|
25
26
|
},
|
|
26
27
|
"exports": {
|
|
27
28
|
"./package.json": "./package.json",
|
|
@@ -37,6 +38,10 @@
|
|
|
37
38
|
"./components/drawables/Text.svelte": "./components/drawables/Text.svelte",
|
|
38
39
|
"./components/drawables": "./components/drawables/index.js",
|
|
39
40
|
"./components/motions": "./components/motions.js",
|
|
41
|
+
"./components/mouse/MouseClickable.svelte": "./components/mouse/MouseClickable.svelte",
|
|
42
|
+
"./components/mouse/MouseEventArea.svelte": "./components/mouse/MouseEventArea.svelte",
|
|
43
|
+
"./components/mouse/MouseLeftClick.svelte": "./components/mouse/MouseLeftClick.svelte",
|
|
44
|
+
"./components/mouse": "./components/mouse.js",
|
|
40
45
|
"./components/resources/images": "./components/resources/images.js",
|
|
41
46
|
"./drawables": "./drawables.js",
|
|
42
47
|
".": "./index.js",
|
package/setup.js
CHANGED
|
@@ -2,6 +2,7 @@ import { getContext, setContext } from 'svelte';
|
|
|
2
2
|
const GAME = Symbol();
|
|
3
3
|
const LAYER = Symbol();
|
|
4
4
|
const DRAWABLE = Symbol();
|
|
5
|
+
const BOX = Symbol();
|
|
5
6
|
export const setupGame = function (context) {
|
|
6
7
|
if (getContext(GAME)) {
|
|
7
8
|
throw new Error("Cannot Mount Game inside a Game");
|
package/types/contexts.d.ts
CHANGED
|
@@ -1,16 +1,22 @@
|
|
|
1
1
|
import { Writable } from 'svelte/store';
|
|
2
2
|
import { Timing } from "../components/motions";
|
|
3
3
|
import { Controller } from "../components/controller";
|
|
4
|
+
import { Mouse } from "../components/mouse";
|
|
4
5
|
|
|
5
6
|
export type GameContext = {
|
|
6
7
|
assign: (context: { draw: Function }) => () => any,
|
|
7
8
|
width: Writable<number>,
|
|
8
9
|
height: Writable<number>,
|
|
9
10
|
background: Writable<string>,
|
|
10
|
-
createTimer: Timing["
|
|
11
|
+
createTimer: Timing["createTimer"],
|
|
12
|
+
createBurst: Timing["createBurst"],
|
|
11
13
|
onKeyboardEvent: Controller["on"],
|
|
12
14
|
isKeyboardPressed: Controller["isPressed"],
|
|
13
15
|
getKeyboardStore: Controller["getStore"],
|
|
16
|
+
onMouseEvent: Mouse["on"],
|
|
17
|
+
isMousePressed: Mouse["isPressed"],
|
|
18
|
+
getMousePosition: Mouse["getPosition"],
|
|
19
|
+
getMouseStore: Mouse["getStore"],
|
|
14
20
|
onFrame: (callback: Function) => () => any,
|
|
15
21
|
onBeforeFrame: (callback: Function) => () => any,
|
|
16
22
|
onAfterFrame: (callback: Function) => () => any,
|