unreal/AdventurePlayerController.cpp

91 lines
2.8 KiB
C++

// 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"
#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()
{
// Always call this.
Super::SetupInputComponent();
// This is initialized on startup, you can go straight to binding
InputComponent->BindAction("LeftClick", IE_Pressed, this, &AAdventurePlayerController::LeftClick);
InputComponent->BindAction("DebugAlt", IE_Pressed, this, &AAdventurePlayerController::TogglePlacing); // Change binding eventually
}
void AAdventurePlayerController::LeftClick()
{
if (!IsValid(HoveredHex)) { return; }
if (bInPlacementMode) { PlaceObject(PlaceObjClass, HoveredHex); }
}
TArray<AHexTile*> AAdventurePlayerController::Vision()
{
TArray<AHexTile*> Results;
TArray<AHexTile*> Visible = MapRef->BreadthFirstSearch(CurrentHex, 7);
for (auto& Hex : Visible) {
if (ExploredHexes.Contains(Hex)) { continue; }
Results.Add(Hex);
ExploredHexes.Add(Hex);
}
return Results;
}
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()));
SpawnedObj->Origin = OnHex;
// set Hexes to bOccupied according to BlockVectors;
OnHex->bFree = false; // exact Hexes are eventually to be determined by MapObjectClass's BlockingVectors
MapRef->MapObjects.Add(OnHex, SpawnedObj); // maybe this as well
MapRef->IncID++;
SpawnedObj->ID = MapRef->IncID;
MapRef->MapObjectsByID.Add(MapRef->IncID, SpawnedObj);
}