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