Base functionality for MapObject placement

This commit is contained in:
2022-01-24 21:26:06 +01:00
parent 74eab48a6e
commit 237c056b30
8 changed files with 93 additions and 79 deletions
+42 -10
View File
@@ -6,18 +6,27 @@
#include "HexTile.h"
#include "AdventureCameraPawn.h"
#include "AdventureCharacter.h"
#include "MapObject.h"
AAdventurePlayerController::AAdventurePlayerController()
{
PrimaryActorTick.bCanEverTick = true;
PrimaryActorTick.bStartWithTickEnabled = true;
AutoReceiveInput = EAutoReceiveInput::Player0;
}
void AAdventurePlayerController::BeginPlay()
{
Super::BeginPlay();
World = GetWorld();
HoveredHex = CurrentHex;
}
// Called every frame
void AAdventurePlayerController::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (bInPlacementMode) { FitOnGrid(PlaceObj); }
}
void AAdventurePlayerController::SetupInputComponent()
@@ -26,18 +35,41 @@ void AAdventurePlayerController::SetupInputComponent()
Super::SetupInputComponent();
// This is initialized on startup, you can go straight to binding
// InputComponent->BindAction("LeftClick", IE_Pressed, this, &AAdventurePlayerController::AdvClick);
InputComponent->BindAction("LeftClick", IE_Pressed, this, &AAdventurePlayerController::LeftClick);
InputComponent->BindAction("DebugAlt", IE_Pressed, this, &AAdventurePlayerController::TogglePlacing); // Change binding eventually
}
void AAdventurePlayerController::AdvClick()
void AAdventurePlayerController::LeftClick()
{
FHitResult Hit;
GetHitResultUnderCursor(ECollisionChannel::ECC_Vehicle,false,Hit);
if (bInPlacementMode) { PlaceObject(PlaceObjClass, HoveredHex); }
}
if (IsValid(Hit.GetActor()))
{
AHexTile* HitHex = (AHexTile*)Hit.GetActor();
// MapRef->FindPathAStar(CurrentHex, HitHex);
// UE_LOG(LogTemp, Warning, TEXT("%d"), HitHex->Index);
void AAdventurePlayerController::TogglePlacing()
{
bInPlacementMode = !bInPlacementMode;
if (bInPlacementMode) {
PlaceObj = World->SpawnActor<AMapObject>(PlaceObjClass, FTransform());
}
else { if (IsValid(PlaceObj)) { PlaceObj->Destroy(); } }
}
void AAdventurePlayerController::FitOnGrid(AMapObject* MapObject)
{
if (!IsValid(HoveredHex)) { return; }
if (HoveredHex->bFree) {
MapObject->SetActorLocation(FVector(HoveredHex->GetActorLocation()));
}
}
// called from BP; generally takes the Hex under the player cursor as argument
void AAdventurePlayerController::PlaceObject(TSubclassOf<AMapObject> MapObjClass, AHexTile* OnHex)
{
// spawn this Actor at World location of Origin Hex;
AMapObject* SpawnedObj = World->SpawnActor<AMapObject>(MapObjClass, FTransform(OnHex->GetActorTransform().GetLocation()));
// Origin = OnHex;
SpawnedObj->Origin = OnHex;
// set Hexes to bOccupied according to BlockVectors;
OnHex->bFree = false;
// set bPlacementMode = false;
//
}