From d62c5105cd5fbf5b26001f05bd2512e371464459 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 30 Sep 2023 16:27:39 +0300 Subject: [PATCH] implemented add-resource command --- CMakeLists.txt | 2 +- addResourceCommand.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++ addResourceCommand.h | 19 ++++++++++++++++++ console.cpp | 3 +++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 addResourceCommand.cpp create mode 100644 addResourceCommand.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5fbbd57..8e7e208 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,7 @@ set(GUI tooltip.cpp concreteGuiManager.cpp ${BUTTONS} ${LISTBOXES}) set(STATES activeGameState.cpp inGameAppState.cpp guiAppState.cpp mapEditorAppState.cpp) set(UTIL util.cpp binds.h) set(CONTROLLERS unitFrameController.cpp cameraController.cpp) -set(CONSOLE console.cpp abstractCommand.cpp addUnitCommand.cpp) +set(CONSOLE console.cpp abstractCommand.cpp addUnitCommand.cpp addResourceCommand.cpp) set(CORE gameManager.cpp defConfigs.cpp ${CONSOLE} ${CONTROLLERS}) set(PROJECTILES projectile.cpp) set(FX explosion.cpp) diff --git a/addResourceCommand.cpp b/addResourceCommand.cpp new file mode 100644 index 0000000..a0f4c37 --- /dev/null +++ b/addResourceCommand.cpp @@ -0,0 +1,44 @@ +#include "addResourceCommand.h" +#include "map.h" +#include "player.h" + +namespace battleship{ + void AddResourceCommand::validate(){ + if(arguments.size() != 3) + return; + + playerId = atoi(arguments[0].c_str()); + + if(playerId != 0 && playerId != 1) + return; + + resourceId = atoi(arguments[1].c_str()); + + if(!(0 <= resourceId && resourceId <= 2)) + return; + + resourceAmmount = atoi(arguments[2].c_str()); + } + + void AddResourceCommand::addResource(){ + Player *player = Map::getSingleton()->getPlayer(playerId); + + switch(resourceId){ + case 0: + player->addRefineds(resourceAmmount); + break; + case 1: + player->addWealth(resourceAmmount); + break; + case 2: + player->addResearch(resourceAmmount); + break; + } + } + + void AddResourceCommand::execute(){ + AbstractCommand::handle(); + validate(); + addResource(); + } +} diff --git a/addResourceCommand.h b/addResourceCommand.h new file mode 100644 index 0000000..b5c55bb --- /dev/null +++ b/addResourceCommand.h @@ -0,0 +1,19 @@ +#ifndef ADD_RESOURCE_COMMAND_H +#define ADD_RESOURCE_COMMAND_H + +#include "abstractCommand.h" + +namespace battleship{ + class AddResourceCommand : public AbstractCommand{ + public: + AddResourceCommand(std::string argsStr) : AbstractCommand(argsStr){} + void execute(); + private: + void validate(); + void addResource(); + + int playerId, resourceId, resourceAmmount; + }; +} + +#endif diff --git a/console.cpp b/console.cpp index 370324d..421a745 100755 --- a/console.cpp +++ b/console.cpp @@ -1,5 +1,6 @@ #include "console.h" #include "addUnitCommand.h" +#include "addResourceCommand.h" using namespace std; @@ -15,5 +16,7 @@ namespace battleship{ if(cmdName == "add-unit") AddUnitCommand(argsStr).execute(); + else if(cmdName == "add-resource") + AddResourceCommand(argsStr).execute(); } }