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"
19 namespace standalone {
21 DirectCLIFeedback::DirectCLIFeedback(Player &p, HUD &h)
27 void DirectCLIFeedback::Error(const std::string &msg) {
31 void DirectCLIFeedback::Message(const std::string &msg) {
35 void DirectCLIFeedback::Broadcast(const std::string &msg) {
40 MasterState::MasterState(
43 const Generator::Config &gc,
44 const World::Config &wc,
52 , world(res.block_types, wc)
53 , spawn_index(world.Chunks().MakeIndex(wc.spawn, 3))
54 , player(*world.AddPlayer(config.player.name))
56 , hud(env, config, player)
57 , manip(env.audio, sounds, player.GetEntity())
58 , input(world, player, manip)
59 , interface(config, env.keymap, input, *this)
61 , chunk_loader(world.Chunks(), generator, save)
62 , chunk_renderer(player.GetChunks())
63 , spawner(world, res.models)
64 , sky(env.loader.LoadCubeMap("skybox"))
66 , cli_ctx(player, hud)
67 , preload(env, chunk_loader, chunk_renderer)
68 , unload(env, world.Chunks(), save)
69 , chat(env, *this, *this) {
70 res.Load(env.loader, "default");
71 if (res.models.size() < 2) {
72 throw std::runtime_error("need at least two models to run");
74 res.models[0].Instantiate(player.GetEntity().GetModel());
75 sounds.Load(env.loader, res.snd_index);
76 spawner.LimitModels(1, res.models.size());
77 interface.SetInventorySlots(res.block_types.size() - 1);
78 generator.LoadTypes(res.block_types);
79 chunk_renderer.LoadTextures(env.loader, res.tex_index);
80 chunk_renderer.FogDensity(wc.fog_density);
81 if (save.Exists(player)) {
88 MasterState::~MasterState() {
89 world.Chunks().UnregisterIndex(spawn_index);
93 void MasterState::OnResume() {
94 if (spawn_index.MissingChunks() > 0) {
95 env.state.Push(&preload);
98 std::cout << "chunk preloading complete" << std::endl;
101 spawn_player = false;
103 hud.KeepMessages(false);
107 void MasterState::OnPause() {
111 void MasterState::OnFocus() {
112 if (config.input.mouse) {
113 env.window.GrabMouse();
118 void MasterState::OnBlur() {
119 env.window.ReleaseMouse();
124 void MasterState::Handle(const SDL_Event &event) {
125 switch (event.type) {
127 // TODO: move to interface
128 if (event.key.keysym.sym == SDLK_RETURN) {
130 env.state.Push(&chat);
131 hud.KeepMessages(true);
132 } else if (event.key.keysym.sym == SDLK_SLASH) {
134 env.state.Push(&chat);
135 hud.KeepMessages(true);
137 interface.HandlePress(event.key);
141 interface.HandleRelease(event.key);
143 case SDL_MOUSEBUTTONDOWN:
144 interface.HandlePress(event.button);
146 case SDL_MOUSEBUTTONUP:
147 interface.HandleRelease(event.button);
149 case SDL_MOUSEMOTION:
150 interface.Handle(event.motion);
153 interface.Handle(event.wheel);
163 void MasterState::Update(int dt) {
166 if (input.BlockFocus()) {
167 hud.FocusBlock(input.BlockFocus().GetChunk(), input.BlockFocus().block);
168 } else if (input.EntityFocus()) {
169 hud.FocusEntity(input.EntityFocus().GetEntity());
173 hud.Display(res.block_types[player.GetInventorySlot() + 1]);
175 chunk_loader.Update(dt);
176 chunk_renderer.Update(dt);
178 glm::mat4 trans = player.GetEntity().Transform(player.GetEntity().ChunkCoords());
179 glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
180 glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f));
181 env.audio.Position(player.GetEntity().Position());
182 env.audio.Velocity(player.GetEntity().Velocity());
183 env.audio.Orientation(dir, up);
186 void MasterState::Render(Viewport &viewport) {
187 viewport.WorldPosition(player.GetEntity().ViewTransform(player.GetEntity().ChunkCoords()));
188 if (config.video.world) {
189 chunk_renderer.Render(viewport);
190 world.Render(viewport);
191 if (config.video.debug) {
192 world.RenderDebug(viewport);
194 sky.Render(viewport);
196 hud.Render(viewport);
200 void MasterState::SetAudio(bool b) {
201 config.audio.enabled = b;
203 hud.PostMessage("Audio enabled");
205 hud.PostMessage("Audio disabled");
209 void MasterState::SetVideo(bool b) {
210 config.video.world = b;
212 hud.PostMessage("World rendering enabled");
214 hud.PostMessage("World rendering disabled");
218 void MasterState::SetHUD(bool b) {
219 config.video.hud = b;
221 hud.PostMessage("HUD rendering enabled");
223 hud.PostMessage("HUD rendering disabled");
227 void MasterState::SetDebug(bool b) {
228 config.video.debug = b;
230 hud.PostMessage("Debug rendering enabled");
232 hud.PostMessage("Debug rendering disabled");
236 void MasterState::NextCamera() {
237 if (iszero(env.viewport.CameraOffset())) {
238 env.viewport.OffsetCamera(glm::vec3(0.0f, 0.0f, -5.0f));
240 env.viewport.OffsetCamera(glm::vec3(0.0f, 0.0f, 0.0f));
244 void MasterState::Exit() {
246 env.state.Switch(&unload);
249 void MasterState::OnLineSubmit(const std::string &line) {
253 if (line[0] == '/' && line.size() > 1 && line[1] != '/') {
254 cli.Execute(cli_ctx, line.substr(1));
256 hud.PostMessage(line);
261 PreloadState::PreloadState(Environment &env, ChunkLoader &loader, ChunkRenderer &render)
262 : ProgressState(env, "Preloading chunks: %d/%d (%d%%)")
266 , total(loader.ToLoad())
271 void PreloadState::Update(int) {
272 loader.LoadN(per_update);
273 if (loader.ToLoad() <= 0) {
275 render.Update(render.MissingChunks());
277 SetProgress(total - loader.ToLoad(), total);
282 UnloadState::UnloadState(
285 const WorldSave &save)
286 : ProgressState(env, "Unloading chunks: %d/%d (%d%%)")
290 , cur(chunks.begin())
293 , total(chunks.NumLoaded())
299 void UnloadState::OnResume() {
300 cur = chunks.begin();
303 total = chunks.NumLoaded();
307 void UnloadState::Handle(const SDL_Event &) {
311 void UnloadState::Update(int) {
312 for (std::size_t i = 0; i < per_update && cur != end; ++i, ++cur, ++done) {
313 if (cur->ShouldUpdateSave()) {
320 SetProgress(done, total);