git repos / saland

commit 44151aef

Poul Sander · 2019-04-15 15:17
44151aef78f63bce1ce6592f9eadeb67cbe0cded patch · browse files
parent 8ad103d9581dcf3507ffbd37555b4473362333e8

Add command for resetting a world back to default

Changed files

M src/saland/Game.cpp before
M src/saland/Game.hpp before
M src/saland/GameRegion.cpp before
M src/saland/GameRegion.hpp before
M src/saland/console/Console.cpp before
M src/saland/console/Console.hpp before
diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp index 8b1e075..1487202 100644 --- a/src/saland/Game.cpp +++ b/src/saland/Game.cpp
@@ -66,13 +66,14 @@ bool PlaceablesSortLowerY(const std::shared_ptr<Placeable>& i, const std::shared
return i->Y+i->Radius > j->Y+j->Radius;
}
+static bool reset_region = false;
static bool teleport = false;
static int teleportX = 0;
static int teleportY = 0;
struct GotoConsoleCommand : public ConsoleCommand {
virtual std::string getCommand() const override {return "goto";}
- virtual std::string run(std::vector<std::string> args) {
+ virtual std::string run(const std::vector<std::string>& args) override {
if (args.size() != 3) {
return "Must be ran like \"goto X Y\"";
}
@@ -83,7 +84,16 @@ struct GotoConsoleCommand : public ConsoleCommand {
}
};
+struct ResetRegionConsoleCommand : public ConsoleCommand {
+ virtual std::string getCommand() const override {return "reset_region";}
+ virtual std::string run(const std::vector<std::string>&) override {
+ reset_region = true;
+ return "Region reset queued!";
+ }
+};
+
static GotoConsoleCommand gcc;
+static ResetRegionConsoleCommand rrcc;
struct Game::GameImpl {
GameRegion gameRegion;
@@ -121,9 +131,9 @@ static SpawnPoint GetSpawnpoint(const sago::tiled::TileMap& tm) {
return ret;
}
-void Game::ResetWorld(int x, int y) {
+void Game::ResetWorld(int x, int y, bool forceResetWorld) {
data->gameRegion.SaveRegion();
- data->gameRegion.Init(x, y, data->worldName);
+ data->gameRegion.Init(x, y, data->worldName, forceResetWorld);
data->human.reset(new Human());
data->gameRegion.placeables.push_back(data->human);
b2BodyDef myBodyDef;
@@ -144,9 +154,10 @@ void Game::ResetWorld(int x, int y) {
Game::Game() {
RegisterCommand(&gcc);
+ RegisterCommand(&rrcc);
data.reset(new Game::GameImpl());
data->lastUpdate = SDL_GetTicks();
- ResetWorld(0,0);
+ ResetWorld(0, 0, false);
data->bottomField.SetHolder(globalData.dataHolder);
data->bottomField.SetFontSize(20);
@@ -169,14 +180,14 @@ static void SetLengthToOne(float& x, float& y) {
static std::string GetLayerInfoForTile(const World& w, int x, int y) {
std::stringstream ret;
if (!sago::tiled::tileInBound(w.tm, x, y)) {
- ret << "Out of bound\n";
+ ret << "Out of bound";
return ret.str();
}
for (size_t i = 0; i < w.tm.layers.size(); ++i) {
const sago::tiled::TileLayer& tl = w.tm.layers[i];
int tile = sago::tiled::getTileFromLayer(w.tm, tl, x, y);
if (tile != 0) {
- ret << "(" << tl.name << ":" << tile << ")\n";
+ ret << "(" << tl.name << ":" << tile << ")";
}
}
return ret.str();
@@ -397,25 +408,29 @@ void Game::Update() {
data->gameRegion.physicsBox->Step(deltaTime / 1000.0f / 60.0f, velocityIterations, positionIterations);
std::sort(data->gameRegion.placeables.begin(), data->gameRegion.placeables.end(), sort_placeable);
if (data->human->X < 0) {
- ResetWorld(data->gameRegion.GetRegionX()-1, data->gameRegion.GetRegionY());
+ ResetWorld(data->gameRegion.GetRegionX()-1, data->gameRegion.GetRegionY(), false);
data->human->body->SetTransform(b2Vec2(data->gameRegion.world.tm.width, data->gameRegion.world.tm.height/2),data->human->body->GetAngle()) ;
}
if (data->human->X > data->gameRegion.world.tm.width*32) {
- ResetWorld(data->gameRegion.GetRegionX()+1, data->gameRegion.GetRegionY());
+ ResetWorld(data->gameRegion.GetRegionX()+1, data->gameRegion.GetRegionY(), false);
data->human->body->SetTransform(b2Vec2(0.01f, data->gameRegion.world.tm.height/2),data->human->body->GetAngle()) ;
}
if (data->human->Y < 0) {
- ResetWorld(data->gameRegion.GetRegionX(), data->gameRegion.GetRegionY()-1);
+ ResetWorld(data->gameRegion.GetRegionX(), data->gameRegion.GetRegionY()-1, false);
data->human->body->SetTransform(b2Vec2(data->gameRegion.world.tm.width/2, data->gameRegion.world.tm.height),data->human->body->GetAngle()) ;
}
if (data->human->Y > data->gameRegion.world.tm.height*32) {
- ResetWorld(data->gameRegion.GetRegionX(), data->gameRegion.GetRegionY()+1);
+ ResetWorld(data->gameRegion.GetRegionX(), data->gameRegion.GetRegionY()+1, false);
data->human->body->SetTransform(b2Vec2(data->gameRegion.world.tm.width/2, 0.01f),data->human->body->GetAngle()) ;
}
if (teleport) {
- ResetWorld(teleportX, teleportY);
+ ResetWorld(teleportX, teleportY, false);
teleport = false;
}
+ if (reset_region) {
+ ResetWorld(data->gameRegion.GetRegionX(), data->gameRegion.GetRegionY(), true);
+ reset_region = false;
+ }
if (data->consoleActive && data->console && !data->console->IsActive()) {
data->consoleActive = false;
}
diff --git a/src/saland/Game.hpp b/src/saland/Game.hpp index bcd6605..6ce128c 100644 --- a/src/saland/Game.hpp +++ b/src/saland/Game.hpp
@@ -39,7 +39,7 @@ public:
private:
struct GameImpl;
std::unique_ptr<GameImpl> data;
- void ResetWorld(int region_x, int region_y);
+ void ResetWorld(int region_x, int region_y, bool forceResetWorld);
};
#endif /* GAME_HPP */
diff --git a/src/saland/GameRegion.cpp b/src/saland/GameRegion.cpp index a40e8fe..3fb3bde 100644 --- a/src/saland/GameRegion.cpp +++ b/src/saland/GameRegion.cpp
@@ -24,7 +24,7 @@ https://github.com/sago007/saland
#include "GameRegion.hpp"
GameRegion::GameRegion() {
- Init(0, 0, "world1");
+ Init(0, 0, "world1", false);
}
static std::string createFileName(int x, int y,const std::string& worldName) {
@@ -32,12 +32,12 @@ static std::string createFileName(int x, int y,const std::string& worldName) {
return ret;
}
-void GameRegion::Init(int x, int y, const std::string& worldName) {
+void GameRegion::Init(int x, int y, const std::string& worldName, bool forceResetWorld) {
region_x = x;
region_y = y;
mapFileName = createFileName(region_x, region_y, worldName);
std::string loadMap = mapFileName;
- if (!sago::FileExists(loadMap.c_str())) {
+ if (!sago::FileExists(loadMap.c_str()) || forceResetWorld) {
loadMap = "maps/sample1.tmx";
if (region_x == 0 && region_y == -2) {
loadMap = "maps/city_0x-2.tmx";
diff --git a/src/saland/GameRegion.hpp b/src/saland/GameRegion.hpp index 1ff33ff..024f601 100644 --- a/src/saland/GameRegion.hpp +++ b/src/saland/GameRegion.hpp
@@ -31,7 +31,7 @@ https://github.com/sago007/saland
class GameRegion {
public:
GameRegion();
- void Init(int x, int y, const std::string& worldName);
+ void Init(int x, int y, const std::string& worldName, bool forceResetWorld);
std::vector<std::shared_ptr<Placeable> > placeables;
std::shared_ptr<b2World> physicsBox;
void SaveRegion();
diff --git a/src/saland/console/Console.cpp b/src/saland/console/Console.cpp index c2619d5..4d444f7 100644 --- a/src/saland/console/Console.cpp +++ b/src/saland/console/Console.cpp
@@ -24,6 +24,7 @@ https://github.com/sago007/saland
#include "Console.hpp"
#include "../GameDraw.hpp"
#include "../globals.hpp"
+#include <boost/tokenizer.hpp>
static void SetFieldValues(sago::SagoTextField& field) {
field.SetHolder(&globalData.spriteHolder->GetDataHolder());
@@ -41,7 +42,7 @@ void RegisterCommand(ConsoleCommand* command) {
struct HelpConsoleCommand : public ConsoleCommand {
virtual std::string getCommand() const override {return "help"; }
- virtual std::string run(std::vector<std::string>) override {
+ virtual std::string run(const std::vector<std::string>&) override {
std::string helpMessage = "Allowed commands: ";
for (const auto& cmd : commands) {
helpMessage += cmd.first + ", ";
@@ -110,11 +111,14 @@ void Console::removeChar() {
}
}
-static std::vector<std::string> splitByWhitespace(std::string const &input) {
- std::istringstream buffer(input);
- std::vector<std::string> ret((std::istream_iterator<std::string>(buffer)),
- std::istream_iterator<std::string>());
- return ret;
+static std::vector<std::string> splitByWhitespace(std::string const &line) {
+ std::vector<std::string> arg;
+ boost::escaped_list_separator<char> els('\\',' ','\"');
+ boost::tokenizer<boost::escaped_list_separator<char> > tok(line, els);
+ for (const std::string& item : tok) {
+ arg.push_back(item);
+ }
+ return arg;
}
void Console::ProcessCommand(const std::string& command) {
diff --git a/src/saland/console/Console.hpp b/src/saland/console/Console.hpp index 2306548..4eb336e 100644 --- a/src/saland/console/Console.hpp +++ b/src/saland/console/Console.hpp
@@ -32,7 +32,7 @@ https://github.com/sago007/saland
struct ConsoleCommand {
virtual std::string getCommand() const {return "version"; }
- virtual std::string run(std::vector<std::string>) { return "Not implemented"; }
+ virtual std::string run(const std::vector<std::string>&) { return "Not implemented"; }
};
/**