From a36648eabe81996b9b937731c112b3cb18bae94e Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 21 Jan 2024 15:42:17 +0200 Subject: [PATCH] launchable cruise missiles --- Assets/Scripts/Core/options.lua | 2 +- .../Projectiles/projectileData.lua | 10 +-- CMakeLists.txt | 4 +- activeGameState.cpp | 7 +++ cruiseMissile.cpp | 63 +++++++++++++++++++ cruiseMissile.h | 21 +++++++ gameObjectFactory.cpp | 12 ++-- projectile.cpp | 2 +- projectile.h | 3 +- unit.cpp | 10 +++ unit.h | 5 +- 11 files changed, 123 insertions(+), 16 deletions(-) create mode 100644 cruiseMissile.cpp create mode 100644 cruiseMissile.h diff --git a/Assets/Scripts/Core/options.lua b/Assets/Scripts/Core/options.lua index a6165ec..7dbb7b3 100644 --- a/Assets/Scripts/Core/options.lua +++ b/Assets/Scripts/Core/options.lua @@ -21,7 +21,7 @@ mappings = { {bind = 17, trigger = 341, action = true, bindType = 0, inOptions = true}, {bind = 18, trigger = 340, action = true, bindType = 0, inOptions = true}, {bind = 19, trigger = 80, action = true, bindType = 0, inOptions = true}, - {bind = 20, trigger = 67, 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 = 22, trigger = 48, action = true, bindType = 0, inOptions = true}, {bind = 23, trigger = 49, action = true, bindType = 0, inOptions = true}, diff --git a/Assets/Scripts/GameObjects/Projectiles/projectileData.lua b/Assets/Scripts/GameObjects/Projectiles/projectileData.lua index 3b73cde..40dde71 100644 --- a/Assets/Scripts/GameObjects/Projectiles/projectileData.lua +++ b/Assets/Scripts/GameObjects/Projectiles/projectileData.lua @@ -1,15 +1,15 @@ -ProjectileClass = {GUIDED_MISSILE = 0, TORPEDO = 1} +ProjectileClass = {CRUISE_MISSILE = 0, TORPEDO = 1} projectiles = { projectileClass = { - ProjectileClass.GUIDED_MISSILE, + ProjectileClass.CRUISE_MISSILE, ProjectileClass.TORPEDO, }, - speed = {.5, .06}, + speed = {.1, .06}, rayLength = {1.25, .75}, damage = {450, 300}, name = { - "Guided missile", + "Cruise missile", "Torpedo" }, basePath = { @@ -17,7 +17,7 @@ projectiles = { PATH .. "Models/GameObjects/Projectiles/Torpedos/", }, meshPath = { - "guidedMissile.xml", + "cruiseMissile.xml", "torpedo.xml", }, unitCornerPoints = { diff --git a/CMakeLists.txt b/CMakeLists.txt index bbee4c0..02cc85c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,9 +21,9 @@ set(UTIL util.cpp binds.h) set(CONTROLLERS gameObjectFrameController.cpp cameraController.cpp) set(CONSOLE console.cpp abstractCommand.cpp addUnitCommand.cpp addResourceCommand.cpp) set(CORE gameManager.cpp game.cpp defConfigs.cpp ${CONSOLE} ${CONTROLLERS}) -set(PROJECTILES projectile.cpp) +set(PROJECTILES projectile.cpp cruiseMissile.cpp) set(FX fx.cpp) -set(VEHICLES vehicle.cpp engineer.cpp transport.cpp submarine.cpp) +set(VEHICLES vehicle.cpp engineer.cpp transport.cpp) set(STRUCTURES structure.cpp factory.cpp pointDefense.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}) diff --git a/activeGameState.cpp b/activeGameState.cpp index a54afec..ceb057e 100755 --- a/activeGameState.cpp +++ b/activeGameState.cpp @@ -407,6 +407,13 @@ namespace battleship{ break; case Bind::EJECT_GARRISON: if(isPressed) issueOrder(Order::TYPE::EJECT, vector{}, shiftPressed); + break; + case Bind::LAUNCH: + if(isPressed){ + castRayToTerrain(); + issueOrder(Order::TYPE::LAUNCH, targets, shiftPressed); + } + break; case Bind::TOGGLE_SUB: break; diff --git a/cruiseMissile.cpp b/cruiseMissile.cpp new file mode 100644 index 0000000..e52dff8 --- /dev/null +++ b/cruiseMissile.cpp @@ -0,0 +1,63 @@ +#include "cruiseMissile.h" +#include "unit.h" + +#include + +#include + +namespace battleship{ + using namespace std; + using namespace vb01; + + CruiseMissile::CruiseMissile(Unit *unit, Vector3 tp, Vector3 pos, Quaternion rot) : Projectile(unit, 0, pos, rot), targetPoint(tp), flightStage(FlightStage::ASCENT){ + Vector3 unitDir = unit->getDirVec(); + Vector3 leftDir = unit->getLeftVec(); + Vector3 targDir = (Vector3(targetPoint.x, pos.y, targetPoint.z) - pos).norm(); + + bool left = (leftDir.getAngleBetween(targDir) < PI / 2); + float angle = unitDir.getAngleBetween(targDir) * (left ? 1 : -1); + orientAt(Quaternion(angle, Vector3::VEC_J) * rot); + } + + void CruiseMissile::pitch(float rotAngle){ + Vector3 horDir = Vector3(dirVec.x, 0, dirVec.z).norm(); + float angleToVertDir = dirVec.getAngleBetween(horDir); + Quaternion rotQuat = Quaternion(angleToVertDir > rotAngle ? rotAngle : angleToVertDir, leftVec) * rot; + float minHeight = 20; + + if(flightStage == FlightStage::ASCENT && pos.y - initPos.y > minHeight){ + orientAt(rotQuat); + + if(dirVec.y <= 0) + flightStage = FlightStage::CRUISE; + } + else if(flightStage == FlightStage::DESCENT && dirVec.getAngleBetween((Vector3(targetPoint.x, initPos.y, targetPoint.z) - initPos).norm()) < PI / 2) + orientAt(rotQuat); + } + + void CruiseMissile::cruise(){ + float minDist = 3; + + if(pos.getDistanceFrom(Vector3(targetPoint.x, pos.y, targetPoint.z)) < minDist) + flightStage = FlightStage::DESCENT; + } + + void CruiseMissile::update(){ + GameObject::update(); + placeAt(pos + dirVec * speed); + + float rotAngle = .1, eps = .0001; + + switch(flightStage){ + case FlightStage::ASCENT: + case FlightStage::DESCENT: + pitch(rotAngle); + break; + case FlightStage::CRUISE: + cruise(); + break; + } + + //if(!exploded && flightStage == FlightStage::DESCENT) checkForCollision(); + } +} diff --git a/cruiseMissile.h b/cruiseMissile.h new file mode 100644 index 0000000..a312235 --- /dev/null +++ b/cruiseMissile.h @@ -0,0 +1,21 @@ +#ifndef CRUISE_MISSILE_H +#define CRUISE_MISSILE_H + +#include "projectile.h" + +namespace battleship{ + class CruiseMissile : public Projectile{ + public: + CruiseMissile(Unit*, vb01::Vector3, vb01::Vector3, vb01::Quaternion); + void update(); + private: + enum class FlightStage{ASCENT, CRUISE, DESCENT}; + FlightStage flightStage; + vb01::Vector3 targetPoint; + + void pitch(float); + void cruise(); + }; +} + +#endif diff --git a/gameObjectFactory.cpp b/gameObjectFactory.cpp index 2834e80..6c8f7d6 100644 --- a/gameObjectFactory.cpp +++ b/gameObjectFactory.cpp @@ -3,11 +3,10 @@ #include "factory.h" #include "engineer.h" #include "transport.h" -#include "submarine.h" #include "projectile.h" #include "resourceDeposit.h" #include "pointDefense.h" -#include "projectile.h" +#include "cruiseMissile.h" #include "defConfigs.h" #include @@ -28,9 +27,6 @@ namespace battleship{ return new Engineer(player, id, pos, rot); case UnitClass::TRANSPORT: return new Transport(player, id, pos, rot); - case UnitClass::MISSILE_SUBMARINE: - case UnitClass::STEALTH_SUBMARINE: - return new Submarine(player, id, pos, rot); case UnitClass::LAND_FACTORY: case UnitClass::NAVAL_FACTORY: return new Factory(player, id, pos, rot, buildStatus); @@ -49,6 +45,12 @@ namespace battleship{ int projectileClass = SOL_LUA_STATE["projectiles"]["projectileClass"][id + 1]; switch((ProjectileClass)projectileClass){ + case ProjectileClass::CRUISE_MISSILE: + { + Order::Target target = unit->getOrder(0).targets[0]; + Vector3 targetPos = (target.unit ? target.unit->getPos() : target.pos); + return new CruiseMissile(unit, targetPos, pos, rot); + } default: return new Projectile(unit, id, pos, rot); } diff --git a/projectile.cpp b/projectile.cpp index bfacb2c..2aad455 100755 --- a/projectile.cpp +++ b/projectile.cpp @@ -24,7 +24,7 @@ namespace battleship{ using namespace configData; using namespace gameBase; - Projectile::Projectile(Unit *un, int id, Vector3 pos, Quaternion rot) : GameObject(GameObject::Type::PROJECTILE, id, un->getPlayer(), pos, rot), unit(un){ + Projectile::Projectile(Unit *un, int id, Vector3 pos, Quaternion rot) : GameObject(GameObject::Type::PROJECTILE, id, un->getPlayer(), pos, rot), unit(un), initPos(pos){ initProperties(); initModel(); initSound(); diff --git a/projectile.h b/projectile.h index 7ec3970..d7f9727 100755 --- a/projectile.h +++ b/projectile.h @@ -12,7 +12,7 @@ namespace vb01{ } namespace battleship { - enum class ProjectileClass{GUIDED_MISSILE, TORPEDO}; + enum class ProjectileClass{CRUISE_MISSILE, TORPEDO}; class Unit; class Projectile : public GameObject{ @@ -32,6 +32,7 @@ namespace battleship { bool exploded = false; float speed, rayLength; int damage; + vb01::Vector3 initPos; Unit *unit = nullptr; sf::SoundBuffer *shotSfxBuffer, *explosionSfxBuffer; sf::Sound *shotSfx = nullptr, *explosionSfx = nullptr; diff --git a/unit.cpp b/unit.cpp index 9f006bf..f212a50 100755 --- a/unit.cpp +++ b/unit.cpp @@ -218,6 +218,16 @@ namespace battleship{ delete node; } + void Unit::launch(Order order){ + for(Weapon *weapon : weapons) + if(weapon->getProjectileId() == 0){ + weapon->fire(order); + break; + } + + removeOrder(0); + } + float Unit::calculateRotation(Vector3 dir, float angle, float maxTurnAngle){ float rotSpeed = (maxTurnAngle > angle ? angle : maxTurnAngle); diff --git a/unit.h b/unit.h index 95be756..7bf3588 100755 --- a/unit.h +++ b/unit.h @@ -67,6 +67,7 @@ namespace battleship{ Weapon(Unit*, sol::table); ~Weapon(); virtual void fire(Order); + inline int getProjectileId(){return projId;} inline int getRateOfFire(){return rateOfFire;} inline int getDamage(){return damage;} inline int getMinRange(){return minRange;} @@ -125,6 +126,8 @@ namespace battleship{ inline int getDeathHp(){return DEATH_HP;} inline bool isVehicle(){return gameBase::generateView()["units"]["isVehicle"][id + 1];} inline bool isTargetToTheRight(vb01::Vector3 dir, vb01::Vector3 lv){return lv.getAngleBetween(dir) > vb01::PI / 2;} + inline Order getOrder(int i){return orders[i];} + inline int getNumOrders(){return orders.size();} private: void renderOrderLine(bool); void updateScreenCoordinates(); @@ -161,7 +164,7 @@ namespace battleship{ virtual void build(Order){} virtual void move(Order){} virtual void patrol(Order){} - virtual void launch(Order){} + virtual void launch(Order); float calculateRotation(vb01::Vector3, float, float); void removeBar(vb01::Node*); vb01::Node* createBar(vb01::Vector2, vb01::Vector2, vb01::Vector4);