]> git.localhorst.tv Git - blank.git/blob - src/server/ServerState.cpp
store shapes in models rather than meshes
[blank.git] / src / server / ServerState.cpp
1 #include "ServerState.hpp"
2
3 #include "../app/Environment.hpp"
4 #include "../app/TextureIndex.hpp"
5 #include "../io/WorldSave.hpp"
6 #include "../net/io.hpp"
7
8 #include <iostream>
9
10
11 namespace blank {
12 namespace server {
13
14 ServerState::ServerState(
15         HeadlessEnvironment &env,
16         const Generator::Config &gc,
17         const World::Config &wc,
18         const WorldSave &ws,
19         const Config &config
20 )
21 : env(env)
22 , shapes()
23 , block_types()
24 , world(block_types, wc)
25 , generator(gc)
26 , chunk_loader(world.Chunks(), generator, ws)
27 , skeletons()
28 , spawner(world, skeletons, env.rng)
29 , server(config.net, world, wc, ws)
30 , loop_timer(16) {
31         TextureIndex tex_index;
32         env.loader.LoadShapes("default", shapes);
33         env.loader.LoadBlockTypes("default", block_types, tex_index, shapes);
34         generator.LoadTypes(block_types);
35         skeletons.Load(shapes);
36         spawner.LimitSkeletons(1, skeletons.size());
37         spawner.LoadTextures(tex_index);
38         server.SetPlayerModel(skeletons[0]);
39
40         loop_timer.Start();
41
42         std::cout << "listening on UDP port " << config.net.port << std::endl;
43 }
44
45 ServerState::~ServerState() {
46
47 }
48
49
50 void ServerState::Handle(const SDL_Event &event) {
51         if (event.type == SDL_QUIT) {
52                 std::cout << "saving remaining chunks" << std::endl;
53                 for (Chunk &chunk : world.Chunks()) {
54                         if (chunk.ShouldUpdateSave()) {
55                                 chunk_loader.SaveFile().Write(chunk);
56                         }
57                 }
58                 env.state.PopAll();
59         }
60 }
61
62
63 void ServerState::Update(int dt) {
64         loop_timer.Update(dt);
65         server.Handle();
66         int world_dt = 0;
67         while (loop_timer.HitOnce()) {
68                 spawner.Update(loop_timer.Interval());
69                 world.Update(loop_timer.Interval());
70                 world_dt += loop_timer.Interval();
71                 loop_timer.PopIteration();
72         }
73         chunk_loader.Update(dt);
74         if (world_dt > 0) {
75                 server.Update(world_dt);
76         }
77 }
78
79
80 void ServerState::Render(Viewport &viewport) {
81
82 }
83
84 }
85 }