]> git.localhorst.tv Git - blank.git/blob - src/world/World.hpp
split chunk redering from world model
[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         Entity &Player() { return *player; }
67         Entity &AddEntity() { entities.emplace_back(); return entities.back(); }
68
69         Chunk &PlayerChunk();
70
71         void Update(int dt);
72
73         void Render(Viewport &);
74
75 private:
76         const BlockTypeRegistry &block_type;
77
78         Generator generate;
79         ChunkLoader chunks;
80
81         Entity *player;
82         std::list<Entity> entities;
83
84         glm::vec3 light_direction;
85         float fog_density;
86
87 };
88
89 }
90
91 #endif