]> git.localhorst.tv Git - blank.git/blob - src/world/World.hpp
check for entities under crosshair
[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 Viewport;
19 class WorldCollision;
20
21 class World {
22
23 public:
24         struct Config {
25                 // initial player position
26                 glm::vec3 spawn = { 0.0f, 0.0f, 0.0f };
27                 // direction facing towards(!) the light
28                 glm::vec3 light_direction = { -1.0f, -3.0f, -2.0f };
29                 // fade out reaches 1/e (0.3679) at 1/fog_density,
30                 // gets less than 0.01 at e/(2 * fog_density)
31                 // I chose 0.011 because it yields 91 and 124 for those, so
32                 // slightly less than 6 and 8 chunks
33                 float fog_density = 0.011f;
34
35                 Generator::Config gen = Generator::Config();
36                 ChunkLoader::Config load = ChunkLoader::Config();
37         };
38
39         World(const Assets &, const Config &, const WorldSave &);
40
41         // check if this ray hits a block
42         bool Intersection(
43                 const Ray &,
44                 const glm::mat4 &M,
45                 Chunk *&chunk,
46                 int &blkid,
47                 float &dist,
48                 glm::vec3 &normal);
49
50         // check if this ray hits an entity
51         bool Intersection(
52                 const Ray &,
53                 const glm::mat4 &M,
54                 Entity *&entity,
55                 float &dist,
56                 glm::vec3 &normal);
57
58         bool Intersection(const Entity &e, std::vector<WorldCollision> &);
59         void Resolve(Entity &e, std::vector<WorldCollision> &);
60
61         BlockTypeRegistry &BlockTypes() noexcept { return block_type; }
62         ChunkLoader &Loader() noexcept { return chunks; }
63
64         Entity &Player() { return *player; }
65         Entity &AddEntity() { entities.emplace_back(); return entities.back(); }
66
67         Chunk &PlayerChunk();
68         Chunk &Next(const Chunk &to, const glm::ivec3 &dir);
69
70         void Update(int dt);
71
72         void Render(Viewport &);
73
74 private:
75         BlockTypeRegistry block_type;
76
77         ArrayTexture block_tex;
78
79         Generator generate;
80         ChunkLoader chunks;
81
82         Entity *player;
83         std::list<Entity> entities;
84
85         glm::vec3 light_direction;
86         float fog_density;
87
88 };
89
90 }
91
92 #endif