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