From 0770d64784ccb1e74d5a4b9a99f5415cce0b5713 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Tue, 20 Jan 2026 20:26:30 +0200 Subject: [PATCH] removing projectiles if they go out of map bounds --- CMakeLists.txt | 2 +- cruiseMissile.cpp | 7 +++++-- depthCharge.cpp | 6 +++++- gameObjectFactory.cpp | 3 +++ missile.cpp | 7 ++++++- projectile.cpp | 25 +++++++++++++++---------- projectile.h | 3 +-- shell.cpp | 7 ++++++- torpedo.cpp | 17 +++++++++++++++++ torpedo.h | 15 +++++++++++++++ 10 files changed, 74 insertions(+), 18 deletions(-) create mode 100644 torpedo.cpp create mode 100644 torpedo.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 21c0338..554c9c1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,7 +32,7 @@ set(CONTROLLERS gameObjectFrameController.cpp cameraController.cpp) set(CONSOLE console.cpp abstractCommand.cpp addUnitCommand.cpp addResourceCommand.cpp addTechnologyCommand.cpp toggleDebugCommand.cpp) set(CORE gameManager.cpp game.cpp pathfinder.cpp environment.cpp fxManager.cpp defConfigs.cpp player.cpp trader.cpp ${CONSOLE} ${CONTROLLERS}) -set(PROJECTILES projectile.cpp missile.cpp cruiseMissile.cpp shell.cpp depthCharge.cpp) +set(PROJECTILES projectile.cpp missile.cpp cruiseMissile.cpp shell.cpp depthCharge.cpp torpedo.cpp torpedo.h) set(VEHICLES vehicle.cpp submarine.cpp engineer.cpp resourceRover.cpp freezer.cpp) set(STRUCTURES structure.cpp factory.cpp pointDefense.cpp extractor.cpp researchStruct.cpp iceSheet.cpp) set(WEAPONS weapon.cpp) diff --git a/cruiseMissile.cpp b/cruiseMissile.cpp index 04884ac..c43998e 100644 --- a/cruiseMissile.cpp +++ b/cruiseMissile.cpp @@ -72,7 +72,10 @@ namespace battleship{ break; } - if(flightStage == FlightStage::DESCENT) - Projectile::checkCollision(); + if(flightStage == FlightStage::DESCENT && !remove){ + checkSurfaceCollision(false); + if(remove) return; + checkUnitCollision(); + } } } diff --git a/depthCharge.cpp b/depthCharge.cpp index d79a650..083e7be 100644 --- a/depthCharge.cpp +++ b/depthCharge.cpp @@ -8,6 +8,10 @@ namespace battleship{ void DepthCharge::update(){ Projectile::update(); - if(!remove) checkUnitCollision(); + if(!remove){ + checkSurfaceCollision(true); + if(remove) return; + checkUnitCollision(); + } } } diff --git a/gameObjectFactory.cpp b/gameObjectFactory.cpp index 226a043..5a4873d 100644 --- a/gameObjectFactory.cpp +++ b/gameObjectFactory.cpp @@ -14,6 +14,7 @@ #include "cruiseMissile.h" #include "iceSheet.h" #include "shell.h" +#include "torpedo.h" #include "depthCharge.h" #include "defConfigs.h" @@ -77,6 +78,8 @@ namespace battleship{ return new CruiseMissile(unit, id, targetPos, pos, rot); case ProjectileClass::MISSILE: return new Missile(unit, id, targetPos, pos, rot); + case ProjectileClass::TORPEDO: + return new Torpedo(unit, id, pos, rot); case ProjectileClass::DEPTH_CHARGE: return new DepthCharge(unit, id, pos, rot); default: diff --git a/missile.cpp b/missile.cpp index 434ec14..5fd6e49 100644 --- a/missile.cpp +++ b/missile.cpp @@ -22,6 +22,11 @@ namespace battleship{ Projectile::update(); destructable->update(); - checkCollision(); + + if(!remove){ + checkSurfaceCollision(false); + if(remove) return; + checkUnitCollision(); + } } } diff --git a/projectile.cpp b/projectile.cpp index 8835615..d76085e 100755 --- a/projectile.cpp +++ b/projectile.cpp @@ -105,20 +105,25 @@ namespace battleship{ detonate(targetUnits[i]); } - void Projectile::checkSurfaceCollision(){ + //TODO fix map boundary checks + void Projectile::checkSurfaceCollision(bool useBottomCell){ Map *map = Map::getSingleton(); + vector &cells = map->getCells(); int cellId = map->getCellId(pos, false); + Vector3 mapSize = map->getMapSize(); - if((pos + dirVec * rayLength).y <= map->getCells()[cellId].pos.y) - detonate(); - } - - void Projectile::checkCollision(){ - if(!remove){ - checkSurfaceCollision(); - if(remove) return; - checkUnitCollision(); + if(cellId < 0 || fabs(pos.x) > .5 * mapSize.x || fabs(pos.z) > .5 * mapSize.z){ + remove = true; + return; } + + if(useBottomCell && !cells[cellId].underWaterCellIds.empty()){ + int numUnderwaterCells = cells[cellId].underWaterCellIds.size(); + cellId = cells[cellId].underWaterCellIds[numUnderwaterCells - 1]; + } + + if((pos + dirVec * rayLength).y <= cells[cellId].pos.y + .5 * map->getCellSize().y) + detonate(); } void Projectile::debug(){ diff --git a/projectile.h b/projectile.h index 59d0309..bb81c9a 100755 --- a/projectile.h +++ b/projectile.h @@ -28,8 +28,7 @@ namespace battleship { protected: virtual void reinit(); virtual void checkUnitCollision(); - virtual void checkSurfaceCollision(); - void checkCollision(); + virtual void checkSurfaceCollision(bool); vb01::Vector3 initPos; ProjectileClass projClass; diff --git a/shell.cpp b/shell.cpp index a7febf7..72e3157 100644 --- a/shell.cpp +++ b/shell.cpp @@ -32,6 +32,11 @@ namespace battleship{ model->lookAt(dirVec, upVec); Projectile::update(); - checkCollision(); + + if(!remove){ + checkSurfaceCollision(false); + if(remove) return; + checkUnitCollision(); + } } } diff --git a/torpedo.cpp b/torpedo.cpp new file mode 100644 index 0000000..61aa8cf --- /dev/null +++ b/torpedo.cpp @@ -0,0 +1,17 @@ +#include "torpedo.h" + +namespace battleship{ + using namespace vb01; + + Torpedo::Torpedo(Unit *un, int id, Vector3 pos, Quaternion rot) : Projectile(un, id, pos, rot){} + + void Torpedo::update(){ + Projectile::update(); + + if(!remove){ + checkSurfaceCollision(true); + if(remove) return; + checkUnitCollision(); + } + } +} diff --git a/torpedo.h b/torpedo.h new file mode 100644 index 0000000..2e8dffe --- /dev/null +++ b/torpedo.h @@ -0,0 +1,15 @@ +#ifndef TORPEDO_H +#define TORPEDO_H + +#include "projectile.h" + +namespace battleship{ + class Torpedo : public Projectile{ + public: + Torpedo(Unit*, int, vb01::Vector3, vb01::Quaternion); + void update(); + private: + }; +} + +#endif