]> git.localhorst.tv Git - blank.git/blob - src/standalone/MasterState.cpp
basic message state
[blank.git] / src / standalone / MasterState.cpp
1 #include "MasterState.hpp"
2
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"
8
9 #include <SDL.h>
10
11
12 namespace blank {
13 namespace standalone {
14
15 MasterState::MasterState(
16         Environment &env,
17         Config &config,
18         const Generator::Config &gc,
19         const World::Config &wc,
20         const WorldSave &save
21 )
22 : config(config)
23 , env(env)
24 , block_types()
25 , save(save)
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)
32 , generator(gc)
33 , chunk_loader(world.Chunks(), generator, save)
34 , chunk_renderer(player.GetChunks())
35 , skeletons()
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);
45         skeletons.Load();
46         spawner.LimitSkeletons(0, skeletons.Size());
47         if (save.Exists(player)) {
48                 save.Read(player);
49         }
50 }
51
52
53 void MasterState::OnEnter() {
54         env.state.Push(&preload);
55         env.window.GrabMouse();
56 }
57
58
59 void MasterState::Handle(const SDL_Event &event) {
60         switch (event.type) {
61                 case SDL_KEYDOWN:
62                         interface.HandlePress(event.key);
63                         break;
64                 case SDL_KEYUP:
65                         interface.HandleRelease(event.key);
66                         break;
67                 case SDL_MOUSEBUTTONDOWN:
68                         interface.HandlePress(event.button);
69                         break;
70                 case SDL_MOUSEBUTTONUP:
71                         interface.HandleRelease(event.button);
72                         break;
73                 case SDL_MOUSEMOTION:
74                         interface.Handle(event.motion);
75                         break;
76                 case SDL_MOUSEWHEEL:
77                         interface.Handle(event.wheel);
78                         break;
79                 case SDL_QUIT:
80                         Exit();
81                         break;
82                 default:
83                         break;
84         }
85 }
86
87 void MasterState::Update(int dt) {
88         input.Update(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);
93         } else {
94                 hud.FocusNone();
95         }
96         hud.Display(block_types[player.GetInventorySlot() + 1]);
97         hud.Update(dt);
98         spawner.Update(dt);
99         world.Update(dt);
100         chunk_loader.Update(dt);
101         chunk_renderer.Update(dt);
102
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);
109 }
110
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);
117         }
118         hud.Render(viewport);
119 }
120
121
122 void MasterState::SetAudio(bool b) {
123         config.audio.enabled = b;
124         if (b) {
125                 hud.PostMessage("Audio enabled");
126         } else {
127                 hud.PostMessage("Audio disabled");
128         }
129 }
130
131 void MasterState::SetVideo(bool b) {
132         config.video.world = b;
133         if (b) {
134                 hud.PostMessage("World rendering enabled");
135         } else {
136                 hud.PostMessage("World rendering disabled");
137         }
138 }
139
140 void MasterState::SetHUD(bool b) {
141         config.video.hud = b;
142         if (b) {
143                 hud.PostMessage("HUD rendering enabled");
144         } else {
145                 hud.PostMessage("HUD rendering disabled");
146         }
147 }
148
149 void MasterState::SetDebug(bool b) {
150         config.video.debug = b;
151         if (b) {
152                 hud.PostMessage("Debug rendering enabled");
153         } else {
154                 hud.PostMessage("Debug rendering disabled");
155         }
156 }
157
158 void MasterState::Exit() {
159         save.Write(player);
160         env.state.Switch(&unload);
161 }
162
163 }
164 }