aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-15 16:30:35 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-15 16:30:35 +0200
commit3d9a47f0f6dcb6e6e33bf2d2bd34fa903422ac62 (patch)
treec93706980d97f8ccea419e3aed8488903c0ed4bd
parent3f375a193d0f10a8121d568a745a7903c075c568 (diff)
implement forward time jumps
-rw-r--r--BlueTileBehavior.cpp6
-rw-r--r--BlueTileBehavior.h1
-rw-r--r--Museum.cpp20
-rw-r--r--Museum.h3
-rw-r--r--TimeTravelCommand.cpp2
-rw-r--r--docs/class-diag.puml3
6 files changed, 25 insertions, 10 deletions
diff --git a/BlueTileBehavior.cpp b/BlueTileBehavior.cpp
index b0716bc..a2b3422 100644
--- a/BlueTileBehavior.cpp
+++ b/BlueTileBehavior.cpp
@@ -18,7 +18,7 @@ void BlueTileBehavior::step(Artist * artist) {
if (artist->data.vy == 0) dy = 1;
}
-void BlueTileBehavior::update_neighbor(Tile & here, int dx, int dy) {
+static void update_neighbor(Tile & here, int dx, int dy) {
if (dx == 0 && dy == 0) return;
Tile * neighbor = here.get_neighbor(dx, dy);
if (neighbor == &here) return;
@@ -30,8 +30,8 @@ void BlueTileBehavior::update_neighbor(Tile & here, int dx, int dy) {
void BlueTileBehavior::update(Tile & tile) {
if (this->interactions < 1) return;
- this->update_neighbor(tile, this->dx, this->dy);
- this->update_neighbor(tile, -this->dx, -this->dy);
+ update_neighbor(tile, this->dx, this->dy);
+ update_neighbor(tile, -this->dx, -this->dy);
tile.set_type(YellowTileBehavior::type);
}
diff --git a/BlueTileBehavior.h b/BlueTileBehavior.h
index 3aba86b..15257b3 100644
--- a/BlueTileBehavior.h
+++ b/BlueTileBehavior.h
@@ -16,7 +16,6 @@ private:
BlueTileBehavior() = default;
private:
- virtual void update_neighbor(Tile & here, int dx, int dy);
int dx = 0;
int dy = 0;
};
diff --git a/Museum.cpp b/Museum.cpp
index 0003162..df9144a 100644
--- a/Museum.cpp
+++ b/Museum.cpp
@@ -20,14 +20,30 @@ void Museum::update() {
this->canvas.update();
}
+void Museum::time_jump(long offset) {
+ if (offset >= 0) {
+ this->jump = offset;
+ return;
+ }
+
+
+}
+
void Museum::work() {
while (this->working) {
- if (this->paused) continue; // wait if paused
+ // immediately process forward jumps, even if paused
+ if (this->jump > 0) {
+ while (--this->jump > 0)
+ this->update();
+ }
+
+ // wait with regular update if paused
+ if (this->paused) continue;
+ // regular update
auto next = chrono::steady_clock::now() + this->tick_interval;
this->update();
this_thread::sleep_until(next);
- this->tick++;
}
}
diff --git a/Museum.h b/Museum.h
index 33ad7e1..73183c3 100644
--- a/Museum.h
+++ b/Museum.h
@@ -21,13 +21,14 @@ public:
void set_pause(bool paused);
bool get_pause() { return this->paused; }
void update();
+ void time_jump(long offset);
private:
- unsigned long long tick = 0;
bool paused = false;
private:
static constexpr std::chrono::milliseconds tick_interval = 15ms;
+ unsigned long jump = 0;
bool working = true;
std::thread * worker = nullptr;
void work();
diff --git a/TimeTravelCommand.cpp b/TimeTravelCommand.cpp
index c7dfc82..8a24234 100644
--- a/TimeTravelCommand.cpp
+++ b/TimeTravelCommand.cpp
@@ -11,6 +11,6 @@ void TimeTravelCommand::backwards() {
void TimeTravelCommand::execute(long offset) {
Museum & museum = this->get_museum();
- // TODO
+ museum.time_jump(offset);
}
diff --git a/docs/class-diag.puml b/docs/class-diag.puml
index a502503..cfa954d 100644
--- a/docs/class-diag.puml
+++ b/docs/class-diag.puml
@@ -103,7 +103,7 @@ rectangle Group_Model as "Model" <<group>> {
+ update()
--
- paused : bool <<+get>> <<+set>>
- - tick : unsigned long long
+ - jump : unsigned long
--
- working : bool
- worker : thread *
@@ -216,7 +216,6 @@ rectangle Group_TileBehavior as "Tile behavior" <<group>> {
class BlueTileBehavior {
- type = "B" : <<static constexpr>>
--
- - update_neighbor(here : Tile &, dx, dy)
- dx : int
- dy : int
}