git repos / saland

commit 8ad103d9

Poul Sander · 2019-04-15 09:38
8ad103d9581dcf3507ffbd37555b4473362333e8 patch · browse files
parent 4717dc564947a9241b690971e25e3c89a8399519

Can now use "goto" in the console

Changed files

M src/saland/Game.cpp 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 c62b539..8b1e075 100644 --- a/src/saland/Game.cpp +++ b/src/saland/Game.cpp
@@ -66,6 +66,25 @@ bool PlaceablesSortLowerY(const std::shared_ptr<Placeable>& i, const std::shared
return i->Y+i->Radius > j->Y+j->Radius;
}
+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) {
+ if (args.size() != 3) {
+ return "Must be ran like \"goto X Y\"";
+ }
+ teleport = true;
+ teleportX = std::stoi(args[1]);
+ teleportY = std::stoi(args[2]);
+ return "Teleport queued!";
+ }
+};
+
+static GotoConsoleCommand gcc;
+
struct Game::GameImpl {
GameRegion gameRegion;
std::shared_ptr<Human> human;
@@ -124,6 +143,7 @@ void Game::ResetWorld(int x, int y) {
}
Game::Game() {
+ RegisterCommand(&gcc);
data.reset(new Game::GameImpl());
data->lastUpdate = SDL_GetTicks();
ResetWorld(0,0);
@@ -392,6 +412,10 @@ void Game::Update() {
ResetWorld(data->gameRegion.GetRegionX(), data->gameRegion.GetRegionY()+1);
data->human->body->SetTransform(b2Vec2(data->gameRegion.world.tm.width/2, 0.01f),data->human->body->GetAngle()) ;
}
+ if (teleport) {
+ ResetWorld(teleportX, teleportY);
+ teleport = false;
+ }
if (data->consoleActive && data->console && !data->console->IsActive()) {
data->consoleActive = false;
}
diff --git a/src/saland/console/Console.cpp b/src/saland/console/Console.cpp index d702496..c2619d5 100644 --- a/src/saland/console/Console.cpp +++ b/src/saland/console/Console.cpp
@@ -32,7 +32,28 @@ static void SetFieldValues(sago::SagoTextField& field) {
field.SetOutline(1, {64,64,64,255});
}
+static std::map<std::string, ConsoleCommand*> commands;
+
+void RegisterCommand(ConsoleCommand* command) {
+ commands[command->getCommand()] = command;
+}
+
+struct HelpConsoleCommand : public ConsoleCommand {
+ virtual std::string getCommand() const override {return "help"; }
+
+ virtual std::string run(std::vector<std::string>) override {
+ std::string helpMessage = "Allowed commands: ";
+ for (const auto& cmd : commands) {
+ helpMessage += cmd.first + ", ";
+ }
+ return helpMessage;
+ }
+};
+
+static HelpConsoleCommand hcc;
+
Console::Console() {
+ RegisterCommand(&hcc);
editPosition = editLine.begin();
SDL_StartTextInput();
editField.SetHolder(&globalData.spriteHolder->GetDataHolder());
@@ -89,14 +110,28 @@ 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;
+}
+
void Console::ProcessCommand(const std::string& command) {
sago::SagoTextField f;
SetFieldValues(f);
f.SetText(command);
historyField.push_back(std::move(f));
+ std::vector<std::string> commandVector = splitByWhitespace(command);
sago::SagoTextField response;
SetFieldValues(response);
- response.SetText(std::string(" \"")+command+"\" not recognized");
+ if (commandVector.size() && commands.find(commandVector[0]) != commands.end() ) {
+ std::string ret = commands[commandVector[0]]->run(commandVector);
+ response.SetText(ret);
+ }
+ else {
+ response.SetText(std::string(" \"")+command+"\" not recognized");
+ }
historyField.push_back(std::move(response));
}
diff --git a/src/saland/console/Console.hpp b/src/saland/console/Console.hpp index 3823cb0..2306548 100644 --- a/src/saland/console/Console.hpp +++ b/src/saland/console/Console.hpp
@@ -30,6 +30,16 @@ https://github.com/sago007/saland
#include <vector>
#include "utf8.h"
+struct ConsoleCommand {
+ virtual std::string getCommand() const {return "version"; }
+ virtual std::string run(std::vector<std::string>) { return "Not implemented"; }
+};
+
+/**
+ * Registers a command for the console. The pointer MUST have static life time!
+ * */
+void RegisterCommand(ConsoleCommand* command);
+
class Console : public sago::GameStateInterface {
public:
Console();