summary refs log tree commit diff stats
path: root/src/systems/mapping.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2018-04-28 09:22:44 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2018-04-28 09:22:44 -0400
commit8016a7146fec3f6f43ca05723441750e5aae3d4d (patch)
tree0d527c1af80cf9ac34a027f9ee6f1acbb95db9f4 /src/systems/mapping.cpp
parentf782b81ba10c9b7a1e221b16de0aaa7b6c521729 (diff)
downloadtherapy-8016a7146fec3f6f43ca05723441750e5aae3d4d.tar.gz
therapy-8016a7146fec3f6f43ca05723441750e5aae3d4d.tar.bz2
therapy-8016a7146fec3f6f43ca05723441750e5aae3d4d.zip
Restructured the way the world is loaded
The World class was removed and replaced by the RealizingSystem and RealizableComponent. The realizable entity is intended to be a singleton and to represent the world. The Map class was also removed and integrated into the MappableComponent.

These changes are to facilitate implementation of map objects without needing special intermediary objects (including the Map class). Now, map entities are created as soon as the world is created, and map object entities will be as well. They will simply be deactivated while the map is not active. Multiple players are now slightly better supported, which will be important in the future.

This will likely become inefficient as the world becomes bigger, and some sort of sector-loading process will have to be designed. This also reduces the usefulness of EntityManager's entity-searching capabilities (which are not the most efficiently implemented currently anyway), and will likely in the future require some added functionality to better search subsets of entities.

