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
21 changes: 13 additions & 8 deletions e2e/part-main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ const clientSave: Save = {
path: "@nanoforge-dev/graphics-2d",
},
],
components: [{ name: "ExampleComponent", path: "./components/example.component" }],
components: [
{
name: "ExampleComponent",
path: "./components/example.component",
paramsNames: ["exampleValueName", "exampleNum"],
},
],
systems: [{ name: "exampleSystem", path: "./systems/example.system" }],
entities: [
{
id: "player",
components: [
{
name: "ExampleComponent",
paramsValues: ["test", 5],
},
],
components: {
ExampleComponent: { exampleValueName: "test", exampleNum: 5 },
},
},
],
};
Expand Down Expand Up @@ -212,7 +215,9 @@ describe("part-main schematic", () => {

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].paramsValues[0]");
expect(content).toContain(
'options.editor.save.entities[0].components["ExampleComponent"]["exampleValueName"]',
);
});

it("should import components and systems with relative path to root", () => {
Expand Down
35 changes: 32 additions & 3 deletions src/utils/main/main-functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ describe("generateMain", () => {
path: "@nanoforge/ecs",
},
],
components: [{ name: "Position", path: "./components/position" }],
components: [{ name: "Position", path: "./components/position", paramsNames: ["x", "y"] }],
systems: [{ name: "PhysicsSystem", path: "./systems/physics" }],
entities: [
{
id: "player",
components: [{ name: "Position", paramsValues: [0, 0] }],
components: { Position: { x: 1, y: 2 } },
},
],
};
Expand All @@ -100,7 +100,36 @@ describe("generateMain", () => {
);
expect(result).toContain("const registry = ecs.registry;");
expect(result).toContain("const player = registry.spawnEntity();");
expect(result).toContain("registry.addComponent(player, new Position(0, 0));");
expect(result).toContain("registry.addComponent(player, new Position(1, 2));");
expect(result).toContain("registry.addSystem(PhysicsSystem);");
});

it("should generate entity even in reverse order", () => {
const save: Save = {
libraries: [
{
id: "ecs",
type: SaveLibraryTypeEnum.COMPONENT_SYSTEM,
name: "ECS",
path: "@nanoforge/ecs",
},
],
components: [{ name: "Position", path: "./components/position", paramsNames: ["y", "x"] }],
systems: [{ name: "PhysicsSystem", path: "./systems/physics" }],
entities: [
{
id: "player",
components: { Position: { x: 1, y: 2 } },
},
],
};
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(2, 1));");
expect(result).toContain("registry.addSystem(PhysicsSystem);");
});
});
2 changes: 1 addition & 1 deletion src/utils/main/main-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const generateMain = (options: MainOptions, save: Save) => {
.generateRegistry(save.libraries)

.generateInitFunctionIfNeeded(initFunctions, InitFunctionEnum.BEFORE_REGISTRY_INIT)
.generateEntities(save.entities)
.generateEntities(save.components, save.entities)
.generateSystems(save.systems)
.generateInitFunctionIfNeeded(initFunctions, InitFunctionEnum.AFTER_REGISTRY_INIT)

Expand Down
31 changes: 24 additions & 7 deletions src/utils/main/main.generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,19 +132,36 @@ describe("MainGenerator", () => {

describe("generateEntities", () => {
it("should generate entity spawn and component additions", () => {
const components = [
{
name: "Position",
path: "non",
paramsNames: ["x", "y"],
},
{
name: "Velocity",
path: "non",
paramsNames: ["fast"],
},
];
const entities = [
{
id: "player",
components: [
{ name: "Position", paramsValues: [0, 0] },
{ name: "Velocity", paramsValues: [1] },
],
components: {
Position: {
x: 1,
y: 2,
},
Velocity: {
fast: 3,
},
},
},
];
const result = new MainGenerator().generateEntities(entities).toString();
const result = new MainGenerator().generateEntities(components, entities).toString();
expect(result).toContain("const player = registry.spawnEntity();");
expect(result).toContain("registry.addComponent(player, new Position(0, 0));");
expect(result).toContain("registry.addComponent(player, new Velocity(1));");
expect(result).toContain("registry.addComponent(player, new Position(1, 2));");
expect(result).toContain("registry.addComponent(player, new Velocity(3));");
});
});

Expand Down
30 changes: 20 additions & 10 deletions src/utils/main/main.generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ export class MainGenerator {
return this;
}

generateEntities(entities: SaveEntity[]): MainGenerator {
entities.forEach((entity, index) => this.generateEntity(entity, index));
generateEntities(components: SaveComponent[], entities: SaveEntity[]): MainGenerator {
entities.forEach((entity, index) => this.generateEntity(components, entity, index));
return this;
}

Expand Down Expand Up @@ -159,16 +159,26 @@ export class MainGenerator {
return this;
}

private generateEntity(entity: SaveEntity, entityIndex: number): void {
private generateEntity(
components: SaveComponent[],
entity: SaveEntity,
entityIndex: number,
): void {
this.writeLine(`const ${entity.id} = registry.spawnEntity();`);
entity.components.forEach(({ name, paramsValues: rawParams }, componentIndex) => {
Object.entries(entity.components).forEach(([componentName, rawParams]) => {
const originalsParams = components.find(({ name }) => name === componentName)?.paramsNames;
if (!originalsParams)
throw new Error("Missing component in entity or inexistent saved component");
const params: string[] = !this.editor
? rawParams.map(getStringParam)
: rawParams.map(
(_param, index) =>
`options.editor.save.entities[${entityIndex}].components[${componentIndex}].paramsValues[${index}]`,
);
this.writeLine(`registry.addComponent(${entity.id}, new ${name}(${params.join(", ")}));`);
? Object.values(originalsParams).map((ogParam) => {
return getStringParam(rawParams[ogParam]);
})
: Object.values(originalsParams).map((ogParam) => {
return `options.editor.save.entities[${entityIndex}].components["${componentName}"]["${ogParam}"]`;
});
this.writeLine(
`registry.addComponent(${entity.id}, new ${componentName}(${params.join(", ")}));`,
);
});
this.endSection();
}
Expand Down
6 changes: 2 additions & 4 deletions src/utils/main/save.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface SaveLibrary {
export interface SaveComponent {
name: string;
path: string;
paramsNames: string[];
}

export interface SaveSystem {
Expand All @@ -26,10 +27,7 @@ export interface SaveSystem {

export interface SaveEntity {
id: string;
components: {
name: string;
paramsValues: any[];
}[];
components: Record<string, Record<string, any>>;
}

export interface Save {
Expand Down
Loading