From 77be863f4f15d2481a64e4e8dadb4060a6e4e590 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 11 Feb 2018 12:34:52 -0500 Subject: Implemented map rendering and basic collision Only wall and platform collision currently works, and map edges are not currently implemented. --- src/systems/mapping.cpp | 143 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 src/systems/mapping.cpp (limited to 'src/systems/mapping.cpp') 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 @@ +#include "mapping.h" +#include "components/mappable.h" +#include "game.h" +#include "consts.h" + +#include + +template +inline void addBoundary( + Storage& boundaries, + int axis, + int lower, + int upper, + MappableComponent::Boundary::Type type) +{ + boundaries.emplace(std::piecewise_construct, + std::tie(axis), + std::tie(axis, lower, upper, type)); +} + +void MappingSystem::render(Texture& texture) +{ + auto entities = game_.getEntityManager().getEntitiesWithComponents< + MappableComponent>(); + + for (id_type entity : entities) + { + auto& mappable = game_.getEntityManager(). + getComponent(entity); + + const Map& map = game_.getWorld().getMap(mappable.getMapId()); + + for (int i = 0; i < MAP_WIDTH * MAP_HEIGHT; i++) + { + int x = i % MAP_WIDTH; + int y = i / MAP_WIDTH; + int tile = map.getTiles()[i]; + + if (tile > 0) + { + Rectangle dst { + x * TILE_WIDTH, + y * TILE_HEIGHT, + TILE_WIDTH, + TILE_HEIGHT}; + + Rectangle src { + (tile % TILESET_COLS) * TILE_WIDTH, + (tile / TILESET_COLS) * TILE_HEIGHT, + TILE_WIDTH, + TILE_HEIGHT}; + + texture.blit(mappable.getTileset(), std::move(src), std::move(dst)); + } + } + + int startX = ((GAME_WIDTH / TILE_WIDTH) / 2) - (map.getTitle().size() / 2); + for (size_t i = 0; i < map.getTitle().size(); i++) + { + Rectangle src { + (map.getTitle()[i] % FONT_COLS) * TILE_WIDTH, + (map.getTitle()[i] / FONT_COLS) * TILE_HEIGHT, + TILE_WIDTH, + TILE_HEIGHT}; + + Rectangle dst { + (startX + static_cast(i)) * TILE_WIDTH, + 24 * TILE_HEIGHT, + TILE_WIDTH, + TILE_HEIGHT}; + + texture.blit(mappable.getFont(), std::move(src), std::move(dst)); + } + } +} + +void MappingSystem::loadMap(size_t mapId) +{ + id_type mapEntity = game_.getEntityManager().emplaceEntity(); + + auto& mappable = game_.getEntityManager(). + emplaceComponent(mapEntity, + Texture("res/tiles.png"), + Texture("res/font.bmp")); + + mappable.setMapId(mapId); + + const Map& map = game_.getWorld().getMap(mappable.getMapId()); + + for (size_t i = 0; i < MAP_WIDTH * MAP_HEIGHT; i++) + { + size_t x = i % MAP_WIDTH; + size_t y = i / MAP_WIDTH; + int tile = map.getTiles()[i]; + + if ((tile >= 5) && (tile <= 7)) + { + addBoundary( + mappable.getDownBoundaries(), + y * TILE_HEIGHT, + x * TILE_WIDTH, + (x + 1) * TILE_WIDTH, + MappableComponent::Boundary::Type::platform); + } else if ((tile > 0) && (tile < 28)) + { + addBoundary( + mappable.getRightBoundaries(), + x * TILE_WIDTH, + y * TILE_HEIGHT, + (y+1) * TILE_HEIGHT, + MappableComponent::Boundary::Type::wall); + + addBoundary( + mappable.getLeftBoundaries(), + (x+1) * TILE_WIDTH, + y * TILE_HEIGHT, + (y+1) * TILE_HEIGHT, + MappableComponent::Boundary::Type::wall); + + addBoundary( + mappable.getDownBoundaries(), + y * TILE_HEIGHT, + x * TILE_WIDTH, + (x+1) * TILE_WIDTH, + MappableComponent::Boundary::Type::wall); + + addBoundary( + mappable.getUpBoundaries(), + (y+1) * TILE_HEIGHT, + x * TILE_WIDTH, + (x+1) * TILE_WIDTH, + MappableComponent::Boundary::Type::wall); + } else if (tile == 42) + { + addBoundary( + mappable.getDownBoundaries(), + y * TILE_HEIGHT, + x * TILE_WIDTH, + (x+1) * TILE_WIDTH, + MappableComponent::Boundary::Type::danger); + } + } +} -- cgit 1.4.1