diff options
| author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-05-17 15:55:37 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-05-17 15:55:37 -0400 |
| commit | 90aadf3844386824140a20d7fbb847bc16009a94 (patch) | |
| tree | 6f83fce90e71abb22b1a8f3e09c79963b2a34d5d /src/systems/mapping.cpp | |
| parent | bc63fa57ced1c7329f7fdcfd168eaf7e290158bc (diff) | |
| parent | 86f0106d0523825549f1e74b835688c78a10cf6c (diff) | |
| download | therapy-90aadf3844386824140a20d7fbb847bc16009a94.tar.gz therapy-90aadf3844386824140a20d7fbb847bc16009a94.tar.bz2 therapy-90aadf3844386824140a20d7fbb847bc16009a94.zip | |
Merge pull request #7 from hatkirby/es-rewrite
The ECS rewrite exceeds the original branch in functionality, so it is time to merge it in.
Diffstat (limited to 'src/systems/mapping.cpp')
| -rw-r--r-- | src/systems/mapping.cpp | 187 |
1 files changed, 187 insertions, 0 deletions
| diff --git a/src/systems/mapping.cpp b/src/systems/mapping.cpp new file mode 100644 index 0000000..1275e11 --- /dev/null +++ b/src/systems/mapping.cpp | |||
| @@ -0,0 +1,187 @@ | |||
| 1 | #include "mapping.h" | ||
| 2 | #include "components/mappable.h" | ||
| 3 | #include "systems/realizing.h" | ||
| 4 | #include "game.h" | ||
| 5 | #include "consts.h" | ||
| 6 | |||
| 7 | template <typename Storage> | ||
| 8 | inline void addBoundary( | ||
| 9 | Storage& boundaries, | ||
| 10 | int axis, | ||
| 11 | int lower, | ||
| 12 | int upper, | ||
| 13 | MappableComponent::Boundary::Type type) | ||
| 14 | { | ||
| 15 | boundaries.emplace(std::piecewise_construct, | ||
| 16 | std::tie(axis), | ||
| 17 | std::tie(axis, lower, upper, type)); | ||
| 18 | } | ||
| 19 | |||
| 20 | void MappingSystem::render(Texture& texture) | ||
| 21 | { | ||
| 22 | id_type map = | ||
| 23 | game_.getSystemManager().getSystem<RealizingSystem>().getActiveMap(); | ||
| 24 | |||
| 25 | auto& mappable = game_.getEntityManager(). | ||
| 26 | getComponent<MappableComponent>(map); | ||
| 27 | |||
| 28 | for (int i = 0; i < MAP_WIDTH * MAP_HEIGHT; i++) | ||
| 29 | { | ||
| 30 | int x = i % MAP_WIDTH; | ||
| 31 | int y = i / MAP_WIDTH; | ||
| 32 | int tile = mappable.tiles[i]; | ||
| 33 | |||
| 34 | if (tile > 0) | ||
| 35 | { | ||
| 36 | Rectangle dst { | ||
| 37 | x * TILE_WIDTH, | ||
| 38 | y * TILE_HEIGHT, | ||
| 39 | TILE_WIDTH, | ||
| 40 | TILE_HEIGHT}; | ||
| 41 | |||
| 42 | Rectangle src { | ||
| 43 | (tile % TILESET_COLS) * TILE_WIDTH, | ||
| 44 | (tile / TILESET_COLS) * TILE_HEIGHT, | ||
| 45 | TILE_WIDTH, | ||
| 46 | TILE_HEIGHT}; | ||
| 47 | |||
| 48 | game_.getRenderer().blit( | ||
| 49 | mappable.tileset, | ||
| 50 | texture, | ||
| 51 | std::move(src), | ||
| 52 | std::move(dst)); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | int startX = ((GAME_WIDTH / TILE_WIDTH) / 2) - (mappable.title.size() / 2); | ||
| 57 | |||
| 58 | for (size_t i = 0; i < mappable.title.size(); i++) | ||
| 59 | { | ||
| 60 | Rectangle src { | ||
| 61 | (mappable.title[i] % FONT_COLS) * TILE_WIDTH, | ||
| 62 | (mappable.title[i] / FONT_COLS) * TILE_HEIGHT, | ||
| 63 | TILE_WIDTH, | ||
| 64 | TILE_HEIGHT}; | ||
| 65 | |||
| 66 | Rectangle dst { | ||
| 67 | (startX + static_cast<int>(i)) * TILE_WIDTH, | ||
| 68 | 24 * TILE_HEIGHT, | ||
| 69 | TILE_WIDTH, | ||
| 70 | TILE_HEIGHT}; | ||
| 71 | |||
| 72 | game_.getRenderer().blit( | ||
| 73 | mappable.font, | ||
| 74 | texture, | ||
| 75 | std::move(src), | ||
| 76 | std::move(dst)); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | void MappingSystem::generateBoundaries(id_type mapEntity) | ||
| 81 | { | ||
| 82 | auto& mappable = game_.getEntityManager(). | ||
| 83 | getComponent<MappableComponent>(mapEntity); | ||
| 84 | |||
| 85 | addBoundary( | ||
| 86 | mappable.leftBoundaries, | ||
| 87 | -WALL_GAP, | ||
| 88 | 0, | ||
| 89 | MAP_HEIGHT * TILE_HEIGHT, | ||
| 90 | MappableComponent::Boundary::Type::adjacency); | ||
| 91 | |||
| 92 | addBoundary( | ||
| 93 | mappable.rightBoundaries, | ||
| 94 | GAME_WIDTH + WALL_GAP, | ||
| 95 | 0, | ||
| 96 | MAP_HEIGHT * TILE_HEIGHT, | ||
| 97 | MappableComponent::Boundary::Type::adjacency); | ||
| 98 | |||
| 99 | addBoundary( | ||
| 100 | mappable.upBoundaries, | ||
| 101 | -WALL_GAP, | ||
| 102 | 0, | ||
| 103 | GAME_WIDTH, | ||
| 104 | MappableComponent::Boundary::Type::adjacency); | ||
| 105 | |||
| 106 | addBoundary( | ||
| 107 | mappable.downBoundaries, | ||
| 108 | MAP_HEIGHT * TILE_HEIGHT + WALL_GAP, | ||
| 109 | 0, | ||
| 110 | GAME_WIDTH, | ||
| 111 | MappableComponent::Boundary::Type::adjacency); | ||
| 112 | |||
| 113 | for (size_t i = 0; i < MAP_WIDTH * MAP_HEIGHT; i++) | ||
| 114 | { | ||
| 115 | size_t x = i % MAP_WIDTH; | ||
| 116 | size_t y = i / MAP_WIDTH; | ||
| 117 | int tile = mappable.tiles[i]; | ||
| 118 | |||
| 119 | if ((tile >= 5) && (tile <= 7)) | ||
| 120 | { | ||
| 121 | addBoundary( | ||
| 122 | mappable.downBoundaries, | ||
| 123 | y * TILE_HEIGHT, | ||
| 124 | x * TILE_WIDTH, | ||
| 125 | (x + 1) * TILE_WIDTH, | ||
| 126 | MappableComponent::Boundary::Type::platform); | ||
| 127 | } else if ((tile > 0) && (tile < 28)) | ||
| 128 | { | ||
| 129 | addBoundary( | ||
| 130 | mappable.rightBoundaries, | ||
| 131 | x * TILE_WIDTH, | ||
| 132 | y * TILE_HEIGHT, | ||
| 133 | (y+1) * TILE_HEIGHT, | ||
| 134 | MappableComponent::Boundary::Type::wall); | ||
| 135 | |||
| 136 | addBoundary( | ||
| 137 | mappable.leftBoundaries, | ||
| 138 | (x+1) * TILE_WIDTH, | ||
| 139 | y * TILE_HEIGHT, | ||
| 140 | (y+1) * TILE_HEIGHT, | ||
| 141 | MappableComponent::Boundary::Type::wall); | ||
| 142 | |||
| 143 | addBoundary( | ||
| 144 | mappable.downBoundaries, | ||
| 145 | y * TILE_HEIGHT, | ||
| 146 | x * TILE_WIDTH, | ||
| 147 | (x+1) * TILE_WIDTH, | ||
| 148 | MappableComponent::Boundary::Type::wall); | ||
| 149 | |||
| 150 | addBoundary( | ||
| 151 | mappable.upBoundaries, | ||
| 152 | (y+1) * TILE_HEIGHT, | ||
| 153 | x * TILE_WIDTH, | ||
| 154 | (x+1) * TILE_WIDTH, | ||
| 155 | MappableComponent::Boundary::Type::wall); | ||
| 156 | } else if (tile == 42) | ||
| 157 | { | ||
| 158 | addBoundary( | ||
| 159 | mappable.rightBoundaries, | ||
| 160 | x * TILE_WIDTH, | ||
| 161 | y * TILE_HEIGHT, | ||
| 162 | (y+1) * TILE_HEIGHT, | ||
| 163 | MappableComponent::Boundary::Type::danger); | ||
| 164 | |||
| 165 | addBoundary( | ||
| 166 | mappable.leftBoundaries, | ||
| 167 | (x+1) * TILE_WIDTH, | ||
| 168 | y * TILE_HEIGHT, | ||
| 169 | (y+1) * TILE_HEIGHT, | ||
| 170 | MappableComponent::Boundary::Type::danger); | ||
| 171 | |||
| 172 | addBoundary( | ||
| 173 | mappable.downBoundaries, | ||
| 174 | y * TILE_HEIGHT + 1, | ||
| 175 | x * TILE_WIDTH, | ||
| 176 | (x+1) * TILE_WIDTH, | ||
| 177 | MappableComponent::Boundary::Type::danger); | ||
| 178 | |||
| 179 | addBoundary( | ||
| 180 | mappable.upBoundaries, | ||
| 181 | (y+1) * TILE_HEIGHT, | ||
| 182 | x * TILE_WIDTH, | ||
| 183 | (x+1) * TILE_WIDTH, | ||
| 184 | MappableComponent::Boundary::Type::danger); | ||
| 185 | } | ||
| 186 | } | ||
| 187 | } | ||
