Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/docs/schematics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ The save file is a JSON document with the following structure:
id: string;
components: {
name: string;
params: string[];
paramsValues: any[];
}[];
}[];
}
Expand Down
16 changes: 13 additions & 3 deletions e2e/part-main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@ const clientSave: Save = {
],
components: [{ name: "ExampleComponent", path: "./components/example.component" }],
systems: [{ name: "exampleSystem", path: "./systems/example.system" }],
entities: [{ id: "player", components: [{ name: "ExampleComponent", params: ['"test"', "5"] }] }],
entities: [
{
id: "player",
components: [
{
name: "ExampleComponent",
paramsValues: ["test", 5],
},
],
},
],
};

const serverSave: Save = {
Expand Down Expand Up @@ -200,9 +210,9 @@ describe("part-main schematic", () => {
expect(content).toContain("export async function main(options: IEditorRunOptions)");
});

it("should use entity params from editor save", () => {
it("should use entity paramsValues from editor save", () => {
const content = tree.readContent("/my-app/.nanoforge/editor/client/main.ts");
expect(content).toContain("options.editor.save.entities[0].components[0].params[0]");
expect(content).toContain("options.editor.save.entities[0].components[0].paramsValues[0]");
});

it("should import components and systems with relative path to root", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"components": [
{
"name": "ExampleComponent",
"params": ["\"example\"", "10"]
"paramsValues": ["example", 10]
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"components": [
{
"name": "ExampleComponent",
"params": ["\"example\"", "10"]
"paramsValues": ["example", 10]
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,33 @@ export default ExampleComponent.name;
export const EDITOR_COMPONENT_MANIFEST = {
name: "Example",
description: "Example component description",
params: {
paramA: {
type: "string",
name: "Param A",
description: "Param A description",
example: "Example value",
},
paramB: {
type: "number",
name: "Param B",
description: "Param B description",
example: 3,
},
paramC: {
type: "boolean",
name: "Param C",
description: "Param C description",
example: true,
default: false,
// Not required because it has a default value
optional: true,
},
},
params: [
[
{
type: "string",
name: "paramA",
description: "Param A description",
example: "Example value",
},
],
[
{
type: "number",
name: "paramB",
description: "Param B description",
example: 3,
},
],
[
{
type: "boolean",
name: "paramC",
description: "Param C description",
example: true,
default: false,
// Not required because it has a default value
optional: true,
},
],
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"components": [
{
"name": "ExampleComponent",
"params": ["\"example\"", "10"]
"paramsValues": ["example", 10]
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"components": [
{
"name": "ExampleComponent",
"params": ["\"example\"", "10"]
"paramsValues": ["example", 10]
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,33 @@ export default ExampleComponent.name;
export const EDITOR_COMPONENT_MANIFEST: EditorComponentManifest = {
name: "Example",
description: "Example component description",
params: {
paramA: {
type: "string",
name: "Param A",
description: "Param A description",
example: "Example value",
},
paramB: {
type: "number",
name: "Param B",
description: "Param B description",
example: 3,
},
paramC: {
type: "boolean",
name: "Param C",
description: "Param C description",
example: true,
default: false,
// Not required because it has a default value
optional: true,
},
},
params: [
[
{
type: "string",
name: "paramA",
description: "Param A description",
example: "Example value",
},
],
[
{
type: "number",
name: "paramB",
description: "Param B description",
example: 3,
},
],
[
{
type: "boolean",
name: "paramC",
description: "Param C description",
example: true,
default: false,
// Not required because it has a default value
optional: true,
},
],
],
};
23 changes: 16 additions & 7 deletions src/utils/main/main-functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const emptySave: Save = {
describe("generateMain", () => {
it("should generate a client main with TypeScript types", () => {
const result = generateMain(
{ part: "client", language: "ts", initFunctions: false },
{ part: "client", language: "ts", initFunctions: false, editor: false },
emptySave,
);
expect(result).toContain("NanoforgeFactory.createClient()");
Expand All @@ -23,7 +23,7 @@ describe("generateMain", () => {

it("should generate a server main without types", () => {
const result = generateMain(
{ part: "server", language: "js", initFunctions: false },
{ part: "server", language: "js", initFunctions: false, editor: false },
emptySave,
);
expect(result).toContain("NanoforgeFactory.createServer()");
Expand All @@ -32,7 +32,10 @@ describe("generateMain", () => {
});

it("should include init function imports and calls when enabled", () => {
const result = generateMain({ part: "client", language: "ts", initFunctions: true }, emptySave);
const result = generateMain(
{ part: "client", language: "ts", initFunctions: true, editor: false },
emptySave,
);
expect(result).toContain('import { beforeInit } from "./init/before-init";');
expect(result).toContain("await beforeInit(app);");
expect(result).toContain("await afterInit(app);");
Expand All @@ -44,7 +47,7 @@ describe("generateMain", () => {

it("should not include init functions when disabled", () => {
const result = generateMain(
{ part: "client", language: "ts", initFunctions: false },
{ part: "client", language: "ts", initFunctions: false, editor: false },
emptySave,
);
expect(result).not.toContain("beforeInit");
Expand All @@ -63,7 +66,10 @@ describe("generateMain", () => {
},
],
};
const result = generateMain({ part: "client", language: "ts", initFunctions: false }, save);
const result = generateMain(
{ part: "client", language: "ts", initFunctions: false, editor: false },
save,
);
expect(result).toContain('import { Graphics } from "@nanoforge/graphics";');
expect(result).toContain("const gfx = new Graphics();");
expect(result).toContain("app.useGraphics(gfx);");
Expand All @@ -84,11 +90,14 @@ describe("generateMain", () => {
entities: [
{
id: "player",
components: [{ name: "Position", params: ["0", "0"] }],
components: [{ name: "Position", paramsValues: [0, 0] }],
},
],
};
const result = generateMain({ part: "client", language: "ts", initFunctions: false }, save);
const result = generateMain(
{ part: "client", language: "ts", initFunctions: false, editor: false },
save,
);
expect(result).toContain("const registry = ecs.registry;");
expect(result).toContain("const player = registry.spawnEntity();");
expect(result).toContain("registry.addComponent(player, new Position(0, 0));");
Expand Down
7 changes: 7 additions & 0 deletions src/utils/main/main-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const getStringParam = (value: any): string => {
if (typeof value === "string") return `"${value}"`;
if (typeof value === "number") return value.toString();
if (typeof value === "boolean") return value ? "true" : "false";
if (typeof value === "object") return JSON.stringify(value);
return "undefined";
};
4 changes: 2 additions & 2 deletions src/utils/main/main.generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ describe("MainGenerator", () => {
{
id: "player",
components: [
{ name: "Position", params: ["0", "0"] },
{ name: "Velocity", params: ["1"] },
{ name: "Position", paramsValues: [0, 0] },
{ name: "Velocity", paramsValues: [1] },
],
},
];
Expand Down
11 changes: 6 additions & 5 deletions src/utils/main/main.generator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { InitFunctionEnum } from "@utils/main/enums";
import { joinRelative } from "@utils/path";

import { LIBS_FUNCTIONS_NAME } from "./const";
import { InitFunctionEnum } from "./enums";
import { getStringParam } from "./main-utils";
import {
type SaveComponent,
type SaveEntity,
Expand Down Expand Up @@ -160,12 +161,12 @@ export class MainGenerator {

private generateEntity(entity: SaveEntity, entityIndex: number): void {
this.writeLine(`const ${entity.id} = registry.spawnEntity();`);
entity.components.forEach(({ name, params: rawParams }, componentIndex) => {
const params = !this.editor
? rawParams
entity.components.forEach(({ name, paramsValues: rawParams }, componentIndex) => {
const params: string[] = !this.editor
? rawParams.map(getStringParam)
: rawParams.map(
(_param, index) =>
`options.editor.save.entities[${entityIndex}].components[${componentIndex}].params[${index}]`,
`options.editor.save.entities[${entityIndex}].components[${componentIndex}].paramsValues[${index}]`,
);
this.writeLine(`registry.addComponent(${entity.id}, new ${name}(${params.join(", ")}));`);
});
Expand Down
2 changes: 1 addition & 1 deletion src/utils/main/save.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface SaveEntity {
id: string;
components: {
name: string;
params: string[];
paramsValues: any[];
}[];
}

Expand Down
Loading