feat: add play / editor mode

This commit is contained in:
2026-10-22 01:25:53 +02:00
parent 3df0f89343
commit e5d37965a1
13 changed files with 304 additions and 22 deletions
+39
View File
@@ -37,6 +37,20 @@ namespace {
std::string GetTypeName() const override { return "TestComponent"; }
};
class CountingComponent final : public Component {
public:
explicit CountingComponent(GameObject& owner) : Component(owner, "CountingComponent") {}
void Update(float) override { ++updates; }
void LateUpdate(float) override { ++lateUpdates; }
void FixedUpdate(float) override { ++fixedUpdates; }
std::string GetTypeName() const override { return "CountingComponent"; }
int updates{0};
int lateUpdates{0};
int fixedUpdates{0};
};
class RemovingListener final : public EventListener {
public:
explicit RemovingListener(std::unique_ptr<RemovingListener>* victim)
@@ -268,6 +282,30 @@ namespace {
SceneManager::GetInstance().Destroy();
}
void testEditModeSkipsUpdates()
{
SceneManager& sceneManager = SceneManager::GetInstance();
Scene& scene = sceneManager.CreateScene("edit mode");
auto* counter = scene.CreateGameObject("counter")->AddComponent<CountingComponent>();
sceneManager.SetPlaying(false);
sceneManager.Update(0.1f);
sceneManager.FixedUpdate(0.1f);
sceneManager.LateUpdate(0.1f);
check(counter->updates == 0 && counter->fixedUpdates == 0 && counter->lateUpdates == 0,
"edit mode must not tick components");
check(scene.GetObjects().size() == 1, "edit mode must still add objects created in the editor");
sceneManager.SetPlaying(true);
sceneManager.Update(0.1f);
sceneManager.FixedUpdate(0.1f);
sceneManager.LateUpdate(0.1f);
check(counter->updates == 1 && counter->fixedUpdates == 1 && counter->lateUpdates == 1,
"play mode must tick components");
sceneManager.Destroy();
}
}
void RunCoreTests()
@@ -280,4 +318,5 @@ void RunCoreTests()
testEngineComponentRegistration();
testSceneCurrentCamera();
testHierarchyMoves();
testEditModeSkipsUpdates();
}
+35
View File
@@ -334,6 +334,40 @@ namespace {
SceneManager::GetInstance().Destroy();
}
void testPlayModeSnapshotRestore()
{
Scene& scene = SceneManager::GetInstance().CreateScene("snapshot");
GameObject* box = scene.CreateGameObject("box");
box->GetTransform().SetLocalPosition({1.f, 2.f, 3.f});
scene.CreateGameObject("other");
scene.CommitPendingAdditions();
scene.SetSelectedObject(box->GetId());
auto snapshot = SceneSerializer::Capture(scene);
check(snapshot.has_value(), "a scene must be capturable without a file");
(*snapshot)["selected"] = box->GetId();
// What play mode does to the scene before Stop restores it
box->GetTransform().SetLocalPosition({9.f, 9.f, 9.f});
scene.CreateGameObject("spawned during play");
scene.CommitPendingAdditions();
check(SceneSerializer::LoadFromJson(scene, *snapshot), "restoring a snapshot must succeed");
scene.CommitPendingAdditions();
check(scene.GetObjects().size() == 2 && findByName(scene, "spawned during play") == nullptr,
"restoring must drop objects created after the snapshot");
const GameObject* restored = findByName(scene, "box");
check(restored != nullptr && close(restored->GetTransform().GetLocalPosition().x, 1.f),
"restoring must bring back the captured transforms");
check(scene.GetSelectedObject() == restored, "the selection must follow the restored object");
check(!SceneSerializer::LoadFromJson(scene, nlohmann::json{{"objects", 5}}),
"an invalid snapshot must be rejected");
check(scene.GetObjects().size() == 2, "a rejected snapshot must leave the scene alone");
SceneManager::GetInstance().Destroy();
}
}
void RunSerializationTests()
@@ -350,4 +384,5 @@ void RunSerializationTests()
testSceneFileIdsAreLocal();
testMaterialJsonRoundTrip();
testSceneValidatesEmbeddedMaterials();
testPlayModeSnapshotRestore();
}