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