[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
|
||||
Save/save*
|
||||
Save/save_*
|
||||
Data/Media/Maps/save*
|
||||
Data/Media/Maps/save_*
|
||||
*.swp
|
||||
*.o
|
||||
*.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) {
|
||||
_saveGameID = savegameIDArg;
|
||||
NewSavegame(savegameIDArg);
|
||||
_map.Load("map");
|
||||
|
||||
int spawnX;
|
||||
int spawnY;
|
||||
@ -317,6 +316,12 @@ void Game::NewSavegame(const string savegameIDArg) {
|
||||
doc.LinkEndChild(saveElement);
|
||||
|
||||
doc.SaveFile(saveFilename.c_str());
|
||||
|
||||
stringstream mapPath;
|
||||
mapPath << "Data/Media/Maps/" << _saveGameID;
|
||||
|
||||
_map.New();
|
||||
_map.Save(_saveGameID);
|
||||
}
|
||||
|
||||
void Game::LoadSavegame(const string savegameIDArg) {
|
||||
@ -386,16 +391,12 @@ void Game::LoadSavegame(const string savegameIDArg) {
|
||||
// </health>
|
||||
|
||||
_player->SetHealthLiteral(playerHealth);
|
||||
|
||||
// <map> - Parse the map file.
|
||||
dataElem = dataElem->NextSiblingElement("map");
|
||||
assert(dataElem != NULL);
|
||||
_map.Load(dataElem->GetText());
|
||||
// </map>
|
||||
}
|
||||
// <save>
|
||||
|
||||
// </save>
|
||||
|
||||
_map.Load(_saveGameID);
|
||||
}
|
||||
|
||||
void Game::SaveSavegame(void) {
|
||||
@ -448,20 +449,17 @@ void Game::SaveSavegame(void) {
|
||||
TiXmlText* healthText = new TiXmlText(healthString.str().c_str());
|
||||
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(xElement);
|
||||
//saveElement->LinkEndChild(yElement);
|
||||
saveElement->LinkEndChild(levelElement);
|
||||
saveElement->LinkEndChild(expElement);
|
||||
saveElement->LinkEndChild(healthElement);
|
||||
saveElement->LinkEndChild(mapElement);
|
||||
|
||||
doc.LinkEndChild(decl);
|
||||
doc.LinkEndChild(saveElement);
|
||||
|
||||
doc.SaveFile(saveFilename.c_str());
|
||||
|
||||
_map.Save(_saveGameID);
|
||||
}
|
||||
|
@ -172,7 +172,7 @@ void WorldManager::OnPlayerAttack(Player* player) {
|
||||
delete npc;
|
||||
|
||||
if(_npcs.empty()) {
|
||||
_level->Load("map");
|
||||
_level->New();
|
||||
|
||||
int spawnX;
|
||||
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) {
|
||||
Unload();
|
||||
_currentMap = filename;
|
||||
@ -55,6 +85,7 @@ void LevelGen::Load(const string& filename) {
|
||||
stringstream tilePath;
|
||||
tilePath << "../Data/Media/Images/Tiles/" << dataElem->GetText() << ".png";
|
||||
_tile[x][y].SetTileTexture(_tileTextures.Add(tilePath.str()));
|
||||
_tile[x][y].SetTileTextureName(dataElem->GetText());
|
||||
// <tileTexture> - Finished applying the texture, move to the next sibling.
|
||||
|
||||
// <solidTile> - Check to see if the tile is solid or not.
|
||||
@ -76,6 +107,7 @@ void LevelGen::Load(const string& filename) {
|
||||
stringstream entityPath;
|
||||
entityPath << "../Data/Media/Images/Entities/" << entityName << ".png";
|
||||
_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].GetTileY() + TILE_HEIGHT / 2 - _tile[x][y].GetEntityHeight() / 2);
|
||||
@ -110,9 +142,68 @@ void LevelGen::Load(const string& filename) {
|
||||
// </map>
|
||||
levelWidth = x * TILE_WIDTH;
|
||||
levelHeight = y * TILE_HEIGHT;
|
||||
|
||||
_world = WorldManager(this);
|
||||
|
||||
GenerateEnemies();
|
||||
}
|
||||
|
||||
// procedural generation
|
||||
DoMagic();
|
||||
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) {
|
||||
@ -170,11 +261,14 @@ void LevelGen::Unload(void) {
|
||||
}
|
||||
|
||||
void LevelGen::DoMagic(void) {
|
||||
GenerateEntities("tree", 25);
|
||||
GenerateEntities("hedge", 15);
|
||||
GenerateEntities("barrel", 40);
|
||||
MakeWalkingPaths();
|
||||
GenerateEnemies();
|
||||
GenerateEntities("tree", 25);
|
||||
GenerateEntities("hedge", 15);
|
||||
GenerateEntities("barrel", 40);
|
||||
GenerateEntities("closedChest", 100);
|
||||
GenerateEntities("closedChestMetal", 150);
|
||||
GenerateEntities("closedChestMetal2", 250);
|
||||
MakeWalkingPaths();
|
||||
GenerateEnemies();
|
||||
}
|
||||
|
||||
void LevelGen::GenerateEntities(const string& name, int frequency) {
|
||||
@ -185,7 +279,8 @@ void LevelGen::GenerateEntities(const string& name, int frequency) {
|
||||
for(int y = 0; y < BOUNDARIES_Y; y++) {
|
||||
nextEntityGen--;
|
||||
if(!_tile[x][y].GetTileSolidity() && !_tile[x][y].GetEntitySolitity() && nextEntityGen <= 0) {
|
||||
_tile[x][y].SetEntityTexture(_entityTextures.AddAlpha(filename));
|
||||
_tile[x][y].SetEntityTextureName(name);
|
||||
_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].GetTileY() + TILE_HEIGHT / 2 - _tile[x][y].GetEntityHeight() / 2);
|
||||
|
@ -24,7 +24,9 @@ public:
|
||||
LevelGen(void);
|
||||
~LevelGen(void);
|
||||
|
||||
void New(void);
|
||||
void Load(const string& filename);
|
||||
void Save(const string& filename);
|
||||
void Update(void);
|
||||
void Render(void);
|
||||
|
||||
|
@ -37,3 +37,6 @@ int MapElement::GetX(void) { return x; }
|
||||
int MapElement::GetY(void) { return y; }
|
||||
int MapElement::GetWidth(void) { return _texture->w; }
|
||||
int MapElement::GetHeight(void) { return _texture->h; }
|
||||
|
||||
void MapElement::SetTextureName(string name) { _textureName = name; }
|
||||
string MapElement::GetTextureName(void) { return _textureName; }
|
||||
|
@ -25,9 +25,13 @@ public:
|
||||
int GetY(void);
|
||||
int GetWidth(void);
|
||||
int GetHeight(void);
|
||||
|
||||
void SetTextureName(string path);
|
||||
string GetTextureName(void);
|
||||
|
||||
protected:
|
||||
SDL_Surface* _texture;
|
||||
string _textureName;
|
||||
|
||||
bool _solid;
|
||||
|
||||
|
@ -18,6 +18,8 @@ public:
|
||||
|
||||
// Tile Mutators.
|
||||
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); }
|
||||
bool GetTileSolidity(void) { return _tile.GetSolidity(); }
|
||||
// Well, it kinda helps if I lay the
|
||||
@ -26,9 +28,11 @@ public:
|
||||
void SetTileXY(int xArg, int yArg) { _tile.SetXY(xArg, yArg); }
|
||||
int GetTileX(void) { return _tile.GetX(); }
|
||||
int GetTileY(void) { return _tile.GetY(); }
|
||||
|
||||
|
||||
// Entity Mutators.
|
||||
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 SetEntitySolidity(bool arg) { _entity.SetSolidity(arg); }
|
||||
bool GetEntitySolitity(void) { return _entity.GetSolidity(); }
|
||||
@ -38,6 +42,7 @@ public:
|
||||
int GetEntityY(void) { return _entity.GetY(); }
|
||||
int GetEntityWidth(void) { return _entity.GetWidth(); }
|
||||
int GetEntityHeight(void) { return _entity.GetHeight(); }
|
||||
string GetEntityTextureName(void) { return _entity.GetTextureName(); }
|
||||
|
||||
// ZLevel Mutators.
|
||||
void SetZLevel(int arg) { _zLevel = arg; }
|
||||
|
Loading…
Reference in New Issue
Block a user