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