Browse Source

Add collision layer to MapLoad

master
Riyyi 9 years ago
parent
commit
f21211acb3
  1. BIN
      src/data/gfx/Collision.png
  2. 25
      src/data/map/level1.csv
  3. 45
      src/data/map/level1.json
  4. 23
      src/data/map/level1.tmx
  5. 50
      src/map.cpp
  6. 5
      src/map.h
  7. 117
      src/mapload.cpp
  8. 11
      src/mapload.h
  9. 6
      src/state/maingame.cpp

BIN
src/data/gfx/Collision.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

25
src/data/map/level1.csv

@ -1,25 +0,0 @@
0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,-1,-1,-1,-1,-1
96,97,98,99,96,97,98,99,96,97,98,99,96,97,98,99,96,97,98,99,-1,-1,-1,-1,-1
192,193,194,195,192,193,194,195,192,193,194,195,192,193,194,195,192,193,194,195,-1,-1,-1,-1,-1
288,289,290,291,288,289,290,291,288,289,290,291,288,289,290,291,288,289,290,291,-1,-1,-1,-1,-1
384,385,386,387,384,385,386,387,384,385,386,387,384,385,386,387,384,385,386,387,-1,-1,-1,-1,-1
2152,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,2171,-1,-1,-1,-1,-1
2248,2249,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,2267,-1,-1,-1,-1,-1
2344,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,2363,-1,-1,-1,-1,-1
2440,2441,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,2459,-1,-1,-1,-1,-1
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
-1,-1,-1,-1,-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,-1,-1,-1,-1
1 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 -1 -1 -1 -1 -1
2 96 97 98 99 96 97 98 99 96 97 98 99 96 97 98 99 96 97 98 99 -1 -1 -1 -1 -1
3 192 193 194 195 192 193 194 195 192 193 194 195 192 193 194 195 192 193 194 195 -1 -1 -1 -1 -1
4 288 289 290 291 288 289 290 291 288 289 290 291 288 289 290 291 288 289 290 291 -1 -1 -1 -1 -1
5 384 385 386 387 384 385 386 387 384 385 386 387 384 385 386 387 384 385 386 387 -1 -1 -1 -1 -1
6 2152 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 2171 -1 -1 -1 -1 -1
7 2248 2249 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 2267 -1 -1 -1 -1 -1
8 2344 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 2363 -1 -1 -1 -1 -1
9 2440 2441 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 2459 -1 -1 -1 -1 -1
10 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
11 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
12 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
13 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
14 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
15 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
16 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
17 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
18 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
19 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
20 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
21 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
22 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
23 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
24 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
25 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

45
src/data/map/level1.json

File diff suppressed because one or more lines are too long

23
src/data/map/level1.tmx

File diff suppressed because one or more lines are too long

50
src/map.cpp

