Fixed vision blocking; MapObject::Occupy() now blueprint implementable

This commit is contained in:
Maximilian Fajnberg 2022-06-20 15:45:30 +02:00
parent 068beeacfd
commit 276f8e2b50
7 changed files with 25 additions and 24 deletions

View File

@ -8,7 +8,7 @@
#include "Kismet/GameplayStatics.h"
#include "Algo/Reverse.h"
#include "Util/IndexPriorityQueue.h"
#include <cmath.h>
#include <tgmath.h>
// Sets default values
AAdventureMap::AAdventureMap()
@ -106,13 +106,10 @@ FHexVector AAdventureMap::AxialRound(float qf, float rf)
return FHexVector(q, r);
}
float AAdventureMap::Lerp(int32 a, int32 b, int32 t)
float AAdventureMap::Lerp(int32 a, int32 b, float t)
{
return float(a + (b - a) * t);
}
// FHexVector SampleHex = MapRef->AxialRound(Lerpl(a, b, t), Lerp(x, y, z));
//
TArray<AHexTile*> AAdventureMap::Neighbors(AHexTile* OfHex, bool bFreeOnly = false)
{

View File

@ -81,9 +81,9 @@ public:
bool DiagIsReachable(AHexTile* InStart, FHexVector InDiagUnitVec);
UFUNCTION(BlueprintCallable, Category = "Utility")
float Lerp(int32 a, int32 b, int32 t);
float Lerp(int32 a, int32 b, float t);
UFUNCTION(BlueprintCallable, Category = "Utility")
FHexVector AxialRound(float q, float r)
FHexVector AxialRound(float q, float r);
UFUNCTION(BlueprintCallable, Category = "Utility")
TArray<AHexTile*> Neighbors(AHexTile* OfHex, bool bFreeOnly);

View File

@ -55,15 +55,14 @@ void AAdventurePlayerController::LeftClick()
else { return; }
}
TSet<AHexTile*> AAdventurePlayerController::Vision(int32 Radius)
TArray<AHexTile*> AAdventurePlayerController::Vision(int32 Radius)
{
TSet<AHexTile*> InRange = MapRef->BreadthFirstSearch(CurrentHex, Radius);
TSet<AHexTile*> Results;
TArray<AHexTile*> Results;
for (auto& Hex : InRange) {
if (Hex->Distance(CurrentHex) == Radius)
{
AHexTile* Target;
for (int32 i = 1; i <= Radius; i++)
{
float t = 1.0f / Radius * i;
@ -73,9 +72,11 @@ TSet<AHexTile*> AAdventurePlayerController::Vision(int32 Radius)
AHexTile* SampleHex = MapRef->Grid[MapRef->GridIndex(Sample.Q, Sample.R)];
Results.Add(SampleHex);
ExploredHexes.Add(Hex);
if (!SampleHex->bFree || ExploredHexes.Contains(SampleHex))
if (!ExploredHexes.Contains(SampleHex))
{ ExploredHexes.Add(Hex); }
if (!SampleHex->bFree)
{ break; }
}
}
@ -131,13 +132,15 @@ void AAdventurePlayerController::FitOnGrid(AMapObject* MapObject)
void AAdventurePlayerController::PlaceObject(TSubclassOf<AMapObject> MapObjClass, AHexTile* HoveredTile)
{
AMapObject* SpawnedObj = World->SpawnActor<AMapObject>(MapObjClass, FTransform(HoveredTile->GetActorTransform().GetLocation()));
SpawnedObj->MapRef = MapRef;
SpawnedObj->Origin = HoveredTile;
SpawnedObj->bPlaced = true;
HoveredTile->bFree = false;
HoveredTile->MapObject = SpawnedObj;
SpawnedObj->Occupy();
MapRef->MapObjects.Add(HoveredTile, SpawnedObj);
MapRef->IncID++;
SpawnedObj->ID = MapRef->IncID;
MapRef->MapObjectsByID.Add(MapRef->IncID, SpawnedObj);
SpawnedObj->Occupy();
}

View File

@ -57,7 +57,7 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
int32 VisionRadius = 6;
UFUNCTION(BlueprintCallable)
TSet<AHexTile*> Vision(int32 Radius);
TArray<AHexTile*> Vision(int32 Radius);
UPROPERTY(EditAnywhere, BlueprintReadWrite)
TSet<AHexTile*> ExploredHexes;

View File

@ -62,7 +62,7 @@ public:
// MapObject Placement
UPROPERTY(BlueprintReadWrite, VisibleAnywhere)
bool bFree = true;
UPROPERTY(BlueprintReadOnly, VisibleInstanceOnly)
UPROPERTY(BlueprintReadWrite, VisibleAnywhere)
AMapObject* MapObject;
UPROPERTY(BlueprintReadWrite, EditDefaultsOnly)
bool bCanActivate = false;

View File

@ -21,7 +21,6 @@ AMapObject::AMapObject()
void AMapObject::BeginPlay()
{
Super::BeginPlay();
Occupy();
}
// Called every frame
@ -39,11 +38,10 @@ void AMapObject::Activate()
}
// Any subclass of MapObject has a defined array of Vectors relative to its origin which has to occupy() upon being placed on the map.
void AMapObject::Occupy()
{
for (auto& V : Occupying) {
AHexTile* OccupiedHex = MapRef->Grid[MapRef->GridIndex(Origin->Q + V.Q, Origin->R + V.R)];
OccupiedHex->bFree = false;
OccupiedHex->MapObject = this;
}
}
//void AMapObject::Occupy(int32 Q, int32 R)
//{
// AHexTile* OccupiedHex = MapRef->Grid[MapRef->GridIndex(Q, R)];
//
// OccupiedHex->bFree = false;
// OccupiedHex->MapObject = this;
//}

View File

@ -35,6 +35,8 @@ public:
int32 ID;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Generation")
class AHexTile* Origin; // very important
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Generation")
bool bPlaced;
UPROPERTY(BlueprintReadWrite, VisibleAnywhere, Category = "Generation")
TArray<FHexVector> Occupying;
@ -54,6 +56,7 @@ public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION(BlueprintImplementableEvent)
void Occupy();
};