@hi-ashleyj/llama 0.8.2 → 0.10.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 +29 -0
- package/dist/Game.svelte +123 -0
- package/{components → dist}/Game.svelte.d.ts +4 -4
- package/dist/GameObject.svelte +38 -0
- package/{components → dist}/GameObject.svelte.d.ts +2 -2
- package/dist/Layer.svelte +58 -0
- package/{components → dist}/Layer.svelte.d.ts +3 -2
- package/dist/controllers/MouseClickable.svelte +50 -0
- package/{components/mouse → dist/controllers}/MouseClickable.svelte.d.ts +2 -2
- package/dist/controllers/MouseEventArea.svelte +58 -0
- package/{components/mouse → dist/controllers}/MouseEventArea.svelte.d.ts +7 -3
- package/{components/controller.d.ts → dist/controllers/keyboard.d.ts} +6 -6
- package/{components/controller.js → dist/controllers/keyboard.js} +9 -9
- package/{components → dist/controllers}/mouse.d.ts +5 -1
- package/{components → dist/controllers}/mouse.js +24 -6
- package/dist/core-contexts.d.ts +48 -0
- package/{setup.js → dist/core-contexts.js} +7 -19
- package/dist/drawable.d.ts +14 -0
- package/dist/drawable.js +13 -0
- package/dist/drawables/Arc.svelte +31 -0
- package/{components → dist}/drawables/Arc.svelte.d.ts +3 -2
- package/dist/drawables/Circle.svelte +25 -0
- package/{components → dist}/drawables/Circle.svelte.d.ts +3 -2
- package/dist/drawables/DisplayImage.svelte +17 -0
- package/{components → dist}/drawables/DisplayImage.svelte.d.ts +2 -2
- package/dist/drawables/MultiLineText.svelte +43 -0
- package/{components → dist}/drawables/MultiLineText.svelte.d.ts +5 -4
- package/dist/drawables/Rectangle.svelte +22 -0
- package/{components → dist}/drawables/Rectangle.svelte.d.ts +3 -2
- package/dist/drawables/RoundedRectangle.svelte +24 -0
- package/dist/drawables/RoundedRectangle.svelte.d.ts +19 -0
- package/dist/drawables/Text.svelte +37 -0
- package/{components → dist}/drawables/Text.svelte.d.ts +5 -4
- package/{components → dist}/drawables/index.d.ts +1 -0
- package/{components → dist}/drawables/index.js +1 -0
- package/dist/extras/Empty.svelte +0 -0
- package/dist/extras/Empty.svelte.d.ts +18 -0
- package/dist/extras/LayerPortal.svelte +24 -0
- package/dist/extras/LayerPortal.svelte.d.ts +18 -0
- package/dist/extras/Rotate.svelte +25 -0
- package/dist/extras/Rotate.svelte.d.ts +22 -0
- package/dist/extras/SceneSwitcher.svelte +20 -0
- package/dist/extras/SceneSwitcher.svelte.d.ts +16 -0
- package/dist/extras/SceneTransition.svelte +40 -0
- package/dist/extras/SceneTransition.svelte.d.ts +21 -0
- package/dist/extras/SensibleDefaultStyles.svelte +22 -0
- package/dist/extras/SensibleDefaultStyles.svelte.d.ts +20 -0
- package/dist/extras/Tweened.svelte +9 -0
- package/dist/extras/Tweened.svelte.d.ts +21 -0
- package/dist/extras/scenes.d.ts +6 -0
- package/dist/extras/scenes.js +22 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +24 -0
- package/package.json +26 -28
- package/components/Game.svelte +0 -116
- package/components/GameObject.svelte +0 -40
- package/components/Layer.svelte +0 -47
- package/components/drawables/Arc.svelte +0 -27
- package/components/drawables/Circle.svelte +0 -23
- package/components/drawables/DisplayImage.svelte +0 -17
- package/components/drawables/MultiLineText.svelte +0 -43
- package/components/drawables/Rectangle.svelte +0 -21
- package/components/drawables/Text.svelte +0 -34
- package/components/mouse/MouseClickable.svelte +0 -49
- package/components/mouse/MouseEventArea.svelte +0 -55
- package/components/mouse/MouseLeftClick.svelte +0 -33
- package/components/mouse/MouseLeftClick.svelte.d.ts +0 -16
- package/drawables.d.ts +0 -1
- package/drawables.js +0 -1
- package/index.d.ts +0 -12
- package/index.js +0 -11
- package/resources.d.ts +0 -1
- package/resources.js +0 -1
- package/setup.d.ts +0 -12
- package/types/contexts.d.ts +0 -35
- package/types/index.d.ts +0 -1
- /package/{components → dist/controllers}/motions.d.ts +0 -0
- /package/{components → dist/controllers}/motions.js +0 -0
- /package/{components → dist}/resources/images.d.ts +0 -0
- /package/{components → dist}/resources/images.js +0 -0
package/README.md
CHANGED
|
@@ -3,4 +3,33 @@ This thing isn't ready - and probably will never be fully done. This is just as
|
|
|
3
3
|
|
|
4
4
|
## Seriously, don't use this yet.
|
|
5
5
|
|
|
6
|
+
## A Good Starting point.
|
|
7
|
+
I'd recommend starting with a sveltekit project configured with TS. That's what I use basically 100% of the time.
|
|
8
|
+
To get started:
|
|
9
|
+
|
|
10
|
+
```html
|
|
11
|
+
/// +page.svelte
|
|
12
|
+
<script lang="ts">
|
|
13
|
+
|
|
14
|
+
import { SensibleDefaultStyles, Game } from "@hi-ashleyj/llama";
|
|
15
|
+
|
|
16
|
+
</script>
|
|
17
|
+
|
|
18
|
+
// Auto-configure a wrapper and some global styles. Optional.
|
|
19
|
+
<SensibleDefaultStyles>
|
|
20
|
+
|
|
21
|
+
<Game>
|
|
22
|
+
|
|
23
|
+
</Game>
|
|
24
|
+
|
|
25
|
+
</SensibleDefaultStyles>
|
|
26
|
+
|
|
27
|
+
<style>
|
|
28
|
+
|
|
29
|
+
@font-face {
|
|
30
|
+
// ...define fonts
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
</style>
|
|
34
|
+
```
|
|
6
35
|
|
package/dist/Game.svelte
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
<script>import { writable } from "svelte/store";
|
|
2
|
+
import { onMount } from "svelte";
|
|
3
|
+
import { setupGame } from "./core-contexts.js";
|
|
4
|
+
import { Timing } from "./controllers/motions.js";
|
|
5
|
+
import { Keyboard } from "./controllers/keyboard.js";
|
|
6
|
+
import { Mouse } from "./controllers/mouse.js";
|
|
7
|
+
export let width = 1920;
|
|
8
|
+
export let height = 1080;
|
|
9
|
+
export let background = "#000000";
|
|
10
|
+
const widthStore = writable(1920);
|
|
11
|
+
const heightStore = writable(1080);
|
|
12
|
+
const backgroundStore = writable("#000000");
|
|
13
|
+
$: {
|
|
14
|
+
$widthStore = width;
|
|
15
|
+
}
|
|
16
|
+
$: {
|
|
17
|
+
$heightStore = height;
|
|
18
|
+
}
|
|
19
|
+
$: {
|
|
20
|
+
$backgroundStore = background;
|
|
21
|
+
}
|
|
22
|
+
const layerDrawables = /* @__PURE__ */ new Set();
|
|
23
|
+
const layerAssignments = /* @__PURE__ */ new Map();
|
|
24
|
+
const draw = function() {
|
|
25
|
+
for (let layer of layerDrawables) {
|
|
26
|
+
if (layer.isStatic())
|
|
27
|
+
continue;
|
|
28
|
+
layer.draw();
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const assign = function(ctx, obj) {
|
|
32
|
+
layerDrawables.add(obj);
|
|
33
|
+
layerAssignments.set(obj.name, ctx);
|
|
34
|
+
return () => {
|
|
35
|
+
layerDrawables.delete(obj);
|
|
36
|
+
layerAssignments.delete(obj.name);
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
const timing = new Timing();
|
|
40
|
+
const keyboard = new Keyboard();
|
|
41
|
+
const mouse = new Mouse();
|
|
42
|
+
const frameEvents = /* @__PURE__ */ new Set();
|
|
43
|
+
const frameBeforeEvents = /* @__PURE__ */ new Set();
|
|
44
|
+
const frameAfterEvents = /* @__PURE__ */ new Set();
|
|
45
|
+
export const context = {
|
|
46
|
+
width: widthStore,
|
|
47
|
+
height: heightStore,
|
|
48
|
+
background: backgroundStore,
|
|
49
|
+
getLayerByName: (name) => layerAssignments.get(name) ?? null,
|
|
50
|
+
assign,
|
|
51
|
+
createTimer: timing.createTimer.bind(timing),
|
|
52
|
+
createBurst: timing.createBurst.bind(timing),
|
|
53
|
+
onKeyboardEvent: keyboard.on.bind(keyboard),
|
|
54
|
+
isKeyboardPressed: keyboard.isPressed.bind(keyboard),
|
|
55
|
+
getKeyboardStore: keyboard.getStore.bind(keyboard),
|
|
56
|
+
onMouseEvent: mouse.on.bind(mouse),
|
|
57
|
+
isMousePressed: mouse.isPressed.bind(mouse),
|
|
58
|
+
getMousePosition: mouse.getPosition.bind(mouse),
|
|
59
|
+
getMouseStore: mouse.getStore.bind(mouse),
|
|
60
|
+
onFrame: (callback) => {
|
|
61
|
+
frameEvents.add(callback);
|
|
62
|
+
return () => frameEvents.delete(callback);
|
|
63
|
+
},
|
|
64
|
+
onBeforeFrame: (callback) => {
|
|
65
|
+
frameBeforeEvents.add(callback);
|
|
66
|
+
return () => frameBeforeEvents.delete(callback);
|
|
67
|
+
},
|
|
68
|
+
onAfterFrame: (callback) => {
|
|
69
|
+
frameAfterEvents.add(callback);
|
|
70
|
+
return () => frameAfterEvents.delete(callback);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
setupGame(context);
|
|
74
|
+
let last = -1;
|
|
75
|
+
const loop = function(time) {
|
|
76
|
+
if (last < 0)
|
|
77
|
+
last = time;
|
|
78
|
+
let delta = time - last;
|
|
79
|
+
if (delta > 1e3) {
|
|
80
|
+
requestAnimationFrame(loop);
|
|
81
|
+
return last = time;
|
|
82
|
+
}
|
|
83
|
+
frameBeforeEvents.forEach((callback) => callback({ delta, time }));
|
|
84
|
+
timing.update(delta);
|
|
85
|
+
frameEvents.forEach((callback) => callback({ delta, time }));
|
|
86
|
+
draw();
|
|
87
|
+
frameAfterEvents.forEach((callback) => callback({ delta, time }));
|
|
88
|
+
requestAnimationFrame(loop);
|
|
89
|
+
last = time;
|
|
90
|
+
};
|
|
91
|
+
onMount(() => {
|
|
92
|
+
requestAnimationFrame(loop);
|
|
93
|
+
keyboard.start();
|
|
94
|
+
mouse.start();
|
|
95
|
+
});
|
|
96
|
+
let wiw = 0;
|
|
97
|
+
let wih = 0;
|
|
98
|
+
$: {
|
|
99
|
+
mouse.changeWindowDimensions(wiw, wih);
|
|
100
|
+
}
|
|
101
|
+
$: {
|
|
102
|
+
mouse.setHeight(height);
|
|
103
|
+
}
|
|
104
|
+
$: {
|
|
105
|
+
mouse.setWidth(width);
|
|
106
|
+
}
|
|
107
|
+
</script>
|
|
108
|
+
|
|
109
|
+
<div class="game" style:background-color={background}>
|
|
110
|
+
<slot />
|
|
111
|
+
</div>
|
|
112
|
+
|
|
113
|
+
<svelte:window bind:innerHeight={wih} bind:innerWidth={wiw}></svelte:window>
|
|
114
|
+
|
|
115
|
+
<style>
|
|
116
|
+
.game {
|
|
117
|
+
position: relative;
|
|
118
|
+
width: 100%;
|
|
119
|
+
height: 100%;
|
|
120
|
+
padding: 0;
|
|
121
|
+
margin: 0;
|
|
122
|
+
}
|
|
123
|
+
</style>
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type
|
|
1
|
+
import { SvelteComponent } from "svelte";
|
|
2
|
+
import { type GameContext } from "./core-contexts.js";
|
|
3
3
|
declare const __propDef: {
|
|
4
4
|
props: {
|
|
5
5
|
width?: number | undefined;
|
|
@@ -17,7 +17,7 @@ declare const __propDef: {
|
|
|
17
17
|
export type GameProps = typeof __propDef.props;
|
|
18
18
|
export type GameEvents = typeof __propDef.events;
|
|
19
19
|
export type GameSlots = typeof __propDef.slots;
|
|
20
|
-
export default class Game extends
|
|
21
|
-
get context():
|
|
20
|
+
export default class Game extends SvelteComponent<GameProps, GameEvents, GameSlots> {
|
|
21
|
+
get context(): any;
|
|
22
22
|
}
|
|
23
23
|
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
<script>import { setupDrawable } from "./drawable.js";
|
|
2
|
+
import { onMount } from "svelte";
|
|
3
|
+
export let x = 0;
|
|
4
|
+
export let y = 0;
|
|
5
|
+
export let w = 0;
|
|
6
|
+
export let h = 0;
|
|
7
|
+
export let centered = false;
|
|
8
|
+
export let opacity = 1;
|
|
9
|
+
$:
|
|
10
|
+
ax = centered ? x - w / 2 : x;
|
|
11
|
+
$:
|
|
12
|
+
ay = centered ? y - h / 2 : y;
|
|
13
|
+
const targets = /* @__PURE__ */ new Set();
|
|
14
|
+
const draw = function({ width, height, ctx }) {
|
|
15
|
+
if (opacity <= 0)
|
|
16
|
+
return;
|
|
17
|
+
if (opacity < 1) {
|
|
18
|
+
ctx.globalAlpha = opacity;
|
|
19
|
+
}
|
|
20
|
+
targets.forEach((f) => f.draw({ width, height, ctx }, { x: ax, y: ay, w, h }));
|
|
21
|
+
if (opacity < 1) {
|
|
22
|
+
ctx.globalAlpha = 1;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
let register = setupDrawable({
|
|
26
|
+
assign: (ctx) => {
|
|
27
|
+
targets.add(ctx);
|
|
28
|
+
return () => {
|
|
29
|
+
targets.delete(ctx);
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
onMount(() => {
|
|
34
|
+
return register({ draw });
|
|
35
|
+
});
|
|
36
|
+
</script>
|
|
37
|
+
|
|
38
|
+
<slot/>
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SvelteComponent } from "svelte";
|
|
2
2
|
declare const __propDef: {
|
|
3
3
|
props: {
|
|
4
4
|
x?: number | undefined;
|
|
@@ -21,6 +21,6 @@ declare const __propDef: {
|
|
|
21
21
|
export type GameObjectProps = typeof __propDef.props;
|
|
22
22
|
export type GameObjectEvents = typeof __propDef.events;
|
|
23
23
|
export type GameObjectSlots = typeof __propDef.slots;
|
|
24
|
-
export default class GameObject extends
|
|
24
|
+
export default class GameObject extends SvelteComponent<GameObjectProps, GameObjectEvents, GameObjectSlots> {
|
|
25
25
|
}
|
|
26
26
|
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
<script>import { setupLayer, getGame } from "./core-contexts.js";
|
|
2
|
+
import { onMount } from "svelte";
|
|
3
|
+
export let zIndex = 0;
|
|
4
|
+
export let staticMode = false;
|
|
5
|
+
let shouldRenderNextFrame = true;
|
|
6
|
+
let canvas;
|
|
7
|
+
$:
|
|
8
|
+
ctx = typeof canvas !== "undefined" ? canvas.getContext("2d") : null;
|
|
9
|
+
let targets = /* @__PURE__ */ new Set();
|
|
10
|
+
const { width, height } = getGame();
|
|
11
|
+
const draw = () => {
|
|
12
|
+
if (ctx === null)
|
|
13
|
+
return;
|
|
14
|
+
ctx.clearRect(0, 0, $width, $height);
|
|
15
|
+
targets.forEach((f) => f.draw({ width: $width, height: $height, ctx }));
|
|
16
|
+
};
|
|
17
|
+
let register = setupLayer({
|
|
18
|
+
assign: (ctx2) => {
|
|
19
|
+
targets.add(ctx2);
|
|
20
|
+
return () => {
|
|
21
|
+
targets.delete(ctx2);
|
|
22
|
+
};
|
|
23
|
+
},
|
|
24
|
+
requestFrame: () => {
|
|
25
|
+
if (!staticMode)
|
|
26
|
+
return;
|
|
27
|
+
shouldRenderNextFrame = true;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
export let name;
|
|
31
|
+
onMount(() => {
|
|
32
|
+
return register({
|
|
33
|
+
draw,
|
|
34
|
+
isStatic: () => {
|
|
35
|
+
if (shouldRenderNextFrame) {
|
|
36
|
+
shouldRenderNextFrame = false;
|
|
37
|
+
draw();
|
|
38
|
+
}
|
|
39
|
+
return staticMode;
|
|
40
|
+
},
|
|
41
|
+
name
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
</script>
|
|
45
|
+
|
|
46
|
+
<canvas width={$width} height={$height} bind:this={canvas} style:z-index={zIndex}></canvas>
|
|
47
|
+
<slot />
|
|
48
|
+
|
|
49
|
+
<style>
|
|
50
|
+
canvas {
|
|
51
|
+
position: absolute;
|
|
52
|
+
top: 0;
|
|
53
|
+
left: 0;
|
|
54
|
+
width: 100%;
|
|
55
|
+
height: 100%;
|
|
56
|
+
object-fit: contain;
|
|
57
|
+
}
|
|
58
|
+
</style>
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SvelteComponent } from "svelte";
|
|
2
2
|
declare const __propDef: {
|
|
3
3
|
props: {
|
|
4
4
|
zIndex?: number | undefined;
|
|
5
5
|
staticMode?: boolean | undefined;
|
|
6
|
+
name: string;
|
|
6
7
|
};
|
|
7
8
|
events: {
|
|
8
9
|
[evt: string]: CustomEvent<any>;
|
|
@@ -14,6 +15,6 @@ declare const __propDef: {
|
|
|
14
15
|
export type LayerProps = typeof __propDef.props;
|
|
15
16
|
export type LayerEvents = typeof __propDef.events;
|
|
16
17
|
export type LayerSlots = typeof __propDef.slots;
|
|
17
|
-
export default class Layer extends
|
|
18
|
+
export default class Layer extends SvelteComponent<LayerProps, LayerEvents, LayerSlots> {
|
|
18
19
|
}
|
|
19
20
|
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
<script>import { getGame } from "../core-contexts.js";
|
|
2
|
+
import { setupDrawable } from "../drawable.js";
|
|
3
|
+
import { onMount, createEventDispatcher } from "svelte";
|
|
4
|
+
import { MOUSE_ACTION } from "./mouse.js";
|
|
5
|
+
let tx = 0;
|
|
6
|
+
let ty = 0;
|
|
7
|
+
let tw = 0;
|
|
8
|
+
let th = 0;
|
|
9
|
+
let dispatch = createEventDispatcher();
|
|
10
|
+
let context = getGame();
|
|
11
|
+
const draw = function(_info, { x, y, w, h }) {
|
|
12
|
+
tx = x;
|
|
13
|
+
ty = y;
|
|
14
|
+
tw = w;
|
|
15
|
+
th = h;
|
|
16
|
+
};
|
|
17
|
+
let register = setupDrawable({});
|
|
18
|
+
onMount(() => {
|
|
19
|
+
let event = context.onMouseEvent(null, MOUSE_ACTION.DOWN, ({ key }) => {
|
|
20
|
+
let x = context.getMousePosition("mouse_x");
|
|
21
|
+
if (x < tx || x > tx + tw)
|
|
22
|
+
return;
|
|
23
|
+
let y = context.getMousePosition("mouse_y");
|
|
24
|
+
if (y < ty || y > ty + th)
|
|
25
|
+
return;
|
|
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>
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SvelteComponent } from "svelte";
|
|
2
2
|
declare const __propDef: {
|
|
3
3
|
props: Record<string, never>;
|
|
4
4
|
events: {
|
|
@@ -16,6 +16,6 @@ declare const __propDef: {
|
|
|
16
16
|
export type MouseClickableProps = typeof __propDef.props;
|
|
17
17
|
export type MouseClickableEvents = typeof __propDef.events;
|
|
18
18
|
export type MouseClickableSlots = typeof __propDef.slots;
|
|
19
|
-
export default class MouseClickable extends
|
|
19
|
+
export default class MouseClickable extends SvelteComponent<MouseClickableProps, MouseClickableEvents, MouseClickableSlots> {
|
|
20
20
|
}
|
|
21
21
|
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
<script>import { getGame } from "../core-contexts.js";
|
|
2
|
+
import { setupDrawable } from "../drawable.js";
|
|
3
|
+
import { onMount, createEventDispatcher } from "svelte";
|
|
4
|
+
import { MOUSE_ACTION } from "./mouse.js";
|
|
5
|
+
let tx = 0;
|
|
6
|
+
let ty = 0;
|
|
7
|
+
let tw = 0;
|
|
8
|
+
let th = 0;
|
|
9
|
+
let dispatch = createEventDispatcher();
|
|
10
|
+
let context = getGame();
|
|
11
|
+
let mouseX = context.getMouseStore("mouse_x");
|
|
12
|
+
let mouseY = context.getMouseStore("mouse_y");
|
|
13
|
+
export let hover = false;
|
|
14
|
+
const draw = function(_info, { x, y, w, h }) {
|
|
15
|
+
tx = x;
|
|
16
|
+
ty = y;
|
|
17
|
+
tw = w;
|
|
18
|
+
th = h;
|
|
19
|
+
if ($mouseX < tx || $mouseX > tx + tw)
|
|
20
|
+
return hover = false;
|
|
21
|
+
if ($mouseY < ty || $mouseY > ty + th)
|
|
22
|
+
return hover = false;
|
|
23
|
+
hover = true;
|
|
24
|
+
};
|
|
25
|
+
let register = setupDrawable({});
|
|
26
|
+
onMount(() => {
|
|
27
|
+
let event = context.onMouseEvent(null, MOUSE_ACTION.DOWN, ({ key }) => {
|
|
28
|
+
if ($mouseX < tx || $mouseX > tx + tw)
|
|
29
|
+
return;
|
|
30
|
+
if ($mouseY < ty || $mouseY > ty + th)
|
|
31
|
+
return;
|
|
32
|
+
dispatch("click");
|
|
33
|
+
switch (key) {
|
|
34
|
+
case "mouse_left": {
|
|
35
|
+
dispatch("left");
|
|
36
|
+
dispatch("leftorright");
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
case "mouse_right": {
|
|
40
|
+
dispatch("right");
|
|
41
|
+
dispatch("leftorright");
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
case "mouse_middle": {
|
|
45
|
+
dispatch("middle");
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
dispatch("other");
|
|
49
|
+
});
|
|
50
|
+
let deregister = register({ draw });
|
|
51
|
+
return () => {
|
|
52
|
+
event();
|
|
53
|
+
deregister();
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
</script>
|
|
57
|
+
|
|
58
|
+
<slot {hover} />
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SvelteComponent } from "svelte";
|
|
2
2
|
declare const __propDef: {
|
|
3
3
|
props: {
|
|
4
4
|
hover?: boolean | undefined;
|
|
@@ -13,11 +13,15 @@ declare const __propDef: {
|
|
|
13
13
|
} & {
|
|
14
14
|
[evt: string]: CustomEvent<any>;
|
|
15
15
|
};
|
|
16
|
-
slots: {
|
|
16
|
+
slots: {
|
|
17
|
+
default: {
|
|
18
|
+
hover: boolean;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
17
21
|
};
|
|
18
22
|
export type MouseEventAreaProps = typeof __propDef.props;
|
|
19
23
|
export type MouseEventAreaEvents = typeof __propDef.events;
|
|
20
24
|
export type MouseEventAreaSlots = typeof __propDef.slots;
|
|
21
|
-
export default class MouseEventArea extends
|
|
25
|
+
export default class MouseEventArea extends SvelteComponent<MouseEventAreaProps, MouseEventAreaEvents, MouseEventAreaSlots> {
|
|
22
26
|
}
|
|
23
27
|
export {};
|
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
import type { Writable } from "svelte/store";
|
|
2
|
-
export declare enum
|
|
2
|
+
export declare enum KEYBOARD_ACTION {
|
|
3
3
|
DOWN = "down",
|
|
4
4
|
UP = "up",
|
|
5
5
|
PRESS = "press"
|
|
6
6
|
}
|
|
7
|
-
export declare class
|
|
7
|
+
export declare class Keyboard {
|
|
8
8
|
constructor();
|
|
9
9
|
events: Set<{
|
|
10
10
|
key: string | null;
|
|
11
|
-
action:
|
|
11
|
+
action: KEYBOARD_ACTION;
|
|
12
12
|
call: (e: {
|
|
13
13
|
key: string | null;
|
|
14
|
-
action:
|
|
14
|
+
action: KEYBOARD_ACTION;
|
|
15
15
|
}) => any | void;
|
|
16
16
|
}>;
|
|
17
17
|
keyState: Map<string, boolean>;
|
|
18
18
|
keyStores: Map<string, Writable<boolean>>;
|
|
19
19
|
start(): void;
|
|
20
|
-
on(key: string | null, action:
|
|
20
|
+
on(key: string | null, action: KEYBOARD_ACTION, callback: (e: {
|
|
21
21
|
key: string | null;
|
|
22
|
-
action:
|
|
22
|
+
action: KEYBOARD_ACTION;
|
|
23
23
|
}) => any | void): () => any;
|
|
24
24
|
getStore(key: string): Writable<boolean>;
|
|
25
25
|
isPressed(key: string): boolean;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { writable } from "svelte/store";
|
|
2
|
-
export var
|
|
3
|
-
(function (
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
})(
|
|
8
|
-
export class
|
|
2
|
+
export var KEYBOARD_ACTION;
|
|
3
|
+
(function (KEYBOARD_ACTION) {
|
|
4
|
+
KEYBOARD_ACTION["DOWN"] = "down";
|
|
5
|
+
KEYBOARD_ACTION["UP"] = "up";
|
|
6
|
+
KEYBOARD_ACTION["PRESS"] = "press";
|
|
7
|
+
})(KEYBOARD_ACTION || (KEYBOARD_ACTION = {}));
|
|
8
|
+
export class Keyboard {
|
|
9
9
|
constructor() {
|
|
10
10
|
this.events = new Set();
|
|
11
11
|
this.keyState = new Map();
|
|
@@ -19,7 +19,7 @@ export class Controller {
|
|
|
19
19
|
let eklc = e.key.toLowerCase();
|
|
20
20
|
this.keyState.set(eklc, true);
|
|
21
21
|
this.events.forEach(({ key, action, call }) => {
|
|
22
|
-
if ((key === eklc || key === null) && action ===
|
|
22
|
+
if ((key === eklc || key === null) && action === KEYBOARD_ACTION.DOWN)
|
|
23
23
|
call({ key: eklc, action });
|
|
24
24
|
});
|
|
25
25
|
if (this.keyStores.has(eklc)) {
|
|
@@ -30,7 +30,7 @@ export class Controller {
|
|
|
30
30
|
let eklc = e.key.toLowerCase();
|
|
31
31
|
this.keyState.set(eklc, false);
|
|
32
32
|
this.events.forEach(({ key, action, call }) => {
|
|
33
|
-
if ((key === eklc || key === null) && action ===
|
|
33
|
+
if ((key === eklc || key === null) && action === KEYBOARD_ACTION.UP)
|
|
34
34
|
call({ key: eklc, action });
|
|
35
35
|
});
|
|
36
36
|
if (this.keyStores.has(eklc)) {
|
|
@@ -5,7 +5,7 @@ export declare enum MOUSE_ACTION {
|
|
|
5
5
|
MOVE = "move"
|
|
6
6
|
}
|
|
7
7
|
type MouseEventBoolean = "mouse_left" | "mouse_middle" | "mouse_right";
|
|
8
|
-
type MouseEventNumber = "mouse_x" | "mouse_y";
|
|
8
|
+
type MouseEventNumber = "mouse_x" | "mouse_y" | "mouse_scroll_y" | "mouse_scroll_x";
|
|
9
9
|
export declare class Mouse {
|
|
10
10
|
constructor();
|
|
11
11
|
events: Set<{
|
|
@@ -20,6 +20,8 @@ export declare class Mouse {
|
|
|
20
20
|
mouseStores: Map<MouseEventBoolean | MouseEventNumber, Writable<boolean | number>>;
|
|
21
21
|
innerWidth: number;
|
|
22
22
|
innerHeight: number;
|
|
23
|
+
drawWidth: number;
|
|
24
|
+
drawHeight: number;
|
|
23
25
|
start(): void;
|
|
24
26
|
on(key: MouseEventBoolean | MouseEventNumber | null, action: MOUSE_ACTION, callback: (e: {
|
|
25
27
|
key: string | null;
|
|
@@ -29,5 +31,7 @@ export declare class Mouse {
|
|
|
29
31
|
isPressed(key: MouseEventBoolean): boolean;
|
|
30
32
|
getPosition(key: MouseEventNumber): number;
|
|
31
33
|
changeWindowDimensions(width: number, height: number): void;
|
|
34
|
+
setWidth(width: number): void;
|
|
35
|
+
setHeight(height: number): void;
|
|
32
36
|
}
|
|
33
37
|
export {};
|
|
@@ -3,8 +3,8 @@ export var MOUSE_ACTION;
|
|
|
3
3
|
(function (MOUSE_ACTION) {
|
|
4
4
|
MOUSE_ACTION["DOWN"] = "down";
|
|
5
5
|
MOUSE_ACTION["UP"] = "up";
|
|
6
|
-
// CLICK = "click",
|
|
7
6
|
MOUSE_ACTION["MOVE"] = "move";
|
|
7
|
+
// CLICK = "click",
|
|
8
8
|
})(MOUSE_ACTION || (MOUSE_ACTION = {}));
|
|
9
9
|
export class Mouse {
|
|
10
10
|
constructor() {
|
|
@@ -17,6 +17,8 @@ export class Mouse {
|
|
|
17
17
|
mouseStores;
|
|
18
18
|
innerWidth = 0;
|
|
19
19
|
innerHeight = 0;
|
|
20
|
+
drawWidth = 1920;
|
|
21
|
+
drawHeight = 1080;
|
|
20
22
|
start() {
|
|
21
23
|
window.addEventListener("pointerdown", (e) => {
|
|
22
24
|
e.preventDefault();
|
|
@@ -79,15 +81,15 @@ export class Mouse {
|
|
|
79
81
|
let adjustedY;
|
|
80
82
|
if (this.innerWidth / this.innerHeight > 16 / 9) {
|
|
81
83
|
// TODO: De Hard-code this
|
|
82
|
-
const scaleFactor =
|
|
83
|
-
const realCanvasSize =
|
|
84
|
+
const scaleFactor = this.drawHeight / this.innerHeight;
|
|
85
|
+
const realCanvasSize = this.drawWidth / scaleFactor;
|
|
84
86
|
adjustedY = rawY * scaleFactor;
|
|
85
87
|
adjustedX = (rawX - ((this.innerWidth - realCanvasSize) / 2)) * scaleFactor;
|
|
86
88
|
}
|
|
87
89
|
else {
|
|
88
90
|
// TODO: De Hard-code this
|
|
89
|
-
const scaleFactor =
|
|
90
|
-
const realCanvasSize =
|
|
91
|
+
const scaleFactor = this.drawWidth / this.innerWidth;
|
|
92
|
+
const realCanvasSize = this.drawHeight / scaleFactor;
|
|
91
93
|
adjustedX = rawX * scaleFactor;
|
|
92
94
|
adjustedY = (rawY - ((this.innerHeight - realCanvasSize) / 2)) * scaleFactor;
|
|
93
95
|
}
|
|
@@ -106,6 +108,16 @@ export class Mouse {
|
|
|
106
108
|
this.mouseStores.get("mouse_y")?.set(adjustedY);
|
|
107
109
|
}
|
|
108
110
|
});
|
|
111
|
+
window.addEventListener("wheel", (e) => {
|
|
112
|
+
let rawX = e.deltaX;
|
|
113
|
+
let rawY = e.deltaY;
|
|
114
|
+
this.events.forEach(({ key, action, call }) => {
|
|
115
|
+
if ((key === "mouse_scroll_x" || key === null) && action === MOUSE_ACTION.MOVE && Math.abs(rawX) > 0)
|
|
116
|
+
call({ key: "mouse_scroll_x", action });
|
|
117
|
+
if ((key === "mouse_scroll_y" || key === null) && action === MOUSE_ACTION.MOVE && Math.abs(rawY) > 0)
|
|
118
|
+
call({ key: "mouse_scroll_y", action });
|
|
119
|
+
});
|
|
120
|
+
});
|
|
109
121
|
window.addEventListener("contextmenu", (e) => {
|
|
110
122
|
e.preventDefault();
|
|
111
123
|
});
|
|
@@ -121,7 +133,7 @@ export class Mouse {
|
|
|
121
133
|
if (this.mouseStores.has(key)) {
|
|
122
134
|
return this.mouseStores.get(key);
|
|
123
135
|
}
|
|
124
|
-
if (key === "mouse_x" || key === "mouse_y") {
|
|
136
|
+
if (key === "mouse_x" || key === "mouse_y" || key === "mouse_scroll_x" || key === "mouse_scroll_y") {
|
|
125
137
|
let wr = writable(this.mouseState.get(key) ?? 0);
|
|
126
138
|
this.mouseStores.set(key, wr);
|
|
127
139
|
return wr;
|
|
@@ -140,4 +152,10 @@ export class Mouse {
|
|
|
140
152
|
this.innerHeight = height;
|
|
141
153
|
this.innerWidth = width;
|
|
142
154
|
}
|
|
155
|
+
setWidth(width) {
|
|
156
|
+
this.drawWidth = width;
|
|
157
|
+
}
|
|
158
|
+
setHeight(height) {
|
|
159
|
+
this.drawHeight = height;
|
|
160
|
+
}
|
|
143
161
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Writable } from 'svelte/store';
|
|
2
|
+
import type { Timing } from "./controllers/motions";
|
|
3
|
+
import type { Keyboard } from "./controllers/keyboard";
|
|
4
|
+
import type { Mouse } from "./controllers/mouse";
|
|
5
|
+
import { type DrawableContext } from './drawable';
|
|
6
|
+
export type DestroyFunction = () => any;
|
|
7
|
+
export type RegisterFunction<T> = (run: T) => DestroyFunction;
|
|
8
|
+
export type GameContext = {
|
|
9
|
+
assign: (ctx: LayerContext, obj: LayerDrawable) => DestroyFunction;
|
|
10
|
+
width: Writable<number>;
|
|
11
|
+
height: Writable<number>;
|
|
12
|
+
background: Writable<string>;
|
|
13
|
+
createTimer: Timing["createTimer"];
|
|
14
|
+
createBurst: Timing["createBurst"];
|
|
15
|
+
onKeyboardEvent: Keyboard["on"];
|
|
16
|
+
isKeyboardPressed: Keyboard["isPressed"];
|
|
17
|
+
getKeyboardStore: Keyboard["getStore"];
|
|
18
|
+
onMouseEvent: Mouse["on"];
|
|
19
|
+
isMousePressed: Mouse["isPressed"];
|
|
20
|
+
getMousePosition: Mouse["getPosition"];
|
|
21
|
+
getMouseStore: Mouse["getStore"];
|
|
22
|
+
onFrame: (callback: (info: {
|
|
23
|
+
delta: number;
|
|
24
|
+
time: number;
|
|
25
|
+
}) => any | void) => () => any;
|
|
26
|
+
onBeforeFrame: (callback: (info: {
|
|
27
|
+
delta: number;
|
|
28
|
+
time: number;
|
|
29
|
+
}) => any | void) => () => any;
|
|
30
|
+
onAfterFrame: (callback: (info: {
|
|
31
|
+
delta: number;
|
|
32
|
+
time: number;
|
|
33
|
+
}) => any | void) => () => any;
|
|
34
|
+
getLayerByName: (name: string) => LayerContext | null;
|
|
35
|
+
};
|
|
36
|
+
export declare const setupGame: (context: GameContext) => void;
|
|
37
|
+
export declare const getGame: () => GameContext;
|
|
38
|
+
export type LayerContext = Required<DrawableContext<null>> & {
|
|
39
|
+
requestFrame: (...optional: any[]) => any;
|
|
40
|
+
};
|
|
41
|
+
export type LayerDrawable = {
|
|
42
|
+
draw: () => any | void;
|
|
43
|
+
isStatic: () => boolean;
|
|
44
|
+
name: string;
|
|
45
|
+
};
|
|
46
|
+
export declare const setupLayer: (context: LayerContext) => RegisterFunction<LayerDrawable>;
|
|
47
|
+
export declare const getLayer: () => LayerContext;
|
|
48
|
+
export declare const getTriggerLayerRender: () => (...optional: any[]) => any;
|