@ -11,10 +11,11 @@ TileSet::~TileSet() {
this->Clear();
}
Map::Map()
{
Map::Map() {
this->ground1 = new Layer();
this->above1 = new Layer();
this->collision = new Layer();
this->tileSet = new TileSet();
this->layer1 = new Layer();
}
void Map::Render(sf::RenderWindow* window) {
@ -29,7 +30,7 @@ void Map::Render(sf::RenderWindow* window) {
sf::Sprite tile;
for (int y = renderHeight.x; y < renderHeight.y; y++) {
for (int x = renderWidth.x; x < renderWidth.y; x++) {
index = this->layer1->data[y][x];
index = this->ground1->data[y][x];
if(index != 0) {
tile.setTexture(*this->tileSet->tile[index]);
tile.setPosition(sf::Vector2f(x * this->tileSet->tileWidth, y * this->tileSet->tileHeight));
@ -39,42 +40,9 @@ void Map::Render(sf::RenderWindow* window) {
}
}
Map::~Map()
{
Map::~Map() {
delete this->tileSet;
delete this->layer1;
delete this->ground1;
delete this->above1;
delete this->collision;
}
//void drawMap(sf::RenderWindow* window)
//{
// sf::Sprite tile;
// int itr = 0;
// int location;
// int maxWidth = nearbyint((view.getCenter().x + view.getSize().x / 2) / map->tileSet->tileWidth) + 1;
// int minWidth = nearbyint((view.getCenter().x - view.getSize().x / 2) / map->tileSet->tileWidth) - 1;
// int maxHeight = nearbyint((view.getCenter().y + view.getSize().y / 2) / map->tileSet->tileHeight) + 1;
// int minHeight = nearbyint((view.getCenter().y - view.getSize().y / 2) / map->tileSet->tileHeight) - 1;
// for (int y = minHeight; y < maxHeight && y < map->height; y++)
// {
// if (y < 0)
// {
// y = 0;
// }
// for (int x = minWidth; x < maxWidth && x < map->width; x++)
// {
// if (x < 0)
// {
// x = 0;
// }
// location = map->data[y][x];
// if (location != 0)
// {
// tile.setTexture(*map->tileSet->data[location]);
// tile.setPosition(sf::Vector2f(x * map->tileSet->tileWidth, y * map->tileSet->tileHeight));
// window->draw(tile);
// }
// itr++;
// }
// }
//}

5
src/map.h

@ -15,6 +15,7 @@ public:
int height;
// name
// opacity
// properties
// type
bool visible;
int width;
@ -58,7 +59,9 @@ public:
// Global
int height;
Layer* layer1;
Layer* ground1;
Layer* above1;
Layer* collision;
// "nextobjectid":1,
// "orientation":"orthogonal",
// "properties":

117
src/mapload.cpp

@ -1,16 +1,25 @@
#include "mapload.h"
MapLoad::MapLoad()
{
}
MapLoad::MapLoad(Map *map, std::string mapName, int update) {
std::string content;
rapidjson::Document document;
MapLoad::~MapLoad()
{
this->GetFile(&content, mapName);
this->Parser(&content, &document);
this->LoadLayer(document, map);
if(!update) {
this->LoadTileSet(document, map);
}
this->LoadMap(document, map);
// for (int i = 0; i < 100; i++) {
// for (int j = 0; j < 100; j++) {
// std::cout << map->collision->data[i][j] << std::endl;
// }
// }
}
void MapLoad::GetFile(std::string* content, std::string mapName) {
void MapLoad::GetFile(std::string *content, std::string mapName) {
// Get the content of the map file and append it to content
std::ifstream openFile(mapName);
if (openFile.is_open())
@ -30,13 +39,56 @@ void MapLoad::GetFile(std::string* content, std::string mapName) {
}
}
void MapLoad::Parser(std::string* content, Map* map) {
void MapLoad::Parser(std::string *content, rapidjson::Document *document) {
// 1. Parse a JSON string into DOM.
rapidjson::Document document;
document.Parse(content->c_str());
document->Parse(content->c_str());
}
void MapLoad::LoadLayer(const rapidjson::Document& document, Map *map) {
int loopCount = document["layers"].Size();
Layer* current;
for(unsigned int i = 0; i < loopCount; i++) {
current = NULL;
if(document["layers"][i]["properties"]["name"] == "Ground1") {
current = map->ground1;
}
else if(document["layers"][i]["properties"]["name"] == "Above1") {
current = map->above1;
}
if(document["layers"][i]["properties"]["name"] == "Collision") {
current = map->collision;
}
if(current != NULL) {
current->data.clear();
int height = document["layers"][i]["height"].GetInt();
int width = document["layers"][i]["width"].GetInt();
const rapidjson::Value& mapArray = document["layers"][i]["data"];
assert(mapArray.IsArray());
int itr = 0;
for (int y = 0; y < height; y++) {
std::vector<int> row;
for (int x = 0; x < width; x++) {
row.push_back(mapArray[itr].GetInt());
itr++;
}
current->data.push_back(row);
}
// Process map
// TILESET
current->height = height;
current->visible = document["layers"][i]["visible"].GetBool();
current->width = width;
current->x = document["layers"][i]["x"].GetInt();
current->y = document["layers"][i]["y"].GetInt();
}
}
}
void MapLoad::LoadTileSet(const rapidjson::Document& document, Map *map) {
map->tileSet->Clear();
map->tileSet->image = document["tilesets"][0]["image"].GetString();
map->tileSet->imageHeight = document["tilesets"][0]["imageheight"].GetInt();
@ -67,33 +119,9 @@ void MapLoad::Parser(std::string* content, Map* map) {
map->tileSet->tile.push_back(texture);
}
}
}
// LAYER 1
map->layer1->data.clear();
height = document["layers"][0]["height"].GetInt();
width = document["layers"][0]["width"].GetInt();
const rapidjson::Value& mapArray = document["layers"][0]["data"];
assert(mapArray.IsArray());
int itr = 0;
for (int y = 0; y < height; y++) {
std::vector<int> row;
for (int x = 0; x < width; x++) {
row.push_back(mapArray[itr].GetInt());
itr++;
}
map->layer1->data.push_back(row);
}
map->layer1->height = height;
map->layer1->visible = document["layers"][0]["visible"].GetBool();
map->layer1->width = width;
map->layer1->x = document["layers"][0]["x"].GetInt();
map->layer1->y = document["layers"][0]["y"].GetInt();
void MapLoad::LoadMap(const rapidjson::Document& document, Map *map) {
// MAP
map->height = document["layers"][0]["height"].GetInt();
// Layer
@ -101,16 +129,9 @@ void MapLoad::Parser(std::string* content, Map* map) {
// TileSet
map->tilewidth = document["tilewidth"].GetInt();
map->width = document["layers"][0]["width"].GetInt();
// for (int i = 0; i < map->data1.size(); i++) {
// for (int j = 0; j < width; j++) {
// std::cout << map->data1[i][j] << std::endl;
// }
// }
}
void MapLoad::Load(Map* map, std::string mapName) {
std::string content;
GetFile(&content, mapName);
Parser(&content, map);
MapLoad::~MapLoad()
{
}

11
src/mapload.h

@ -11,12 +11,13 @@
class MapLoad
{
public:
MapLoad();
MapLoad(Map *map, std::string mapName, int update = 0);
void GetFile(std::string *content, std::string mapName);
void Parser(std::string *content, rapidjson::Document *document);
void LoadLayer(const rapidjson::Document &document, Map *map);
void LoadTileSet(const rapidjson::Document& document, Map *map);
void LoadMap(const rapidjson::Document& document, Map *map);
~MapLoad();
void GetFile(std::string* content, std::string mapName);
void Parser(std::string* content, Map* map);
void Load(Map* map, std::string mapName);
};
#endif // MAPLOAD_H

6
src/state/maingame.cpp

@ -17,8 +17,7 @@ void MainGame::Initialize(sf::RenderWindow* window) {
// Load Map
this->map = new Map();
MapLoad mapLoad;
mapLoad.Load(this->map, "data/map/level1.json");
MapLoad(this->map, "data/map/level1.json");
// Load Camera
this->camera = new Camera(window);
@ -39,8 +38,7 @@ void MainGame::Update(sf::RenderWindow* window) {
if(inputManager.IsPressed(InputManager::LoadMap)) {
std::cout << "Loading Map..." << std::endl;
MapLoad mapLoad;
mapLoad.Load(this->map, "data/map/level1.json");
MapLoad(this->map, "data/map/level1.json", 1);
}
}

Loading…
Cancel
Save