Le joueur peut enfin faire apparaître le projectile dans la scène après avoir appuyé sur la touche "Espace". Cependant le Mesh n'apparait toujours pas pour l'instant mais j'ai presque fini.
110 lines
3.9 KiB
C++
110 lines
3.9 KiB
C++
#include "M4_PlayerPawn.h"
|
|
#include "EnhancedInputComponent.h"
|
|
#include "M4_Gamemode.h"
|
|
#include "M4_Projectile.h"
|
|
#include "GameFramework/PlayerController.h"
|
|
#include "Kismet/KismetMathLibrary.h"
|
|
#include "Camera/CameraComponent.h"
|
|
|
|
AM4_PlayerPawn::AM4_PlayerPawn()
|
|
{
|
|
PrimaryActorTick.bCanEverTick = true;
|
|
PrimaryActorTick.bStartWithTickEnabled = true;
|
|
AutoPossessPlayer = EAutoReceiveInput::Player0;
|
|
SpawnCollisionHandlingMethod = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
|
|
|
|
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
|
|
|
|
MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
|
|
MeshComponent->SetupAttachment(RootComponent);
|
|
MeshComponent->SetMobility(EComponentMobility::Movable);
|
|
MeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
|
|
MeshComponent->SetCollisionProfileName(UCollisionProfile::Pawn_ProfileName);
|
|
MeshComponent->SetRelativeScale3D(FVector(1.0f, MeshScale.Y, MeshScale.X));
|
|
|
|
static ConstructorHelpers::FObjectFinder<UStaticMesh> DefaultMeshRef(TEXT("/Game/CTP/04_Mesh/SM_Cube.SM_Cube"));
|
|
if (DefaultMeshRef.Succeeded())
|
|
{
|
|
MeshComponent->SetStaticMesh(DefaultMeshRef.Object);
|
|
}
|
|
|
|
static ConstructorHelpers::FObjectFinder<UInputAction> MoveActionRef(TEXT("/Game/CTP/03_Input/IA_Move.IA_Move"));
|
|
if (MoveActionRef.Succeeded())
|
|
{
|
|
MoveAction = MoveActionRef.Object;
|
|
}
|
|
|
|
static ConstructorHelpers::FObjectFinder<UInputAction> ShootActionRef(TEXT("/Game/CTP/03_Input/IA_Shoot.IA_Shoot"));
|
|
if (ShootActionRef.Succeeded())
|
|
{
|
|
ShootAction = ShootActionRef.Object;
|
|
}
|
|
}
|
|
|
|
void AM4_PlayerPawn::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
SetActorLocation(FVector(0.0f, 0.0f, -400.0f));
|
|
}
|
|
|
|
void AM4_PlayerPawn::Tick(float DeltaTime)
|
|
{
|
|
Super::Tick(DeltaTime);
|
|
|
|
if (AM4_Gamemode* GM = Cast<AM4_Gamemode>(GetWorld()->GetAuthGameMode()))
|
|
{
|
|
FVector NewLocation = GetActorLocation() + FVector( 0.0f, LastMoveValue.Y, LastMoveValue.X) * MoveSpeed * DeltaTime;
|
|
NewLocation.Z = FMath::Clamp(NewLocation.Z, GM->Bounds.Min.X, GM->Bounds.Max.X);
|
|
NewLocation.Y = FMath::Clamp(NewLocation.Y, GM->Bounds.Min.Y, GM->Bounds.Max.Y);
|
|
SetActorLocation(NewLocation);
|
|
}
|
|
}
|
|
|
|
void AM4_PlayerPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
|
|
{
|
|
Super::SetupPlayerInputComponent(PlayerInputComponent);
|
|
if (UEnhancedInputComponent* Input = Cast<UEnhancedInputComponent>(PlayerInputComponent))
|
|
{
|
|
if (MoveAction)
|
|
{
|
|
Input->BindAction(MoveAction, ETriggerEvent::Triggered, this, &AM4_PlayerPawn::Move);
|
|
}
|
|
|
|
if (ShootAction)
|
|
{
|
|
Input->BindAction(ShootAction, ETriggerEvent::Triggered, this, &AM4_PlayerPawn::Shoot);
|
|
}
|
|
}
|
|
}
|
|
|
|
void AM4_PlayerPawn::Move(const FInputActionInstance& Instance)
|
|
{
|
|
LastMoveValue = Instance.GetValue().Get<FVector2D>().GetSafeNormal();
|
|
}
|
|
|
|
void AM4_PlayerPawn::Shoot(const FInputActionInstance& Inst)
|
|
{
|
|
if (Proj)
|
|
{
|
|
UWorld* World = GetWorld();
|
|
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Blue, TEXT("Using the Wand!"));
|
|
|
|
if (World)
|
|
{
|
|
FActorSpawnParameters SpawnParams;
|
|
SpawnParams.Owner = this;
|
|
const FVector InitialLocation = FVector(0.0f, GetActorLocation().Y, GetActorLocation().Z);
|
|
|
|
AM4_Projectile* Projectile = World->SpawnActor<AM4_Projectile>(Proj, InitialLocation, FRotator(), SpawnParams);
|
|
FVector LaunchDirection = FVector(0.0f, InitialLocation.Y + 1.f,InitialLocation.Z);
|
|
|
|
if (Projectile)
|
|
{
|
|
Projectile->ShootDirection(LaunchDirection);
|
|
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Green, TEXT("Spell Casted!"));
|
|
}
|
|
}
|
|
|
|
}
|
|
};
|