Skip to content

Commit ca067a2

Browse files
committed
fix: corrected transitions between *play/pause/stop* scene states
1 parent 1e91551 commit ca067a2

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/Runtime/Scene.cc

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace volt::runtime {
1414
{}
1515

1616
Scene::Scene(const std::string &name)
17-
: m_name{name}, m_running{false}
17+
: m_name{name}, m_running{false}, m_paused{false}
1818
{}
1919

2020
Scene::~Scene(void) {
@@ -60,9 +60,11 @@ namespace volt::runtime {
6060
void Scene::Play(void) noexcept {
6161
if (not m_running) m_running = true;
6262
// Box2D physics world creation
63-
b2WorldDef worldDef = b2DefaultWorldDef();
64-
worldDef.gravity = {0.0f, 9.81f};
65-
m_worldID = b2CreateWorld(&worldDef);
63+
if (not m_paused) {
64+
b2WorldDef worldDef = b2DefaultWorldDef();
65+
worldDef.gravity = {0.0f, 9.81f};
66+
m_worldID = b2CreateWorld(&worldDef);
67+
}
6668
// Box2D body creation of all entities with Rigidbody2D
6769
ForAll<TransformComponent, Rigidbody2DComponent, renderer::SpriteRendererComponent>([this](auto e, auto &t, auto &rb, auto &sr) {
6870
rb.SetExtent({sr.scale * sr.sprite.width() * 0.5f, sr.scale * sr.sprite.height() * 0.5f});
@@ -76,20 +78,23 @@ namespace volt::runtime {
7678
b2Polygon polygon { b2MakeBox(rb.GetExtent().x, rb.GetExtent().y) };
7779
b2ShapeDef shapeDef { b2DefaultShapeDef() };
7880
b2CreatePolygonShape(rb.GetBodyID(), &shapeDef, &polygon);
79-
// Save initial position and rotation
80-
initial_positions[e.GetID()] = t.position;
81-
initial_rotations[e.GetID()] = t.rotation;
81+
// Save initial position and rotation (if wasn't in paused state, to not overwrite actual initial values)
82+
if (not m_paused) {
83+
initial_positions[e.GetID()] = t.position;
84+
initial_rotations[e.GetID()] = t.rotation;
85+
}
8286
});
87+
if (m_paused) m_paused = false;
8388
}
8489

8590
void Scene::Pause(void) noexcept {
8691
if (m_running) m_running = false;
92+
if (not m_paused) m_paused = true;
8793
}
8894

8995
void Scene::Stop(void) noexcept {
90-
Pause();
91-
// TODO: figure out how to reset/rewind the scene
92-
// Maybe iterate both hashmaps and ...
96+
if (m_running) m_running = false;
97+
if (m_paused) m_paused = false;
9398
for (auto &[id, pos] : initial_positions) {
9499
auto &t { FindEntityByID(id)->GetComponent<TransformComponent>() };
95100
t.position = pos;

src/Runtime/Scene.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ namespace volt::runtime {
3636
friend class Entity;
3737
std::string m_name;
3838
bool m_running;
39+
bool m_paused;
3940
ECS_t m_registry;
4041
b2WorldId m_worldID;
4142
std::unordered_map<core::SnowflakeID::value_type, EID> m_ids;

0 commit comments

Comments
 (0)