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
6 changes: 2 additions & 4 deletions packages/core-editor/src/editor/core-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,8 @@ export class CoreEditor {
const reg = this.ecsLibrary.registry;
return reg.entityFromIndex(
reg
.getComponents({ name: "__RESERVED_entityId" })
// @todo There is an issue here, getIndex return index from 1 but entityFromIndex get index from 0
// This is a temp fix
.getIndex({ name: "__RESERVED_entityId", entityId: entityId }) - 1,
.getIndexedZipper([{ name: `__RESERVED_entityId` }])
.find(({ __RESERVED_entityId }) => __RESERVED_entityId.entityId === entityId).id,
);
}
}
41 changes: 19 additions & 22 deletions packages/core-editor/test/editor-feature.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type IRunOptions } from "@nanoforge-dev/common";
import { afterEach, describe, expect, it, vi } from "vitest";

import { type ApplicationConfig } from "../../core/src/application/application-config";
import { CoreEvents } from "../src/common/context/events/core-events";
import { CoreEvents } from "../src";
import { type Save, type SaveComponent, type SaveEntity } from "../src/common/context/save.type";
import { type Core } from "../src/core/core";
import { CoreEditor } from "../src/editor/core-editor";
Expand Down Expand Up @@ -35,14 +35,25 @@ describe("EditorFeatures", () => {

describe("askEntitiesHotReload", () => {
it("should reload entities with new save variables", async () => {
const getIndex = vi.fn((component) => {
return Number(component.entityId.slice(-1));
});

const FakeRegistry = vi.fn(
class {
addComponent = vi.fn();
getComponents = vi.fn(() => ({ getIndex }));
getIndexedZipper = vi.fn(() => [
{
id: 2,
__RESERVED_entityId: {
entityId: "ent2",
name: "__RESERVED_entityId",
},
},
{
id: 3,
__RESERVED_entityId: {
entityId: "ent3",
name: "__RESERVED_entityId",
},
},
]);
getEntityComponent = vi.fn((entity: number, component) => {
return (
{
Expand Down Expand Up @@ -75,9 +86,7 @@ describe("EditorFeatures", () => {
)[entity]?.[component.name];
});
entityFromIndex = vi.fn((index) => {
// @todo There is an issue here, see src/editor/core-editor.ts:97
// This is a temp fix
return index + 1;
return index;
});
},
);
Expand Down Expand Up @@ -136,19 +145,7 @@ describe("EditorFeatures", () => {
getComponentSystemLibrary: () => ({ library: { registry: fakeReg } }),
} as unknown as ApplicationConfig,
).hotReloadEvent({ components, entities } as any as Save);
expect(fakeReg.getComponents).toHaveBeenCalledWith({ name: "__RESERVED_entityId" });
expect(getIndex).toHaveBeenNthCalledWith(1, {
entityId: "ent2",
name: "__RESERVED_entityId",
});
expect(getIndex).toHaveBeenNthCalledWith(2, {
entityId: "ent2",
name: "__RESERVED_entityId",
});
expect(getIndex).toHaveBeenNthCalledWith(3, {
entityId: "ent3",
name: "__RESERVED_entityId",
});
expect(fakeReg.getIndexedZipper).toHaveBeenCalledWith([{ name: "__RESERVED_entityId" }]);
expect(fakeReg.getEntityComponent).toHaveBeenNthCalledWith(1, 2, { name: "Position" });
expect(fakeReg.getEntityComponent).toHaveBeenNthCalledWith(2, 2, { name: "Bullets" });
expect(fakeReg.getEntityComponent).toHaveBeenNthCalledWith(3, 3, { name: "Position" });
Expand Down
1 change: 1 addition & 0 deletions packages/ecs-lib/wasm/SparseArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ namespace nfo {
*/
[[nodiscard]] size_type get_index(const_reference_type value) const
{
// @todo This is not how we compare js values so this function is broken for now
auto it = std::find(_data.begin(), _data.end(), value);
if (it != _data.end()) {
return static_cast<size_type>(std::distance(_data.begin(), it));
Expand Down
Loading