1 #include "MasterState.hpp"
3 #include "../app/Config.hpp"
4 #include "../app/Environment.hpp"
5 #include "../app/init.hpp"
6 #include "../app/TextureIndex.hpp"
7 #include "../io/WorldSave.hpp"
13 namespace standalone {
15 MasterState::MasterState(
18 const Generator::Config &gc,
19 const World::Config &wc,
26 , world(block_types, wc)
27 , player(*world.AddPlayer(config.player.name))
28 , hud(env, config, player)
29 , manip(env, player.GetEntity())
30 , input(world, player, manip)
31 , interface(config, env.keymap, input, *this)
33 , chunk_loader(world.Chunks(), generator, save)
34 , chunk_renderer(player.GetChunks())
36 , spawner(world, skeletons, gc.seed)
37 , sky(env.loader.LoadCubeMap("skybox"))
38 , preload(env, chunk_loader, chunk_renderer)
39 , unload(env, world.Chunks(), save) {
40 TextureIndex tex_index;
41 env.loader.LoadBlockTypes("default", block_types, tex_index);
42 interface.SetInventorySlots(block_types.Size() - 1);
43 chunk_renderer.LoadTextures(env.loader, tex_index);
44 chunk_renderer.FogDensity(wc.fog_density);
46 spawner.LimitSkeletons(0, skeletons.Size());
47 if (save.Exists(player)) {
53 void MasterState::OnEnter() {
54 env.state.Push(&preload);
55 env.window.GrabMouse();
59 void MasterState::Handle(const SDL_Event &event) {
62 interface.HandlePress(event.key);
65 interface.HandleRelease(event.key);
67 case SDL_MOUSEBUTTONDOWN:
68 interface.HandlePress(event.button);
70 case SDL_MOUSEBUTTONUP:
71 interface.HandleRelease(event.button);
74 interface.Handle(event.motion);
77 interface.Handle(event.wheel);
87 void MasterState::Update(int dt) {
89 if (input.BlockFocus()) {
90 hud.FocusBlock(input.BlockFocus().GetChunk(), input.BlockFocus().block);
91 } else if (input.EntityFocus()) {
92 hud.FocusEntity(*input.EntityFocus().entity);
96 hud.Display(block_types[player.GetInventorySlot() + 1]);
100 chunk_loader.Update(dt);
101 chunk_renderer.Update(dt);
103 glm::mat4 trans = player.GetEntity().Transform(player.GetEntity().ChunkCoords());
104 glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
105 glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f));
106 env.audio.Position(player.GetEntity().Position());
107 env.audio.Velocity(player.GetEntity().Velocity());
108 env.audio.Orientation(dir, up);
111 void MasterState::Render(Viewport &viewport) {
112 viewport.WorldPosition(player.GetEntity().Transform(player.GetEntity().ChunkCoords()));
113 if (config.video.world) {
114 chunk_renderer.Render(viewport);
115 world.Render(viewport);
116 sky.Render(viewport);
118 hud.Render(viewport);
122 void MasterState::SetAudio(bool b) {
123 config.audio.enabled = b;
125 hud.PostMessage("Audio enabled");
127 hud.PostMessage("Audio disabled");
131 void MasterState::SetVideo(bool b) {
132 config.video.world = b;
134 hud.PostMessage("World rendering enabled");
136 hud.PostMessage("World rendering disabled");
140 void MasterState::SetHUD(bool b) {
141 config.video.hud = b;
143 hud.PostMessage("HUD rendering enabled");
145 hud.PostMessage("HUD rendering disabled");
149 void MasterState::SetDebug(bool b) {
150 config.video.debug = b;
152 hud.PostMessage("Debug rendering enabled");
154 hud.PostMessage("Debug rendering disabled");
158 void MasterState::Exit() {
160 env.state.Switch(&unload);