From 728f4b6d4a31355198383b7ad95e6758010ed780 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 9 Feb 2025 15:14:34 +0200 Subject: [PATCH] shifting submarine frame dive depth --- Assets/Scripts/Core/options.lua | 2 +- activeGameState.cpp | 88 +++++++++------------------------ activeGameState.h | 1 + binds.h | 2 +- buildButton.cpp | 3 +- defConfigs.h | 2 +- gameObjectFrameController.cpp | 73 ++++++++++++++++++++++----- gameObjectFrameController.h | 15 +++--- gameObjectListbox.cpp | 2 +- inGameAppState.cpp | 2 +- mapEditorAppState.cpp | 6 +-- 11 files changed, 105 insertions(+), 91 deletions(-) diff --git a/Assets/Scripts/Core/options.lua b/Assets/Scripts/Core/options.lua index ac32739..8c95ea4 100644 --- a/Assets/Scripts/Core/options.lua +++ b/Assets/Scripts/Core/options.lua @@ -22,7 +22,7 @@ mappings = { {bind = 18, trigger = 340, action = true, bindType = 0, inOptions = true}, {bind = 19, trigger = 80, action = true, bindType = 0, inOptions = true}, {bind = 20, trigger = 76, action = true, bindType = 0, inOptions = true}, - {bind = 21, trigger = 83, action = true, bindType = 0, inOptions = true}, + {bind = 21, trigger = 32, action = true, bindType = 0, inOptions = true}, {bind = 22, trigger = 48, action = true, bindType = 0, inOptions = true}, {bind = 23, trigger = 49, action = true, bindType = 0, inOptions = true}, {bind = 24, trigger = 50, action = true, bindType = 0, inOptions = true}, diff --git a/activeGameState.cpp b/activeGameState.cpp index f232627..fffb9bc 100755 --- a/activeGameState.cpp +++ b/activeGameState.cpp @@ -170,20 +170,21 @@ namespace battleship{ fc->placeGameObjectFrame(fc->getNumGameObjectFrames() - 1, targets[0].pos, selectedUnits[i]->getWidth(), selectedUnits[i]->getLength()); } - fc->setRotatingFrames(true); selectingDestOrient = true; + fc->setPlacingOnSurface(true); + fc->setRotating(true); } else if(selectingDestOrient && !orderMouseClicked){ selectingDestOrient = false; - fc->setPlacingFrames(false); - fc->setRotatingFrames(false); + fc->setPlacingOnSurface(false); + fc->setRotating(false); fc->removeGameObjectFrames(); } renderUnits(); - if(fc->isPlacingFrames()) + if(fc->isPlacingOnSurface()) fc->update(); CameraController *camCtr = CameraController::getSingleton(); @@ -220,8 +221,8 @@ namespace battleship{ mainPlayer->deselectUnits(); GameObjectFrameController *ufCtr = GameObjectFrameController::getSingleton(); ufCtr->removeGameObjectFrames(); - ufCtr->setPlacingFrames(false); - ufCtr->setRotatingFrames(false); + ufCtr->setPlacingOnSurface(false); + ufCtr->setRotating(false); unitGuiScreen = ""; ConcreteGuiManager::getSingleton()->readLuaScreenScript("activeGameState.lua"); @@ -441,53 +442,8 @@ namespace battleship{ Map *map = Map::getSingleton(); vector results = map->raycastTerrain(camPos, rayDir, true); - if(!results.empty()){ - GameObjectFrameController *ufCtr = GameObjectFrameController::getSingleton(); - - for(Unit *u : mainPlayer->getSelectedUnits()){ - Vector3 pos = u->getPos(); - Node *nodeParent = map->getNodeParent(); - - if(u->getType() == UnitType::UNDERWATER && nodeParent->getNumChildren() > 0){ - Vector3 cellSize = map->getCellSize(), waterBodyPos; - bool inWater = false; - - for(int i = 1; i < nodeParent->getNumChildren(); i++){ - Vector3 wPos = nodeParent->getChild(i)->getPosition(); - Vector3 wSize = ((Quad*)nodeParent->getChild(i)->getMesh(0))->getSize(); - - if(fabs(wPos.x - pos.x) < .5 * wSize.x && fabs(wPos.z - pos.z) < .5 * wSize.y){ - waterBodyPos = wPos; - inWater = true; - break; - } - } - - if(inWater){ - vector res = map->raycastTerrain( - Vector3(results[0].pos.x, 100, results[0].pos.z), - -Vector3::VEC_J, - false - ); - - vector &cells = map->getCells(); - int cid = map->getCellId(results[0].pos, false); - int numSubmarineCells = cells[cid].underWaterCellIds.size(); - float maxDepth = cells[cid].pos.y; - float minDepth = (numSubmarineCells > 0 ? cells[cells[cid].underWaterCellIds[numSubmarineCells - 1]].pos.y : maxDepth) - .5 * cellSize.y; - - float newDepth = res[0].pos.y + depth * (waterBodyPos.y - res[0].pos.y); - - if(newDepth < minDepth) newDepth = minDepth; - else if(newDepth > maxDepth) newDepth = maxDepth; - - results[0].pos.y = newDepth; - } - } - } - + if(!results.empty()) targets.push_back(Order::Target(nullptr, results[0].pos)); - } } void ActiveGameState::enableUnitState(Unit::State state){ @@ -536,8 +492,8 @@ namespace battleship{ quad->setSize(Vector3::VEC_ZERO); quad->updateVerts(quad->getMeshBase()); - ufCtr->setPlacingFrames(false); - ufCtr->setRotatingFrames(false); + ufCtr->setPlacingOnSurface(false); + ufCtr->setRotating(false); ufCtr->removeGameObjectFrames(); } @@ -556,7 +512,7 @@ namespace battleship{ if(controlPressed || (targets[0].unit && targets[0].unit->getPlayer()->getTeam() != mainPlayer->getTeam())) type = Order::TYPE::ATTACK; - else if(ufCtr->isPlacingFrames() && !selectingDestOrient) + else if(ufCtr->isPlacingOnSurface() && !selectingDestOrient) type = Order::TYPE::BUILD; issueOrder(type, targets, shiftPressed); @@ -587,7 +543,7 @@ namespace battleship{ castRayToTerrain(); issueOrder(Order::TYPE::ATTACK, targets, shiftPressed); } - else if(selectedUnits[0]->getUnitClass() == UnitClass::ENGINEER && ufCtr->isPlacingFrames()){ + else if(selectedUnits[0]->getUnitClass() == UnitClass::ENGINEER && ufCtr->isPlacingOnSurface()){ castRayToTerrain(); GameObjectFrame gmObjFr = ufCtr->getGameObjectFrame(0); @@ -639,7 +595,9 @@ namespace battleship{ break; } - case Bind::TOGGLE_SUB: + case Bind::SHIFT_SUB_DEPTH: + ufCtr->setPlacingVertically(isPressed); + ufCtr->setRotating(false); break; case Bind::ZOOM_IN: if (zooms > -NUM_MAX_ZOOMS) { @@ -656,7 +614,7 @@ namespace battleship{ } break; case Bind::LOOK_AROUND: - if(!ufCtr->isPlacingFrames()) + if(!ufCtr->isPlacingOnSurface()) CameraController::getSingleton()->setLookingAround(isPressed); break; case Bind::HALT: @@ -667,18 +625,14 @@ namespace battleship{ //TODO reimplement submarine diving mechanic case Bind::LEFT_CONTROL: controlPressed = isPressed; - if(isPressed) depth -= 0.05; break; case Bind::LEFT_SHIFT: { shiftPressed = isPressed; - GameObjectFrameController *ufCtr = GameObjectFrameController::getSingleton(); ufCtr->setPaintSelecting(shiftPressed); if(isPressed){ - depth += 0.05; - Vector3 startPos = Root::getSingleton()->getCamera()->getPosition(); vector results = Map::getSingleton()->raycastTerrain(startPos, (screenToSpace(getCursorPos()) - startPos).norm(), true); @@ -721,7 +675,7 @@ namespace battleship{ break; case Bind::DESELECT_STRUCTURE: ufCtr->removeGameObjectFrames(); - ufCtr->setPlacingFrames(false); + ufCtr->setPlacingOnSurface(false); buildableStructSelected = false; break; } @@ -739,13 +693,19 @@ namespace battleship{ dirProj = Vector3(dirProj.x, 0, dirProj.z).norm(); CameraController::getSingleton()->orientCamera(Vector3(0, 1, 0).cross(dirProj), strength); } + else if(ufCtr->isPlacingVertically()){ + depth -= .2 * strength; + + if(depth < 0) depth = 0; + else if(depth > 1) depth = 1; + } break; case Bind::LOOK_LEFT: case Bind::LOOK_RIGHT: if(camCtr->isLookingAround()) CameraController::getSingleton()->orientCamera(Vector3(0, 1, 0), strength); - else if(ufCtr->isRotatingFrames()) + else if(ufCtr->isRotating()) ufCtr->rotateGameObjectFrames(100 * strength); break; diff --git a/activeGameState.h b/activeGameState.h index dc55bbe..358125b 100755 --- a/activeGameState.h +++ b/activeGameState.h @@ -36,6 +36,7 @@ namespace battleship{ inline std::vector& getUnitGroup(int i){return unitGroups[i];} inline void setBuildableStructSelected(bool bss){this->buildableStructSelected = bss;} inline bool isBuildableStructSelected(){return buildableStructSelected;} + inline float getDepth(){return depth;} private: enum CursorState{ NORMAL, diff --git a/binds.h b/binds.h index a0d1205..97498d2 100644 --- a/binds.h +++ b/binds.h @@ -24,7 +24,7 @@ namespace battleship{ LEFT_SHIFT, SELECT_PATROL_POINTS, LAUNCH, - TOGGLE_SUB, + SHIFT_SUB_DEPTH, GROUP_0, GROUP_1, GROUP_2, diff --git a/buildButton.cpp b/buildButton.cpp index f1de07b..af0b31e 100644 --- a/buildButton.cpp +++ b/buildButton.cpp @@ -27,8 +27,7 @@ namespace battleship{ if(builder->getBuildableUnit(slotId).buildable){ GameObjectFrameController *ufCtr = GameObjectFrameController::getSingleton(); ufCtr->addGameObjectFrame(GameObjectFrame(builder->getBuildableUnit(slotId).id, GameObject::Type::UNIT)); - ufCtr->setPlacingFrames(true); - + ufCtr->setPlacingOnSurface(true); activeState->setBuildableStructSelected(true); } } diff --git a/defConfigs.h b/defConfigs.h index ba81256..b7f719e 100755 --- a/defConfigs.h +++ b/defConfigs.h @@ -97,7 +97,7 @@ namespace battleship{ Bind::LEFT_SHIFT, Bind::SELECT_PATROL_POINTS, Bind::LAUNCH, - Bind::TOGGLE_SUB, + Bind::SHIFT_SUB_DEPTH, Bind::GROUP_0, Bind::GROUP_1, Bind::GROUP_2, diff --git a/gameObjectFrameController.cpp b/gameObjectFrameController.cpp index 7a65ba1..3a077e7 100644 --- a/gameObjectFrameController.cpp +++ b/gameObjectFrameController.cpp @@ -1,6 +1,7 @@ #include #include "gameObjectFrameController.h" +#include "activeGameState.h" #include "resourceDeposit.h" #include "defConfigs.h" #include "player.h" @@ -11,10 +12,12 @@ #include #include -#include +#include #include #include +#include + #include namespace battleship{ @@ -116,29 +119,77 @@ namespace battleship{ for(GameObjectFrame &frame : gameObjectFrames) frame.update(); + Map *map = Map::getSingleton(); Vector3 startPos = Root::getSingleton()->getCamera()->getPosition(); - vector results = Map::getSingleton()->raycastTerrain(startPos, (screenToSpace(getCursorPos()) - startPos).norm(), true); + Vector3 endPos = screenToSpace(getCursorPos()); + vector results = map->raycastTerrain(startPos, (endPos - startPos).norm(), true); if(results.empty()) return; - Vector3 newPos, rowEnd; - sol::table sizeTable = generateView()[gameObjectFrames[0].getGameObjTableName()][gameObjectFrames[0].getId() + 1]["size"]; - float width = sizeTable["x"], length = sizeTable["z"]; + float width, length; - if(paintSelecting) + if(paintSelecting){ + sol::table sizeTable = generateView()[gameObjectFrames[0].getGameObjTableName()][gameObjectFrames[0].getId() + 1]["size"]; + width = sizeTable["x"], length = sizeTable["z"]; paintSelect(results[0].pos, width, length); - else if(!(paintSelecting || rotatingStructure)) - newPos = results[0].pos; + } + + if(placingVertically){ + if(!minDepthCalculated){ + placementPos = results[0].pos; + + Node *nodeParent = map->getNodeParent(); + Vector3 cellSize = map->getCellSize(), waterBodyPos; + bool inWater = false; + + for(int i = 1; i < nodeParent->getNumChildren(); i++){ + Vector3 wPos = nodeParent->getChild(i)->getPosition(); + Vector3 wSize = ((Quad*)nodeParent->getChild(i)->getMesh(0))->getSize(); + + if(fabs(wPos.x - endPos.x) < .5 * wSize.x && fabs(wPos.z - endPos.z) < .5 * wSize.y){ + waterBodyPos = wPos; + inWater = true; + break; + } + } + + if(inWater){ + vector res = map->raycastTerrain( + Vector3(endPos.x, 100, endPos.z), + -Vector3::VEC_J, + false + ); + + vector &cells = map->getCells(); + int cid = map->getCellId(placementPos, false); + int numSubmarineCells = cells[cid].underWaterCellIds.size(); + maxDepth = cells[cid].pos.y; + minDepth = (numSubmarineCells > 0 ? cells[cells[cid].underWaterCellIds[numSubmarineCells - 1]].pos.y : maxDepth) - .5 * cellSize.y; + minDepthCalculated = true; + } + } + else{ + ActiveGameState *activeState = (ActiveGameState*)(GameManager::getSingleton()->getStateManager()->getAppStateByType((int)AppStateType::ACTIVE_STATE)); + float newDepth = minDepth + activeState->getDepth() * (maxDepth - minDepth); + placementPos.y = newDepth; + } + } + else{ + minDepthCalculated = false; + + if(placingOnSurface) + placementPos = results[0].pos; + } for(int i = 0; i < gameObjectFrames.size(); i++){ if(paintSelecting){ float hypothenuse = sqrt(width * width + length * length); Vector3 dir = (results[0].pos - paintSelectRowStart).norm(); - newPos = paintSelectRowStart + dir * hypothenuse * i; + placementPos = paintSelectRowStart + dir * hypothenuse * i; } - - if(!rotatingStructure) placeGameObjectFrame(i, newPos, width, length); + if(!rotating && (placingOnSurface || placingVertically)) + placeGameObjectFrame(i, placementPos, width, length); } } diff --git a/gameObjectFrameController.h b/gameObjectFrameController.h index aaf83fa..d7e49d1 100644 --- a/gameObjectFrameController.h +++ b/gameObjectFrameController.h @@ -25,12 +25,14 @@ namespace battleship{ inline int getNumGameObjectFrames(){return gameObjectFrames.size();} inline void addGameObjectFrame(GameObjectFrame u){gameObjectFrames.push_back(u);} inline GameObjectFrame& getGameObjectFrame(int i){return gameObjectFrames[i];} - inline bool isPlacingFrames(){return placingStructures;} - inline void setPlacingFrames(bool ps){placingStructures = ps;} inline bool isPaintSelecting(){return paintSelecting;} inline void setPaintSelecting(bool ps){paintSelecting = ps;} - inline bool isRotatingFrames(){return rotatingStructure;} - inline void setRotatingFrames(bool rs){rotatingStructure = rs;} + inline bool isRotating(){return rotating;} + inline void setRotating(bool rs){rotating = rs;} + inline bool isPlacingOnSurface(){return placingOnSurface;} + inline void setPlacingOnSurface(bool ps){placingOnSurface = ps;} + inline bool isPlacingVertically(){return placingVertically;} + inline void setPlacingVertically(bool v){this->placingVertically = v;} inline void setPaintSelectRowStart(vb01::Vector3 st){paintSelectRowStart = st;} private: GameObjectFrameController(){} @@ -38,8 +40,9 @@ namespace battleship{ void snapToObj(GameObjectFrame&, GameObject::Type, int, float); std::vector gameObjectFrames; - vb01::Vector3 paintSelectRowStart, rowDir; - bool placingStructures = false, paintSelecting = false, rotatingStructure = false; + vb01::Vector3 paintSelectRowStart, placementPos; + float minDepth, maxDepth; + bool paintSelecting = false, rotating = false, placingOnSurface = false, placingVertically = false, minDepthCalculated = false; }; } diff --git a/gameObjectListbox.cpp b/gameObjectListbox.cpp index bb65af0..cae5092 100644 --- a/gameObjectListbox.cpp +++ b/gameObjectListbox.cpp @@ -16,6 +16,6 @@ namespace battleship{ GameObjectFrameController *ufCtr = GameObjectFrameController::getSingleton(); GameObject::Type type = (unitListbox ? GameObject::Type::UNIT : GameObject::Type::RESOURCE_DEPOSIT); ufCtr->addGameObjectFrame(GameObjectFrame(selectedOption, type)); - ufCtr->setPlacingFrames(true); + ufCtr->setPlacingOnSurface(true); } } diff --git a/inGameAppState.cpp b/inGameAppState.cpp index 47a028e..26a4c23 100755 --- a/inGameAppState.cpp +++ b/inGameAppState.cpp @@ -106,7 +106,7 @@ namespace battleship{ void InGameAppState::onAction(int bind, bool isPressed) { switch((Bind)bind){ case Bind::TOGGLE_MAIN_MENU: - if(isPressed && !GameObjectFrameController::getSingleton()->isPlacingFrames()) + if(isPressed && !GameObjectFrameController::getSingleton()->isPlacingOnSurface()) Game::getSingleton()->togglePause(); break; } diff --git a/mapEditorAppState.cpp b/mapEditorAppState.cpp index c31bab8..f4d63f1 100644 --- a/mapEditorAppState.cpp +++ b/mapEditorAppState.cpp @@ -624,7 +624,7 @@ namespace battleship{ GameObjectFrameController *ufCtr = GameObjectFrameController::getSingleton(); - if(ufCtr->isPlacingFrames()) + if(ufCtr->isPlacingOnSurface()) ufCtr->update(); } @@ -659,7 +659,7 @@ namespace battleship{ switch((Bind)bind){ case Bind::LOOK_AROUND: - if(ufCtr->isPlacingFrames()){ + if(ufCtr->isPlacingOnSurface()){ Player *player = Game::getSingleton()->getPlayer(0); GameObjectFrame &frame = ufCtr->getGameObjectFrame(0); Model *model = frame.getModel(); @@ -678,7 +678,7 @@ namespace battleship{ break; case Bind::ROTATE_OBJ_FRAME: ufCtr->removeGameObjectFrames(); - ufCtr->setPlacingFrames(false); + ufCtr->setPlacingOnSurface(false); break; case Bind::INCREASE_RADIUS: if(isPressed) mapEditor->updateCircleRadius(true);