mirror of
https://github.com/ApfelTeeSaft/PlanetFleet.git
synced 2026-08-26 19:33:38 +00:00
refactored console
This commit is contained in:
+3
-2
@@ -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)
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
#include "abstractCommand.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <vector.h>
|
||||
|
||||
namespace battleship{
|
||||
using namespace std;
|
||||
|
||||
vector<string> AbstractCommand::explodeString(string commandStr){
|
||||
vector<int> spaceIds;
|
||||
vector<string> 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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef ABSTRACT_COMMAND_H
|
||||
#define ABSTRACT_COMMAND_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
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<std::string> arguments;
|
||||
private:
|
||||
std::vector<std::string> explodeString(std::string);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,59 @@
|
||||
#include <solUtil.h>
|
||||
#include <stateManager.h>
|
||||
|
||||
#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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef ADD_UNIT_COMMAND_H
|
||||
#define ADD_UNIT_COMMAND_H
|
||||
|
||||
#include "abstractCommand.h"
|
||||
|
||||
#include <vector.h>
|
||||
#include <quaternion.h>
|
||||
|
||||
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
|
||||
Executable
+19
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifndef CONSOLE_COMMAND_H
|
||||
#define CONSOLE_COMMAND_H
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace battleship{
|
||||
class Console{
|
||||
public:
|
||||
static void execute(std::string);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,73 +0,0 @@
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <cmath>
|
||||
#include <vector.h>
|
||||
|
||||
#include <solUtil.h>
|
||||
#include <stateManager.h>
|
||||
|
||||
#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<string> 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<string> &fullCommand){
|
||||
vector<int> 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<string> &fullCommand){
|
||||
//addUnit <playerId> <unitId> [<posX> <posY> <posZ> ]
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
#pragma once
|
||||
#ifndef CONSOLE_COMMAND_H
|
||||
#define CONSOLE_COMMAND_H
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
#include <vector.h>
|
||||
#include <quaternion.h>
|
||||
|
||||
namespace battleship{
|
||||
class ConsoleCommand{
|
||||
public:
|
||||
static void execute(std::string);
|
||||
private:
|
||||
static void parse(std::string, std::vector<std::string>&);
|
||||
static void validateAddUnitArguments(std::vector<std::string>&);
|
||||
static void executeAddUnit(int, int, vb01::Vector3, vb01::Quaternion);
|
||||
static void validateDebugUnitsArguments();
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
+3
-3
@@ -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<string> difficultyLevels, vector<string> factions, string mapName) : AbstractAppState(
|
||||
|
||||
Reference in New Issue
Block a user