1 #include "DirectCLIFeedback.hpp"
2 #include "MasterState.hpp"
3 #include "PreloadState.hpp"
4 #include "UnloadState.hpp"
6 #include "../app/Config.hpp"
7 #include "../app/Environment.hpp"
8 #include "../app/init.hpp"
9 #include "../geometry/distance.hpp"
10 #include "../io/WorldSave.hpp"
11 #include "../world/ChunkLoader.hpp"
12 #include "../world/ChunkRenderer.hpp"
18 namespace standalone {
20 DirectCLIFeedback::DirectCLIFeedback(Player &p, HUD &h)
26 void DirectCLIFeedback::Error(const std::string &msg) {
30 void DirectCLIFeedback::Message(const std::string &msg) {
34 void DirectCLIFeedback::Broadcast(const std::string &msg) {
39 MasterState::MasterState(
42 const Generator::Config &gc,
43 const World::Config &wc,
51 , world(res.block_types, wc)
52 , spawn_index(world.Chunks().MakeIndex(wc.spawn, 3))
53 , player(*world.AddPlayer(config.player.name))
55 , hud(env, config, player)
56 , manip(env.audio, sounds, player.GetEntity())
57 , input(world, player, manip)
58 , interface(config, env.keymap, input, *this)
60 , chunk_loader(world.Chunks(), generator, save)
61 , chunk_renderer(player.GetChunks())
62 , spawner(world, res.models)
63 , sky(env.loader.LoadCubeMap("skybox"))
65 , cli_ctx(player, hud)
66 , preload(env, chunk_loader, chunk_renderer)
67 , unload(env, world.Chunks(), save)
68 , chat(env, *this, *this) {
69 res.Load(env.loader, "default");
70 if (res.models.size() < 2) {
71 throw std::runtime_error("need at least two models to run");
73 res.models[0].Instantiate(player.GetEntity().GetModel());
74 sounds.Load(env.loader, res.snd_index);
75 spawner.LimitModels(1, res.models.size());
76 interface.SetInventorySlots(res.block_types.size() - 1);
77 generator.LoadTypes(res.block_types);
78 chunk_renderer.LoadTextures(env.loader, res.tex_index);
79 chunk_renderer.FogDensity(wc.fog_density);
80 if (save.Exists(player)) {
87 MasterState::~MasterState() {
88 world.Chunks().UnregisterIndex(spawn_index);
92 void MasterState::OnResume() {
93 if (spawn_index.MissingChunks() > 0) {
94 env.state.Push(&preload);
101 hud.KeepMessages(false);
105 void MasterState::OnPause() {
109 void MasterState::OnFocus() {
110 if (config.input.mouse) {
111 env.window.GrabMouse();
116 void MasterState::OnBlur() {
117 env.window.ReleaseMouse();
122 void MasterState::Handle(const SDL_Event &event) {
123 switch (event.type) {
125 // TODO: move to interface
126 if (event.key.keysym.sym == SDLK_RETURN) {
128 env.state.Push(&chat);
129 hud.KeepMessages(true);
130 } else if (event.key.keysym.sym == SDLK_SLASH) {
132 env.state.Push(&chat);
133 hud.KeepMessages(true);
135 interface.HandlePress(event.key);
139 interface.HandleRelease(event.key);
141 case SDL_MOUSEBUTTONDOWN:
142 interface.HandlePress(event.button);
144 case SDL_MOUSEBUTTONUP:
145 interface.HandleRelease(event.button);
147 case SDL_MOUSEMOTION:
148 interface.Handle(event.motion);
151 interface.Handle(event.wheel);
161 void MasterState::Update(int dt) {
164 if (input.BlockFocus()) {
165 hud.FocusBlock(input.BlockFocus().GetChunk(), input.BlockFocus().block);
166 } else if (input.EntityFocus()) {
167 hud.FocusEntity(input.EntityFocus().GetEntity());
171 hud.Display(res.block_types[player.GetInventorySlot() + 1]);
173 chunk_loader.Update(dt);
174 chunk_renderer.Update(dt);
176 glm::mat4 trans = player.GetEntity().Transform(player.GetEntity().ChunkCoords());
177 glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
178 glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f));
179 env.audio.Position(player.GetEntity().Position());
180 env.audio.Velocity(player.GetEntity().Velocity());
181 env.audio.Orientation(dir, up);
184 void MasterState::Render(Viewport &viewport) {
185 viewport.WorldPosition(player.GetEntity().ViewTransform(player.GetEntity().ChunkCoords()));
186 if (config.video.world) {
187 chunk_renderer.Render(viewport);
188 world.Render(viewport);
189 if (config.video.debug) {
190 world.RenderDebug(viewport);
192 sky.Render(viewport);
194 hud.Render(viewport);
198 void MasterState::SetAudio(bool b) {
199 config.audio.enabled = b;
201 hud.PostMessage("Audio enabled");
203 hud.PostMessage("Audio disabled");
207 void MasterState::SetVideo(bool b) {
208 config.video.world = b;
210 hud.PostMessage("World rendering enabled");
212 hud.PostMessage("World rendering disabled");
216 void MasterState::SetHUD(bool b) {
217 config.video.hud = b;
219 hud.PostMessage("HUD rendering enabled");
221 hud.PostMessage("HUD rendering disabled");
225 void MasterState::SetDebug(bool b) {
226 config.video.debug = b;
228 hud.PostMessage("Debug rendering enabled");
230 hud.PostMessage("Debug rendering disabled");
234 void MasterState::NextCamera() {
235 if (iszero(env.viewport.CameraOffset())) {
236 env.viewport.OffsetCamera(glm::vec3(0.0f, 0.0f, -5.0f));
238 env.viewport.OffsetCamera(glm::vec3(0.0f, 0.0f, 0.0f));
242 void MasterState::Exit() {
244 env.state.Switch(&unload);
247 void MasterState::OnLineSubmit(const std::string &line) {
251 if (line[0] == '/' && line.size() > 1 && line[1] != '/') {
252 cli.Execute(cli_ctx, line.substr(1));
254 hud.PostMessage(line);
259 PreloadState::PreloadState(Environment &env, ChunkLoader &loader, ChunkRenderer &render)
260 : ProgressState(env, "Preloading chunks: %d/%d (%d%%)")
264 , total(loader.ToLoad())
269 void PreloadState::Update(int dt) {
270 loader.LoadN(per_update);
271 if (loader.ToLoad() <= 0) {
273 render.Update(render.MissingChunks());
275 SetProgress(total - loader.ToLoad(), total);
280 UnloadState::UnloadState(
283 const WorldSave &save)
284 : ProgressState(env, "Unloading chunks: %d/%d (%d%%)")
288 , cur(chunks.begin())
291 , total(chunks.NumLoaded())
297 void UnloadState::OnResume() {
298 cur = chunks.begin();
301 total = chunks.NumLoaded();
305 void UnloadState::Handle(const SDL_Event &) {
309 void UnloadState::Update(int dt) {
310 for (std::size_t i = 0; i < per_update && cur != end; ++i, ++cur, ++done) {
311 if (cur->ShouldUpdateSave()) {
318 SetProgress(done, total);