1 #include "MasterState.hpp"
3 #include "../app/Config.hpp"
4 #include "../app/Environment.hpp"
5 #include "../app/init.hpp"
6 #include "../app/TextureIndex.hpp"
12 namespace standalone {
14 MasterState::MasterState(
17 const Generator::Config &gc,
18 const World::Config &wc,
24 , world(block_types, wc)
25 , player(*world.AddPlayer(config.player.name))
26 , hud(env, config, player)
27 , manip(env, player.GetEntity())
28 , input(world, player, manip)
29 , interface(config, env.keymap, input, *this)
31 , chunk_loader(world.Chunks(), generator, save)
32 , chunk_renderer(player.GetChunks())
34 , spawner(world, skeletons, gc.seed)
35 , sky(env.loader.LoadCubeMap("skybox"))
36 , preload(env, chunk_loader, chunk_renderer)
37 , unload(env, world.Chunks(), save) {
38 TextureIndex tex_index;
39 env.loader.LoadBlockTypes("default", block_types, tex_index);
40 interface.SetInventorySlots(block_types.Size() - 1);
41 chunk_renderer.LoadTextures(env.loader, tex_index);
42 chunk_renderer.FogDensity(wc.fog_density);
44 spawner.LimitSkeletons(0, skeletons.Size());
48 void MasterState::OnEnter() {
49 env.state.Push(&preload);
50 env.window.GrabMouse();
54 void MasterState::Handle(const SDL_Event &event) {
57 interface.HandlePress(event.key);
60 interface.HandleRelease(event.key);
62 case SDL_MOUSEBUTTONDOWN:
63 interface.HandlePress(event.button);
65 case SDL_MOUSEBUTTONUP:
66 interface.HandleRelease(event.button);
69 interface.Handle(event.motion);
72 interface.Handle(event.wheel);
75 env.state.Switch(&unload);
82 void MasterState::Update(int dt) {
84 if (input.BlockFocus()) {
85 hud.FocusBlock(input.BlockFocus().GetChunk(), input.BlockFocus().block);
86 } else if (input.EntityFocus()) {
87 hud.FocusEntity(*input.EntityFocus().entity);
91 hud.Display(block_types[player.GetInventorySlot() + 1]);
95 chunk_loader.Update(dt);
96 chunk_renderer.Update(dt);
98 glm::mat4 trans = player.GetEntity().Transform(player.GetEntity().ChunkCoords());
99 glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
100 glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f));
101 env.audio.Position(player.GetEntity().Position());
102 env.audio.Velocity(player.GetEntity().Velocity());
103 env.audio.Orientation(dir, up);
106 void MasterState::Render(Viewport &viewport) {
107 viewport.WorldPosition(player.GetEntity().Transform(player.GetEntity().ChunkCoords()));
108 if (config.video.world) {
109 chunk_renderer.Render(viewport);
110 world.Render(viewport);
111 sky.Render(viewport);
113 hud.Render(viewport);
117 void MasterState::SetAudio(bool b) {
118 config.audio.enabled = b;
120 hud.PostMessage("Audio enabled");
122 hud.PostMessage("Audio disabled");
126 void MasterState::SetVideo(bool b) {
127 config.video.world = b;
129 hud.PostMessage("World rendering enabled");
131 hud.PostMessage("World rendering disabled");
135 void MasterState::SetHUD(bool b) {
136 config.video.hud = b;
138 hud.PostMessage("HUD rendering enabled");
140 hud.PostMessage("HUD rendering disabled");
144 void MasterState::SetDebug(bool b) {
145 config.video.debug = b;
147 hud.PostMessage("Debug rendering enabled");
149 hud.PostMessage("Debug rendering disabled");
153 void MasterState::Exit() {