2022-01-11 11:07:17 +01:00
|
|
|
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
|
|
|
|
|
|
#include "AdventurePlayerController.h"
|
|
|
|
#include "AdventureMap.h"
|
|
|
|
#include "HexTile.h"
|
|
|
|
#include "AdventureCameraPawn.h"
|
|
|
|
#include "AdventureCharacter.h"
|
2022-01-24 21:26:06 +01:00
|
|
|
#include "MapObject.h"
|
2022-01-11 11:07:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
AAdventurePlayerController::AAdventurePlayerController()
|
|
|
|
{
|
|
|
|
PrimaryActorTick.bCanEverTick = true;
|
|
|
|
PrimaryActorTick.bStartWithTickEnabled = true;
|
2022-01-24 21:26:06 +01:00
|
|
|
AutoReceiveInput = EAutoReceiveInput::Player0;
|
2022-01-16 21:19:34 +01:00
|
|
|
}
|
|
|
|
void AAdventurePlayerController::BeginPlay()
|
|
|
|
{
|
|
|
|
Super::BeginPlay();
|
|
|
|
World = GetWorld();
|
2022-01-24 21:26:06 +01:00
|
|
|
HoveredHex = CurrentHex;
|
|
|
|
}
|
|
|
|
// Called every frame
|
|
|
|
void AAdventurePlayerController::Tick(float DeltaTime)
|
|
|
|
{
|
|
|
|
Super::Tick(DeltaTime);
|
|
|
|
|
|
|
|
if (bInPlacementMode) { FitOnGrid(PlaceObj); }
|
2022-01-16 21:19:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AAdventurePlayerController::SetupInputComponent()
|
|
|
|
{
|
|
|
|
// Always call this.
|
|
|
|
Super::SetupInputComponent();
|
|
|
|
|
|
|
|
// This is initialized on startup, you can go straight to binding
|
2022-01-24 21:26:06 +01:00
|
|
|
InputComponent->BindAction("LeftClick", IE_Pressed, this, &AAdventurePlayerController::LeftClick);
|
|
|
|
InputComponent->BindAction("DebugAlt", IE_Pressed, this, &AAdventurePlayerController::TogglePlacing); // Change binding eventually
|
|
|
|
}
|
|
|
|
|
|
|
|
void AAdventurePlayerController::LeftClick()
|
|
|
|
{
|
2022-01-25 18:30:49 +01:00
|
|
|
if (!IsValid(HoveredHex)) { return; }
|
2022-01-24 21:26:06 +01:00
|
|
|
if (bInPlacementMode) { PlaceObject(PlaceObjClass, HoveredHex); }
|
2022-01-16 21:19:34 +01:00
|
|
|
}
|
|
|
|
|
2022-02-03 00:56:31 +01:00
|
|
|
TArray<AHexTile*> AAdventurePlayerController::Vision(int32 Radius)
|
2022-01-26 16:48:58 +01:00
|
|
|
{
|
|
|
|
TArray<AHexTile*> Results;
|
2022-02-03 00:56:31 +01:00
|
|
|
TSet<AHexTile*> Visible;
|
|
|
|
if (CurrentHex->bDiagMove) { Radius = FMath::FloorToInt(float(Radius) * (2.f / 3.f)); }
|
|
|
|
Visible = MapRef->BreadthFirstSearch(CurrentHex, Radius);
|
2022-01-26 16:48:58 +01:00
|
|
|
for (auto& Hex : Visible) {
|
|
|
|
if (ExploredHexes.Contains(Hex)) { continue; }
|
|
|
|
Results.Add(Hex);
|
|
|
|
ExploredHexes.Add(Hex);
|
|
|
|
}
|
|
|
|
return Results;
|
|
|
|
}
|
|
|
|
|
2022-01-24 21:26:06 +01:00
|
|
|
void AAdventurePlayerController::TogglePlacing()
|
2022-01-16 21:19:34 +01:00
|
|
|
{
|
2022-01-24 21:26:06 +01:00
|
|
|
bInPlacementMode = !bInPlacementMode;
|
2022-01-29 20:54:35 +01:00
|
|
|
if (bInPlacementMode) { PlaceObj = World->SpawnActor<AMapObject>(PlaceObjClass, FTransform()); }
|
2022-01-24 21:26:06 +01:00
|
|
|
else { if (IsValid(PlaceObj)) { PlaceObj->Destroy(); } }
|
|
|
|
}
|
2022-01-16 21:19:34 +01:00
|
|
|
|
2022-01-24 21:26:06 +01:00
|
|
|
void AAdventurePlayerController::FitOnGrid(AMapObject* MapObject)
|
|
|
|
{
|
|
|
|
if (!IsValid(HoveredHex)) { return; }
|
2022-01-29 20:54:35 +01:00
|
|
|
if (HoveredHex->bFree) { MapObject->SetActorLocation(FVector(HoveredHex->GetActorLocation())); }
|
2022-01-24 21:26:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// called from BP; generally takes the Hex under the player cursor as argument
|
|
|
|
void AAdventurePlayerController::PlaceObject(TSubclassOf<AMapObject> MapObjClass, AHexTile* OnHex)
|
|
|
|
{
|
|
|
|
AMapObject* SpawnedObj = World->SpawnActor<AMapObject>(MapObjClass, FTransform(OnHex->GetActorTransform().GetLocation()));
|
|
|
|
SpawnedObj->Origin = OnHex;
|
2022-01-29 20:54:35 +01:00
|
|
|
OnHex->bFree = false;
|
|
|
|
SpawnedObj->Occupy();
|
2022-01-25 18:30:49 +01:00
|
|
|
|
2022-01-29 20:54:35 +01:00
|
|
|
MapRef->MapObjects.Add(OnHex, SpawnedObj);
|
2022-01-25 18:30:49 +01:00
|
|
|
MapRef->IncID++;
|
|
|
|
SpawnedObj->ID = MapRef->IncID;
|
|
|
|
MapRef->MapObjectsByID.Add(MapRef->IncID, SpawnedObj);
|
2022-01-11 11:07:17 +01:00
|
|
|
}
|