]> git.localhorst.tv Git - blobs.git/blob - src/world/Planet.hpp
eat what's here
[blobs.git] / src / world / Planet.hpp
1 #ifndef BLOBS_WORLD_PLANET_HPP_
2 #define BLOBS_WORLD_PLANET_HPP_
3
4 #include "Body.hpp"
5
6 #include "Set.hpp"
7 #include "Tile.hpp"
8 #include "../graphics/glm.hpp"
9 #include "../graphics/SimpleVAO.hpp"
10
11 #include <cassert>
12 #include <vector>
13 #include <GL/glew.h>
14
15
16 namespace blobs {
17 namespace world {
18
19 class TileType;
20
21 /// A planet has six surfaces, numbered 0 to 5, each filled with
22 /// sidelength² tiles.
23 class Planet
24 : public Body {
25
26 public:
27         explicit Planet(int sidelength);
28         ~Planet();
29
30         Planet(const Planet &) = delete;
31         Planet &operator =(const Planet &) = delete;
32
33         Planet(Planet &&) = delete;
34         Planet &operator =(Planet &&) = delete;
35
36 public:
37         /// Get the tile at given surface and coordinates.
38         Tile &TileAt(int surface, int x, int y) {
39                 return tiles[IndexOf(surface, x, y)];
40         }
41         const Tile &TileAt(int surface, int x, int y) const {
42                 return tiles[IndexOf(surface, x, y)];
43         }
44
45         const TileType &TypeAt(int surface, int x, int y) const;
46
47         /// Convert coordinates into a tile index.
48         int IndexOf(int surface, int x, int y) const {
49                 assert(0 <= surface && surface <= 5);
50                 assert(0 <= x && x <= sidelength);
51                 assert(0 <= y && y <= sidelength);
52                 return surface * TilesPerSurface() + y * SideLength() + x;
53         }
54         /// The length of the side of each surface.
55         int SideLength() const {
56                 return sidelength;
57         }
58         /// The number of tiles of one surface.
59         int TilesPerSurface() const {
60                 return SideLength() * SideLength();
61         }
62         /// Total number of tiles of all surfaces combined.
63         int TilesTotal() const {
64                 return 6 * TilesPerSurface();
65         }
66
67         glm::dvec3 TileCenter(int surface, int x, int y) const noexcept;
68
69         void BuildVAO(const Set<TileType> &);
70         void Draw(app::Assets &, graphics::Viewport &) override;
71
72 private:
73         int sidelength;
74         std::vector<Tile> tiles;
75
76         struct Attributes {
77                 glm::vec3 position;
78                 glm::vec3 tex_coord;
79         };
80         graphics::SimpleVAO<Attributes, unsigned int> vao;
81
82 };
83
84 void GenerateEarthlike(const Set<TileType> &, Planet &) noexcept;
85 void GenerateTest(const Set<TileType> &, Planet &) noexcept;
86
87 }
88 }
89
90 #endif