I've been programming an adventure game and have everything working up until the movement throughout a two-dimensional array dungeon. The Maps are their own struct, while the Character is a class. One of the member variables of the Character class is a Map, curMap
, which represents the map the player is currently on. However, in testing in Main.cpp, the works but the map doesn't update properly. Here's the code, after which I'll explain what I've already done.
Maps.h
#include <string>
#include <vector>
using namespace std;
struct Map {
vector<string> layout;
vector<int> exits; //indices in allMaps array
string name;
int level;
Map();
Map(vector<string> l, vector<int> e, string n, int lvl);
};
extern Map noMap;
extern Map highashPlainsA, highashPlainsB, highashPlainsC; //Highash Plains
extern Map alnwick, alnwickForge, alnwickMarket, alnwickInn; //Alnwick
extern vector<Map> allMaps;
extern vector<string> blankLayout;
extern vector<string> plainsLayoutA, plainsLayoutB, plainsLayoutC; //Highash Plains
extern vector<string> alnwickLayout; //Major Towns
extern vector<string> forgeLayout, marketLayout, innLayout; //Town Buildings
Maps.cpp
Map::Map() :
layout(blankLayout),
exits({0, 0, 0, 0}),
name("_NO_MAP_"),
level(0){}
Map::Map(vector<string> l, vector<int> e, string n, int lvl) :
layout(l),
exits(e),
name(n),
level(lvl){}
Map noMap = Map();
Map highashPlainsA = Map(plainsLayoutA, {4, 3, 2}, "Highash Plains A", 1);
// Map highashPlainsB = Map(plainsLayoutB, {}, "Highash Plains B", 1);
// Map highashPlainsC = Map(plainsLayoutC, {}, "Highash Plains C", 1);
// Map alnwick = Map(alnwickLayout, {}, "Alnwick", 1);
// Map alnwickForge = Map(forgeLayout, {}, "Alnwick Forge", 1);
// Map alnwickMarket = Map(marketLayout, {}, "Alnwick Market", 1);
// Map alnwickInn = Map(innLayout, {}, "Alnwick Inn", 1);
vector<Map> allMaps = {noMap, highashPlainsA/*, highashPlainsB, highashPlainsC, alnwick, alnwickForge, alnwickMarket, alnwickInn*/};
vector<string> blankLayout =
{"##############################",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"##############################"};
vector<string> plainsLayoutA =
{"##########################33##",
"# G ##### ####### E #### 2",
"# # # D # # G # 2",
"# # # ## ####################",
"# # ##E# # G G#",
"#E# G# G G#",
"# # ##########E############",
"# #### #DE G ED#",
"# ###D #DE G ED#",
"################111###########"};
vector<string> plainsLayoutB =
{"##############################",
"#D #E #### D #### #",
"# # # ##G #### #",
"# # ##E##### #",
"# #G#### #",
"# #D# #### ###### #",
"# E # #GGGD# #",
"# # ##EE### #",
"#D E E # #",
"##########################11##"};
vector<string> plainsLayoutC = {};
vector<string> alnwickLayout = {};
vector<string> forgeLayout = {};
vector<string> marketLayout = {};
vector<string> innLayout = {};
Character.h (I cut out a lot of unimportant code here)
class Character {
Map curMap;
public:
Character();
~Character();
void UpdateMap();
void SetMap(Map map);
Map GetMap();
void ActionLoop();
};
extern Character player;
Character.cpp (Left out said unimportant code here as well)
Character::Character() : curMap(highashPlainsA){}
void Character::UpdateMap(){
// sHandler.ClearScreen(); //In a separate class that does work, is just a call to system("cls")
// sHandler.DisplayMap(curMap); //For loop from 0 to (but not including) the size of the layout vector, uses cout to print each row. Works given the correct layout.
}
void Character::SetMap(Map map){
cout << "OLD MAP NAME: " << curMap.name << "\n";
curMap = map;
curMap.layout = map.layout;
curMap.exits = map.exits;
curMap.name = map.name;
curMap.level = map.level;
cout << "NEW MAP NAME: " << curMap.name << "\n";
UpdateMap();
}
Map Character::GetMap(){
return curMap;
}
void Character::ActionLoop(){
while (true){
MoveHandler();
}
}
Character player = Character();
Main.cpp
#include "Setup.h"
#include <windows.h>
#include <stdio.h>
int main(){
// sHandler.SetupScreen(); // WINAPI stuff to set console colors
player.SetMap(highashPlainsA);
cout << "\nSIZE OF CURRENT LAYOUT: " << player.GetMap().layout.size() << "\n";
cout << "SIZE OF PLAINS A: " << plainsLayoutA.size() << "\n";
cout << "Name of map: " << player.GetMap().name << "\n";
// cout << "Layout of map: \n" << player.GetMap().layout[0] << "\n" << player.GetMap().layout[1] << "\n" << player.GetMap().layout[2] << "\n"<< player.GetMap().layout[3] << "\n"; // Crashes because for whatever reason the layout size is 0?
system("pause"); // Get input before current map written to screen
sHandler.DisplayMap(player.GetMap());
system("pause"); // Wait again before clearing in UpdateMap()
player.UpdateMap(); //Reset map just in case
player.ActionLoop(); // Run the movement to test the layout, if correct
return 0;
}
What has been tested: In Character::SetMap()
I even tried changing each individual variable, but shouldn't setting the Map as a whole change all of those values? Additionally, in Map::Map(vector<string> l, vector<int> e, string n, int lvl)
, I tried it without an initializer list but it still didn't work. I didn't really expect it to, but it was worth a shot I guess.
The issues: The character class does initialize the curMap
member, and calls the default constructor to do so. This is intended functionality, because it doesn't matter what map the player is on during setup. However, upon trying to set the map to another precreated Map instance, the layout doesn't change at all, and instead sets the layout to a completely empty vector. What is going on, and how can I fix it?
Aucun commentaire:
Enregistrer un commentaire