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,
27 , world(block_types, wc)
28 , spawn_index(world.Chunks().MakeIndex(wc.spawn, 3))
29 , player(*world.AddPlayer(config.player.name))
31 , hud(env, config, player)
32 , manip(env, player.GetEntity())
33 , input(world, player, manip)
34 , interface(config, env.keymap, input, *this)
36 , chunk_loader(world.Chunks(), generator, save)
37 , chunk_renderer(player.GetChunks())
39 , spawner(world, skeletons, env.rng)
40 , sky(env.loader.LoadCubeMap("skybox"))
41 , preload(env, chunk_loader, chunk_renderer)
42 , unload(env, world.Chunks(), save) {
43 TextureIndex tex_index;
44 env.loader.LoadShapes("default", shapes);
45 env.loader.LoadBlockTypes("default", block_types, tex_index, shapes);
46 skeletons.Load(shapes, tex_index);
47 interface.SetInventorySlots(block_types.size() - 1);
48 generator.LoadTypes(block_types);
49 chunk_renderer.LoadTextures(env.loader, tex_index);
50 chunk_renderer.FogDensity(wc.fog_density);
51 spawner.LimitSkeletons(0, skeletons.size());
52 if (save.Exists(player)) {
59 MasterState::~MasterState() {
60 world.Chunks().UnregisterIndex(spawn_index);
64 void MasterState::OnResume() {
65 if (spawn_index.MissingChunks() > 0) {
66 env.state.Push(&preload);
68 if (config.input.mouse) {
69 env.window.GrabMouse();
77 void MasterState::OnPause() {
78 env.window.ReleaseMouse();
82 void MasterState::Handle(const SDL_Event &event) {
85 interface.HandlePress(event.key);
88 interface.HandleRelease(event.key);
90 case SDL_MOUSEBUTTONDOWN:
91 interface.HandlePress(event.button);
93 case SDL_MOUSEBUTTONUP:
94 interface.HandleRelease(event.button);
97 interface.Handle(event.motion);
100 interface.Handle(event.wheel);
110 void MasterState::Update(int dt) {
112 if (input.BlockFocus()) {
113 hud.FocusBlock(input.BlockFocus().GetChunk(), input.BlockFocus().block);
114 } else if (input.EntityFocus()) {
115 hud.FocusEntity(*input.EntityFocus().entity);
119 hud.Display(block_types[player.GetInventorySlot() + 1]);
123 chunk_loader.Update(dt);
124 chunk_renderer.Update(dt);
126 glm::mat4 trans = player.GetEntity().Transform(player.GetEntity().ChunkCoords());
127 glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
128 glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f));
129 env.audio.Position(player.GetEntity().Position());
130 env.audio.Velocity(player.GetEntity().Velocity());
131 env.audio.Orientation(dir, up);
134 void MasterState::Render(Viewport &viewport) {
135 viewport.WorldPosition(player.GetEntity().Transform(player.GetEntity().ChunkCoords()));
136 if (config.video.world) {
137 chunk_renderer.Render(viewport);
138 world.Render(viewport);
139 sky.Render(viewport);
141 hud.Render(viewport);
145 void MasterState::SetAudio(bool b) {
146 config.audio.enabled = b;
148 hud.PostMessage("Audio enabled");
150 hud.PostMessage("Audio disabled");
154 void MasterState::SetVideo(bool b) {
155 config.video.world = b;
157 hud.PostMessage("World rendering enabled");
159 hud.PostMessage("World rendering disabled");
163 void MasterState::SetHUD(bool b) {
164 config.video.hud = b;
166 hud.PostMessage("HUD rendering enabled");
168 hud.PostMessage("HUD rendering disabled");
172 void MasterState::SetDebug(bool b) {
173 config.video.debug = b;
175 hud.PostMessage("Debug rendering enabled");
177 hud.PostMessage("Debug rendering disabled");
181 void MasterState::Exit() {
183 env.state.Switch(&unload);