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-06-01 20:25:33 +02:00
|
|
|
#include "MovementArrow.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-06-01 20:25:33 +02:00
|
|
|
|
2022-01-24 21:26:06 +01:00
|
|
|
HoveredHex = CurrentHex;
|
2022-06-01 20:25:33 +02:00
|
|
|
|
2022-01-24 21:26:06 +01:00
|
|
|
}
|
|
|
|
// 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);
|
2022-06-01 20:25:33 +02:00
|
|
|
InputComponent->BindAction("DebugAlt", IE_Pressed, this, &AAdventurePlayerController::EnablePlacing); // Change binding eventually
|
|
|
|
InputComponent->BindAction("DebugAlt", IE_Released, this, &AAdventurePlayerController::DisablePlacing); // Change binding eventually
|
2022-01-24 21:26:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AAdventurePlayerController::LeftClick()
|
|
|
|
{
|
2022-06-01 20:25:33 +02:00
|
|
|
if (IsValid(HoveredHex)) {
|
|
|
|
if (!bInPlacementMode) {
|
|
|
|
|
|
|
|
}
|
|
|
|
else { PlaceObject(PlaceObjClass, HoveredHex); }
|
|
|
|
}
|
|
|
|
else { return; }
|
2022-01-16 21:19:34 +01:00
|
|
|
}
|
|
|
|
|
2022-02-03 01:11:06 +01:00
|
|
|
TArray<AHexTile*> AAdventurePlayerController::Vision(int32 Radius)
|
2022-01-26 16:48:58 +01:00
|
|
|
{
|
|
|
|
TArray<AHexTile*> Results;
|
2022-02-03 01:11:06 +01:00
|
|
|
TSet<AHexTile*> Visible;
|
|
|
|
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-06-01 20:25:33 +02:00
|
|
|
void AAdventurePlayerController::MarkPath(TArray<AHexTile*> Path)
|
|
|
|
{
|
2022-06-08 11:39:41 +02:00
|
|
|
if (Path.IsEmpty()) { return; }
|
|
|
|
else if (!IsValid(Path[0])) { return; }
|
|
|
|
else if (CurrentHex->Distance(Path[0])>1) { return; }
|
|
|
|
|
2022-06-01 20:25:33 +02:00
|
|
|
FHexVector DirA = FHexVector(Path[0]->Q - CurrentHex->Q, Path[0]->R - CurrentHex->R);
|
|
|
|
FHexVector DirB;
|
|
|
|
for (int32 i = 0; i < Path.Num() - 1; i++)
|
|
|
|
{
|
|
|
|
DirB = FHexVector(Path[i + 1]->Q - Path[i]->Q, Path[i + 1]->R - Path[i]->R);
|
|
|
|
AMovementArrow* Arrow = World->SpawnActor<AMovementArrow>(MoveArrowClass, Path[i]->GetActorTransform());
|
|
|
|
Arrow->MapRef = MapRef;
|
|
|
|
Arrow->SetVariant(DirA, DirB);
|
|
|
|
PathArrows.Add(Arrow);
|
|
|
|
DirA = DirB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void AAdventurePlayerController::ClearPath()
|
|
|
|
{
|
|
|
|
for (AMovementArrow* Arrow : PathArrows) {
|
|
|
|
Arrow->Destroy();
|
|
|
|
}
|
|
|
|
PathArrows.Empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AAdventurePlayerController::EnablePlacing()
|
|
|
|
{
|
|
|
|
bInPlacementMode = true;
|
|
|
|
PlaceObj = World->SpawnActor<AMapObject>(PlaceObjClass, FTransform());
|
|
|
|
}
|
|
|
|
|
|
|
|
void AAdventurePlayerController::DisablePlacing()
|
2022-01-16 21:19:34 +01:00
|
|
|
{
|
2022-06-01 20:25:33 +02:00
|
|
|
bInPlacementMode = false;
|
|
|
|
// if (IsValid(PlaceObj)) { PlaceObj->Destroy(); }
|
2022-01-24 21:26:06 +01:00
|
|
|
}
|
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-02-03 01:11:06 +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-02-03 01:11:06 +01:00
|
|
|
OnHex->bFree = false;
|
|
|
|
SpawnedObj->Occupy();
|
2022-01-25 18:30:49 +01:00
|
|
|
|
2022-02-03 01:11:06 +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
|
|
|
}
|