Browse Source

Add collision layer to MapLoad

master
Riyyi 10 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. 111
      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(); this->Clear();
} }
Map::Map() Map::Map() {
{ this->ground1 = new Layer();
this->above1 = new Layer();
this->collision = new Layer();
this->tileSet = new TileSet(); this->tileSet = new TileSet();
this->layer1 = new Layer();
} }
void Map::Render(sf::RenderWindow* window) { void Map::Render(sf::RenderWindow* window) {
@ -29,7 +30,7 @@ void Map::Render(sf::RenderWindow* window) {
sf::Sprite tile; sf::Sprite tile;
for (int y = renderHeight.x; y < renderHeight.y; y++) { for (int y = renderHeight.x; y < renderHeight.y; y++) {
for (int x = renderWidth.x; x < renderWidth.y; x++) { for (int x = renderWidth.x; x < renderWidth.y; x++) {
index = this->layer1->data[y][x]; index = this->ground1->data[y][x];
if(index != 0) { if(index != 0) {
tile.setTexture(*this->tileSet->tile[index]); tile.setTexture(*this->tileSet->tile[index]);
tile.setPosition(sf::Vector2f(x * this->tileSet->tileWidth, y * this->tileSet->tileHeight)); 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->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; int height;
// name // name
// opacity // opacity
// properties
// type // type
bool visible; bool visible;
int width; int width;
@ -58,7 +59,9 @@ public:
// Global // Global
int height; int height;
Layer* layer1; Layer* ground1;
Layer* above1;
Layer* collision;
// "nextobjectid":1, // "nextobjectid":1,
// "orientation":"orthogonal", // "orientation":"orthogonal",
// "properties": // "properties":

111
src/mapload.cpp

@ -1,13 +1,22 @@
#include "mapload.h" #include "mapload.h"
MapLoad::MapLoad() MapLoad::MapLoad(Map *map, std::string mapName, int update) {
{ std::string content;
rapidjson::Document document;
this->GetFile(&content, mapName);
this->Parser(&content, &document);
this->LoadLayer(document, map);
if(!update) {
this->LoadTileSet(document, map);
} }
this->LoadMap(document, map);
MapLoad::~MapLoad() // 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) {
@ -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. // 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;
}
// Process map if(current != NULL) {
// TILESET 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);
}
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->Clear();
map->tileSet->image = document["tilesets"][0]["image"].GetString(); map->tileSet->image = document["tilesets"][0]["image"].GetString();
map->tileSet->imageHeight = document["tilesets"][0]["imageheight"].GetInt(); 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); 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; void MapLoad::LoadMap(const rapidjson::Document& document, Map *map) {
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();
// MAP // MAP
map->height = document["layers"][0]["height"].GetInt(); map->height = document["layers"][0]["height"].GetInt();
// Layer // Layer
@ -101,16 +129,9 @@ void MapLoad::Parser(std::string* content, Map* map) {
// TileSet // TileSet
map->tilewidth = document["tilewidth"].GetInt(); map->tilewidth = document["tilewidth"].GetInt();
map->width = document["layers"][0]["width"].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) { MapLoad::~MapLoad()
std::string content; {
GetFile(&content, mapName);
Parser(&content, map);
} }

11
src/mapload.h

@ -11,12 +11,13 @@
class MapLoad class MapLoad
{ {
public: public:
MapLoad(); MapLoad(Map *map, std::string mapName, int update = 0);
~MapLoad();
void GetFile(std::string *content, std::string mapName); void GetFile(std::string *content, std::string mapName);
void Parser(std::string* content, Map* map); void Parser(std::string *content, rapidjson::Document *document);
void Load(Map* map, std::string mapName); 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();
}; };
#endif // MAPLOAD_H #endif // MAPLOAD_H

6
src/state/maingame.cpp

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

Loading…
Cancel
Save