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