2022-01-11 11:07:17 +01:00
|
|
|
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
|
|
|
|
|
|
#include "AdventureMap.h"
|
|
|
|
#include "HexTile.h"
|
|
|
|
#include "AdventureCameraPawn.h"
|
|
|
|
#include "AdventureCharacter.h"
|
|
|
|
#include "Kismet/GameplayStatics.h"
|
|
|
|
|
|
|
|
|
|
|
|
// Sets default values
|
|
|
|
AAdventureMap::AAdventureMap()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called when the game starts or when spawned
|
|
|
|
void AAdventureMap::BeginPlay()
|
|
|
|
{
|
|
|
|
Super::BeginPlay();
|
|
|
|
UWorld* World = GetWorld();
|
|
|
|
if (IsValid(BaseTileClass))
|
|
|
|
{
|
|
|
|
MakeGrid();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called once on Begin Play
|
|
|
|
void AAdventureMap::MakeGrid()
|
|
|
|
{
|
|
|
|
UWorld* World = GetWorld();
|
|
|
|
FVector NextHexAt = FVector();
|
|
|
|
float HexWidth = sqrt(3) * TileSize;
|
|
|
|
int QOffset = 0;
|
|
|
|
|
|
|
|
for (int r = 1; r <= GridSize; r++)
|
|
|
|
{
|
|
|
|
float XOffset = 0.f;
|
|
|
|
|
|
|
|
if (r % 2 != 0)
|
|
|
|
{
|
|
|
|
if (r > 1)
|
|
|
|
{
|
|
|
|
QOffset--; // The Q axis is (i.e. columns are) oriented diagonally.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
XOffset = HexWidth / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int q = 1; q <= GridSize; q++)
|
|
|
|
{
|
|
|
|
NextHexAt.X = XOffset + (HexWidth * q);
|
|
|
|
NextHexAt.Y = TileSize * 1.5f * r;
|
|
|
|
NextHexAt.Z = 0.f;
|
|
|
|
|
|
|
|
FTransform SpawnTransform = FTransform(NextHexAt);
|
|
|
|
AHexTile* Tile = World->SpawnActor<AHexTile>(BaseTileClass, SpawnTransform);
|
|
|
|
|
|
|
|
Tile->Q = q - 1 + QOffset;
|
|
|
|
Tile->R = r - 1;
|
|
|
|
|
|
|
|
Grid.Add(Tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& tile : Grid)
|
|
|
|
{
|
|
|
|
tile->Index = GridIndex(tile->Q, tile->R);
|
|
|
|
}
|
2022-01-11 18:16:26 +01:00
|
|
|
|
|
|
|
bHexGridReady = true;
|
2022-01-11 11:07:17 +01:00
|
|
|
}
|
|
|
|
|
2022-01-12 18:22:16 +01:00
|
|
|
// Every Hex Tile's index within the Grid Array can be derived from its Q and R coordinates
|
|
|
|
int32 AAdventureMap::GridIndex(int32 qAxial, int32 rAxial)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The Q axis is (i.e. columns are) oriented diagonally.
|
|
|
|
* The Hex Grid has a rough square shape, hence the Q coordinates must be offset by -1 every other row.
|
|
|
|
*/
|
|
|
|
int32 column = qAxial + FMath::FloorToInt(rAxial / 2);
|
|
|
|
return (rAxial * GridSize) + column;
|
|
|
|
}
|
|
|
|
|
2022-01-11 11:07:17 +01:00
|
|
|
AHexTile* AAdventureMap::RandomHex()
|
|
|
|
{
|
2022-01-11 18:16:26 +01:00
|
|
|
int32 RandHex = GridIndex(FMath::RandRange(0, GridSize), FMath::RandRange(0, GridSize));
|
2022-01-12 18:22:16 +01:00
|
|
|
while (RandHex > Grid.Num())
|
|
|
|
{
|
|
|
|
RandHex = GridIndex(FMath::RandRange(0, GridSize), FMath::RandRange(0, GridSize));
|
|
|
|
}
|
2022-01-11 18:16:26 +01:00
|
|
|
return Grid[RandHex];
|
2022-01-12 18:22:16 +01:00
|
|
|
}
|
2022-01-13 16:34:06 +01:00
|
|
|
|
|
|
|
TArray<AHexTile*> AAdventureMap::Neighbors(AHexTile* OfHex)
|
|
|
|
{
|
|
|
|
TArray<AHexTile*> Neighbors;
|
|
|
|
int32 Index;
|
|
|
|
|
|
|
|
Index = GridIndex(OfHex->Q + 1 , OfHex->R + 0 );
|
2022-01-13 16:54:37 +01:00
|
|
|
if ( Index >= 0&& Index < Grid.Num() )
|
|
|
|
{
|
|
|
|
if (OfHex->Distance(Grid[Index]) == 1) { Neighbors.Add(Grid[Index]); }
|
|
|
|
}
|
2022-01-13 16:34:06 +01:00
|
|
|
|
|
|
|
Index = GridIndex(OfHex->Q + 1 , OfHex->R - 1 );
|
2022-01-13 16:54:37 +01:00
|
|
|
if (Index >= 0 && Index < Grid.Num())
|
|
|
|
{
|
|
|
|
if (OfHex->Distance(Grid[Index]) == 1) { Neighbors.Add(Grid[Index]); }
|
|
|
|
}
|
2022-01-13 16:34:06 +01:00
|
|
|
|
|
|
|
Index = GridIndex(OfHex->Q + 0 , OfHex->R - 1 );
|
2022-01-13 16:54:37 +01:00
|
|
|
if (Index >= 0 && Index < Grid.Num())
|
|
|
|
{
|
|
|
|
if (OfHex->Distance(Grid[Index]) == 1) { Neighbors.Add(Grid[Index]); }
|
|
|
|
}
|
2022-01-13 16:34:06 +01:00
|
|
|
|
|
|
|
Index = GridIndex(OfHex->Q - 1 , OfHex->R + 0 );
|
2022-01-13 16:54:37 +01:00
|
|
|
if (Index >= 0 && Index < Grid.Num())
|
|
|
|
{
|
|
|
|
if (OfHex->Distance(Grid[Index]) == 1) { Neighbors.Add(Grid[Index]); }
|
|
|
|
}
|
2022-01-13 16:34:06 +01:00
|
|
|
|
|
|
|
Index = GridIndex(OfHex->Q - 1 , OfHex->R + 1 );
|
2022-01-13 16:54:37 +01:00
|
|
|
if (Index >= 0 && Index < Grid.Num())
|
|
|
|
{
|
|
|
|
if (OfHex->Distance(Grid[Index]) == 1) { Neighbors.Add(Grid[Index]); }
|
|
|
|
}
|
2022-01-13 16:34:06 +01:00
|
|
|
|
|
|
|
Index = GridIndex(OfHex->Q + 0 , OfHex->R + 1 );
|
2022-01-13 16:54:37 +01:00
|
|
|
if (Index >= 0 && Index < Grid.Num())
|
|
|
|
{
|
|
|
|
if (OfHex->Distance(Grid[Index]) == 1) { Neighbors.Add(Grid[Index]); }
|
|
|
|
}
|
2022-01-13 16:34:06 +01:00
|
|
|
|
2022-01-13 16:54:37 +01:00
|
|
|
return Neighbors;
|
2022-01-13 16:34:06 +01:00
|
|
|
}
|