diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp index 0b95b84..1da4c37 100644 --- a/src/saland/Game.cpp +++ b/src/saland/Game.cpp @@ -42,6 +42,7 @@ https://github.com/sago007/saland #include #include #include "console/Console.hpp" +#include "GameConsoleCommand.hpp" int32 velocityIterations = 6; int32 positionIterations = 2; @@ -212,6 +213,7 @@ Game::Game() { RegisterCommand(&rrcc); RegisterCommand(&sc); RegisterCommand(&cct); + GameConsoleCommandRegister(); data.reset(new Game::GameImpl()); data->human.reset(new Human()); data->lastUpdate = SDL_GetTicks(); diff --git a/src/saland/GameConsoleCommand.cpp b/src/saland/GameConsoleCommand.cpp new file mode 100644 index 0000000..5be7fdd --- /dev/null +++ b/src/saland/GameConsoleCommand.cpp @@ -0,0 +1,57 @@ +/* +=========================================================================== + * Saland Adventures +Copyright (C) 2014-2019 Poul Sander + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see http://www.gnu.org/licenses/ + +Source information and contacts persons can be found at +https://github.com/sago007/saland +=========================================================================== +*/ + +#include "../global.hpp" +#include "console/Console.hpp" + +struct ConcoleCommandGiveItem : public ConsoleCommand { + virtual std::string getCommand() const override {return "give";} + virtual std::string run(const std::vector& args) override { + if (args.size() != 3) { + throw std::runtime_error("Must be called like: give ITEM_NAME QUANTITY"); + } + std::string item_name = args.at(1); + int quantity = 1; + try { + quantity = std::stoi(args.at(2)); + } catch (...) { + std::string error_msg = "Failed to convert \""; + error_msg += args.at(2)+ "\" into an integer"; + throw std::runtime_error(error_msg); + } + globalData.player.item_inventory[item_name] += quantity; + return std::string("Added ")+std::to_string(quantity)+" of "+item_name; + } + + virtual std::string helpMessage() const override { + return "Give item to player. Must be called like: give ITEM_NAME QUANTITY"; + } +}; + +static ConcoleCommandGiveItem cc_give_item; + + + +void GameConsoleCommandRegister() { + RegisterCommand(&cc_give_item); +} \ No newline at end of file diff --git a/src/saland/GameConsoleCommand.hpp b/src/saland/GameConsoleCommand.hpp new file mode 100644 index 0000000..9f7cc68 --- /dev/null +++ b/src/saland/GameConsoleCommand.hpp @@ -0,0 +1,25 @@ +/* +=========================================================================== + * Saland Adventures +Copyright (C) 2014-2019 Poul Sander + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see http://www.gnu.org/licenses/ + +Source information and contacts persons can be found at +https://github.com/sago007/saland +=========================================================================== +*/ + + +void GameConsoleCommandRegister();