]> git.localhorst.tv Git - blank.git/blob - src/world/World.hpp
e0209c96aaf273eb4fab523fd4734b2353e5af28
[blank.git] / src / world / World.hpp
1 #ifndef BLANK_WORLD_WORLD_HPP_
2 #define BLANK_WORLD_WORLD_HPP_
3
4 #include "BlockTypeRegistry.hpp"
5 #include "ChunkLoader.hpp"
6 #include "Entity.hpp"
7 #include "Generator.hpp"
8 #include "../graphics/ArrayTexture.hpp"
9
10 #include <list>
11 #include <vector>
12 #include <glm/glm.hpp>
13
14
15 namespace blank {
16
17 class Assets;
18 class EntityCollision;
19 class Viewport;
20 class WorldCollision;
21
22 class World {
23
24 public:
25         struct Config {
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 Assets &, const Config &, const WorldSave &);
41
42         /// check if this ray hits a block
43         /// depth in the collision is the distance between the ray's
44         /// origin and the intersection point
45         /// M is the global transform for given reference chunk
46         bool Intersection(
47                 const Ray &,
48                 const glm::mat4 &M,
49                 const Chunk::Pos &reference,
50                 WorldCollision &);
51
52         /// check if this ray hits an entity
53         /// intersections with the reference are not tested
54         /// M is the global transform for the chunk of given reference entity
55         bool Intersection(
56                 const Ray &,
57                 const glm::mat4 &M,
58                 const Entity &reference,
59                 EntityCollision &);
60
61         /// check if given entity intersects with the world
62         bool Intersection(const Entity &e, std::vector<WorldCollision> &);
63         void Resolve(Entity &e, std::vector<WorldCollision> &);
64
65         BlockTypeRegistry &BlockTypes() noexcept { return block_type; }
66         ChunkLoader &Loader() noexcept { return chunks; }
67
68         Entity &Player() { return *player; }
69         Entity &AddEntity() { entities.emplace_back(); return entities.back(); }
70
71         Chunk &PlayerChunk();
72
73         void Update(int dt);
74
75         void Render(Viewport &);
76
77 private:
78         BlockTypeRegistry block_type;
79
80         ArrayTexture block_tex;
81
82         Generator generate;
83         ChunkLoader chunks;
84
85         Entity *player;
86         std::list<Entity> entities;
87
88         glm::vec3 light_direction;
89         float fog_density;
90
91 };
92
93 }
94
95 #endif