A lot of the components were also rewritten to use bare member variables instead of accessor methods, as they never had special functionality and just took up space. These components were also documented.
Diffstat (limited to 'src/systems/mapping.cpp')
-rw-r--r--src/systems/mapping.cpp127
1 files changed, 60 insertions, 67 deletions
diff --git a/src/systems/mapping.cpp b/src/systems/mapping.cpp index a3a17ec..af67aed 100644 --- a/src/systems/mapping.cpp +++ b/src/systems/mapping.cpp
@@ -1,5 +1,7 @@
1#include "mapping.h" 1#include "mapping.h"
2#include "components/mappable.h" 2#include "components/mappable.h"
3#include "components/realizable.h"
4#include "systems/realizing.h"
3#include "game.h" 5#include "game.h"
4#include "consts.h" 6#include "consts.h"
5 7
@@ -18,104 +20,95 @@ inline void addBoundary(
18 20
19void MappingSystem::render(Texture& texture) 21void MappingSystem::render(Texture& texture)
20{ 22{
21 auto entities = game_.getEntityManager().getEntitiesWithComponents< 23 auto& realizable = game_.getEntityManager().
22 MappableComponent>(); 24 getComponent<RealizableComponent>(
25 game_.getSystemManager().getSystem<RealizingSystem>().getSingleton());
23 26
24 for (id_type entity : entities) 27 id_type map = realizable.activeMap;
25 {
26 auto& mappable = game_.getEntityManager().
27 getComponent<MappableComponent>(entity);
28 28
29 const Map& map = game_.getWorld().getMap(mappable.getMapId()); 29 auto& mappable = game_.getEntityManager().
30 getComponent<MappableComponent>(map);
30 31
31 for (int i = 0; i < MAP_WIDTH * MAP_HEIGHT; i++) 32 for (int i = 0; i < MAP_WIDTH * MAP_HEIGHT; i++)
32 { 33 {
33 int x = i % MAP_WIDTH; 34 int x = i % MAP_WIDTH;
34 int y = i / MAP_WIDTH; 35 int y = i / MAP_WIDTH;
35 int tile = map.getTiles()[i]; 36 int tile = mappable.tiles[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 game_.getRenderer().blit(
52 mappable.getTileset(),
53 texture,
54 std::move(src),
55 std::move(dst));
56 }
57 }
58 37
59 int startX = ((GAME_WIDTH / TILE_WIDTH) / 2) - (map.getTitle().size() / 2); 38 if (tile > 0)
60 for (size_t i = 0; i < map.getTitle().size(); i++)
61 { 39 {
62 Rectangle src { 40 Rectangle dst {
63 (map.getTitle()[i] % FONT_COLS) * TILE_WIDTH, 41 x * TILE_WIDTH,
64 (map.getTitle()[i] / FONT_COLS) * TILE_HEIGHT, 42 y * TILE_HEIGHT,
65 TILE_WIDTH, 43 TILE_WIDTH,
66 TILE_HEIGHT}; 44 TILE_HEIGHT};
67 45
68 Rectangle dst { 46 Rectangle src {
69 (startX + static_cast<int>(i)) * TILE_WIDTH, 47 (tile % TILESET_COLS) * TILE_WIDTH,
70 24 * TILE_HEIGHT, 48 (tile / TILESET_COLS) * TILE_HEIGHT,
71 TILE_WIDTH, 49 TILE_WIDTH,
72 TILE_HEIGHT}; 50 TILE_HEIGHT};
73 51
74 game_.getRenderer().blit( 52 game_.getRenderer().blit(
75 mappable.getFont(), 53 mappable.tileset,
76 texture, 54 texture,
77 std::move(src), 55 std::move(src),
78 std::move(dst)); 56 std::move(dst));
79 } 57 }
80 } 58 }
59
60 int startX = ((GAME_WIDTH / TILE_WIDTH) / 2) - (mappable.title.size() / 2);
61
62 for (size_t i = 0; i < mappable.title.size(); i++)
63 {
64 Rectangle src {
65 (mappable.title[i] % FONT_COLS) * TILE_WIDTH,
66 (mappable.title[i] / FONT_COLS) * TILE_HEIGHT,
67 TILE_WIDTH,
68 TILE_HEIGHT};
69
70 Rectangle dst {
71 (startX + static_cast<int>(i)) * TILE_WIDTH,
72 24 * TILE_HEIGHT,
73 TILE_WIDTH,
74 TILE_HEIGHT};
75
76 game_.getRenderer().blit(
77 mappable.font,
78 texture,
79 std::move(src),
80 std::move(dst));
81 }
81} 82}
82 83
83void MappingSystem::loadMap(size_t mapId) 84void MappingSystem::generateBoundaries(id_type mapEntity)
84{ 85{
85 id_type mapEntity = game_.getEntityManager().emplaceEntity();
86
87 auto& mappable = game_.getEntityManager(). 86 auto& mappable = game_.getEntityManager().
88 emplaceComponent<MappableComponent>(mapEntity, 87 getComponent<MappableComponent>(mapEntity);
89 Texture("res/tiles.png"),
90 Texture("res/font.bmp"));
91
92 mappable.setMapId(mapId);
93
94 const Map& map = game_.getWorld().getMap(mappable.getMapId());
95 88
96 addBoundary( 89 addBoundary(
97 mappable.getLeftBoundaries(), 90 mappable.leftBoundaries,
98 -WALL_GAP, 91 -WALL_GAP,
99 0, 92 0,
100 MAP_HEIGHT * TILE_HEIGHT, 93 MAP_HEIGHT * TILE_HEIGHT,
101 MappableComponent::Boundary::Type::adjacency); 94 MappableComponent::Boundary::Type::adjacency);
102 95
103 addBoundary( 96 addBoundary(
104 mappable.getRightBoundaries(), 97 mappable.rightBoundaries,
105 GAME_WIDTH + WALL_GAP, 98 GAME_WIDTH + WALL_GAP,
106 0, 99 0,
107 MAP_HEIGHT * TILE_HEIGHT, 100 MAP_HEIGHT * TILE_HEIGHT,
108 MappableComponent::Boundary::Type::adjacency); 101 MappableComponent::Boundary::Type::adjacency);
109 102
110 addBoundary( 103 addBoundary(
111 mappable.getUpBoundaries(), 104 mappable.upBoundaries,
112 -WALL_GAP, 105 -WALL_GAP,
113 0, 106 0,
114 GAME_WIDTH, 107 GAME_WIDTH,
115 MappableComponent::Boundary::Type::adjacency); 108 MappableComponent::Boundary::Type::adjacency);
116 109
117 addBoundary( 110 addBoundary(
118 mappable.getDownBoundaries(), 111 mappable.downBoundaries,
119 MAP_HEIGHT * TILE_HEIGHT + WALL_GAP, 112 MAP_HEIGHT * TILE_HEIGHT + WALL_GAP,
120 0, 113 0,
121 GAME_WIDTH, 114 GAME_WIDTH,
@@ -125,12 +118,12 @@ void MappingSystem::loadMap(size_t mapId)
125 { 118 {
126 size_t x = i % MAP_WIDTH; 119 size_t x = i % MAP_WIDTH;
127 size_t y = i / MAP_WIDTH; 120 size_t y = i / MAP_WIDTH;
128 int tile = map.getTiles()[i]; 121 int tile = mappable.tiles[i];
129 122
130 if ((tile >= 5) && (tile <= 7)) 123 if ((tile >= 5) && (tile <= 7))
131 { 124 {
132 addBoundary( 125 addBoundary(
133 mappable.getDownBoundaries(), 126 mappable.downBoundaries,
134 y * TILE_HEIGHT, 127 y * TILE_HEIGHT,
135 x * TILE_WIDTH, 128 x * TILE_WIDTH,
136 (x + 1) * TILE_WIDTH, 129 (x + 1) * TILE_WIDTH,
@@ -138,28 +131,28 @@ void MappingSystem::loadMap(size_t mapId)
138 } else if ((tile > 0) && (tile < 28)) 131 } else if ((tile > 0) && (tile < 28))
139 { 132 {
140 addBoundary( 133 addBoundary(
141 mappable.getRightBoundaries(), 134 mappable.rightBoundaries,
142 x * TILE_WIDTH, 135 x * TILE_WIDTH,
143 y * TILE_HEIGHT, 136 y * TILE_HEIGHT,
144 (y+1) * TILE_HEIGHT, 137 (y+1) * TILE_HEIGHT,
145 MappableComponent::Boundary::Type::wall); 138 MappableComponent::Boundary::Type::wall);
146 139
147 addBoundary( 140 addBoundary(
148 mappable.getLeftBoundaries(), 141 mappable.leftBoundaries,
149 (x+1) * TILE_WIDTH, 142 (x+1) * TILE_WIDTH,
150 y * TILE_HEIGHT, 143 y * TILE_HEIGHT,
151 (y+1) * TILE_HEIGHT, 144 (y+1) * TILE_HEIGHT,
152 MappableComponent::Boundary::Type::wall); 145 MappableComponent::Boundary::Type::wall);
153 146
154 addBoundary( 147 addBoundary(
155 mappable.getDownBoundaries(), 148 mappable.downBoundaries,
156 y * TILE_HEIGHT, 149 y * TILE_HEIGHT,
157 x * TILE_WIDTH, 150 x * TILE_WIDTH,
158 (x+1) * TILE_WIDTH, 151 (x+1) * TILE_WIDTH,
159 MappableComponent::Boundary::Type::wall); 152 MappableComponent::Boundary::Type::wall);
160 153
161 addBoundary( 154 addBoundary(
162 mappable.getUpBoundaries(), 155 mappable.upBoundaries,
163 (y+1) * TILE_HEIGHT, 156 (y+1) * TILE_HEIGHT,
164 x * TILE_WIDTH, 157 x * TILE_WIDTH,
165 (x+1) * TILE_WIDTH, 158 (x+1) * TILE_WIDTH,
@@ -167,28 +160,28 @@ void MappingSystem::loadMap(size_t mapId)
167 } else if (tile == 42) 160 } else if (tile == 42)
168 { 161 {
169 addBoundary( 162 addBoundary(
170 mappable.getRightBoundaries(), 163 mappable.rightBoundaries,
171 x * TILE_WIDTH, 164 x * TILE_WIDTH,
172 y * TILE_HEIGHT, 165 y * TILE_HEIGHT,
173 (y+1) * TILE_HEIGHT, 166 (y+1) * TILE_HEIGHT,
174 MappableComponent::Boundary::Type::danger); 167 MappableComponent::Boundary::Type::danger);
175 168
176 addBoundary( 169 addBoundary(
177 mappable.getLeftBoundaries(), 170 mappable.leftBoundaries,
178 (x+1) * TILE_WIDTH, 171 (x+1) * TILE_WIDTH,
179 y * TILE_HEIGHT, 172 y * TILE_HEIGHT,
180 (y+1) * TILE_HEIGHT, 173 (y+1) * TILE_HEIGHT,
181 MappableComponent::Boundary::Type::danger); 174 MappableComponent::Boundary::Type::danger);
182 175
183 addBoundary( 176 addBoundary(
184 mappable.getDownBoundaries(), 177 mappable.downBoundaries,
185 y * TILE_HEIGHT, 178 y * TILE_HEIGHT,
186 x * TILE_WIDTH, 179 x * TILE_WIDTH,
187 (x+1) * TILE_WIDTH, 180 (x+1) * TILE_WIDTH,
188 MappableComponent::Boundary::Type::danger); 181 MappableComponent::Boundary::Type::danger);
189 182
190 addBoundary( 183 addBoundary(
191 mappable.getUpBoundaries(), 184 mappable.upBoundaries,
192 (y+1) * TILE_HEIGHT, 185 (y+1) * TILE_HEIGHT,
193 x * TILE_WIDTH, 186 x * TILE_WIDTH,
194 (x+1) * TILE_WIDTH, 187 (x+1) * TILE_WIDTH,