Files
Tactica/Source/Tactica/Private/TacticaPlayerController.cpp
T

78 lines
1.6 KiB
C++

#include "TacticaPlayerController.h"
#include "GridCell.h"
#include "Components/InputComponent.h"
#include "InputCoreTypes.h"
DEFINE_LOG_CATEGORY_STATIC(LogTacticaInput, Log, All);
ATacticaPlayerController::ATacticaPlayerController()
{
bShowMouseCursor = true;
}
void ATacticaPlayerController::SetupInputComponent()
{
Super::SetupInputComponent();
if (InputComponent)
{
InputComponent->BindKey(EKeys::LeftMouseButton, IE_Pressed, this,
&ATacticaPlayerController::OnClick);
}
}
void ATacticaPlayerController::PlayerTick(float DeltaTime)
{
Super::PlayerTick(DeltaTime);
AGridCell* Cell = GetCellUnderCursor();
if (Cell == HoveredCell)
{
return;
}
if (HoveredCell)
{
HoveredCell->SetHovered(false);
}
HoveredCell = Cell;
if (HoveredCell)
{
HoveredCell->SetHovered(true);
}
}
AGridCell* ATacticaPlayerController::GetCellUnderCursor() const
{
// マスはWorldDynamicのみを対象にする。
// 地形などWorldStaticと上面が同じ高さでも、そちらに邪魔されない
const TArray<TEnumAsByte<EObjectTypeQuery>> ObjectTypes = {
UEngineTypes::ConvertToObjectType(ECC_WorldDynamic) };
FHitResult Hit;
if (GetHitResultUnderCursorForObjects(ObjectTypes, false, Hit))
{
return Cast<AGridCell>(Hit.GetActor());
}
return nullptr;
}
void ATacticaPlayerController::OnClick()
{
AGridCell* Cell = GetCellUnderCursor();
if (!Cell)
{
return; // マス以外のクリックでは選択を変えない
}
if (SelectedCell)
{
SelectedCell->SetSelected(false);
}
SelectedCell = Cell;
SelectedCell->SetSelected(true);
UE_LOG(LogTacticaInput, Log, TEXT("Cell clicked: (%d, %d)"),
Cell->GridCoord.X, Cell->GridCoord.Y);
}