]> git.localhorst.tv Git - blank.git/blob - src/standalone/MasterState.cpp
linear model instance 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 , shapes()
25 , block_types()
26 , save(save)
27 , world(block_types, wc)
28 , spawn_index(world.Chunks().MakeIndex(wc.spawn, 3))
29 , player(*world.AddPlayer(config.player.name))
30 , spawn_player(false)
31 , hud(env, config, player)
32 , manip(env, player.GetEntity())
33 , input(world, player, manip)
34 , interface(config, env.keymap, input, *this)
35 , generator(gc)
36 , chunk_loader(world.Chunks(), generator, save)
37 , chunk_renderer(player.GetChunks())
38 , skeletons()
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)) {
53                 save.Read(player);
54         } else {
55                 spawn_player = true;
56         }
57 }
58
59 MasterState::~MasterState() {
60         world.Chunks().UnregisterIndex(spawn_index);
61 }
62
63
64 void MasterState::OnResume() {
65         if (spawn_index.MissingChunks() > 0) {
66                 env.state.Push(&preload);
67         }
68         if (config.input.mouse) {
69                 env.window.GrabMouse();
70         }
71         if (spawn_player) {
72                 // TODO: spawn
73                 spawn_player = false;
74         }
75 }
76
77 void MasterState::OnPause() {
78         env.window.ReleaseMouse();
79 }
80
81
82 void MasterState::Handle(const SDL_Event &event) {
83         switch (event.type) {
84                 case SDL_KEYDOWN:
85                         interface.HandlePress(event.key);
86                         break;
87                 case SDL_KEYUP:
88                         interface.HandleRelease(event.key);
89                         break;
90                 case SDL_MOUSEBUTTONDOWN:
91                         interface.HandlePress(event.button);
92                         break;
93                 case SDL_MOUSEBUTTONUP:
94                         interface.HandleRelease(event.button);
95                         break;
96                 case SDL_MOUSEMOTION:
97                         interface.Handle(event.motion);
98                         break;
99                 case SDL_MOUSEWHEEL:
100                         interface.Handle(event.wheel);
101                         break;
102                 case SDL_QUIT:
103                         Exit();
104                         break;
105                 default:
106                         break;
107         }
108 }
109
110 void MasterState::Update(int dt) {
111         input.Update(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);
116         } else {
117                 hud.FocusNone();
118         }
119         hud.Display(block_types[player.GetInventorySlot() + 1]);
120         hud.Update(dt);
121         spawner.Update(dt);
122         world.Update(dt);
123         chunk_loader.Update(dt);
124         chunk_renderer.Update(dt);
125
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);
132 }
133
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);
140         }
141         hud.Render(viewport);
142 }
143
144
145 void MasterState::SetAudio(bool b) {
146         config.audio.enabled = b;
147         if (b) {
148                 hud.PostMessage("Audio enabled");
149         } else {
150                 hud.PostMessage("Audio disabled");
151         }
152 }
153
154 void MasterState::SetVideo(bool b) {
155         config.video.world = b;
156         if (b) {
157                 hud.PostMessage("World rendering enabled");
158         } else {
159                 hud.PostMessage("World rendering disabled");
160         }
161 }
162
163 void MasterState::SetHUD(bool b) {
164         config.video.hud = b;
165         if (b) {
166                 hud.PostMessage("HUD rendering enabled");
167         } else {
168                 hud.PostMessage("HUD rendering disabled");
169         }
170 }
171
172 void MasterState::SetDebug(bool b) {
173         config.video.debug = b;
174         if (b) {
175                 hud.PostMessage("Debug rendering enabled");
176         } else {
177                 hud.PostMessage("Debug rendering disabled");
178         }
179 }
180
181 void MasterState::Exit() {
182         save.Write(player);
183         env.state.Switch(&unload);
184 }
185
186 }
187 }