From 2ec163612042bfa5e4e1bf220b489506f7039677 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Wed, 18 Mar 2015 09:48:43 -0400 Subject: Game can now read map file from map editor (also added new map) --- res/maps.xml | 32 ++++- src/components/map_collision.cpp | 29 ++++- src/components/map_collision.h | 3 +- src/components/map_render.cpp | 1 + src/components/physics_body.cpp | 1 + src/components/player_physics.cpp | 1 + src/consts.h | 14 ++ src/game.cpp | 168 +++++++++++++++++++++++- src/game.h | 21 ++- src/main.cpp | 2 +- src/map.cpp | 263 +++++++++++++++++++------------------- src/map.h | 56 +++++--- src/renderer.cpp | 2 +- 13 files changed, 421 insertions(+), 172 deletions(-) create mode 100644 src/consts.h diff --git a/res/maps.xml b/res/maps.xml index dff3775..594cd18 100644 --- a/res/maps.xml +++ b/res/maps.xml @@ -1,5 +1,5 @@ -410Everything Is Embarrassing0,0,0,0,0,0,0,0,0,0,19,0,0,0,0,0,20,0,0,0,0,0,0,0,18,9,8,10,8,11,8,10,10,8,11,8,9,10,21,0, +540Everything Is Embarrassing0,0,0,0,0,0,0,0,0,0,19,0,0,0,0,0,20,0,0,0,0,0,0,0,18,9,8,10,8,11,8,10,10,8,11,8,9,10,21,0, 0,0,0,0,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,18,8,8,17,0,0,0,0,0,0,0,0,0,0,0,0,0,22,21, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,18,8,8,8,8,17,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12, 0,19,0,0,0,0,0,0,0,0,0,0,0,18,8,8,17,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12, @@ -23,7 +23,7 @@ 0,0,0,0,24,3,1,2,1,1,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,19,0,27,1,3,2,26,0,0,0,0,0,0, 1,2,3,1,25,0,0,0,0,0,20,0,0,0,0,0,0,19,0,0,0,0,0,0,20,0,0,0,0,0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,0,0,0,14,0,0,0,0,0,0,0,0,0,0,13,0,0,0,0,0,19,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,14,0,0,0,30,31,32,41,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,20,0,0,0,0,0,0,0,0,0,0,15,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,19,0,0,0, +0,0,0,0,0,0,0,0,0,0,20,0,0,0,0,0,0,15,0,0,0,0,0,0,0,0,0,0,12,0,0,0,19,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,0,0,0,0,0,0,0,0,0,0,13,0,0,0,0,0,0,0,0,0,0,0, +0,0,20,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,20,0,0,0,0,15,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,19,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,0,0,0,0,0,0,0,0,0,0,13,0,0,0,0,0,0,0,0,20,0,0, +0,0,0,0,0,0,20,0,0,0,0,0,0,0,0,0,0,15,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,20,0,0,15,0,0,0,0,0,0,0,0,0,0,13,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,15,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0, + diff --git a/src/components/map_collision.cpp b/src/components/map_collision.cpp index 9afa6f8..9adad26 100644 --- a/src/components/map_collision.cpp +++ b/src/components/map_collision.cpp @@ -1,11 +1,14 @@ #include "map_collision.h" #include "map.h" #include "game.h" +#include "consts.h" MapCollisionComponent::MapCollisionComponent(const Map& map) : map(map) { - addCollision(-6, 0, GAME_WIDTH, Direction::left, (map.getLeftMap() == nullptr) ? Collision::Type::wrap : Collision::Type::teleport); - addCollision(GAME_WIDTH+6, 0, GAME_WIDTH, Direction::right, (map.getRightMap() == nullptr) ? Collision::Type::reverse : Collision::Type::teleport); + addCollision(-6, 0, GAME_HEIGHT, Direction::left, collisionFromMoveType(map.getLeftMoveType())); + addCollision(GAME_WIDTH+6, 0, GAME_HEIGHT, Direction::right, collisionFromMoveType(map.getRightMoveType())); + addCollision(-7, 0, GAME_WIDTH, Direction::up, collisionFromMoveType(map.getUpMoveType())); + addCollision(GAME_HEIGHT+6, 0, GAME_WIDTH, Direction::down, collisionFromMoveType(map.getDownMoveType())); for (int i=0; i -class Map; class Game; class MapCollisionComponent : public Component { @@ -35,6 +35,7 @@ class MapCollisionComponent : public Component { void addCollision(double axis, double lower, double upper, Direction dir, Collision::Type type); void processCollision(Game& game, Entity& collider, Collision collision, Direction dir, std::pair old_position); + Collision::Type collisionFromMoveType(Map::MoveType type); std::list left_collisions; std::list right_collisions; diff --git a/src/components/map_render.cpp b/src/components/map_render.cpp index 10c5db8..45766e1 100644 --- a/src/components/map_render.cpp +++ b/src/components/map_render.cpp @@ -1,6 +1,7 @@ #include "map_render.h" #include "map.h" #include "game.h" +#include "consts.h" MapRenderComponent::MapRenderComponent(const Map& map) : screen(GAME_WIDTH, GAME_HEIGHT) { diff --git a/src/components/physics_body.cpp b/src/components/physics_body.cpp index 72f2fd8..acbdc5d 100644 --- a/src/components/physics_body.cpp +++ b/src/components/physics_body.cpp @@ -1,5 +1,6 @@ #include "physics_body.h" #include "game.h" +#include "consts.h" void PhysicsBodyComponent::receive(Game&, Entity&, const Message& msg) { diff --git a/src/components/player_physics.cpp b/src/components/player_physics.cpp index e366d6e..1d14f35 100644 --- a/src/components/player_physics.cpp +++ b/src/components/player_physics.cpp @@ -1,6 +1,7 @@ #include "player_physics.h" #include "muxer.h" #include "game.h" +#include "consts.h" #define JUMP_VELOCITY(h, l) (-2 * (h) / (l)) #define JUMP_GRAVITY(h, l) (2 * ((h) / (l)) / (l)) diff --git a/src/consts.h b/src/consts.h new file mode 100644 index 0000000..804c761 --- /dev/null +++ b/src/consts.h @@ -0,0 +1,14 @@ +#ifndef CONSTS_H +#define CONSTS_H + +const int TILE_WIDTH = 8; +const int TILE_HEIGHT = 8; +const int GAME_WIDTH = 320; +const int GAME_HEIGHT = 200; +const int MAP_WIDTH = GAME_WIDTH/TILE_WIDTH; +const int MAP_HEIGHT = GAME_HEIGHT/TILE_HEIGHT - 1; + +const int FRAMES_PER_SECOND = 60; +const double SECONDS_PER_FRAME = 1.0 / FRAMES_PER_SECOND; + +#endif diff --git a/src/game.cpp b/src/game.cpp index 1c9bb58..4a08744 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1,4 +1,6 @@ #include "game.h" +#include +#include #include "renderer.h" #include "muxer.h" #include "map.h" @@ -7,11 +9,166 @@ #include "components/player_sprite.h" #include "components/map_render.h" #include "components/map_collision.h" +#include "consts.h" -Game::Game() +Game::Game(const char* mapfile) { + // Load maps + xmlDocPtr doc = xmlParseFile(mapfile); + if (doc == nullptr) + { + exit(2); + } + + xmlNodePtr top = xmlDocGetRootElement(doc); + if (top == nullptr) + { + exit(2); + } + + if (xmlStrcmp(top->name, (const xmlChar*) "world")) + { + exit(2); + } + + for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next) + { + if (!xmlStrcmp(node->name, (const xmlChar*) "startpos")) + { + xmlChar* idKey = xmlGetProp(node, (xmlChar*) "id"); + if (idKey == 0) exit(2); + startMap = atoi((char*) idKey); + xmlFree(idKey); + + xmlChar* posKey = xmlGetProp(node, (xmlChar*) "pos"); + if (posKey == 0) exit(2); + sscanf((char*) posKey, "%d,%d", &startPos.first, &startPos.second); + xmlFree(posKey); + } else if (!xmlStrcmp(node->name, (const xmlChar*) "map")) + { + xmlChar* idKey = xmlGetProp(node, (xmlChar*) "id"); + if (idKey == 0) exit(2); + int theId = atoi((char*) idKey); + xmlFree(idKey); + + Map map {theId}; + + for (xmlNodePtr mapNode = node->xmlChildrenNode; mapNode != NULL; mapNode = mapNode->next) + { + if (!xmlStrcmp(mapNode->name, (const xmlChar*) "name")) + { + xmlChar* key = xmlNodeListGetString(doc, mapNode->xmlChildrenNode, 1); + if (key != 0) + { + map.setTitle((char*) key); + } + + xmlFree(key); + } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "environment")) + { + xmlChar* key = xmlNodeListGetString(doc, mapNode->xmlChildrenNode, 1); + int* mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); + mapdata[0] = atoi(strtok((char*) key, ",\n")); + for (int i=1; i<(MAP_WIDTH*MAP_HEIGHT); i++) + { + mapdata[i] = atoi(strtok(NULL, ",\n")); + } + map.setMapdata(mapdata); + xmlFree(key); + } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "leftmap")) + { + xmlChar* typeKey = xmlGetProp(mapNode, (xmlChar*) "type"); + if (typeKey == 0) exit(2); + map.setLeftMoveType(Map::moveTypeForShort((char*) typeKey)); + xmlFree(typeKey); + + if (Map::moveTypeTakesMap(map.getLeftMoveType())) + { + xmlChar* idKey = xmlGetProp(mapNode, (xmlChar*) "map"); + if (idKey == 0) exit(2); + map.setLeftMapID(atoi((char*) idKey)); + xmlFree(idKey); + } + } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "rightmap")) + { + xmlChar* typeKey = xmlGetProp(mapNode, (xmlChar*) "type"); + if (typeKey == 0) exit(2); + map.setRightMoveType(Map::moveTypeForShort((char*) typeKey)); + xmlFree(typeKey); + + if (Map::moveTypeTakesMap(map.getRightMoveType())) + { + xmlChar* idKey = xmlGetProp(mapNode, (xmlChar*) "map"); + if (idKey == 0) exit(2); + map.setRightMapID(atoi((char*) idKey)); + xmlFree(idKey); + } + } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "upmap")) + { + xmlChar* typeKey = xmlGetProp(mapNode, (xmlChar*) "type"); + if (typeKey == 0) exit(2); + map.setUpMoveType(Map::moveTypeForShort((char*) typeKey)); + xmlFree(typeKey); + + if (Map::moveTypeTakesMap(map.getUpMoveType())) + { + xmlChar* idKey = xmlGetProp(mapNode, (xmlChar*) "map"); + if (idKey == 0) exit(2); + map.setUpMapID(atoi((char*) idKey)); + xmlFree(idKey); + } + } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "downmap")) + { + xmlChar* typeKey = xmlGetProp(mapNode, (xmlChar*) "type"); + if (typeKey == 0) exit(2); + map.setDownMoveType(Map::moveTypeForShort((char*) typeKey)); + xmlFree(typeKey); + + if (Map::moveTypeTakesMap(map.getDownMoveType())) + { + xmlChar* idKey = xmlGetProp(mapNode, (xmlChar*) "map"); + if (idKey == 0) exit(2); + map.setDownMapID(atoi((char*) idKey)); + xmlFree(idKey); + } + } else if (!xmlStrcmp(mapNode->name, (const xmlChar*) "entities")) + { + for (xmlNodePtr entityNode = mapNode->xmlChildrenNode; entityNode != NULL; entityNode = entityNode->next) + { + if (!xmlStrcmp(entityNode->name, (const xmlChar*) "entity")) + { + Map::EntityData data; + + for (xmlNodePtr entityDataNode = entityNode->xmlChildrenNode; entityDataNode != NULL; entityDataNode = entityDataNode->next) + { + if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-type")) + { + xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); + data.name = (char*) key; + xmlFree(key); + } else if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-position")) + { + xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); + sscanf((char*) key, "%d,%d", &data.position.first, &data.position.second); + xmlFree(key); + } + } + + map.addEntity(data); + } + } + } + } + + maps[theId] = map; + } + } + + xmlFreeDoc(doc); + + // Set up entities player = std::make_shared(); - player->position = std::make_pair(100.0,100.0); + player->position = startPos; player->size = std::make_pair(10.0,12.0); auto player_input = std::make_shared(); @@ -23,7 +180,7 @@ Game::Game() auto player_anim = std::make_shared(); player->addComponent(player_anim); - Map& startingMap = Map::getNamedMap("embarass"); + Map& startingMap = maps[startMap]; save = {&startingMap, player->position}; loadMap(startingMap, player->position); @@ -168,3 +325,8 @@ void Game::playerDie() player->send(*this, Message::Type::stopDying); }); } + +const Map& Game::getMap(int id) const +{ + return maps.at(id); +} diff --git a/src/game.h b/src/game.h index 1818cec..3f0fcc8 100644 --- a/src/game.h +++ b/src/game.h @@ -3,21 +3,12 @@ #include #include -#include "renderer.h" #include +#include +#include "map.h" class Entity; -class Map; - -const int TILE_WIDTH = 8; -const int TILE_HEIGHT = 8; -const int GAME_WIDTH = 320; -const int GAME_HEIGHT = 200; -const int MAP_WIDTH = GAME_WIDTH/TILE_WIDTH; -const int MAP_HEIGHT = GAME_HEIGHT/TILE_HEIGHT - 1; - -const int FRAMES_PER_SECOND = 60; -const double SECONDS_PER_FRAME = 1.0 / FRAMES_PER_SECOND; +struct GLFWwindow; struct Savefile { const Map* map; @@ -26,17 +17,21 @@ struct Savefile { class Game { public: - Game(); + Game(const char* maps); void execute(GLFWwindow* window); void loadMap(const Map& map, std::pair position); void detectCollision(Entity& collider, std::pair old_position); void saveGame(); void schedule(double time, std::function callback); void playerDie(); + const Map& getMap(int id) const; private: friend void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); + std::map maps; + int startMap; + std::pair startPos; std::list> entities; std::list> nextEntities; std::pair nextPosition; diff --git a/src/main.cpp b/src/main.cpp index 96eb7da..4157350 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,7 +14,7 @@ int main() // Put this in a block so game goes out of scope before we destroy the renderer { - Game game; + Game game {"res/maps.xml"}; game.execute(window); } diff --git a/src/map.cpp b/src/map.cpp index 11d1d52..fa940ef 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -1,129 +1,34 @@ #include "map.h" -#include "game.h" #include #include -#include #include #include "entityfactory.h" #include "entity.h" +#include "game.h" +#include "consts.h" -static std::map maps; - -Map::Map() +Map::Map(int id) { + this->id = id; mapdata = (int*) calloc(1, sizeof(int)); } -Map::Map(const std::string name) -{ - this->name = name; - - xmlDocPtr doc = xmlParseFile(("maps/" + name + ".xml").c_str()); - if (doc == nullptr) - { - fprintf(stderr, "Error reading map %s\n", name.c_str()); - exit(-1); - } - - xmlNodePtr top = xmlDocGetRootElement(doc); - if (top == nullptr) - { - fprintf(stderr, "Empty map %s\n", name.c_str()); - exit(-1); - } - - if (xmlStrcmp(top->name, (const xmlChar*) "map-def")) - { - fprintf(stderr, "Invalid map definition %s\n", name.c_str()); - exit(-1); - } - - for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next) - { - if (!xmlStrcmp(node->name, (const xmlChar*) "name")) - { - xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); - if (key != 0) - { - title = (char*) key; - } - - xmlFree(key); - } else if (!xmlStrcmp(node->name, (const xmlChar*) "environment")) - { - xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); - mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); - mapdata[0] = atoi(strtok((char*) key, ",\n")); - for (int i=1; i<(MAP_WIDTH*MAP_HEIGHT); i++) - { - mapdata[i] = atoi(strtok(NULL, ",\n")); - } - xmlFree(key); - } else if (!xmlStrcmp(node->name, (const xmlChar*) "entities")) - { - for (xmlNodePtr entityNode = node->xmlChildrenNode; entityNode != NULL; entityNode = entityNode->next) - { - if (!xmlStrcmp(entityNode->name, (const xmlChar*) "entity")) - { - EntityData data; - for (xmlNodePtr entityDataNode = entityNode->xmlChildrenNode; entityDataNode != NULL; entityDataNode = entityDataNode->next) - { - if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-type")) - { - xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); - if (key != 0) - { - data.name = (char*) key; - } - - xmlFree(key); - } else if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-position")) - { - xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); - sscanf((char*) key, "%lf,%lf", &data.position.first, &data.position.second); - xmlFree(key); - } - } - - entities.push_back(data); - } - } - } else if (!xmlStrcmp(node->name, (const xmlChar*) "leftmap")) - { - xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); - if (key != 0) - { - leftMap = &Map::getNamedMap((char*) key); - } - - xmlFree(key); - } else if (!xmlStrcmp(node->name, (const xmlChar*) "rightmap")) - { - xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); - if (key != 0) - { - rightMap = &Map::getNamedMap((char*) key); - } - - xmlFree(key); - } - } - - xmlFreeDoc(doc); -} - Map::Map(const Map& map) { mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); memcpy(mapdata, map.mapdata, MAP_WIDTH*MAP_HEIGHT*sizeof(int)); + id = map.id; title = map.title; leftMap = map.leftMap; rightMap = map.rightMap; - + downMap = map.downMap; + upMap = map.upMap; + leftType = map.leftType; + rightType = map.rightType; + upType = map.upType; + downType = map.downType; entities = map.entities; - - name = map.name; } Map::Map(Map&& map) : Map() @@ -149,8 +54,19 @@ void swap(Map& first, Map& second) std::swap(first.title, second.title); std::swap(first.leftMap, second.leftMap); std::swap(first.rightMap, second.rightMap); + std::swap(first.downMap, second.downMap); + std::swap(first.upMap, second.upMap); + std::swap(first.leftType, second.leftType); + std::swap(first.rightType, second.rightType); + std::swap(first.upType, second.upType); + std::swap(first.downType, second.downType); + std::swap(first.id, second.id); std::swap(first.entities, second.entities); - std::swap(first.name, second.name); +} + +int Map::getID() const +{ + return id; } const int* Map::getMapdata() const @@ -163,53 +79,140 @@ std::string Map::getTitle() const return title; } -const Map* Map::getLeftMap() const +void Map::createEntities(std::list>& entities) const +{ + for (auto data : this->entities) + { + auto entity = EntityFactory::createNamedEntity(data.name); + entity->position = data.position; + + entities.push_back(entity); + } +} + +bool Map::operator==(const Map& other) const +{ + return id == other.id; +} + +bool Map::operator!=(const Map& other) const +{ + return id != other.id; +} + +Map::MoveType Map::moveTypeForShort(std::string str) +{ + if (str == "wrap") return MoveType::Wrap; + if (str == "warp") return MoveType::Warp; + if (str == "reverseWarp") return MoveType::ReverseWarp; + + return MoveType::Wall; +} + +Map::MoveType Map::getLeftMoveType() const +{ + return leftType; +} + +Map::MoveType Map::getRightMoveType() const +{ + return rightType; +} + +Map::MoveType Map::getUpMoveType() const +{ + return upType; +} + +Map::MoveType Map::getDownMoveType() const +{ + return downType; +} + +int Map::getLeftMapID() const { return leftMap; } -const Map* Map::getRightMap() const +int Map::getRightMapID() const { return rightMap; } -void Map::setLeftMap(const Map* m) +int Map::getUpMapID() const { - leftMap = m; + return upMap; } -void Map::setRightMap(const Map* m) +int Map::getDownMapID() const { - rightMap = m; + return downMap; } -void Map::createEntities(std::list>& entities) const +bool Map::moveTypeTakesMap(MoveType type) { - for (auto data : this->entities) + switch (type) { - auto entity = EntityFactory::createNamedEntity(data.name); - entity->position = data.position; - - entities.push_back(entity); + case MoveType::Wall: return false; + case MoveType::Wrap: return false; + case MoveType::Warp: return true; + case MoveType::ReverseWarp: return true; } } -bool Map::operator==(const Map& other) const +void Map::setMapdata(int* mapdata) { - return name == other.name; + free(this->mapdata); + this->mapdata = mapdata; } -bool Map::operator!=(const Map& other) const +void Map::setTitle(std::string title) { - return name != other.name; + this->title = title; } -Map& Map::getNamedMap(const std::string name) +void Map::setLeftMoveType(MoveType type) { - if (maps.count(name) == 0) - { - maps[name] = Map {name}; - } - - return maps[name]; + leftType = type; } + +void Map::setRightMoveType(MoveType type) +{ + rightType = type; +} + +void Map::setUpMoveType(MoveType type) +{ + upType = type; +} + +void Map::setDownMoveType(MoveType type) +{ + downType = type; +} + +void Map::setLeftMapID(int id) +{ + leftMap = id; +} + +void Map::setRightMapID(int id) +{ + rightMap = id; +} + +void Map::setUpMapID(int id) +{ + upMap = id; +} + +void Map::setDownMapID(int id) +{ + downMap = id; +} + +void Map::addEntity(EntityData& data) +{ + entities.push_back(data); +} + diff --git a/src/map.h b/src/map.h index 3b3d42c..1234dbb 100644 --- a/src/map.h +++ b/src/map.h @@ -8,37 +8,63 @@ class Entity; class Map { public: - Map(); - Map(std::string name); + Map(int id); + Map() : Map(-1) {} Map(const Map& map); Map(Map&& map); ~Map(); Map& operator= (Map other); friend void swap(Map& first, Map& second); - static Map& getNamedMap(std::string name); + enum class MoveType { + Wall, + Wrap, + Warp, + ReverseWarp + }; + + struct EntityData { + std::string name; + std::pair position; + }; + + static MoveType moveTypeForShort(std::string str); + static bool moveTypeTakesMap(MoveType type); + int getID() const; const int* getMapdata() const; std::string getTitle() const; - const Map* getLeftMap() const; - const Map* getRightMap() const; - void setLeftMap(const Map* m); - void setRightMap(const Map* m); + MoveType getLeftMoveType() const; + MoveType getRightMoveType() const; + MoveType getUpMoveType() const; + MoveType getDownMoveType() const; + int getLeftMapID() const; + int getRightMapID() const; + int getUpMapID() const; + int getDownMapID() const; + void createEntities(std::list>& entities) const; bool operator==(const Map& other) const; bool operator!=(const Map& other) const; - private: - struct EntityData { - std::string name; - std::pair position; - }; + void setMapdata(int* mapdata); + void setTitle(std::string title); + void setLeftMoveType(MoveType type); + void setRightMoveType(MoveType type); + void setUpMoveType(MoveType type); + void setDownMoveType(MoveType type); + void setLeftMapID(int id); + void setRightMapID(int id); + void setUpMapID(int id); + void setDownMapID(int id); + void addEntity(EntityData& data); + private: int* mapdata; std::string title; - std::string name; - const Map* leftMap = nullptr; - const Map* rightMap = nullptr; + int id; std::list entities; + MoveType leftType, rightType, upType, downType; + int leftMap, rightMap, upMap, downMap; }; #endif diff --git a/src/renderer.cpp b/src/renderer.cpp index d50901b..99d5389 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -5,9 +5,9 @@ #include #include #include -#include "game.h" #include #include +#include "consts.h" // include stb_image #define STB_IMAGE_IMPLEMENTATION -- cgit 1.4.1