corrected unit garrisoning and ejecting

This commit is contained in:
devZoGok
2024-01-14 10:53:08 +02:00
parent 2776486a17
commit 55d9ba0acc
3 changed files with 28 additions and 8 deletions
+19 -2
View File
@@ -302,9 +302,26 @@ namespace battleship{
}
void Unit::eject(Order order){
Map *map = Map::getSingleton();
int currCellId = map->getCellId(pos);
vector<Map::Cell> &cells = map->getCells();
int adjWaterCellId = -1, adjLandCellId = -1;
for(Map::Edge edge : cells[currCellId].edges){
if(adjLandCellId == -1 && cells[edge.destCellId].type == Map::Cell::LAND)
adjLandCellId = edge.destCellId;
if(adjWaterCellId == -1 && cells[edge.destCellId].type == Map::Cell::WATER)
adjWaterCellId = edge.destCellId;
}
if(garrisonSlots.size() > 0){
for(Order::Target targ : order.targets)
((Vehicle*)targ.unit)->exitGarrisonable();
for(Order::Target targ : order.targets){
bool exitToLandCell = (targ.unit->getType() == UnitType::LAND && adjLandCellId != -1);
bool exitToWaterCell = ((targ.unit->getType() == UnitType::SEA_LEVEL || targ.unit->getType() == UnitType::UNDERWATER) && adjWaterCellId != -1);
if(exitToLandCell || exitToWaterCell)
((Vehicle*)targ.unit)->exitGarrisonable(cells[exitToLandCell ? adjLandCellId : adjWaterCellId].pos);
}
removeOrder(0);
}
+8 -5
View File
@@ -165,8 +165,8 @@ namespace battleship{
removeOrder(0);
}
void Vehicle::exitGarrisonable(){
placeAt(garrisonable->getPos());
void Vehicle::exitGarrisonable(Vector3 exitPos){
placeAt(exitPos);
garrisonable->updateGarrison(this, false);
garrisonable = nullptr;
}
@@ -196,7 +196,7 @@ namespace battleship{
void Vehicle::garrison(Order order){
Unit *targUnit = order.targets[0].unit;
float distToGarrisonable = pos.getDistanceFrom(targUnit->getPos()), garrisonDist = .5 * Map::getSingleton()->getCellSize().x;
float distToGarrisonable = pos.getDistanceFrom(targUnit->getPos()), garrisonDist = Map::getSingleton()->getCellSize().x;
if(distToGarrisonable > garrisonDist)
navigateToTarget(garrisonDist);
@@ -244,13 +244,16 @@ namespace battleship{
int dest = map->getCellId(destPos);
vector<int> path = pathfinder->findPath(cells, source, dest, (int)type);
for(int i = 0; i < path.size(); i++)
if((ship && cells[path[i]].type != Map::Cell::WATER) || (type == UnitType::LAND && cells[path[i]].type != Map::Cell::LAND)){
for(int i = 0; i < path.size(); i++){
if((ship && cells[path[i]].type != Map::Cell::WATER) || (order.type != Order::TYPE::GARRISON && type == UnitType::LAND && cells[path[i]].type != Map::Cell::LAND)){
path = vector<int>(path.begin(), path.begin() + i);
order.targets[0].unit = nullptr;
order.targets[0].pos = cells[path[i - 1]].pos;
break;
}
else if(order.type == Order::TYPE::GARRISON && type == UnitType::LAND && cells[path[i]].type != Map::Cell::LAND && path.size() - 1 != i)
return;
}
for(int p : path)
addPathpoint(cells[p].pos);
+1 -1
View File
@@ -14,7 +14,7 @@ namespace battleship{
~Vehicle();
virtual void update();
void move(Order);
void exitGarrisonable();
void exitGarrisonable(vb01::Vector3);
inline Unit* getGarrisonable(){return garrisonable;}
inline int getGarrisonCategory(){return garrisonCategory;}
private: