diff --git a/CMakeLists.txt b/CMakeLists.txt index 0ae7005..5fbbd57 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,14 +12,15 @@ set(UNIT_BUTTONS unitButton.cpp buildButton.cpp) set(OPTIONS_BUTTONS optionsButton.cpp tabButton.cpp okButton.cpp defaultsButton.cpp) set(IN_GAME_APP_STATE_BUTTONS mainMenuButton.cpp) set(MAP_EDITOR_BUTTONS mapEditorButton.cpp newMapButton.cpp loadMapButton.cpp exportButton.cpp) -set(BUTTONS singlePlayerButton.cpp exitButton.cpp backButton.cpp consoleCommand.cpp playButton.cpp ${OPTIONS_BUTTONS} ${IN_GAME_APP_STATE_BUTTONS} ${MAP_EDITOR_BUTTONS} ${UNIT_BUTTONS}) +set(BUTTONS singlePlayerButton.cpp exitButton.cpp backButton.cpp playButton.cpp ${OPTIONS_BUTTONS} ${IN_GAME_APP_STATE_BUTTONS} ${MAP_EDITOR_BUTTONS} ${UNIT_BUTTONS}) set(LISTBOXES skyboxTextureListbox.cpp landTextureListbox.cpp unitListbox.cpp) 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(CORE gameManager.cpp defConfigs.cpp ${CONTROLLERS}) +set(CONSOLE console.cpp abstractCommand.cpp addUnitCommand.cpp) +set(CORE gameManager.cpp defConfigs.cpp ${CONSOLE} ${CONTROLLERS}) set(PROJECTILES projectile.cpp) set(FX explosion.cpp) set(VEHICLES vehicle.cpp engineer.cpp) diff --git a/abstractCommand.cpp b/abstractCommand.cpp new file mode 100644 index 0000000..5807c62 --- /dev/null +++ b/abstractCommand.cpp @@ -0,0 +1,34 @@ +#include "abstractCommand.h" + +#include +#include + +namespace battleship{ + using namespace std; + + vector AbstractCommand::explodeString(string commandStr){ + vector spaceIds; + vector fullCommand; + + for(int i = 0; i < commandStr.length(); i++) + if(commandStr[i] == ' ') + spaceIds.push_back(i); + + fullCommand.push_back(commandStr.substr(0, spaceIds[0])); + + for(int i = 0; i < spaceIds.size(); i++){ + bool lastSpace = (i == spaceIds.size() - 1); + string argument = commandStr.substr(spaceIds[i] + 1, lastSpace ? string::npos : spaceIds[i + 1] - spaceIds[i] - 1); + fullCommand.push_back(argument); + } + + return fullCommand; + } + + void AbstractCommand::handle(){ + if(cmdStr.find(" ") == -1) + return; + + arguments = explodeString(cmdStr); + } +} diff --git a/abstractCommand.h b/abstractCommand.h new file mode 100644 index 0000000..b02f6eb --- /dev/null +++ b/abstractCommand.h @@ -0,0 +1,22 @@ +#ifndef ABSTRACT_COMMAND_H +#define ABSTRACT_COMMAND_H + +#include +#include + +namespace battleship{ + class AbstractCommand{ + protected: + AbstractCommand(std::string str) : cmdStr(str){} + virtual void execute(){} + virtual void validate(){} + virtual void handle(); + + std::string cmdStr; + std::vector arguments; + private: + std::vector explodeString(std::string); + }; +} + +#endif diff --git a/addUnitCommand.cpp b/addUnitCommand.cpp new file mode 100644 index 0000000..7bc87c3 --- /dev/null +++ b/addUnitCommand.cpp @@ -0,0 +1,59 @@ +#include +#include + +#include "addUnitCommand.h" +#include "console.h" +#include "addUnitCommand.h" +#include "inGameAppState.h" +#include "player.h" +#include "unitFactory.h" + +namespace battleship{ + using namespace gameBase; + using namespace vb01; + + void AddUnitCommand::validate(){ + if(!(arguments.size() == 2 || arguments.size() == 5 || arguments.size() == 9)) + return; + + playerId = atoi(arguments[0].c_str()); + + if(playerId != 0 && playerId != 1) + return; + + unitId = atoi(arguments[1].c_str()); + + if(!(0 <= unitId && unitId <= (int)generateView()["numUnits"])) + return; + + if(arguments.size() >= 5){ + float x = atoi(arguments[2].c_str()); + float y = atoi(arguments[3].c_str()); + float z = atoi(arguments[4].c_str()); + pos = Vector3(x, y, z); + } + else if(2 < arguments.size() && arguments.size() < 5) + return; + + if(arguments.size() == 9){ + float w = atoi(arguments[5].c_str()); + float x = atoi(arguments[6].c_str()); + float y = atoi(arguments[7].c_str()); + float z = atoi(arguments[8].c_str()); + rot = Quaternion(w, x, y, z); + } + else if(5 < arguments.size() && arguments.size() < 9) + return; + } + + void AddUnitCommand::addUnit(){ + Player* player = Map::getSingleton()->getPlayer(playerId); + player->addUnit(UnitFactory::createUnit(player, unitId, pos, rot)); + } + + void AddUnitCommand::execute(){ + AbstractCommand::handle(); + validate(); + addUnit(); + } +} diff --git a/addUnitCommand.h b/addUnitCommand.h new file mode 100644 index 0000000..a0d5429 --- /dev/null +++ b/addUnitCommand.h @@ -0,0 +1,25 @@ +#ifndef ADD_UNIT_COMMAND_H +#define ADD_UNIT_COMMAND_H + +#include "abstractCommand.h" + +#include +#include + +namespace battleship{ + class AddUnitCommand : public AbstractCommand{ + public: + AddUnitCommand(std::string argsStr) : AbstractCommand(argsStr){} + void validate(); + void execute(); + private: + int playerId, unitId; + bool posEnabled, rotEnabled; + vb01::Vector3 pos = vb01::Vector3::VEC_ZERO; + vb01::Quaternion rot = vb01::Quaternion::QUAT_W; + + void addUnit(); + }; +} + +#endif diff --git a/console.cpp b/console.cpp new file mode 100755 index 0000000..370324d --- /dev/null +++ b/console.cpp @@ -0,0 +1,19 @@ +#include "console.h" +#include "addUnitCommand.h" + +using namespace std; + +namespace battleship{ + void Console::execute(string cmdStr) { + int space = cmdStr.find(" "); + string cmdName, argsStr; + + if(space != -1){ + cmdName = cmdStr.substr(0, space); + argsStr = cmdStr.substr(space + 1, string::npos); + } + + if(cmdName == "add-unit") + AddUnitCommand(argsStr).execute(); + } +} diff --git a/console.h b/console.h new file mode 100755 index 0000000..9675c27 --- /dev/null +++ b/console.h @@ -0,0 +1,13 @@ +#ifndef CONSOLE_COMMAND_H +#define CONSOLE_COMMAND_H + +#include + +namespace battleship{ + class Console{ + public: + static void execute(std::string); + }; +} + +#endif diff --git a/consoleCommand.cpp b/consoleCommand.cpp deleted file mode 100755 index 178d7c6..0000000 --- a/consoleCommand.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include -#include -#include -#include - -#include -#include - -#include "consoleCommand.h" -#include "inGameAppState.h" -#include "player.h" -#include "unitFactory.h" - -using namespace std; -using namespace vb01; -using namespace vb01Gui; -using namespace gameBase; - -namespace battleship{ - void ConsoleCommand::execute(string commandStr) { - vector fullCommand; - parse(commandStr, fullCommand); - - if (fullCommand[0] == "add-unit") { - validateAddUnitArguments(fullCommand); - Vector3 pos = Vector3::VEC_ZERO; - Quaternion rot = Quaternion::QUAT_W; - - if(fullCommand.size() == 6) - pos = Vector3(atof(fullCommand[3].c_str()), atof(fullCommand[4].c_str()), atof(fullCommand[5].c_str())); - - executeAddUnit(atoi(fullCommand[1].c_str()), atoi(fullCommand[2].c_str()), pos, rot); - } - } - - void ConsoleCommand::parse(string commandStr, vector &fullCommand){ - vector spaceIds; - - for(int i = 0; i < commandStr.length(); i++) - if(commandStr[i] == ' ') - spaceIds.push_back(i); - - fullCommand.push_back(commandStr.substr(0, spaceIds[0])); - - for(int i = 0; i < spaceIds.size(); i++){ - bool lastSpace = (i == spaceIds.size() - 1); - string argument = commandStr.substr(spaceIds[i] + 1, lastSpace ? string::npos : spaceIds[i + 1] - spaceIds[i] - 1); - fullCommand.push_back(argument); - } - } - - //TODO implement rotation argument - void ConsoleCommand::validateAddUnitArguments(vector &fullCommand){ - //addUnit [ ] - if(fullCommand.size() != 3 || fullCommand.size() != 6) - return; - - int playerId = atoi(fullCommand[1].c_str()); - - if(playerId != 0 && playerId != 1) - return; - - int unitId = atoi(fullCommand[2].c_str()); - - if(!(0 <= unitId && unitId <= (int)generateView()["numUnits"])) - return; - } - - void ConsoleCommand::executeAddUnit(int playerId, int unitId, Vector3 pos, Quaternion rot) { - Player* player = Map::getSingleton()->getPlayer(playerId); - player->addUnit(UnitFactory::createUnit(player, unitId, pos, rot)); - } -} diff --git a/consoleCommand.h b/consoleCommand.h deleted file mode 100755 index 03f712b..0000000 --- a/consoleCommand.h +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once -#ifndef CONSOLE_COMMAND_H -#define CONSOLE_COMMAND_H - -#include -#include - -#include -#include - -namespace battleship{ - class ConsoleCommand{ - public: - static void execute(std::string); - private: - static void parse(std::string, std::vector&); - static void validateAddUnitArguments(std::vector&); - static void executeAddUnit(int, int, vb01::Vector3, vb01::Quaternion); - static void validateDebugUnitsArguments(); - }; -} - -#endif diff --git a/inGameAppState.cpp b/inGameAppState.cpp index 188bf82..9680334 100755 --- a/inGameAppState.cpp +++ b/inGameAppState.cpp @@ -11,7 +11,7 @@ #include "defConfigs.h" #include "inGameAppState.h" -#include "consoleCommand.h" +#include "console.h" #include "unitFrameController.h" #include "concreteGuiManager.h" #include "vessel.h" @@ -36,13 +36,13 @@ namespace battleship{ ConcreteGuiManager::getSingleton()->readLuaScreenScript("console.lua"); } - InGameAppState::ConsoleButton::ConsoleButton::ConsoleCommandEntryButton::ConsoleCommandEntryButton(Textbox *t, Listbox *l, Vector2 pos, Vector2 size, string name) : Button(pos, size, name, GameManager::getSingleton()->getPath() + "Fonts/batang.ttf", -1, true) { + InGameAppState::ConsoleButton::ConsoleButton::ConsoleCommandEntryButton::ConsoleCommandEntryButton(Textbox *t, Listbox *l, Vector2 pos, Vector2 size, string name) : Button(pos, size, name, GameManager::getSingleton()->getPath() + "Fonts/batang.ttf", 257, true) { textbox = t; listbox = l; } void InGameAppState::ConsoleButton::ConsoleButton::ConsoleCommandEntryButton::onClick() { - ConsoleCommand::execute(wstringToString(textbox->getText())); + Console::execute(wstringToString(textbox->getText())); } InGameAppState::InGameAppState(vector difficultyLevels, vector factions, string mapName) : AbstractAppState(