]> git.localhorst.tv Git - blank.git/blob - src/world/World.hpp
defined and implemented join and part packets
[blank.git] / src / world / World.hpp
1 #ifndef BLANK_WORLD_WORLD_HPP_
2 #define BLANK_WORLD_WORLD_HPP_
3
4 #include "ChunkLoader.hpp"
5 #include "Entity.hpp"
6 #include "Generator.hpp"
7
8 #include <list>
9 #include <string>
10 #include <vector>
11 #include <glm/glm.hpp>
12
13
14 namespace blank {
15
16 class BlockTypeRegistry;
17 class EntityCollision;
18 class Viewport;
19 class WorldCollision;
20
21 class World {
22
23 public:
24         struct Config {
25                 std::string name = "default";
26                 // initial player position
27                 glm::vec3 spawn = { 0.0f, 0.0f, 0.0f };
28                 // direction facing towards(!) the light
29                 glm::vec3 light_direction = { -1.0f, -3.0f, -2.0f };
30                 // fade out reaches 1/e (0.3679) at 1/fog_density,
31                 // gets less than 0.01 at e/(2 * fog_density)
32                 // I chose 0.011 because it yields 91 and 124 for those, so
33                 // slightly less than 6 and 8 chunks
34                 float fog_density = 0.011f;
35
36                 Generator::Config gen = Generator::Config();
37                 ChunkLoader::Config load = ChunkLoader::Config();
38         };
39
40         World(const BlockTypeRegistry &, const Config &, const WorldSave &);
41
42         const std::string &Name() const noexcept { return config.name; }
43
44         /// check if this ray hits a block
45         /// depth in the collision is the distance between the ray's
46         /// origin and the intersection point
47         /// M is the global transform for given reference chunk
48         bool Intersection(
49                 const Ray &,
50                 const glm::mat4 &M,
51                 const Chunk::Pos &reference,
52                 WorldCollision &);
53
54         /// check if this ray hits an entity
55         /// intersections with the reference are not tested
56         /// M is the global transform for the chunk of given reference entity
57         bool Intersection(
58                 const Ray &,
59                 const glm::mat4 &M,
60                 const Entity &reference,
61                 EntityCollision &);
62
63         /// check if given entity intersects with the world
64         bool Intersection(const Entity &e, std::vector<WorldCollision> &);
65         void Resolve(Entity &e, std::vector<WorldCollision> &);
66
67         const BlockTypeRegistry &BlockTypes() noexcept { return block_type; }
68         ChunkLoader &Loader() noexcept { return chunks; }
69
70         /// add player with given name
71         /// returns nullptr if the name is already taken
72         Entity *AddPlayer(const std::string &name);
73         Entity &AddEntity() { entities.emplace_back(); return entities.back(); }
74
75         const std::vector<Entity *> &Players() const noexcept { return players; }
76         const std::list<Entity> &Entities() const noexcept { return entities; }
77
78         void Update(int dt);
79
80         void Render(Viewport &);
81
82 private:
83         Config config;
84
85         const BlockTypeRegistry &block_type;
86
87         Generator generate;
88         ChunkLoader chunks;
89
90         std::vector<Entity *> players;
91         std::list<Entity> entities;
92
93         glm::vec3 light_direction;
94         float fog_density;
95
96 };
97
98 }
99
100 #endif