]> git.localhorst.tv Git - blank.git/blob - src/app/UnloadState.cpp
fix loop removing players from world
[blank.git] / src / app / UnloadState.cpp
1 #include "UnloadState.hpp"
2
3 #include "Environment.hpp"
4 #include "../io/WorldSave.hpp"
5 #include "../world/ChunkLoader.hpp"
6
7
8 namespace blank {
9
10 UnloadState::UnloadState(
11         Environment &env,
12         ChunkStore &chunks,
13         const WorldSave &save)
14 : env(env)
15 , chunks(chunks)
16 , save(save)
17 , progress(env.assets.large_ui_font)
18 , cur(chunks.begin())
19 , end(chunks.end())
20 , done(0)
21 , total(chunks.NumLoaded())
22 , per_update(64) {
23         progress.Position(glm::vec3(0.0f), Gravity::CENTER);
24         progress.Template("Unloading chunks: %d/%d (%d%%)");
25 }
26
27
28 void UnloadState::OnResume() {
29         cur = chunks.begin();
30         end = chunks.end();
31         done = 0;
32         total = chunks.NumLoaded();
33 }
34
35
36 void UnloadState::Handle(const SDL_Event &) {
37         // ignore everything
38 }
39
40 void UnloadState::Update(int dt) {
41         for (std::size_t i = 0; i < per_update && cur != end; ++i, ++cur, ++done) {
42                 if (cur->ShouldUpdateSave()) {
43                         save.Write(*cur);
44                 }
45         }
46         if (cur == end) {
47                 env.state.PopAll();
48         } else {
49                 progress.Update(done, total);
50         }
51 }
52
53 void UnloadState::Render(Viewport &viewport) {
54         progress.Render(viewport);
55 }
56
57 }