Add Map and MapLoad
This commit is contained in:
+1
-1
@@ -46,7 +46,7 @@ public:
|
|||||||
if (this->collisionsEvent != NULL) {
|
if (this->collisionsEvent != NULL) {
|
||||||
for (auto& iterator2 : this->entities) {
|
for (auto& iterator2 : this->entities) {
|
||||||
if (iterator != iterator2) {
|
if (iterator != iterator2) {
|
||||||
if (iterator.second->Collision(iterator2.second)) {
|
if(iterator.second->Collision(iterator2.second)) {
|
||||||
this->collisionsEvent(iterator.second, iterator2.second);
|
this->collisionsEvent(iterator.second, iterator2.second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-4
@@ -19,10 +19,10 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class GameStateParent
|
class GameStateManager
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
GameStateParent() {
|
GameStateManager() {
|
||||||
this->state = NULL;
|
this->state = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +53,7 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
~GameStateParent() {
|
~GameStateManager() {
|
||||||
if(this->state != NULL) {
|
if(this->state != NULL) {
|
||||||
this->state->Destroy(this->window);
|
this->state->Destroy(this->window);
|
||||||
}
|
}
|
||||||
@@ -65,7 +65,7 @@ private:
|
|||||||
GameState* state;
|
GameState* state;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern GameStateParent gameState;
|
extern GameStateManager gameState;
|
||||||
|
|
||||||
#endif // GAMESTATE
|
#endif // GAMESTATE
|
||||||
|
|
||||||
|
|||||||
+3
-2
@@ -4,7 +4,7 @@
|
|||||||
#include "state/maingame.h"
|
#include "state/maingame.h"
|
||||||
|
|
||||||
InputManager inputManager;
|
InputManager inputManager;
|
||||||
GameStateParent gameState;
|
GameStateManager gameState;
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@@ -16,7 +16,8 @@ int main()
|
|||||||
settings.antialiasingLevel = 16;
|
settings.antialiasingLevel = 16;
|
||||||
|
|
||||||
sf::RenderWindow window(sf::VideoMode(1280, 720), gameName, windowStyle, settings);
|
sf::RenderWindow window(sf::VideoMode(1280, 720), gameName, windowStyle, settings);
|
||||||
window.setVerticalSyncEnabled(true);
|
//window.setVerticalSyncEnabled(true);
|
||||||
|
window.setFramerateLimit(60);
|
||||||
|
|
||||||
gameState.SetWindow(&window);
|
gameState.SetWindow(&window);
|
||||||
gameState.SetState(new MainGame());
|
gameState.SetState(new MainGame());
|
||||||
|
|||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
#include "map.h"
|
||||||
|
|
||||||
|
Map::Map()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Map::~Map()
|
||||||
|
{
|
||||||
|
delete this->tileSet;
|
||||||
|
delete this->layer1;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
#ifndef MAP_H
|
||||||
|
#define MAP_H
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class TileSet
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
//TileSet();
|
||||||
|
//~TileSet();
|
||||||
|
|
||||||
|
// "firstgid":1,
|
||||||
|
std::string image;
|
||||||
|
int imageHeight;
|
||||||
|
int imageWidth;
|
||||||
|
// "margin":0,
|
||||||
|
// "name":"Tilesheet",
|
||||||
|
// "properties":
|
||||||
|
// {
|
||||||
|
|
||||||
|
// },
|
||||||
|
// "spacing":0,
|
||||||
|
int tileHeight;
|
||||||
|
int tileWidth;
|
||||||
|
// "transparentcolor":"#ffffff"
|
||||||
|
// @Todo save all tile textures with pointers
|
||||||
|
// map->tileSet.tile[1];
|
||||||
|
std::vector<int> tile;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Layer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
//Layer();
|
||||||
|
//~Layer();
|
||||||
|
|
||||||
|
std::vector<std::vector<int>> data;
|
||||||
|
int height;
|
||||||
|
// name
|
||||||
|
// opacity
|
||||||
|
// type
|
||||||
|
bool visible;
|
||||||
|
int width;
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Map
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Map();
|
||||||
|
~Map();
|
||||||
|
|
||||||
|
// Global
|
||||||
|
int height;
|
||||||
|
Layer* layer1;
|
||||||
|
// "nextobjectid":1,
|
||||||
|
// "orientation":"orthogonal",
|
||||||
|
// "properties":
|
||||||
|
// {
|
||||||
|
|
||||||
|
// },
|
||||||
|
// "renderorder":"right-down",
|
||||||
|
int tileheight;
|
||||||
|
TileSet* tileSet;
|
||||||
|
int tilewidth;
|
||||||
|
// "version":1,
|
||||||
|
int width;
|
||||||
|
};
|
||||||
|
|
||||||
|
//sf::Sprite* sprite;
|
||||||
|
//for()
|
||||||
|
//{
|
||||||
|
// Sprite sf::Texture = tile.texture
|
||||||
|
// sprire->setPosition(sf:vector2i); 5 * 32, 4 * 32
|
||||||
|
// window->draw(*sprite);
|
||||||
|
//}
|
||||||
|
|
||||||
|
#endif // MAP_H
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
#include "mapload.h"
|
||||||
|
|
||||||
|
MapLoad::MapLoad()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
MapLoad::~MapLoad()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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())
|
||||||
|
{
|
||||||
|
std::string ifStream;
|
||||||
|
while (!openFile.eof())
|
||||||
|
{
|
||||||
|
openFile >> ifStream;
|
||||||
|
|
||||||
|
*content += ifStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
openFile.close();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
std::cout << "MapLoad:GetFile | Couldn't open file: " << mapName << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MapLoad::Parser(std::string* content, Map* map) {
|
||||||
|
// 1. Parse a JSON string into DOM.
|
||||||
|
rapidjson::Document document;
|
||||||
|
document.Parse(content->c_str());
|
||||||
|
|
||||||
|
// Process map
|
||||||
|
// TILESET
|
||||||
|
TileSet* tileSet = new TileSet();
|
||||||
|
tileSet->image = document["tilesets"][0]["image"].GetString();
|
||||||
|
tileSet->imageHeight = document["tilesets"][0]["imageheight"].GetInt();
|
||||||
|
tileSet->imageWidth = document["tilesets"][0]["imagewidth"].GetInt();
|
||||||
|
tileSet->tileHeight = document["tilesets"][0]["tileheight"].GetInt();
|
||||||
|
tileSet->tileWidth = document["tilesets"][0]["tilewidth"].GetInt();
|
||||||
|
|
||||||
|
int height = document["layers"][0]["height"].GetInt();
|
||||||
|
int width = document["layers"][0]["width"].GetInt();
|
||||||
|
|
||||||
|
// LAYER 1
|
||||||
|
Layer* layer = new Layer();
|
||||||
|
|
||||||
|
const rapidjson::Value& mapArray = document["layers"][0]["data"];
|
||||||
|
assert(mapArray.IsArray());
|
||||||
|
|
||||||
|
int y = 0;
|
||||||
|
for (int i = 0; i < height; i++) {
|
||||||
|
std::vector<int> row;
|
||||||
|
|
||||||
|
for (int j = 0; j < width; j++) {
|
||||||
|
row.push_back(mapArray[y].GetInt());
|
||||||
|
y++;
|
||||||
|
}
|
||||||
|
|
||||||
|
layer->data.push_back(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
layer->height = height;
|
||||||
|
layer->visible = document["layers"][0]["visible"].GetBool();
|
||||||
|
layer->width = width;
|
||||||
|
layer->x = document["layers"][0]["x"].GetInt();
|
||||||
|
layer->y = document["layers"][0]["y"].GetInt();
|
||||||
|
|
||||||
|
// MAP
|
||||||
|
map->height = document["layers"][0]["height"].GetInt();
|
||||||
|
map->layer1 = layer;
|
||||||
|
map->tileheight = document["tileheight"].GetInt();
|
||||||
|
map->tileSet = 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);
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef MAPLOAD_H
|
||||||
|
#define MAPLOAD_H
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
#include <rapidjson/document.h>
|
||||||
|
|
||||||
|
#include "map.h"
|
||||||
|
|
||||||
|
class MapLoad
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
MapLoad();
|
||||||
|
~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
-2
@@ -19,11 +19,15 @@ TEMPLATE = app
|
|||||||
|
|
||||||
SOURCES += main.cpp \
|
SOURCES += main.cpp \
|
||||||
inputmanager.cpp \
|
inputmanager.cpp \
|
||||||
state/maingame.cpp
|
state/maingame.cpp \
|
||||||
|
mapload.cpp \
|
||||||
|
map.cpp
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
inputmanager.h \
|
inputmanager.h \
|
||||||
entity.h \
|
entity.h \
|
||||||
entitymanager.h \
|
entitymanager.h \
|
||||||
gamestate.h \
|
gamestate.h \
|
||||||
state/maingame.h
|
state/maingame.h \
|
||||||
|
mapload.h \
|
||||||
|
map.h
|
||||||
|
|||||||
@@ -5,13 +5,19 @@ void UpdateCollisions(Entity* entityA, Entity* entityB) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MainGame::Initialize(sf::RenderWindow* window) {
|
void MainGame::Initialize(sf::RenderWindow* window) {
|
||||||
entityManager = new EntityManager();
|
this->entityManager = new EntityManager();
|
||||||
entityManager->SetCollisionMethod(UpdateCollisions);
|
this->entityManager->SetCollisionMethod(UpdateCollisions);
|
||||||
|
|
||||||
|
// Add entities
|
||||||
this->entityManager->AddEntity("test", new Entity("data\\gfx\\test.png"));
|
this->entityManager->AddEntity("test", new Entity("data\\gfx\\test.png"));
|
||||||
this->entityManager->AddEntity("test", new Entity("data\\gfx\\test.png"));
|
this->entityManager->AddEntity("test", new Entity("data\\gfx\\test.png"));
|
||||||
this->entityManager->Get("test0")->velocity.x = 0.5;
|
this->entityManager->Get("test0")->velocity.x = 0.5;
|
||||||
this->entityManager->Get("test")->setPosition(sf::Vector2f(50, 50));
|
this->entityManager->Get("test")->setPosition(sf::Vector2f(50, 50));
|
||||||
|
|
||||||
|
// Load map
|
||||||
|
this->map = new Map();
|
||||||
|
MapLoad mapLoad;
|
||||||
|
mapLoad.Load(this->map, "data\\map\\level1.json");
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainGame::Update(sf::RenderWindow* window) {
|
void MainGame::Update(sf::RenderWindow* window) {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "gamestate.h"
|
#include "gamestate.h"
|
||||||
#include "entitymanager.h"
|
#include "entitymanager.h"
|
||||||
#include "inputmanager.h"
|
#include "inputmanager.h"
|
||||||
|
#include "mapload.h"
|
||||||
|
|
||||||
class MainGame : public GameState
|
class MainGame : public GameState
|
||||||
{
|
{
|
||||||
@@ -17,6 +18,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
EntityManager* entityManager;
|
EntityManager* entityManager;
|
||||||
|
Map* map;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // MAINGAME_H
|
#endif // MAINGAME_H
|
||||||
|
|||||||
Reference in New Issue
Block a user