diff --git a/Assets/Scripts/GameObjects/Units/structureData.lua b/Assets/Scripts/GameObjects/Units/structureData.lua index adfcca7..addffda 100644 --- a/Assets/Scripts/GameObjects/Units/structureData.lua +++ b/Assets/Scripts/GameObjects/Units/structureData.lua @@ -1,2 +1 @@ -numStructures = 1 maxUnevenness = 0.5 diff --git a/Assets/Scripts/GameObjects/Units/vehicleData.lua b/Assets/Scripts/GameObjects/Units/vehicleData.lua index f97af60..a73f9b7 100644 --- a/Assets/Scripts/GameObjects/Units/vehicleData.lua +++ b/Assets/Scripts/GameObjects/Units/vehicleData.lua @@ -1,4 +1,4 @@ -speed = {.025, .025, .025, .025, .05, .05, .1, .1, .1, .1, .1, .1, .15} -destinationOffset = {.75, .75, .75, .75, 1, 1, .1, .1, .1, .1, .1, .5, .5} -anglePrecision = {.1, .1, .1, .1, .1, .1, .1, .1, .1, .1, .1, 3, 3} -maxTurnAngle = {.1, .1, .1, .1, .5, .5, 1, 1, .5, .5, 1, 2, 2} +units.speed = {.025, .025, .025, .025, .05, .05, .1, .1, .1, .1, .1, .1, .15} +units.destinationOffset = {.75, .75, .75, .75, 1, 1, .1, .1, .1, .1, .1, .5, .5} +units.anglePrecision = {.1, .1, .1, .1, .1, .1, .1, .1, .1, .1, .1, 3, 3} +units.maxTurnAngle = {.1, .1, .1, .1, .5, .5, 1, 1, .5, .5, 1, 2, 2} diff --git a/Assets/Scripts/Gui/engineerCommands.lua b/Assets/Scripts/Gui/engineerCommands.lua index 6387419..4920789 100644 --- a/Assets/Scripts/Gui/engineerCommands.lua +++ b/Assets/Scripts/Gui/engineerCommands.lua @@ -6,6 +6,7 @@ gui = { pos = {x = res.x - 200, y = res.y - 200}, size = {x = 100, y = 100}, name = 'Point defense', + imagePath = '', guiType = GuiType.BUTTON, buttonType = ButtonType.BUILD, trigger = 66, diff --git a/Assets/Scripts/Gui/landFactoryCommands.lua b/Assets/Scripts/Gui/landFactoryCommands.lua new file mode 100644 index 0000000..ee5a327 --- /dev/null +++ b/Assets/Scripts/Gui/landFactoryCommands.lua @@ -0,0 +1,26 @@ +numGui = 2 +res = graphics.resolution +Size = {x = 100, y = 100} + +gui = { + { + pos = {x = res.x - 200, y = res.y - 200}, + size = Size, + name = 'Tank', + imagePath = '', + guiType = GuiType.BUTTON, + buttonType = ButtonType.LAND_FACTORY_TRAIN, + trigger = 84, + unitId = 1 + }, + { + pos = {x = res.x - Size.x - 200, y = res.y - 200}, + size = Size, + name = 'Artillery', + imagePath = '', + guiType = GuiType.BUTTON, + buttonType = ButtonType.LAND_FACTORY_TRAIN, + trigger = 65, + unitId = 2 + } +} diff --git a/Assets/Scripts/Gui/main.lua b/Assets/Scripts/Gui/main.lua index 4494797..fe49e8a 100644 --- a/Assets/Scripts/Gui/main.lua +++ b/Assets/Scripts/Gui/main.lua @@ -23,7 +23,9 @@ ButtonType = { CONSOLE = 19, MAIN_MENU = 20, CONSOLE_COMMAND_OK = 21, - BUILD = 22 + BUILD = 22, + LAND_FACTORY_TRAIN = 23, + NAVAL_FACTORY_TRAIN = 24 } ListboxType = { diff --git a/Assets/Scripts/Gui/navalFactoryCommands.lua b/Assets/Scripts/Gui/navalFactoryCommands.lua new file mode 100644 index 0000000..1347f79 --- /dev/null +++ b/Assets/Scripts/Gui/navalFactoryCommands.lua @@ -0,0 +1,15 @@ +numGui = 1 +res = graphics.resolution + +gui = { + { + pos = {x = res.x - 200, y = res.y - 200}, + size = {x = 100, y = 100}, + name = 'Transport', + imagePath = '', + guiType = GuiType.BUTTON, + buttonType = ButtonType.NAVAL_FACTORY_TRAIN, + trigger = 66, + unitId = 4 + } +} diff --git a/CMakeLists.txt b/CMakeLists.txt index ed24dea..e388e0a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,7 @@ set(CMAKE_BUILD_TYPE Debug) set(BUILD_TESTS ON) cmake_policy(SET CMP0015 NEW) -set(UNIT_BUTTONS unitButton.cpp buildButton.cpp) +set(UNIT_BUTTONS unitButton.cpp buildButton.cpp trainButton.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) @@ -24,7 +24,8 @@ set(CORE gameManager.cpp game.cpp defConfigs.cpp ${CONSOLE} ${CONTROLLERS}) set(PROJECTILES projectile.cpp) set(FX fx.cpp) set(VEHICLES vehicle.cpp engineer.cpp transport.cpp) -set(UNITS gameObjectFactory.cpp unit.cpp structure.cpp ${VEHICLES}) +set(STRUCTURES structure.cpp factory.cpp) +set(UNITS gameObjectFactory.cpp unit.cpp ${STRUCTURES} ${VEHICLES}) set(CONTENT player.cpp pathfinder.cpp map.cpp gameObject.cpp gameObjectFrame.h resourceDeposit.cpp ${UNITS} ${PROJECTILES} ${FX}) set(GAME_SRC ${STATES} ${GUI} ${CORE} ${CONTENT} ${UTIL}) diff --git a/activeGameState.cpp b/activeGameState.cpp index be20ead..0594ae9 100755 --- a/activeGameState.cpp +++ b/activeGameState.cpp @@ -190,16 +190,20 @@ namespace battleship{ for (Unit *u : p->getUnits()) units.push_back(u); + ConcreteGuiManager *guiManager = ConcreteGuiManager::getSingleton(); + for (Unit *u : units) { if (u->getPlayer() == mainPlayer){ Vector3 dragboxSize = ((Quad*)dragboxNode->getMesh(0))->getSize(); Vector3 dragboxOrigin = dragboxNode->getPosition(), dragboxEnd = dragboxOrigin + dragboxSize; Vector2 pos = u->getScreenPos(); - if(unitClassSelected(UnitClass::ENGINEER)){ - ConcreteGuiManager *guiManager = ConcreteGuiManager::getSingleton(); + if(!mainPlayer->getSelectedUnitsByClass(UnitClass::ENGINEER).empty()) guiManager->readLuaScreenScript("engineerCommands.lua"); - } + else if(!mainPlayer->getSelectedUnitsByClass(UnitClass::LAND_FACTORY).empty()) + guiManager->readLuaScreenScript("landFactoryCommands.lua"); + else if(!mainPlayer->getSelectedUnitsByClass(UnitClass::NAVAL_FACTORY).empty()) + guiManager->readLuaScreenScript("navalFactoryCommands.lua"); if(isSelectionBox && fabs(pos.x - dragboxOrigin.x) < .5 * dragboxSize.x && fabs(pos.y - dragboxOrigin.y) < .5 * dragboxSize.y){ if(!u->isSelected()){ @@ -310,13 +314,6 @@ namespace battleship{ } } - bool ActiveGameState::unitClassSelected(UnitClass uc){ - for(Unit *u : mainPlayer->getSelectedUnits()) - if(u->getUnitClass() == uc) - return true; - - return false; - } void ActiveGameState::onAction(int bind, bool isPressed) { GameObjectFrameController *ufCtr = GameObjectFrameController::getSingleton(); diff --git a/activeGameState.h b/activeGameState.h index a4e5d26..af98748 100755 --- a/activeGameState.h +++ b/activeGameState.h @@ -41,7 +41,6 @@ namespace battleship{ void castRayToTerrain(); void issueOrder(Order::TYPE, std::vector, bool); bool isInLineOfSight(vb01::Vector3, float, Unit*); - bool unitClassSelected(UnitClass); inline bool canSelectHoveredOnGameObj(){return gameObjHoveredOn && gameObjHoveredOn->isSelectable() && gameObjHoveredOn->getPlayer() == mainPlayer;} GuiAppState *guiState; diff --git a/addUnitCommand.cpp b/addUnitCommand.cpp index a805aff..9132993 100644 --- a/addUnitCommand.cpp +++ b/addUnitCommand.cpp @@ -13,6 +13,7 @@ namespace battleship{ using namespace gameBase; using namespace vb01; + //TODO implement structure build status argument void AddUnitCommand::validate(){ if(!(arguments.size() == 2 || arguments.size() == 5 || arguments.size() == 9)) return; @@ -49,7 +50,7 @@ namespace battleship{ void AddUnitCommand::addUnit(){ Player* player = Game::getSingleton()->getPlayer(playerId); - player->addUnit(GameObjectFactory::createUnit(player, unitId, pos, rot)); + player->addUnit(GameObjectFactory::createUnit(player, unitId, pos, rot, 100)); } void AddUnitCommand::execute(){ diff --git a/concreteGuiManager.cpp b/concreteGuiManager.cpp index bddb542..323f91b 100644 --- a/concreteGuiManager.cpp +++ b/concreteGuiManager.cpp @@ -20,6 +20,7 @@ #include "inGameAppState.h" #include "mainMenuButton.h" #include "buildButton.h" +#include "trainButton.h" namespace battleship{ using namespace std; @@ -156,7 +157,12 @@ namespace battleship{ break; } case BUILD: - button = new BuildButton(pos, size, (int)guiTable["structureId"], name, (int)guiTable["trigger"], ""); + button = new BuildButton(pos, size, (int)guiTable["structureId"], name, (int)guiTable["trigger"], (string)guiTable["imagePath"]); + break; + case LAND_FACTORY_TRAIN: + button = new TrainButton(pos, size, name, (int)guiTable["trigger"], (string)guiTable["imagePath"], (int)UnitClass::LAND_FACTORY, (int)guiTable["unitId"]); + case NAVAL_FACTORY_TRAIN: + button = new TrainButton(pos, size, name, (int)guiTable["trigger"], (string)guiTable["imagePath"], (int)UnitClass::NAVAL_FACTORY, (int)guiTable["unitId"]); break; } diff --git a/concreteGuiManager.h b/concreteGuiManager.h index b0339ee..67340c0 100644 --- a/concreteGuiManager.h +++ b/concreteGuiManager.h @@ -32,7 +32,9 @@ namespace battleship{ CONSOLE_SCREEN, MAIN_MENU, CONSOLE_COMMAND_OK, - BUILD + BUILD, + LAND_FACTORY_TRAIN, + NAVAL_FACTORY_TRAIN }; enum ListboxType { CONTROLS, diff --git a/factory.cpp b/factory.cpp new file mode 100644 index 0000000..5319da7 --- /dev/null +++ b/factory.cpp @@ -0,0 +1,46 @@ +#include "factory.h" +#include "player.h" +#include "gameObjectFactory.h" + +namespace battleship{ + using namespace vb01; + + Factory::Factory(Player *player, int id, Vector3 pos, Quaternion rot, int buildStatus) : Structure(player, id, pos, rot, buildStatus){ + initProperties(); + } + + void Factory::initProperties(){ + } + + void Factory::update(){ + Structure::update(); + + if(!unitQueue.empty()) + train(); + } + + void Factory::appendToQueue(int uc){ + unitQueue.push_back(uc); + } + + void Factory::train(){ + bool training = !unitQueue.empty(); + buildStatusForeground->setVisible(training); + buildStatusBackground->setVisible(training); + + if(training){ + Unit::displayUnitStats(buildStatusForeground, buildStatusBackground, trainingStatus, 100, Vector2(0, -10)); + + if(canTrain()){ + trainingStatus++; + lastTrainTime = getTime(); + } + + if(trainingStatus >= 100){ + unitQueue.erase(unitQueue.begin()); + trainingStatus = 0; + player->addUnit(GameObjectFactory::createUnit(player, unitQueue[0], pos + 30 * dirVec, rot)); + } + } + } +} diff --git a/factory.h b/factory.h new file mode 100644 index 0000000..b777c4a --- /dev/null +++ b/factory.h @@ -0,0 +1,34 @@ +#ifndef FACTORY_H +#define FACTORY_H + +#include "structure.h" + +#include +#include + +namespace vb01{ + class Node; +} + +namespace battleship{ + class Player; + + class Factory : public Structure{ + public: + Factory(Player*, int, vb01::Vector3, vb01::Quaternion, int = 0); + ~Factory(){} + void update(); + void appendToQueue(int); + private: + const int MAX_LEN_QUEUE = 10; + int trainingStatus = 0, rateOfTraining = 50; + vb01::s64 lastTrainTime = 0; + std::vector unitQueue; + + void train(); + void initProperties(); + inline bool canTrain(){return vb01::getTime() - lastTrainTime > rateOfTraining;} + }; +} + +#endif diff --git a/gameObjectFactory.cpp b/gameObjectFactory.cpp index 6a095d1..8b04477 100644 --- a/gameObjectFactory.cpp +++ b/gameObjectFactory.cpp @@ -1,6 +1,6 @@ #include "gameObjectFactory.h" #include "player.h" -#include "structure.h" +#include "factory.h" #include "engineer.h" #include "transport.h" #include "projectile.h" @@ -16,7 +16,7 @@ namespace battleship{ using namespace vb01; using namespace gameBase; - Unit* GameObjectFactory::createUnit(Player *player, int id, Vector3 pos, Quaternion rot){ + Unit* GameObjectFactory::createUnit(Player *player, int id, Vector3 pos, Quaternion rot, int buildStatus){ sol::state_view SOL_LUA_STATE = generateView(); int unitClass = SOL_LUA_STATE["units"]["unitClass"][id + 1]; @@ -27,10 +27,11 @@ namespace battleship{ return new Transport(player, id, pos, rot); case UnitClass::LAND_FACTORY: case UnitClass::NAVAL_FACTORY: + return new Factory(player, id, pos, rot, buildStatus); case UnitClass::MARKET: case UnitClass::LAB: case UnitClass::POINT_DEFENSE: - return new Structure(player, id, pos, rot); + return new Structure(player, id, pos, rot, buildStatus); default: return new Vehicle(player, id, pos, rot); } diff --git a/gameObjectFactory.h b/gameObjectFactory.h index 655804d..be6e47b 100644 --- a/gameObjectFactory.h +++ b/gameObjectFactory.h @@ -13,7 +13,7 @@ namespace battleship{ class GameObjectFactory{ public: enum GameObjectType{UNIT, PROJECTILE, RESOURCE_DEPOSIT}; - static Unit* createUnit(Player*, int, vb01::Vector3, vb01::Quaternion); + static Unit* createUnit(Player*, int, vb01::Vector3, vb01::Quaternion, int = 0); static Projectile* createProjectile(Unit*, int, vb01::Vector3, vb01::Quaternion); static ResourceDeposit* createResourceDeposit(Player*, int, vb01::Vector3, vb01::Quaternion); private: diff --git a/player.cpp b/player.cpp index 61f4e71..2312583 100755 --- a/player.cpp +++ b/player.cpp @@ -157,6 +157,16 @@ namespace battleship{ return selectedUnit; } + vector Player::getSelectedUnitsByClass(UnitClass uc){ + vector selectedUnits = getSelectedUnits(), unitsByClass; + + for(Unit *u : selectedUnits) + if(u->getUnitClass() == uc) + unitsByClass.push_back(u); + + return unitsByClass; + } + void Player::selectUnits(vector selUnits){ for(Unit *u : selUnits) u->toggleSelection(true); diff --git a/player.h b/player.h index b8e5072..a7c8968 100755 --- a/player.h +++ b/player.h @@ -20,6 +20,7 @@ namespace battleship{ void removeUnit(Unit*); void removeUnit(int); bool isThisPlayersUnit(GameObject*); + std::vector getSelectedUnitsByClass(UnitClass); std::vector getSelectedUnits(); Unit* getSelectedUnit(int); void selectUnits(std::vector); diff --git a/structure.cpp b/structure.cpp index f3d2fc2..9467728 100644 --- a/structure.cpp +++ b/structure.cpp @@ -7,9 +7,7 @@ using namespace vb01; using namespace std; namespace battleship{ - Structure::Structure(Player *player, int id, Vector3 pos, Quaternion rot, int buildStatus) : Unit(player, id, pos, rot){ - this->buildStatus = buildStatus; - + Structure::Structure(Player *player, int id, Vector3 pos, Quaternion rot, int bldSt) : buildStatus(bldSt), Unit(player, id, pos, rot){ Vector2 size = Vector2(lenHpBar, 10); buildStatusBackground = Unit::createBar(Vector2::VEC_ZERO, size, Vector4(0, 0, 0, 1)); buildStatusForeground = Unit::createBar(Vector2::VEC_ZERO, size, Vector4(0, 0, 1, 1)); diff --git a/structure.h b/structure.h index 37574e2..94ff898 100644 --- a/structure.h +++ b/structure.h @@ -17,7 +17,8 @@ namespace battleship{ virtual void update(); private: int buildStatus = 0; - vb01::Node *buildStatusBackground, *buildStatusForeground; + protected: + vb01::Node *buildStatusBackground = nullptr, *buildStatusForeground = nullptr; }; } diff --git a/trainButton.cpp b/trainButton.cpp new file mode 100644 index 0000000..3017873 --- /dev/null +++ b/trainButton.cpp @@ -0,0 +1,27 @@ +#include "trainButton.h" +#include "activeGameState.h" +#include "factory.h" + +#include +#include + +namespace battleship{ + using namespace std; + using namespace vb01; + using namespace gameBase; + + TrainButton::TrainButton(Vector2 pos, Vector2 size, string name, int trigger, string imagePath, int fid, int tuid) : + factoryId(fid), + trainableUnitId(tuid), + UnitButton(pos, size, name, GameManager::getSingleton()->getPath() + "Fonts/batang.ttf", trigger, imagePath){} + + void TrainButton::onClick(){ + ActiveGameState *activeState = (ActiveGameState*)(GameManager::getSingleton()->getStateManager()->getAppStateByType((int)AppStateType::ACTIVE_STATE)); + Player *player = activeState->getPlayer(); + UnitClass facClass = (UnitClass)generateView()["units"]["unitClass"][factoryId + 1]; + vector factories = player->getSelectedUnitsByClass(facClass); + + for(Unit *fac : factories) + ((Factory*)fac)->appendToQueue(trainableUnitId); + } +} diff --git a/trainButton.h b/trainButton.h new file mode 100644 index 0000000..e600dd1 --- /dev/null +++ b/trainButton.h @@ -0,0 +1,16 @@ +#ifndef TRAIN_BUTTON_H +#define TRAIN_BUTTON_H + +#include "unitButton.h" + +namespace battleship{ + class TrainButton : public UnitButton{ + public: + TrainButton(vb01::Vector2, vb01::Vector2, std::string, int, std::string, int, int); + void onClick(); + private: + int factoryId, trainableUnitId; + }; +} + +#endif diff --git a/unit.cpp b/unit.cpp index f06665a..7baf3d6 100755 --- a/unit.cpp +++ b/unit.cpp @@ -24,6 +24,7 @@ using namespace gameBase; using namespace std; namespace battleship{ + //TODO remove the player member field Unit::Unit(Player *player, int id, Vector3 pos, Quaternion rot) : GameObject(GameObject::Type::UNIT, id, player, pos, rot){ this->id = id; this->player = player; diff --git a/vehicle.cpp b/vehicle.cpp index 91842cd..a7ecffd 100644 --- a/vehicle.cpp +++ b/vehicle.cpp @@ -83,7 +83,7 @@ namespace battleship{ } void Vehicle::initProperties(){ - sol::state_view SOL_LUA_STATE = generateView(); + sol::table SOL_LUA_STATE = generateView()[GameObject::getGameObjTableName()]; maxTurnAngle = SOL_LUA_STATE["maxTurnAngle"][id + 1]; speed = SOL_LUA_STATE["speed"][id + 1]; anglePrecision = SOL_LUA_STATE["anglePrecision"][id + 1];