]> git.localhorst.tv Git - blank.git/blob - src/app/WorldState.cpp
more packet introspection from payload
[blank.git] / src / app / WorldState.cpp
1 #include "WorldState.hpp"
2
3 #include "Environment.hpp"
4 #include "init.hpp"
5 #include "TextureIndex.hpp"
6
7 #include <SDL.h>
8
9
10 namespace blank {
11
12 WorldState::WorldState(
13         Environment &env,
14         const Generator::Config &gc,
15         const Interface::Config &ic,
16         const World::Config &wc,
17         const WorldSave &save
18 )
19 : env(env)
20 , block_types()
21 , world(block_types, wc)
22 , interface(ic, env, world, world.AddPlayer(ic.player_name))
23 , generator(gc)
24 , chunk_loader(world.Chunks(), generator, save)
25 , chunk_renderer(*interface.GetPlayer().chunks)
26 , skeletons()
27 , spawner(world, skeletons, gc.seed)
28 , preload(env, chunk_loader, chunk_renderer)
29 , unload(env, world.Chunks(), save) {
30         TextureIndex tex_index;
31         env.loader.LoadBlockTypes("default", block_types, tex_index);
32         chunk_renderer.LoadTextures(env.loader, tex_index);
33         chunk_renderer.FogDensity(wc.fog_density);
34         skeletons.Load();
35         // TODO: better solution for initializing HUD
36         interface.SelectNext();
37 }
38
39
40 void WorldState::OnEnter() {
41         env.state.Push(&preload);
42         env.window.GrabMouse();
43 }
44
45
46 void WorldState::Handle(const SDL_Event &event) {
47         switch (event.type) {
48                 case SDL_KEYDOWN:
49                         interface.HandlePress(event.key);
50                         break;
51                 case SDL_KEYUP:
52                         interface.HandleRelease(event.key);
53                         break;
54                 case SDL_MOUSEBUTTONDOWN:
55                         interface.HandlePress(event.button);
56                         break;
57                 case SDL_MOUSEBUTTONUP:
58                         interface.HandleRelease(event.button);
59                         break;
60                 case SDL_MOUSEMOTION:
61                         interface.Handle(event.motion);
62                         break;
63                 case SDL_MOUSEWHEEL:
64                         interface.Handle(event.wheel);
65                         break;
66                 case SDL_QUIT:
67                         env.state.Switch(&unload);
68                         break;
69                 default:
70                         break;
71         }
72 }
73
74 void WorldState::Update(int dt) {
75         interface.Update(dt);
76         spawner.Update(dt);
77         world.Update(dt);
78         chunk_loader.Update(dt);
79         chunk_renderer.Update(dt);
80
81         Entity &player = *interface.GetPlayer().entity;
82
83         glm::mat4 trans = player.Transform(player.ChunkCoords());
84         glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
85         glm::vec3 up(trans * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f));
86         env.audio.Position(player.Position());
87         env.audio.Velocity(player.Velocity());
88         env.audio.Orientation(dir, up);
89 }
90
91 void WorldState::Render(Viewport &viewport) {
92         Entity &player = *interface.GetPlayer().entity;
93         viewport.WorldPosition(player.Transform(player.ChunkCoords()));
94         chunk_renderer.Render(viewport);
95         world.Render(viewport);
96         interface.Render(viewport);
97 }
98
99 }