summary refs log tree commit diff stats
path: root/src/systems/mapping.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/systems/mapping.cpp')
-rw-r--r--src/systems/mapping.cpp141
1 files changed, 141 insertions, 0 deletions
diff --git a/src/systems/mapping.cpp b/src/systems/mapping.cpp new file mode 100644 index 0000000..5b63ded --- /dev/null +++ b/src/systems/mapping.cpp
@@ -0,0 +1,141 @@
1#include "mapping.h"
2#include "components/mappable.h"
3#include "game.h"
4#include "consts.h"
5
6template <typename Storage>
7inline void addBoundary(
8 Storage& boundaries,
9 int axis,
10 int lower,
11 int upper,
12 MappableComponent::Boundary::Type type)
13{
14 boundaries.emplace(std::piecewise_construct,
15 std::tie(axis),
16 std::tie(axis, lower, upper, type));
17}
18
19void MappingSystem::render(Texture& texture)
20{
21 auto entities = game_.getEntityManager().getEntitiesWithComponents<
22 MappableComponent>();
23
24 for (id_type entity : entities)
25 {
26 auto& mappable = game_.getEntityManager().
27 getComponent<MappableComponent>(entity);
28
29 const Map& map = game_.getWorld().getMap(mappable.getMapId());
30
31 for (int i = 0; i < MAP_WIDTH * MAP_HEIGHT; i++)
32 {
33 int x = i % MAP_WIDTH;
34 int y = i / MAP_WIDTH;
35 int tile = map.getTiles()[i];
36
37 if (tile > 0)
38 {
39 Rectangle dst {
40 x * TILE_WIDTH,
41 y * TILE_HEIGHT,
42 TILE_WIDTH,
43 TILE_HEIGHT};
44
45 Rectangle src {
46 (tile % TILESET_COLS) * TILE_WIDTH,
47 (tile / TILESET_COLS) * TILE_HEIGHT,
48 TILE_WIDTH,
49 TILE_HEIGHT};
50
51 texture.blit(mappable.getTileset(), std::move(src), std::move(dst));
52 }
53 }
54
55 int startX = ((GAME_WIDTH / TILE_WIDTH) / 2) - (map.getTitle().size() / 2);
56 for (size_t i = 0; i < map.getTitle().size(); i++)
57 {
58 Rectangle src {
59 (map.getTitle()[i] % FONT_COLS) * TILE_WIDTH,
60 (map.getTitle()[i] / FONT_COLS) * TILE_HEIGHT,
61 TILE_WIDTH,
62 TILE_HEIGHT};
63
64 Rectangle dst {
65 (startX + static_cast<int>(i)) * TILE_WIDTH,
66 24 * TILE_HEIGHT,
67 TILE_WIDTH,
68 TILE_HEIGHT};
69
70 texture.blit(mappable.getFont(), std::move(src), std::move(dst));
71 }
72 }
73}
74
75void MappingSystem::loadMap(size_t mapId)
76{
77 id_type mapEntity = game_.getEntityManager().emplaceEntity();
78
79 auto& mappable = game_.getEntityManager().
80 emplaceComponent<MappableComponent>(mapEntity,
81 Texture("res/tiles.png"),
82 Texture("res/font.bmp"));
83
84 mappable.setMapId(mapId);
85
86 const Map& map = game_.getWorld().getMap(mappable.getMapId());
87
88 for (size_t i = 0; i < MAP_WIDTH * MAP_HEIGHT; i++)
89 {
90 size_t x = i % MAP_WIDTH;
91 size_t y = i / MAP_WIDTH;
92 int tile = map.getTiles()[i];
93
94 if ((tile >= 5) && (tile <= 7))
95 {
96 addBoundary(
97 mappable.getDownBoundaries(),
98 y * TILE_HEIGHT,
99 x * TILE_WIDTH,
100 (x + 1) * TILE_WIDTH,
101 MappableComponent::Boundary::Type::platform);
102 } else if ((tile > 0) && (tile < 28))
103 {
104 addBoundary(
105 mappable.getRightBoundaries(),
106 x * TILE_WIDTH,
107 y * TILE_HEIGHT,
108 (y+1) * TILE_HEIGHT,
109 MappableComponent::Boundary::Type::wall);
110
111 addBoundary(
112 mappable.getLeftBoundaries(),
113 (x+1) * TILE_WIDTH,
114 y * TILE_HEIGHT,
115 (y+1) * TILE_HEIGHT,
116 MappableComponent::Boundary::Type::wall);
117
118 addBoundary(
119 mappable.getDownBoundaries(),
120 y * TILE_HEIGHT,
121 x * TILE_WIDTH,
122 (x+1) * TILE_WIDTH,
123 MappableComponent::Boundary::Type::wall);
124
125 addBoundary(
126 mappable.getUpBoundaries(),
127 (y+1) * TILE_HEIGHT,
128 x * TILE_WIDTH,
129 (x+1) * TILE_WIDTH,
130 MappableComponent::Boundary::Type::wall);
131 } else if (tile == 42)
132 {
133 addBoundary(
134 mappable.getDownBoundaries(),
135 y * TILE_HEIGHT,
136 x * TILE_WIDTH,
137 (x+1) * TILE_WIDTH,
138 MappableComponent::Boundary::Type::danger);
139 }
140 }
141}