[Add] Saving maps!
This commit is contained in:
parent
4a56e4cd00
commit
132279ccef
2
.gitignore
vendored
2
.gitignore
vendored
@ -14,6 +14,8 @@ Bin/Unuk
|
|||||||
Bin/*.dll
|
Bin/*.dll
|
||||||
Save/save*
|
Save/save*
|
||||||
Save/save_*
|
Save/save_*
|
||||||
|
Data/Media/Maps/save*
|
||||||
|
Data/Media/Maps/save_*
|
||||||
*.swp
|
*.swp
|
||||||
*.o
|
*.o
|
||||||
*.exe
|
*.exe
|
||||||
|
3916
Data/Media/Maps/map
3916
Data/Media/Maps/map
File diff suppressed because it is too large
Load Diff
@ -21,7 +21,6 @@ Game::~Game(void) {
|
|||||||
void Game::New(const string& savegameIDArg) {
|
void Game::New(const string& savegameIDArg) {
|
||||||
_saveGameID = savegameIDArg;
|
_saveGameID = savegameIDArg;
|
||||||
NewSavegame(savegameIDArg);
|
NewSavegame(savegameIDArg);
|
||||||
_map.Load("map");
|
|
||||||
|
|
||||||
int spawnX;
|
int spawnX;
|
||||||
int spawnY;
|
int spawnY;
|
||||||
@ -317,6 +316,12 @@ void Game::NewSavegame(const string savegameIDArg) {
|
|||||||
doc.LinkEndChild(saveElement);
|
doc.LinkEndChild(saveElement);
|
||||||
|
|
||||||
doc.SaveFile(saveFilename.c_str());
|
doc.SaveFile(saveFilename.c_str());
|
||||||
|
|
||||||
|
stringstream mapPath;
|
||||||
|
mapPath << "Data/Media/Maps/" << _saveGameID;
|
||||||
|
|
||||||
|
_map.New();
|
||||||
|
_map.Save(_saveGameID);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::LoadSavegame(const string savegameIDArg) {
|
void Game::LoadSavegame(const string savegameIDArg) {
|
||||||
@ -386,16 +391,12 @@ void Game::LoadSavegame(const string savegameIDArg) {
|
|||||||
// </health>
|
// </health>
|
||||||
|
|
||||||
_player->SetHealthLiteral(playerHealth);
|
_player->SetHealthLiteral(playerHealth);
|
||||||
|
|
||||||
// <map> - Parse the map file.
|
|
||||||
dataElem = dataElem->NextSiblingElement("map");
|
|
||||||
assert(dataElem != NULL);
|
|
||||||
_map.Load(dataElem->GetText());
|
|
||||||
// </map>
|
|
||||||
}
|
}
|
||||||
// <save>
|
// <save>
|
||||||
|
|
||||||
// </save>
|
// </save>
|
||||||
|
|
||||||
|
_map.Load(_saveGameID);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::SaveSavegame(void) {
|
void Game::SaveSavegame(void) {
|
||||||
@ -448,20 +449,17 @@ void Game::SaveSavegame(void) {
|
|||||||
TiXmlText* healthText = new TiXmlText(healthString.str().c_str());
|
TiXmlText* healthText = new TiXmlText(healthString.str().c_str());
|
||||||
healthElement->LinkEndChild(healthText);
|
healthElement->LinkEndChild(healthText);
|
||||||
|
|
||||||
TiXmlElement* mapElement = new TiXmlElement("map");
|
|
||||||
TiXmlText* mapText = new TiXmlText("map"); //TODO: replace with actual map name.
|
|
||||||
mapElement->LinkEndChild(mapText);
|
|
||||||
|
|
||||||
saveElement->LinkEndChild(nameElement);
|
saveElement->LinkEndChild(nameElement);
|
||||||
//saveElement->LinkEndChild(xElement);
|
//saveElement->LinkEndChild(xElement);
|
||||||
//saveElement->LinkEndChild(yElement);
|
//saveElement->LinkEndChild(yElement);
|
||||||
saveElement->LinkEndChild(levelElement);
|
saveElement->LinkEndChild(levelElement);
|
||||||
saveElement->LinkEndChild(expElement);
|
saveElement->LinkEndChild(expElement);
|
||||||
saveElement->LinkEndChild(healthElement);
|
saveElement->LinkEndChild(healthElement);
|
||||||
saveElement->LinkEndChild(mapElement);
|
|
||||||
|
|
||||||
doc.LinkEndChild(decl);
|
doc.LinkEndChild(decl);
|
||||||
doc.LinkEndChild(saveElement);
|
doc.LinkEndChild(saveElement);
|
||||||
|
|
||||||
doc.SaveFile(saveFilename.c_str());
|
doc.SaveFile(saveFilename.c_str());
|
||||||
|
|
||||||
|
_map.Save(_saveGameID);
|
||||||
}
|
}
|
||||||
|
@ -172,7 +172,7 @@ void WorldManager::OnPlayerAttack(Player* player) {
|
|||||||
delete npc;
|
delete npc;
|
||||||
|
|
||||||
if(_npcs.empty()) {
|
if(_npcs.empty()) {
|
||||||
_level->Load("map");
|
_level->New();
|
||||||
|
|
||||||
int spawnX;
|
int spawnX;
|
||||||
int spawnY;
|
int spawnY;
|
||||||
|
@ -12,6 +12,36 @@ LevelGen::~LevelGen(void) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LevelGen::New(void) {
|
||||||
|
Unload();
|
||||||
|
|
||||||
|
_world = WorldManager(this);
|
||||||
|
|
||||||
|
levelWidth = TILE_ARRAY_SIZE;
|
||||||
|
levelHeight = TILE_ARRAY_SIZE;
|
||||||
|
|
||||||
|
for(x = 0; x < levelWidth; x++) {
|
||||||
|
for(y = 0; y < levelHeight; y++) {
|
||||||
|
_tile[x][y].SetTileTextureName("grass");
|
||||||
|
|
||||||
|
stringstream tilePath;
|
||||||
|
tilePath << "../Data/Media/Images/Tiles/" << _tile[x][y].GetTileTextureName() << ".png";
|
||||||
|
|
||||||
|
_tile[x][y].SetTileTexture(_tileTextures.Add(tilePath.str()));
|
||||||
|
_tile[x][y].SetTileSolidity(false);
|
||||||
|
_tile[x][y].SetTileXY(x * TILE_WIDTH, y * TILE_HEIGHT);
|
||||||
|
_tile[x][y].SetEntitySolidity(false);
|
||||||
|
_tile[x][y].SetZLevel(100);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
levelWidth *= TILE_WIDTH;
|
||||||
|
levelHeight *= TILE_HEIGHT;
|
||||||
|
|
||||||
|
// procedural generation
|
||||||
|
DoMagic();
|
||||||
|
}
|
||||||
|
|
||||||
void LevelGen::Load(const string& filename) {
|
void LevelGen::Load(const string& filename) {
|
||||||
Unload();
|
Unload();
|
||||||
_currentMap = filename;
|
_currentMap = filename;
|
||||||
@ -55,6 +85,7 @@ void LevelGen::Load(const string& filename) {
|
|||||||
stringstream tilePath;
|
stringstream tilePath;
|
||||||
tilePath << "../Data/Media/Images/Tiles/" << dataElem->GetText() << ".png";
|
tilePath << "../Data/Media/Images/Tiles/" << dataElem->GetText() << ".png";
|
||||||
_tile[x][y].SetTileTexture(_tileTextures.Add(tilePath.str()));
|
_tile[x][y].SetTileTexture(_tileTextures.Add(tilePath.str()));
|
||||||
|
_tile[x][y].SetTileTextureName(dataElem->GetText());
|
||||||
// <tileTexture> - Finished applying the texture, move to the next sibling.
|
// <tileTexture> - Finished applying the texture, move to the next sibling.
|
||||||
|
|
||||||
// <solidTile> - Check to see if the tile is solid or not.
|
// <solidTile> - Check to see if the tile is solid or not.
|
||||||
@ -76,6 +107,7 @@ void LevelGen::Load(const string& filename) {
|
|||||||
stringstream entityPath;
|
stringstream entityPath;
|
||||||
entityPath << "../Data/Media/Images/Entities/" << entityName << ".png";
|
entityPath << "../Data/Media/Images/Entities/" << entityName << ".png";
|
||||||
_tile[x][y].SetEntityTexture(_entityTextures.AddAlpha(entityPath.str()));
|
_tile[x][y].SetEntityTexture(_entityTextures.AddAlpha(entityPath.str()));
|
||||||
|
_tile[x][y].SetEntityTextureName(entityName);
|
||||||
|
|
||||||
_tile[x][y].SetEntityXY(_tile[x][y].GetTileX() + TILE_WIDTH / 2 - _tile[x][y].GetEntityWidth() / 2,
|
_tile[x][y].SetEntityXY(_tile[x][y].GetTileX() + TILE_WIDTH / 2 - _tile[x][y].GetEntityWidth() / 2,
|
||||||
_tile[x][y].GetTileY() + TILE_HEIGHT / 2 - _tile[x][y].GetEntityHeight() / 2);
|
_tile[x][y].GetTileY() + TILE_HEIGHT / 2 - _tile[x][y].GetEntityHeight() / 2);
|
||||||
@ -111,8 +143,67 @@ void LevelGen::Load(const string& filename) {
|
|||||||
levelWidth = x * TILE_WIDTH;
|
levelWidth = x * TILE_WIDTH;
|
||||||
levelHeight = y * TILE_HEIGHT;
|
levelHeight = y * TILE_HEIGHT;
|
||||||
|
|
||||||
// procedural generation
|
_world = WorldManager(this);
|
||||||
DoMagic();
|
|
||||||
|
GenerateEnemies();
|
||||||
|
}
|
||||||
|
|
||||||
|
void LevelGen::Save(const string& filename){
|
||||||
|
TiXmlDocument doc;
|
||||||
|
|
||||||
|
TiXmlElement* rootElem = new TiXmlElement("map");
|
||||||
|
|
||||||
|
int levelWidthTiles = levelWidth / TILE_WIDTH;
|
||||||
|
int levelHeightTiles = levelHeight / TILE_HEIGHT;
|
||||||
|
|
||||||
|
for(y = 0; y < levelHeightTiles; y++) {
|
||||||
|
TiXmlElement* lineElem = new TiXmlElement("line");
|
||||||
|
|
||||||
|
for(x = 0; x < levelWidthTiles; x++) {
|
||||||
|
TiXmlElement* tileElem = new TiXmlElement("tile");
|
||||||
|
|
||||||
|
TiXmlElement* tileTextureElem = new TiXmlElement("tileTexture");
|
||||||
|
TiXmlText* tileTextureText = new TiXmlText(_tile[x][y].GetTileTextureName());
|
||||||
|
tileTextureElem->LinkEndChild(tileTextureText);
|
||||||
|
|
||||||
|
TiXmlElement* solidTileElem = new TiXmlElement("solidTile");
|
||||||
|
TiXmlText* solidTileText = new TiXmlText(_tile[x][y].GetTileSolidity() ? "true" : "false");
|
||||||
|
solidTileElem->LinkEndChild(solidTileText);
|
||||||
|
|
||||||
|
string entityTextureName = _tile[x][y].GetEntityTextureName();
|
||||||
|
|
||||||
|
TiXmlElement* entityTextureElem = new TiXmlElement("entityTexture");
|
||||||
|
TiXmlText* entityTextureText = new TiXmlText(entityTextureName.empty() ? "null" : entityTextureName);
|
||||||
|
entityTextureElem->LinkEndChild(entityTextureText);
|
||||||
|
|
||||||
|
TiXmlElement* solidEntityElem = new TiXmlElement("solidEntity");
|
||||||
|
TiXmlText* solidEntityText = new TiXmlText(_tile[x][y].GetEntitySolitity() ? "true" : "false");
|
||||||
|
solidEntityElem->LinkEndChild(solidEntityText);
|
||||||
|
|
||||||
|
stringstream zLevelStr;
|
||||||
|
zLevelStr << _tile[x][y].GetZLevel();
|
||||||
|
|
||||||
|
TiXmlElement* zLevelElem = new TiXmlElement("zLevel");
|
||||||
|
TiXmlText* zLevelText = new TiXmlText(zLevelStr.str());
|
||||||
|
zLevelElem->LinkEndChild(zLevelText);
|
||||||
|
|
||||||
|
tileElem->LinkEndChild(tileTextureElem);
|
||||||
|
tileElem->LinkEndChild(solidTileElem);
|
||||||
|
tileElem->LinkEndChild(entityTextureElem);
|
||||||
|
tileElem->LinkEndChild(solidEntityElem);
|
||||||
|
tileElem->LinkEndChild(zLevelElem);
|
||||||
|
|
||||||
|
lineElem->LinkEndChild(tileElem);
|
||||||
|
}
|
||||||
|
|
||||||
|
rootElem->LinkEndChild(lineElem);
|
||||||
|
}
|
||||||
|
|
||||||
|
_currentMap = filename;
|
||||||
|
string fullMapPath = "../Data/Media/Maps/" + filename;
|
||||||
|
|
||||||
|
doc.LinkEndChild(rootElem);
|
||||||
|
doc.SaveFile(fullMapPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LevelGen::Update(void) {
|
void LevelGen::Update(void) {
|
||||||
@ -173,6 +264,9 @@ void LevelGen::DoMagic(void) {
|
|||||||
GenerateEntities("tree", 25);
|
GenerateEntities("tree", 25);
|
||||||
GenerateEntities("hedge", 15);
|
GenerateEntities("hedge", 15);
|
||||||
GenerateEntities("barrel", 40);
|
GenerateEntities("barrel", 40);
|
||||||
|
GenerateEntities("closedChest", 100);
|
||||||
|
GenerateEntities("closedChestMetal", 150);
|
||||||
|
GenerateEntities("closedChestMetal2", 250);
|
||||||
MakeWalkingPaths();
|
MakeWalkingPaths();
|
||||||
GenerateEnemies();
|
GenerateEnemies();
|
||||||
}
|
}
|
||||||
@ -185,6 +279,7 @@ void LevelGen::GenerateEntities(const string& name, int frequency) {
|
|||||||
for(int y = 0; y < BOUNDARIES_Y; y++) {
|
for(int y = 0; y < BOUNDARIES_Y; y++) {
|
||||||
nextEntityGen--;
|
nextEntityGen--;
|
||||||
if(!_tile[x][y].GetTileSolidity() && !_tile[x][y].GetEntitySolitity() && nextEntityGen <= 0) {
|
if(!_tile[x][y].GetTileSolidity() && !_tile[x][y].GetEntitySolitity() && nextEntityGen <= 0) {
|
||||||
|
_tile[x][y].SetEntityTextureName(name);
|
||||||
_tile[x][y].SetEntityTexture(_entityTextures.AddAlpha(filename));
|
_tile[x][y].SetEntityTexture(_entityTextures.AddAlpha(filename));
|
||||||
|
|
||||||
_tile[x][y].SetEntityXY(_tile[x][y].GetTileX() + TILE_WIDTH / 2 - _tile[x][y].GetEntityWidth() / 2,
|
_tile[x][y].SetEntityXY(_tile[x][y].GetTileX() + TILE_WIDTH / 2 - _tile[x][y].GetEntityWidth() / 2,
|
||||||
|
@ -24,7 +24,9 @@ public:
|
|||||||
LevelGen(void);
|
LevelGen(void);
|
||||||
~LevelGen(void);
|
~LevelGen(void);
|
||||||
|
|
||||||
|
void New(void);
|
||||||
void Load(const string& filename);
|
void Load(const string& filename);
|
||||||
|
void Save(const string& filename);
|
||||||
void Update(void);
|
void Update(void);
|
||||||
void Render(void);
|
void Render(void);
|
||||||
|
|
||||||
|
@ -37,3 +37,6 @@ int MapElement::GetX(void) { return x; }
|
|||||||
int MapElement::GetY(void) { return y; }
|
int MapElement::GetY(void) { return y; }
|
||||||
int MapElement::GetWidth(void) { return _texture->w; }
|
int MapElement::GetWidth(void) { return _texture->w; }
|
||||||
int MapElement::GetHeight(void) { return _texture->h; }
|
int MapElement::GetHeight(void) { return _texture->h; }
|
||||||
|
|
||||||
|
void MapElement::SetTextureName(string name) { _textureName = name; }
|
||||||
|
string MapElement::GetTextureName(void) { return _textureName; }
|
||||||
|
@ -26,8 +26,12 @@ public:
|
|||||||
int GetWidth(void);
|
int GetWidth(void);
|
||||||
int GetHeight(void);
|
int GetHeight(void);
|
||||||
|
|
||||||
|
void SetTextureName(string path);
|
||||||
|
string GetTextureName(void);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
SDL_Surface* _texture;
|
SDL_Surface* _texture;
|
||||||
|
string _textureName;
|
||||||
|
|
||||||
bool _solid;
|
bool _solid;
|
||||||
|
|
||||||
|
@ -18,6 +18,8 @@ public:
|
|||||||
|
|
||||||
// Tile Mutators.
|
// Tile Mutators.
|
||||||
SDL_Surface* SetTileTexture(SDL_Surface* arg) { _tile.SetTexture(arg); return NULL; }
|
SDL_Surface* SetTileTexture(SDL_Surface* arg) { _tile.SetTexture(arg); return NULL; }
|
||||||
|
void SetTileTextureName(string path) { _tile.SetTextureName(path); }
|
||||||
|
string GetTileTextureName(void) { return _tile.GetTextureName(); }
|
||||||
void SetTileSolidity(bool arg) { _tile.SetSolidity(arg); }
|
void SetTileSolidity(bool arg) { _tile.SetSolidity(arg); }
|
||||||
bool GetTileSolidity(void) { return _tile.GetSolidity(); }
|
bool GetTileSolidity(void) { return _tile.GetSolidity(); }
|
||||||
// Well, it kinda helps if I lay the
|
// Well, it kinda helps if I lay the
|
||||||
@ -27,8 +29,10 @@ public:
|
|||||||
int GetTileX(void) { return _tile.GetX(); }
|
int GetTileX(void) { return _tile.GetX(); }
|
||||||
int GetTileY(void) { return _tile.GetY(); }
|
int GetTileY(void) { return _tile.GetY(); }
|
||||||
|
|
||||||
|
|
||||||
// Entity Mutators.
|
// Entity Mutators.
|
||||||
void SetEntityTexture(SDL_Surface* arg) { _entity.SetTexture(arg); }
|
void SetEntityTexture(SDL_Surface* arg) { _entity.SetTexture(arg); }
|
||||||
|
void SetEntityTextureName(string path) { _entity.SetTextureName(path); }
|
||||||
void SetEntityXY(int xArg, int yArg) { _entity.SetXY(xArg, yArg); }
|
void SetEntityXY(int xArg, int yArg) { _entity.SetXY(xArg, yArg); }
|
||||||
void SetEntitySolidity(bool arg) { _entity.SetSolidity(arg); }
|
void SetEntitySolidity(bool arg) { _entity.SetSolidity(arg); }
|
||||||
bool GetEntitySolitity(void) { return _entity.GetSolidity(); }
|
bool GetEntitySolitity(void) { return _entity.GetSolidity(); }
|
||||||
@ -38,6 +42,7 @@ public:
|
|||||||
int GetEntityY(void) { return _entity.GetY(); }
|
int GetEntityY(void) { return _entity.GetY(); }
|
||||||
int GetEntityWidth(void) { return _entity.GetWidth(); }
|
int GetEntityWidth(void) { return _entity.GetWidth(); }
|
||||||
int GetEntityHeight(void) { return _entity.GetHeight(); }
|
int GetEntityHeight(void) { return _entity.GetHeight(); }
|
||||||
|
string GetEntityTextureName(void) { return _entity.GetTextureName(); }
|
||||||
|
|
||||||
// ZLevel Mutators.
|
// ZLevel Mutators.
|
||||||
void SetZLevel(int arg) { _zLevel = arg; }
|
void SetZLevel(int arg) { _zLevel = arg; }
|
||||||
|
Loading…
Reference in New Issue
Block a user