]> git.localhorst.tv Git - blank.git/blob - src/world/World.hpp
move spawn index out of world
[blank.git] / src / world / World.hpp
1 #ifndef BLANK_WORLD_WORLD_HPP_
2 #define BLANK_WORLD_WORLD_HPP_
3
4 #include "ChunkStore.hpp"
5 #include "Entity.hpp"
6 #include "Generator.hpp"
7 #include "Player.hpp"
8
9 #include <cstdint>
10 #include <list>
11 #include <string>
12 #include <vector>
13 #include <glm/glm.hpp>
14
15
16 namespace blank {
17
18 class BlockTypeRegistry;
19 class EntityCollision;
20 class Viewport;
21 class WorldCollision;
22
23 class World {
24
25 public:
26         struct Config {
27                 std::string name = "default";
28                 // chunk base where new players are spawned
29                 glm::ivec3 spawn = { 0, 0, 0 };
30                 // direction facing towards(!) the light
31                 glm::vec3 light_direction = { -1.0f, -3.0f, -2.0f };
32                 // fade out reaches 1/e (0.3679) at 1/fog_density,
33                 // gets less than 0.01 at e/(2 * fog_density)
34                 // I chose 0.011 because it yields 91 and 124 for those, so
35                 // slightly less than 6 and 8 chunks
36                 float fog_density = 0.011f;
37         };
38
39         World(const BlockTypeRegistry &, const Config &);
40         ~World();
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         ChunkStore &Chunks() noexcept { return chunks; }
69
70         /// add player with given name
71         /// returns nullptr if the name is already taken
72         Player *AddPlayer(const std::string &name);
73         /// add player with given name and ID
74         /// returns nullptr if the name or ID is already taken
75         Player *AddPlayer(const std::string &name, std::uint32_t id);
76         /// add an entity with an autogenerated ID
77         Entity &AddEntity();
78         /// add entity with given ID
79         /// returns nullptr if the ID is already taken
80         Entity *AddEntity(std::uint32_t id);
81         /// add entity with given ID
82         /// returs an existing entity if ID is already taken
83         Entity &ForceAddEntity(std::uint32_t id);
84
85         const std::list<Player> &Players() const noexcept { return players; }
86         std::list<Entity> &Entities() noexcept { return entities; }
87         const std::list<Entity> &Entities() const noexcept { return entities; }
88
89         void Update(int dt);
90
91         void Render(Viewport &);
92
93 private:
94         using EntityHandle = std::list<Entity>::iterator;
95         EntityHandle RemoveEntity(EntityHandle &);
96
97 private:
98         Config config;
99
100         const BlockTypeRegistry &block_type;
101
102         ChunkStore chunks;
103
104         std::list<Player> players;
105         std::list<Entity> entities;
106
107         glm::vec3 light_direction;
108         float fog_density;
109
110 };
111
112 }
113
114 #endif