From 11afaa32d35926e432910ce1146ca8dbb30e585c Mon Sep 17 00:00:00 2001 From: devZoGok Date: Fri, 16 Sep 2022 22:22:25 +0300 Subject: [PATCH] first submarine submerging iteration --- Assets/Scripts/unitData.lua | 4 +-- activeGameState.cpp | 62 +++++++++++++++++++++++++++---------- activeGameState.h | 15 ++++----- map.cpp | 11 ++++--- map.h | 6 ++-- unit.cpp | 39 ++++++++++++++++++----- unit.h | 5 ++- 7 files changed, 100 insertions(+), 42 deletions(-) diff --git a/Assets/Scripts/unitData.lua b/Assets/Scripts/unitData.lua index 9fea3ca..74f4ad0 100644 --- a/Assets/Scripts/unitData.lua +++ b/Assets/Scripts/unitData.lua @@ -6,7 +6,7 @@ UnitType = {UNDERWATER = 0, SEA_LEVEL = 1, LAND = 2, AIR = 3}; unitClass = { UnitClass.VESSEL, UnitClass.VESSEL, - UnitClass.DESTROYER, UnitClass.DESTROYER, + UnitClass.VESSEL, UnitClass.DESTROYER, UnitClass.CRUISER, UnitClass.CRUISER, UnitClass.AIRCRAFT_CARRIER, UnitClass.AIRCRAFT_CARRIER, UnitClass.SUBMARINE, @@ -14,7 +14,7 @@ unitClass = { }; unitType = { - UnitType.SEA_LEVEL, UnitType.LAND, + UnitType.SEA_LEVEL, UnitType.UNDERWATER, UnitType.SEA_LEVEL, UnitType.SEA_LEVEL, UnitType.SEA_LEVEL, UnitType.SEA_LEVEL, UnitType.SEA_LEVEL, UnitType.SEA_LEVEL, diff --git a/activeGameState.cpp b/activeGameState.cpp index 474691e..a556609 100755 --- a/activeGameState.cpp +++ b/activeGameState.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -58,6 +59,16 @@ namespace battleship{ dragboxNode = new Node(); dragboxNode->attachMesh(dragbox); root->getGuiNode()->attachChild(dragboxNode); + + Text *t = new Text(GameManager::getSingleton()->getPath() + "Fonts/batang.ttf", L"depth: "); + Material *tm = new Material(root->getLibPath() + "text"); + tm->addBoolUniform("texturingEnabled", false); + tm->addVec4Uniform("diffuseColor", Vector4(1, 1, 1, 1)); + t->setMaterial(tm); + textNode = new Node(Vector3(0, 100, 0)); + textNode->addText(t); + + depth = 1; } ActiveGameState::~ActiveGameState() { @@ -65,12 +76,17 @@ namespace battleship{ void ActiveGameState::onAttached() { AbstractAppState::onAttached(); + Root::getSingleton()->getGuiNode()->attachChild(textNode); } - void ActiveGameState::onDettached() { AbstractAppState::onDettached(); + void ActiveGameState::onDettached() { + AbstractAppState::onDettached(); + Root::getSingleton()->getGuiNode()->attachChild(textNode); } void ActiveGameState::update() { + textNode->getText(0)->setText(L"Depth: " + to_wstring(depth)); + if (isSelectionBox) updateSelectionBox(); @@ -222,14 +238,26 @@ namespace battleship{ targets[i + 1] = targets[i]; LineRenderer *lineRenderer = LineRenderer::getSingleton(); + Map *map = Map::getSingleton(); for (int i = 0; i < selectedUnits.size(); ++i) { Unit *u = selectedUnits[i]; + + if(u->getType() == UnitType::UNDERWATER && o.type == Order::TYPE::MOVE){ + for(int j = 0; j < map->getNumWaterBodies(); j++){ + WaterBody w = map->getWaterBody(j); + + if(w.isPointWithin(u->getPos()) && w.isPointWithin(targets[i].pos)){ + targets[i].pos.y = -map->getBottom() + depth * fabs(w.pos.y - map->getBottom()); + break; + } + } + } + lineRenderer->addLine(u->getPos(), targets[i].pos, color); vector lines = lineRenderer->getLines(); o.line = lines[lines.size() - 1]; - if (type != Order::TYPE::LAUNCH || (u->getId() == 4 || u->getId() == 5)) { if(type == Order::TYPE::PATROL){ Vector3 p = u->getPos(); @@ -247,23 +275,23 @@ namespace battleship{ } void ActiveGameState::addTarget() { - Camera *cam = Root::getSingleton()->getCamera(); - Vector3 camPos = cam->getPosition(); - Vector3 endPos = screenToSpace(getCursorPos()); + Camera *cam = Root::getSingleton()->getCamera(); + Vector3 camPos = cam->getPosition(); + Vector3 endPos = screenToSpace(getCursorPos()); - vector results; - Vector3 rayDir = (endPos - camPos).norm(); - Ray::retrieveCollisions(camPos, rayDir, Map::getSingleton()->getNodeParent(), results); - std::map unitData; + vector results; + Vector3 rayDir = (endPos - camPos).norm(); + Ray::retrieveCollisions(camPos, rayDir, Map::getSingleton()->getNodeParent(), results); + std::map unitData; - Ray::sortResults(results); + Ray::sortResults(results); - if(!results.empty()){ - vector ancestors = results[0].mesh->getNode()->getAncestors(); - Node *node = ancestors[ancestors.size() - 2]; - std::map::iterator it = unitData.find(node); - targets.push_back(Order::Target((it != unitData.end() ? unitData[node] : nullptr), results[0].pos)); - } + if(!results.empty()){ + vector ancestors = results[0].mesh->getNode()->getAncestors(); + Node *node = ancestors[ancestors.size() - 2]; + std::map::iterator it = unitData.find(node); + targets.push_back(Order::Target((it != unitData.end() ? unitData[node] : nullptr), results[0].pos)); + } } void ActiveGameState::onAction(int bind, bool isPressed) { @@ -334,9 +362,11 @@ namespace battleship{ break; case Bind::LEFT_CONTROL: controlPressed = isPressed; + if(isPressed) depth -= 0.05; break; case Bind::LEFT_SHIFT: shiftPressed = isPressed; + if(isPressed) depth += 0.05; break; case Bind::SELECT_PATROL_POINTS: selectingPatrolPoints = isPressed; diff --git a/activeGameState.h b/activeGameState.h index 39b4a65..25193da 100755 --- a/activeGameState.h +++ b/activeGameState.h @@ -25,28 +25,29 @@ namespace battleship{ inline std::vector& getSelectedUnits(){return selectedUnits;} inline std::vector& getUnitGroup(int i){return unitGroups[i];} private: - void deselectUnits(); + void deselectUnits(); void renderUnits(); void updateCameraPosition(); void updateSelectionBox(); void addTarget(); void issueOrder(Order::TYPE, bool); void lookAround(bool); - void orientCamera(vb01::Vector3, double); + void orientCamera(vb01::Vector3, double); + bool isInLineOfSight(vb01::Vector3, float, Unit*); GuiAppState *guiState; std::vector players; Player *mainPlayer; - vb01::Quad *dragbox = nullptr; - vb01::Node *dragboxNode = nullptr; - vb01::Vector2 clickPoint; + vb01::Quad *dragbox = nullptr; + vb01::Node *dragboxNode = nullptr, *textNode = nullptr; + vb01::Vector2 clickPoint; std::vector unitScreenPosVec; std::vector unitLightNodes; std::vector unitGroups[9], selectedUnits; - std::vector targets; + std::vector targets; bool isSelectionBox = false, shiftPressed = false, controlPressed = false, selectingPatrolPoints = false, selectingGuidedMissileTarget = false,lookingAround=false; - bool isInLineOfSight(vb01::Vector3, float, Unit*); int playerId, zooms = 0; + float depth = 1; }; } diff --git a/map.cpp b/map.cpp index a5bcc6a..bbfc20c 100755 --- a/map.cpp +++ b/map.cpp @@ -76,7 +76,6 @@ namespace battleship{ mat->addTexUniform("textures[0]", t, true); model->setMaterial(mat); - root->getRootNode()->attachChild(model); nodeParent->attachChild(model); terrainModel = model; } @@ -92,7 +91,8 @@ namespace battleship{ float sizeY = luaManager->getFloatFromTable(table, vector{Index("sizeY", true)}); Quad *quad = new Quad(Vector3(sizeX, sizeY, 1) * 1, true); Material *mat = new Material(root->getLibPath() + "texture"); - mat->addBoolUniform("texturingEnabled", true); + mat->addBoolUniform("texturingEnabled", false); + mat->addVec4Uniform("diffuseColor", Vector4(0, 0, 1, 0.5)); mat->addBoolUniform("lightingEnabled", false); string fr[]{GameManager::getSingleton()->getPath() + "Models/Maps/" + mapName + "/" + luaManager->getStringFromTable(table, vector{Index("albedoMap", true)})}; @@ -167,8 +167,11 @@ namespace battleship{ return (numCellsX * numCellsZ * y + (numCellsX * z + x)); } - bool Map::isPointWithin(int id, Vector3 point, Vector3 cellSize){ + bool Map::isPointWithin(int id, Vector3 point, Vector3 cellSize, bool cellSpatial){ Vector3 cellPos = getCellPos(id, cellSize); - return ((fabs(point.x - cellPos.x) < 0.5 * cellSize.x) && (fabs(point.z - cellPos.z) < 0.5 * cellSize.z)); + bool withinX = (fabs(point.x - cellPos.x) < 0.5 * cellSize.x); + bool withinY = (cellSpatial ? (fabs(point.y - cellPos.y) < 0.5 * cellSize.y) : true); + bool withinZ = (fabs(point.z - cellPos.z) < 0.5 * cellSize.z); + return withinX && withinY && withinZ; } } diff --git a/map.h b/map.h index 6f37ad6..4de3942 100755 --- a/map.h +++ b/map.h @@ -34,19 +34,21 @@ namespace battleship{ void unload(); vb01::Vector3 getCellPos(int, vb01::Vector3); int getCellId(vb01::Vector3, vb01::Vector3); - bool isPointWithin(int, vb01::Vector3, vb01::Vector3); + bool isPointWithin(int, vb01::Vector3, vb01::Vector3, bool = false); inline WaterBody getWaterBody(int i){return waterBodies[i];} inline int getNumWaterBodies(){return waterBodies.size();} inline vb01::Model* getTerrainModel(){return terrainModel;} inline vb01::Node* getNodeParent(){return nodeParent;} inline vb01::Vector3 getSize(){return size;} + inline float getBottom(){return bottom;} private: vb01::Node *nodeParent = nullptr; vb01::Model *terrainModel = nullptr; std::string mapName; std::vector waterBodies; //TODO replace hardcoded values - vb01::Vector3 size = vb01::Vector3(100, 33, 100); + vb01::Vector3 size = vb01::Vector3(100, 20, 100); + float bottom = 14; Map(){} void loadSkybox(gameBase::LuaManager*); diff --git a/unit.cpp b/unit.cpp index d9fafba..dccbf4b 100755 --- a/unit.cpp +++ b/unit.cpp @@ -151,26 +151,34 @@ namespace battleship{ } void Unit::move(Order order, float destOffset) { - Vector3 pointDir = (pathPoints[0] - pos).norm(); - float angle = dirVec.getAngleBetween(pointDir); + Vector3 linDest = Vector3(pathPoints[0].x, pos.y, pathPoints[0].z); + Vector3 destDir = (linDest - pos).norm(); + + float angle = dirVec.getAngleBetween(destDir); UnitDataManager *udm = UnitDataManager::getSingleton(); if(angle > udm->getAnglePrecision()[id]){ float rotSpeed = (maxTurnAngle > angle ? angle : maxTurnAngle); - if(leftVec.getAngleBetween(pointDir) > PI / 2) + if(leftVec.getAngleBetween(destDir) > PI / 2) rotSpeed *= -1; turn(rotSpeed); } else{ - if(pos.getDistanceFrom(pathPoints[0]) > destOffset){ + if(pos.getDistanceFrom(linDest) > destOffset){ float dist = pos.getDistanceFrom(pathPoints[0]); float movementAmmount = (speed > dist ? dist : speed); advance(movementAmmount); } else pathPoints.erase(pathPoints.begin()); + + if(fabs(pos.y - pathPoints[0].y) > destOffset){ + float dist = pos.getDistanceFrom(pathPoints[0]); + float movementAmmount = -(speed > dist ? dist : speed); + advance(movementAmmount, MoveDir::UP); + } } if(pathPoints.empty()) @@ -183,8 +191,22 @@ namespace battleship{ void Unit::launch(Order order) {} - void Unit::advance(float speed) { - pos = pos + dirVec * speed; + void Unit::advance(float speed, MoveDir moveDir) { + Vector3 dir; + + switch(moveDir){ + case MoveDir::FORW: + dir = dirVec; + break; + case MoveDir::LEFT: + dir = leftVec; + break; + case MoveDir::UP: + dir = upVec; + break; + } + + pos = pos + dir * speed; model->setPosition(pos); } @@ -293,16 +315,17 @@ namespace battleship{ int numVerts = 3 * meshData.numTris; for(int k = 0; k < numVerts; k++){ - bool pointWithin = map->isPointWithin(j, verts[k].pos, cellSize); + bool pointWithin = map->isPointWithin(j, verts[k].pos, cellSize, type == UnitType::UNDERWATER); WaterBody waterbody; if(waterbodyId != -1) waterbody = map->getWaterBody(waterbodyId); bool impassibleForSeaUnits = (type == UnitType::SEA_LEVEL && pointWithin && waterbodyId != -1 && verts[k].pos.y > waterbody.pos.y); + bool impassibleForSubs = (type == UnitType::UNDERWATER && pointWithin && waterbodyId != -1); bool impassibleForLandUnits = (type == UnitType::LAND && pointWithin && (waterbodyId != -1 && verts[k].pos.y < waterbody.pos.y)); - if(impassibleForSeaUnits || impassibleForLandUnits){ + if(impassibleForSeaUnits || impassibleForSubs || impassibleForLandUnits){ weight = impassibleNodeVal; break; } diff --git a/unit.h b/unit.h index c415acb..292d629 100755 --- a/unit.h +++ b/unit.h @@ -43,7 +43,7 @@ namespace battleship{ std::vector targets; }; - enum class MoveDir {FORWARD, LEFT, RIGHT}; + enum class MoveDir {LEFT, UP, FORW}; enum class Corner {FRONT_LEFT, FRONT_RIGHT, REAR_LEFT, REAR_RIGHT}; @@ -101,7 +101,6 @@ namespace battleship{ std::vector pathPoints; protected: Player *player; - MoveDir moveDir = MoveDir::FORWARD; UnitClass unitClass; UnitType type; vb01::Vector2 screenPos; @@ -121,7 +120,7 @@ namespace battleship{ virtual void patrol(Order); virtual void launch(Order); virtual void turn(float); - virtual void advance(float); + virtual void advance(float, MoveDir = MoveDir::FORW); void drawCuboid(); }; }