Files
Tactica/Source/Tactica/Private/GridState.cpp
T

74 lines
1.7 KiB
C++

#include "GridState.h"
#include "GridMath.h"
void FGridState::Init(int32 InSizeX, int32 InSizeY)
{
SizeX = FMath::Max(InSizeX, 0);
SizeY = FMath::Max(InSizeY, 0);
Cells.Reset();
Cells.SetNum(SizeX * SizeY);
}
bool FGridState::IsInBounds(FIntPoint Coord) const
{
return FGridMath::IsInBounds(Coord, SizeX, SizeY);
}
const FGridCellData* FGridState::GetCell(FIntPoint Coord) const
{
const int32 Index = FGridMath::CoordToIndex(Coord, SizeX, SizeY);
return Cells.IsValidIndex(Index) ? &Cells[Index] : nullptr;
}
FGridCellData* FGridState::GetCellMutable(FIntPoint Coord)
{
const int32 Index = FGridMath::CoordToIndex(Coord, SizeX, SizeY);
return Cells.IsValidIndex(Index) ? &Cells[Index] : nullptr;
}
void FGridState::SetWalkable(FIntPoint Coord, bool bWalkable)
{
if (FGridCellData* Cell = GetCellMutable(Coord))
{
Cell->bWalkable = bWalkable;
}
}
int32 FGridState::GetOccupant(FIntPoint Coord) const
{
const FGridCellData* Cell = GetCell(Coord);
return Cell ? Cell->OccupantId : INDEX_NONE;
}
bool FGridState::PlaceUnit(int32 UnitId, FIntPoint Coord)
{
FGridCellData* Cell = GetCellMutable(Coord);
if (!Cell || !Cell->bWalkable || Cell->OccupantId != INDEX_NONE)
{
return false;
}
Cell->OccupantId = UnitId;
return true;
}
bool FGridState::MoveUnit(int32 UnitId, FIntPoint From, FIntPoint To)
{
FGridCellData* FromCell = GetCellMutable(From);
FGridCellData* ToCell = GetCellMutable(To);
if (!FromCell || !ToCell || FromCell->OccupantId != UnitId)
{
return false;
}
if (From == To)
{
return true;
}
if (!ToCell->bWalkable || ToCell->OccupantId != INDEX_NONE)
{
return false;
}
FromCell->OccupantId = INDEX_NONE;
ToCell->OccupantId = UnitId;
return true;
}