spaceship.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "Engine/World.h"
#include "Spaceship.generated.h"
class USphereComponent;
class UCameraComponent;
class USpringArmComponent;
class ABullet;
UCLASS()
class SPACESHIPBATTLE_API ASpaceShip : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
ASpaceShip();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UPROPERTY(VisibleAnywhere, Category = "Component");
UStaticMeshComponent* ShipSM;
UPROPERTY(VisibleAnywhere, Category = "Component");
USphereComponent* CollisionComp;
UPROPERTY(VisibleAnywhere, Category = "Component");
UCameraComponent* CameraComp;
UPROPERTY(VisibleAnywhere, Category = "Component");
USpringArmComponent* SpringArmComp;
APlayerController* PC;
UPROPERTY(EditAnywhere, Category = "Fire");
TSubclassOf<ABullet> Bullet;
UPROPERTY(VisibleAnywhere, Category = "Component");
USceneComponent* SpawnPoint;
UPROPERTY(EditAnywhere, Category = "Move");
float Speed;
FTimerHandle TimerHandle_BetweenShot;
UPROPERTY(EditAnywhere, Category = "Fire");
float TimeBetweenShot;
void LookAtCursor();
void MoveUp(float Value);
void MoveRight(float Value);
void Move(float DeltaTime);
void Fire();
void StartFire();
void EndFire();
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
};
spaceship.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "SpaceShip.h"
#include "Components/StaticMeshComponent.h"
#include "Components/SphereComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/PlayerController.h"
#include "Kismet/KismetMathLibrary.h"
#include "GameFramework/SpringArmComponent.h"
#include "Misc/App.h"
#include "Engine/World.h"
#include "Bullet.h"
#include "TimerManager.h"
// Sets default values
ASpaceShip::ASpaceShip()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
CollisionComp = CreateDefaultSubobject<USphereComponent>(TEXT("CollisionCom"));
RootComponent = CollisionComp;
ShipSM = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ShipSM"));
ShipSM->SetupAttachment(RootComponent);
SpringArmComp = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArmComp"));
SpringArmComp->SetupAttachment(RootComponent);
CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("CameraCom"));
CameraComp->SetupAttachment(SpringArmComp);
SpawnPoint = CreateDefaultSubobject<USceneComponent>(TEXT("SpawnPoint"));
SpawnPoint->SetupAttachment(ShipSM);
Speed = 2500.0f;
TimeBetweenShot = 0.2;
}
// Called when the game starts or when spawned
void ASpaceShip::BeginPlay()
{
Super::BeginPlay();
PC = Cast<APlayerController>(GetController());
PC->bShowMouseCursor = true;
}
void ASpaceShip::LookAtCursor()
{
FVector MouseLocation, MouseDirection;
PC->DeprojectMousePositionToWorld(MouseLocation, MouseDirection);
FVector TargetLocation = FVector(MouseLocation.X, MouseLocation.Y, GetActorLocation().Z);
FRotator Rotator = UKismetMathLibrary::FindLookAtRotation(GetActorLocation(), TargetLocation);
SetActorRotation(Rotator);
}
void ASpaceShip::MoveUp(float Value)
{
AddMovementInput(FVector::ForwardVector, Value);
}
void ASpaceShip::MoveRight(float Value)
{
AddMovementInput(FVector::RightVector, Value);
}
void ASpaceShip::Move(float DeltaTime)
{
AddActorWorldOffset(ConsumeMovementInputVector() * Speed * DeltaTime , true);
}
void ASpaceShip::Fire()
{
if (Bullet)
{
FActorSpawnParameters SpawnParams;
GetWorld()->SpawnActor<ABullet>(Bullet, SpawnPoint->GetComponentLocation(), SpawnPoint->GetComponentRotation(), SpawnParams);
}
}
void ASpaceShip::StartFire()
{
GetWorldTimerManager().SetTimer(TimerHandle_BetweenShot, this, &ASpaceShip::Fire, TimeBetweenShot, true, 0.0f);
}
void ASpaceShip::EndFire()
{
GetWorldTimerManager().ClearTimer(TimerHandle_BetweenShot);
}
// Called every frame
void ASpaceShip::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
LookAtCursor();
Move(DeltaTime);
}
// Called to bind functionality to input
void ASpaceShip::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveUp", this, &ASpaceShip::MoveUp);
PlayerInputComponent->BindAxis("MoveRight", this, &ASpaceShip::MoveRight);
PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &ASpaceShip::StartFire);
PlayerInputComponent->BindAction("Fire", IE_Released, this, &ASpaceShip::EndFire);
}
Bullet.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Bullet.generated.h"
class UProjectileMovementComponent;
UCLASS()
class SPACESHIPBATTLE_API ABullet : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ABullet();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UPROPERTY(VisibleAnywhere, Category = "Component");
USceneComponent* RootComp;
UPROPERTY(VisibleAnywhere, Category = "Component");
UStaticMeshComponent* BulletSM;
UPROPERTY(VisibleAnywhere, Category = "Component");
UProjectileMovementComponent* ProjectileMovementComp;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
Bullet.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "Bullet.h"
#include "Components/StaticMeshComponent.h"
#include "Components/SceneComponent.h"
#include "GameFramework/ProjectileMovementComponent.h"
// Sets default values
ABullet::ABullet()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
RootComp = CreateDefaultSubobject<USceneComponent>(TEXT("SceneComp"));
RootComponent = RootComp;
BulletSM = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BulletSM"));
BulletSM->SetupAttachment(RootComp);
ProjectileMovementComp = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovementComp"));
}
// Called when the game starts or when spawned
void ABullet::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ABullet::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}