game/mario/maploader.cpp

75 lines
3.0 KiB
C++
Raw Normal View History

2021-04-25 20:42:55 +00:00
#include "maploader.hpp"
#include "../sdlpp/sdlpp_rectrenderer.hpp"
#include <fstream>
#include "sprites.hpp"
#include "blocks.hpp"
#include "objectids.hpp"
std::shared_ptr<SDLPP::RectangleRender> decodeObject( char obj, double x, double y, std::shared_ptr<SDLPP::Renderer> &renderer) {
switch(obj) {
case 'F':
return createTerrainBlock(FLOOR_OVERWORLD_ID, renderer, x, y, true);
case 'I':
return createTerrainBlock(HILL_OVERWORLD_INCLINE_ID, renderer, x, y);
case 'R':
return createTerrainBlock(HILL_OVERWORLD_DOTS_RIGHT_ID, renderer, x, y);
case 'G':
return createTerrainBlock(HILL_OVERWORLD_FILL_ID, renderer, x, y);
case 'L':
return createTerrainBlock(HILL_OVERWORLD_DOTS_LEFT_ID, renderer, x, y);
case 'D':
return createTerrainBlock(HILL_OVERWORLD_DECLINE_ID, renderer, x, y);
case 'T':
return createTerrainBlock(HILL_OVERWORLD_TOP_ID, renderer, x, y);
case 'q':
return createTerrainBlock(BUSH_OVERWORLD_LEFT_ID, renderer, x, y);
case 'w':
return createTerrainBlock(BUSH_OVERWORLD_MIDDLE_ID, renderer, x, y);
case 'r':
return createTerrainBlock(BUSH_OVERWORLD_RIGHT_ID, renderer, x, y);
case 'a':
return createTerrainBlock(CLOUD_OVERWORLD_LEFT_BOTTOM_ID, renderer, x, y);
case 's':
return createTerrainBlock(CLOUD_OVERWORLD_MIDDLE_BOTTOM_ID, renderer, x, y);
case 'd':
return createTerrainBlock(CLOUD_OVERWORLD_RIGHT_BOTTOM_ID, renderer, x, y);
case 'z':
return createTerrainBlock(CLOUD_OVERWORLD_LEFT_TOP_ID, renderer, x, y);
case 'x':
return createTerrainBlock(CLOUD_OVERWORLD_MIDDLE_TOP_ID, renderer, x, y);
case 'c':
return createTerrainBlock(CLOUD_OVERWORLD_RIGHT_TOP_ID, renderer, x, y);
}
return nullptr;
}
void loadMap(std::shared_ptr<SDLPP::Scene> &scene, std::shared_ptr<SDLPP::RectangleRender> &mario, const std::string &file, std::shared_ptr<SDLPP::Renderer> &renderer) {
std::fstream mapFile;
mapFile.open(file, std::ios::in);
std::string buffer;
std::getline(mapFile, buffer);
auto cols = std::stoi(buffer);
std::getline(mapFile, buffer);
auto rows = std::stoi(buffer);
std::getline(mapFile, buffer);
auto mario_x = std::stoi(buffer);
std::getline(mapFile, buffer);
auto mario_y = std::stoi(buffer);
auto cur_y = 1 - rows * BLOCK_SIZE;
for(size_t i = 0; i < rows; i++) {
std::getline(mapFile, buffer);
auto cur_x = -BLOCK_SIZE;
for(size_t j = 0; j < cols; j++) {
cur_x += BLOCK_SIZE;
if(buffer[j] == ' ')
continue;
auto obj = decodeObject(buffer[j], cur_x, cur_y, renderer);
if(obj != nullptr)
scene->addObject(obj);
}
cur_y += BLOCK_SIZE;
}
mario->setPos(mario_x * BLOCK_SIZE, 1 - (rows - mario_y) * BLOCK_SIZE);
scene->moveZTop(mario);
}