I need to write a component for the scene/entity access >.<
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
"noNonNullAssertion": "off"
|
"noNonNullAssertion": "off"
|
||||||
},
|
},
|
||||||
"suspicious": {
|
"suspicious": {
|
||||||
|
"noTsIgnore": "off",
|
||||||
"noAssignInExpressions": "off"
|
"noAssignInExpressions": "off"
|
||||||
},
|
},
|
||||||
"correctness": {
|
"correctness": {
|
||||||
|
|||||||
+17
-6
@@ -8,11 +8,16 @@ import {
|
|||||||
} from "solid-js";
|
} from "solid-js";
|
||||||
import { FileIcon, FolderIcon, FolderOpenIcon } from "#/dev/icons/index.ts";
|
import { FileIcon, FolderIcon, FolderOpenIcon } from "#/dev/icons/index.ts";
|
||||||
|
|
||||||
export type BrowserFolder = Map<string, string | BrowserFolder>;
|
export type BrowserFile = {
|
||||||
|
path: string;
|
||||||
|
isActive: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type BrowserFolder = Map<string, BrowserFile | BrowserFolder>;
|
||||||
|
|
||||||
export type FileHandler = (path: string) => void;
|
export type FileHandler = (path: string) => void;
|
||||||
|
|
||||||
function isFolder(entry: string | BrowserFolder): entry is BrowserFolder {
|
function isFolder(entry: BrowserFile | BrowserFolder): entry is BrowserFolder {
|
||||||
return typeof (entry as BrowserFolder).size === "number";
|
return typeof (entry as BrowserFolder).size === "number";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,10 +27,16 @@ export const FileBrowser: Component<{
|
|||||||
}> = (props) => {
|
}> = (props) => {
|
||||||
const FileEntry: Component<{
|
const FileEntry: Component<{
|
||||||
name: string;
|
name: string;
|
||||||
path: string;
|
entry: BrowserFile;
|
||||||
}> = ({ name, path }) => {
|
}> = ({ name, entry }) => {
|
||||||
return (
|
return (
|
||||||
<div class="file-label" onClick={[props.handler, path]}>
|
<div
|
||||||
|
class="file-label"
|
||||||
|
classList={{
|
||||||
|
active: entry.isActive,
|
||||||
|
}}
|
||||||
|
onClick={() => props.handler(entry.path)}
|
||||||
|
>
|
||||||
<FileIcon />
|
<FileIcon />
|
||||||
<span>{name}</span>
|
<span>{name}</span>
|
||||||
</div>
|
</div>
|
||||||
@@ -65,7 +76,7 @@ export const FileBrowser: Component<{
|
|||||||
{isFolder(entry) ? (
|
{isFolder(entry) ? (
|
||||||
<FolderEntry folder={entry} name={name} open={index() < 1} />
|
<FolderEntry folder={entry} name={name} open={index() < 1} />
|
||||||
) : (
|
) : (
|
||||||
<FileEntry name={name} path={entry} />
|
<FileEntry name={name} entry={entry} />
|
||||||
)}
|
)}
|
||||||
</li>
|
</li>
|
||||||
);
|
);
|
||||||
|
|||||||
+46
-20
@@ -1,5 +1,5 @@
|
|||||||
import "./main.css";
|
import "./main.css";
|
||||||
import { type Component, createSignal, onMount } from "solid-js";
|
import { type Accessor, type Component, createSignal, onMount } from "solid-js";
|
||||||
import { render } from "solid-js/web";
|
import { render } from "solid-js/web";
|
||||||
import { InputField } from "#/dev/controls/InputField.tsx";
|
import { InputField } from "#/dev/controls/InputField.tsx";
|
||||||
import { RangeSlider } from "#/dev/controls/RangeSlider.tsx";
|
import { RangeSlider } from "#/dev/controls/RangeSlider.tsx";
|
||||||
@@ -19,7 +19,7 @@ async function fetchBrowserData(dir: string): Promise<BrowserFolder> {
|
|||||||
|
|
||||||
const scenes = indexRef.reduce((root, entry) => {
|
const scenes = indexRef.reduce((root, entry) => {
|
||||||
const [name] = entry.substring(1).split("/");
|
const [name] = entry.substring(1).split("/");
|
||||||
root.set(name, name);
|
root.set(name, { path: name, isActive: false });
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
}, new Map() as BrowserFolder);
|
}, new Map() as BrowserFolder);
|
||||||
@@ -28,13 +28,14 @@ async function fetchBrowserData(dir: string): Promise<BrowserFolder> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function mapSceneEntities(sceneData: SceneData): BrowserFolder {
|
function mapSceneEntities(sceneData: SceneData): BrowserFolder {
|
||||||
const sets = {};
|
const sets: Record<string, number> = {};
|
||||||
const entites = sceneData.entities.reduce((root, entiry) => {
|
const entites = sceneData.entities.reduce((root, entiry) => {
|
||||||
sets[entiry.id] = sets[entiry.id] === undefined ? 0 : sets[entiry.id] + 1;
|
sets[entiry.name] =
|
||||||
root.set(
|
sets[entiry.name] === undefined ? 0 : sets[entiry.name] + 1;
|
||||||
`${entiry.id}:${sets[entiry.id]}`,
|
root.set(`${entiry.name}:${sets[entiry.name]}`, {
|
||||||
`${entiry.id}:${sets[entiry.id]}`,
|
path: `${entiry.name}:${sets[entiry.name]}`,
|
||||||
);
|
isActive: false,
|
||||||
|
});
|
||||||
return root;
|
return root;
|
||||||
}, new Map() as BrowserFolder);
|
}, new Map() as BrowserFolder);
|
||||||
|
|
||||||
@@ -43,14 +44,6 @@ function mapSceneEntities(sceneData: SceneData): BrowserFolder {
|
|||||||
|
|
||||||
const scenesRaw = await fetchBrowserData("scenes");
|
const scenesRaw = await fetchBrowserData("scenes");
|
||||||
|
|
||||||
let activeEntity = null;
|
|
||||||
function setActiveEntity(entity: string): void {
|
|
||||||
const [id, index] = entity.split(":");
|
|
||||||
const entities = currentScene.entityInstanceMap.get(id)!.instances;
|
|
||||||
activeEntity = entities[parseInt(index)] as Entity<unknown>;
|
|
||||||
console.log(activeEntity);
|
|
||||||
}
|
|
||||||
|
|
||||||
function adjustGameContainer(gameContainer: HTMLElement, width: number): void {
|
function adjustGameContainer(gameContainer: HTMLElement, width: number): void {
|
||||||
gameContainer.style = `position: relative; top: 0; left: ${width}px; width: ${document.body.offsetWidth - width}px;`;
|
gameContainer.style = `position: relative; top: 0; left: ${width}px; width: ${document.body.offsetWidth - width}px;`;
|
||||||
}
|
}
|
||||||
@@ -82,10 +75,43 @@ const DevTools: Component<{ gameContainer: HTMLElement }> = ({
|
|||||||
mapSceneEntities(currentScene.data),
|
mapSceneEntities(currentScene.data),
|
||||||
);
|
);
|
||||||
|
|
||||||
const [scenes] = createSignal(scenesRaw);
|
const mapActive = (
|
||||||
|
root: string,
|
||||||
|
path: string,
|
||||||
|
accessor: Accessor<BrowserFolder>,
|
||||||
|
): BrowserFolder => {
|
||||||
|
return new Map([
|
||||||
|
[
|
||||||
|
root,
|
||||||
|
(accessor().get(root) as BrowserFolder)
|
||||||
|
.entries()
|
||||||
|
.reduce((acc, [key, value]) => {
|
||||||
|
acc.set(key, {
|
||||||
|
// @ts-ignore: value is BrowserFile
|
||||||
|
path: value.path,
|
||||||
|
isActive: path === key,
|
||||||
|
});
|
||||||
|
|
||||||
const setCurrentScene = async (name: string) => {
|
return acc;
|
||||||
await loadScene(name);
|
}, new Map() as BrowserFolder),
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
};
|
||||||
|
|
||||||
|
let activeEntity = null;
|
||||||
|
const setActiveEntity = (path: string): void => {
|
||||||
|
const [id, index] = path.split(":");
|
||||||
|
const instances = currentScene.entityInstanceMap.get(id)!.instances;
|
||||||
|
activeEntity = instances[parseInt(index)] as Entity<unknown>;
|
||||||
|
setEntities(mapActive("entities", path, entities));
|
||||||
|
console.log(activeEntity);
|
||||||
|
};
|
||||||
|
|
||||||
|
const [scenes, setScenes] = createSignal(scenesRaw);
|
||||||
|
|
||||||
|
const setCurrentScene = async (path: string) => {
|
||||||
|
await loadScene(path);
|
||||||
|
setScenes(mapActive("scenes", path, scenes));
|
||||||
setEntities(mapSceneEntities(currentScene.data));
|
setEntities(mapSceneEntities(currentScene.data));
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -116,7 +142,7 @@ const DevTools: Component<{ gameContainer: HTMLElement }> = ({
|
|||||||
<div class="scene-props">
|
<div class="scene-props">
|
||||||
<InputField
|
<InputField
|
||||||
name="file"
|
name="file"
|
||||||
value={currentScene.data.id}
|
value={currentScene.data.name}
|
||||||
icon={FileIcon()}
|
icon={FileIcon()}
|
||||||
></InputField>
|
></InputField>
|
||||||
<InputField
|
<InputField
|
||||||
|
|||||||
+6
-6
@@ -3,7 +3,7 @@ import { createVector, type Vector } from "#/lib/Vector.ts";
|
|||||||
export type Indirect = never;
|
export type Indirect = never;
|
||||||
|
|
||||||
export type Entity<T> = {
|
export type Entity<T> = {
|
||||||
id: string;
|
name: string;
|
||||||
position: Vector;
|
position: Vector;
|
||||||
} & T;
|
} & T;
|
||||||
|
|
||||||
@@ -20,24 +20,24 @@ export const entityAnimateMap = new Map<string, Animate<Indirect>>();
|
|||||||
export const entityProcessMap = new Map<string, Process<Indirect>>();
|
export const entityProcessMap = new Map<string, Process<Indirect>>();
|
||||||
|
|
||||||
export function useEntity<T extends object>(
|
export function useEntity<T extends object>(
|
||||||
id: string,
|
name: string,
|
||||||
data: T,
|
data: T,
|
||||||
): {
|
): {
|
||||||
animate: (fn: Animate<T>) => void;
|
animate: (fn: Animate<T>) => void;
|
||||||
process: (fn: Process<T>) => void;
|
process: (fn: Process<T>) => void;
|
||||||
} {
|
} {
|
||||||
entityMap.set(id, {
|
entityMap.set(name, {
|
||||||
id,
|
name,
|
||||||
position: createVector(),
|
position: createVector(),
|
||||||
...data,
|
...data,
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
animate(fn) {
|
animate(fn) {
|
||||||
entityAnimateMap.set(id, fn);
|
entityAnimateMap.set(name, fn);
|
||||||
},
|
},
|
||||||
process(fn) {
|
process(fn) {
|
||||||
entityProcessMap.set(id, fn);
|
entityProcessMap.set(name, fn);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-13
@@ -10,7 +10,7 @@ import {
|
|||||||
} from "./Entity.ts";
|
} from "./Entity.ts";
|
||||||
|
|
||||||
export type SceneData = {
|
export type SceneData = {
|
||||||
id: string;
|
name: string;
|
||||||
width: number;
|
width: number;
|
||||||
height: number;
|
height: number;
|
||||||
entities: Array<Entity<unknown>>;
|
entities: Array<Entity<unknown>>;
|
||||||
@@ -42,7 +42,7 @@ const sceneGraph: Graph<string> = new Map();
|
|||||||
|
|
||||||
export const currentScene: Scene = {
|
export const currentScene: Scene = {
|
||||||
data: {
|
data: {
|
||||||
id: "",
|
name: "",
|
||||||
width: 0,
|
width: 0,
|
||||||
height: 0,
|
height: 0,
|
||||||
entities: [],
|
entities: [],
|
||||||
@@ -81,13 +81,13 @@ export function useScene(data: SceneData): {
|
|||||||
) => (id: T[number]) => Promise<void>;
|
) => (id: T[number]) => Promise<void>;
|
||||||
process: (fn: Process) => void;
|
process: (fn: Process) => void;
|
||||||
} {
|
} {
|
||||||
sceneDataMap.set(data.id, data);
|
sceneDataMap.set(data.name, data);
|
||||||
sceneEntiyMap.set(data.id, new Map());
|
sceneEntiyMap.set(data.name, new Map());
|
||||||
|
|
||||||
return {
|
return {
|
||||||
linkScenes(ids) {
|
linkScenes(ids) {
|
||||||
for (const id of ids) {
|
for (const id of ids) {
|
||||||
linkScenes(data.id, id);
|
linkScenes(data.name, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
return async (id) => {
|
return async (id) => {
|
||||||
@@ -95,7 +95,7 @@ export function useScene(data: SceneData): {
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
process(fn) {
|
process(fn) {
|
||||||
sceneProcessMap.set(data.id, (ctx, delta) => {
|
sceneProcessMap.set(data.name, (ctx, delta) => {
|
||||||
animateEntities(ctx, delta);
|
animateEntities(ctx, delta);
|
||||||
processEntities(delta);
|
processEntities(delta);
|
||||||
fn(ctx, delta);
|
fn(ctx, delta);
|
||||||
@@ -128,22 +128,22 @@ export async function loadScene(id: string): Promise<void> {
|
|||||||
|
|
||||||
Promise.all(
|
Promise.all(
|
||||||
data.entities.map(async (entity: Entity<unknown>) => {
|
data.entities.map(async (entity: Entity<unknown>) => {
|
||||||
await import(`#/entities/${entity.id}/index.ts`);
|
await import(`#/entities/${entity.name}/index.ts`);
|
||||||
|
|
||||||
if (!entityInstanceMap.has(entity.id)) {
|
if (!entityInstanceMap.has(entity.name)) {
|
||||||
entityInstanceMap.set(entity.id, {
|
entityInstanceMap.set(entity.name, {
|
||||||
animate: entityAnimateMap.get(entity.id) ?? (() => {}),
|
animate: entityAnimateMap.get(entity.name) ?? (() => {}),
|
||||||
process: entityProcessMap.get(entity.id) ?? (() => {}),
|
process: entityProcessMap.get(entity.name) ?? (() => {}),
|
||||||
instances: [],
|
instances: [],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const instance: Entity<unknown> = {
|
const instance: Entity<unknown> = {
|
||||||
...structuredClone(entityMap.get(entity.id)!),
|
...structuredClone(entityMap.get(entity.name)!),
|
||||||
...structuredClone(entity),
|
...structuredClone(entity),
|
||||||
};
|
};
|
||||||
|
|
||||||
entityInstanceMap.get(entity.id)!.instances.push(instance);
|
entityInstanceMap.get(entity.name)!.instances.push(instance);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
{
|
{
|
||||||
"id": "fooScene",
|
"name": "fooScene",
|
||||||
"width": 80,
|
"width": 80,
|
||||||
"height": 48,
|
"height": 48,
|
||||||
"entities": [
|
"entities": [
|
||||||
{
|
{
|
||||||
"id": "fooEntity",
|
"name": "fooEntity",
|
||||||
"position": {
|
"position": {
|
||||||
"x": 24,
|
"x": 24,
|
||||||
"y": 16
|
"y": 16
|
||||||
|
|||||||
@@ -1,24 +1,24 @@
|
|||||||
{
|
{
|
||||||
"id": "testScene",
|
"name": "testScene",
|
||||||
"width": 80,
|
"width": 80,
|
||||||
"height": 48,
|
"height": 48,
|
||||||
"entities": [
|
"entities": [
|
||||||
{
|
{
|
||||||
"id": "testEntity",
|
"name": "testEntity",
|
||||||
"position": {
|
"position": {
|
||||||
"x": 0,
|
"x": 0,
|
||||||
"y": 0
|
"y": 0
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "fooEntity",
|
"name": "fooEntity",
|
||||||
"position": {
|
"position": {
|
||||||
"x": 16,
|
"x": 16,
|
||||||
"y": 16
|
"y": 16
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "testEntity",
|
"name": "testEntity",
|
||||||
"position": {
|
"position": {
|
||||||
"x": 32,
|
"x": 32,
|
||||||
"y": 32
|
"y": 32
|
||||||
|
|||||||
Reference in New Issue
Block a user