Base functionality for MapObject placement

This commit is contained in:
Maximilian Fajnberg 2022-01-24 21:26:06 +01:00
parent 74eab48a6e
commit 237c056b30
8 changed files with 93 additions and 79 deletions

View File

@ -28,6 +28,7 @@ AAdventureCameraPawn::AAdventureCameraPawn()
ESAMinBoundSlope = -1 / ESASize;
ESAMinBoundIntercept = 0 - (ESAMinBoundSlope * ESASize);
AutoPossessPlayer = EAutoReceiveInput::Player0;
}
// Called when the game starts or when spawned
@ -36,7 +37,7 @@ void AAdventureCameraPawn::BeginPlay()
Super::BeginPlay();
ControllerRef = (AAdventurePlayerController*)UGameplayStatics::GetPlayerController(GetWorld(), 0);
// The Viewport properties are inaccurate right after BeginPlay, so we need to wait a short time before saving them here.
// Viewport properties not accurate on BeginPlay (must wait before accessing)
FTimerHandle UnusedHandle;
GetWorldTimerManager().SetTimer(
UnusedHandle, this, &AAdventureCameraPawn::GetTheDamnViewport, 1, false);
@ -63,8 +64,21 @@ void AAdventureCameraPawn::SetupPlayerInputComponent(UInputComponent* PlayerInpu
{
PlayerInputComponent->BindAxis("Mouse X", this, &AAdventureCameraPawn::EdgeScrollSide);
PlayerInputComponent->BindAxis("Mouse Y", this, &AAdventureCameraPawn::EdgeScrollVert);
PlayerInputComponent->BindAxis("Move AD", this, &AAdventureCameraPawn::ScrollSide);
PlayerInputComponent->BindAxis("Move WS", this, &AAdventureCameraPawn::ScrollVert);
}
}
void AAdventureCameraPawn::ScrollSide(float AxisValue)
{
float DeltaLoc = AxisValue * BaseScrollSpeed * 3;
AddActorLocalOffset(FVector(DeltaLoc, 0, 0));
}
void AAdventureCameraPawn::ScrollVert(float AxisValue)
{
float DeltaLoc = AxisValue * BaseScrollSpeed * -3;
AddActorLocalOffset(FVector(0, DeltaLoc, 0));
}
void AAdventureCameraPawn::EdgeScrollSide(float AxisValue)

View File

@ -42,7 +42,7 @@ public:
UPROPERTY()
FVector AdvPawnLocationDelta;
UPROPERTY(BlueprintReadWrite, Category = "Runtime")
bool bAdvPawnIsMoving;
bool bAdvPawnIsMoving = false;
UPROPERTY(BlueprintReadWrite, Category = "Runtime")
class AHexTile* SelectedHex;
@ -67,6 +67,10 @@ public:
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
UFUNCTION()
void ScrollSide(float AxisValue);
UFUNCTION()
void ScrollVert(float AxisValue);
UFUNCTION(BlueprintCallable)
void EdgeScrollSide(float AxisValue);
UFUNCTION(BlueprintCallable)

View File

@ -118,6 +118,7 @@ TArray<AHexTile*> AAdventureMap::AStar(AHexTile* Start, AHexTile* Goal)
// expand frontier & adjust path data
for (AHexTile* Neighbor : Neighbors(Candidate)) {
if (Neighbor->Distance(Candidate) > 1) { continue; }
if (!(Neighbor->bFree)) { continue; }
if (Processed.Contains(Neighbor)) { continue; }
bool bInToExamine = ToExamine.Contains(Neighbor);
@ -134,13 +135,15 @@ TArray<AHexTile*> AAdventureMap::AStar(AHexTile* Start, AHexTile* Goal)
}
}
}
return LinkPath(Start, Goal);
}
TArray<AHexTile*> AAdventureMap::LinkPath(AHexTile* Start, AHexTile* Goal)
{
TArray<AHexTile*> Path;
if (!IsValid(Goal->CameFrom)) { return Path; }
AHexTile* iPathNode = Goal;
while (iPathNode != Start) {

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()))
void AAdventurePlayerController::TogglePlacing()
{
AHexTile* HitHex = (AHexTile*)Hit.GetActor();
// MapRef->FindPathAStar(CurrentHex, HitHex);
// UE_LOG(LogTemp, Warning, TEXT("%d"), HitHex->Index);
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;
//
}

View File

@ -11,7 +11,7 @@ class AAdventureMap;
class AHexTile;
class AAdventureCameraPawn;
class AAdventureCharacter;
class AMapObject;
/**
*
*/
@ -23,6 +23,7 @@ class FRAY_API AAdventurePlayerController : public APlayerController
public:
AAdventurePlayerController();
// General
UPROPERTY()
UWorld* World;
UPROPERTY()
@ -31,11 +32,31 @@ public:
AHexTile* SpawnHex;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Runtime")
AHexTile* CurrentHex;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
AHexTile* HoveredHex;
UFUNCTION(BlueprintCallable)
void LeftClick();
protected:
virtual void BeginPlay() override;
virtual void SetupInputComponent() override;
public:
void AdvClick();
// Object Placement
UPROPERTY(BlueprintReadWrite, VisibleAnywhere)
bool bInPlacementMode;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
TSubclassOf<AMapObject> PlaceObjClass;
UPROPERTY(BlueprintReadWrite, VisibleAnywhere)
AMapObject* PlaceObj;
UFUNCTION(BlueprintCallable)
void TogglePlacing();
UFUNCTION(BlueprintCallable)
void FitOnGrid(AMapObject* MapObject);
UFUNCTION()
void PlaceObject(TSubclassOf<AMapObject> MapObjClass, AHexTile* OnHex);
// Called every frame
virtual void Tick(float DeltaTime) override;
};

View File

@ -1,34 +0,0 @@
// Fill out your copyright notice in the Description page of Project Settings.
#include "Clickable.h"
// Sets default values for this component's properties
UClickable::UClickable()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void UClickable::BeginPlay()
{
Super::BeginPlay();
// ...
}
// Called every frame
void UClickable::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
// ...
}

View File

@ -1,28 +0,0 @@
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "Clickable.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class FRAY_API UClickable : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UClickable();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
};

View File

@ -49,8 +49,6 @@ public:
int32 MoveCost = 1;
UPROPERTY(BlueprintReadWrite, VisibleInstanceOnly, Category = "Movement")
AHexTile* CameFrom;
UPROPERTY(VisibleInstanceOnly, Category = "Movement")
int32 CostSoFar = 0;
UPROPERTY()
int32 FCost;
UPROPERTY()
@ -58,6 +56,10 @@ public:
UPROPERTY()
int32 HCost;
// MapObject Placement
UPROPERTY(BlueprintReadWrite, VisibleAnywhere)
bool bFree = true;
FORCEINLINE bool operator == (const AHexTile &Other)
{
if (this->Q == Other.Q && this->R == Other.R) { return true; }