AirPlane.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "AirPlane.generated.h"
class USphereComponent;
UCLASS()
class PLANEWAR_API AAirPlane : public APawn
{
GENERATED_BODY()
public:
// Sets default values for this pawn's properties
AAirPlane();
UPROPERTY(VisibleAnywhere, Category = "Component")
USphereComponent* CollisionComp;
UPROPERTY(VisibleAnywhere, Category = "Component")
UStaticMeshComponent* ShipSM;
void MoveUp();
void MoveRight();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
//声明AirPlane基本属性
UPROPERTY(EditAnywhere, Category = "AirPlanValue")
float moveSpeed;
UPROPERTY(EditAnywhere, Category = "AirPlanValue")
float hp;
UPROPERTY(EditAnywhere, Category = "AirPlanValue")
int shieldNum;
public:
//默认方法
// Called every frame
virtual void Tick(float DeltaTime) override;
// Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
//公共方法:
void Move(float DeltaTime);
void Attack();
void UnderAttack();
void Dead();
};
AirPlane.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "AirPlane.h"
#include "Components/StaticMeshComponent.h"
#include "Components/SphereComponent.h"
// Sets default values
AAirPlane::AAirPlane()
{
// 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;
//初始化类属性
moveSpeed = 1000;
hp = 3;
shieldNum = 0;
//初始化一个Mesh,和一个Sphere碰撞体
CollisionComp = CreateDefaultSubobject<USphereComponent>(TEXT("CollisionComp"));
RootComponent = CollisionComp;
ShipSM = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("ShipSM"));
ShipSM->SetupAttachment(RootComponent);
}
// Called when the game starts or when spawned
void AAirPlane::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AAirPlane::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
// Called to bind functionality to input
void AAirPlane::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
void AAirPlane::Move(float DeltaTime)
{
}
void AAirPlane::Attack()
{
}
void AAirPlane::UnderAttack()
{
}
void AAirPlane::Dead()
{
}
PlayerAirPlane.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "AirPlane.h"
#include "Weapon.h"
//#include "PlaneWeapon.h"
//#include "TheWeapon.h"
#include "PlayerAirPlane.generated.h"
/**
*
*/
UCLASS()
class PLANEWAR_API APlayerAirPlane : public AAirPlane
{
GENERATED_BODY()
public:
APlayerAirPlane();
virtual void Tick(float DeltaTime) override;
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
//玩家飞船类中的武器类
/*UPROPERTY(EditAnywhere, Category = "Fire")
TSubclassOf<AWeapon> TmpWeapon;*/
UPROPERTY(EditAnywhere, Category = "Fire")
AWeapon* TmpWeapon;
/*UPROPERTY(EditAnywhere, Category = "TheWeapon")
UTheWeapon* TheWeapon;*/
//公共方法:
void Move(float DeltaTime);
void Attack();
void UnderAttack();
void Dead();
//Player玩家类 新增方法*
void MoveForward(float Value);
void MoveRight(float Value);
protected:
virtual void BeginPlay() override;
};
PlayerAirPlane.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "PlayerAirPlane.h"
#include "Engine.h"
#include "GameFramework/PlayerController.h"
APlayerAirPlane::APlayerAirPlane()
{
//实例化一个武器类
//TmpWeapon = GetWorld()->SpawnActor<AWeapon>(APlayerAirPlane,FVector(0,0,0),FRotator::ZeroRotator);
//TmpWeapon = GetWorld()->SpawnActor(AWeapon::StaticClass(), FTransform(), FRotator::ZeroRotator,FActorSpawnParameters());
//TmpWeapon
/*TmpWeapon1 = CreateDefaultSubobject<UWeaponComponent>(TEXT("Fire1"));
TmpWeapon1->SetupAttachment(RootComponent);*/
//TheWeapon
/*TheWeapon = CreateDefaultSubobject<UTheWeapon>(TEXT("Fire1"));
TheWeapon->SetupAttachment(RootComponent);*/
TmpWeapon = CreateDefaultSubobject<AWeapon>(TEXT("Fire"));
}
//重写Pawn的移动类方法
void APlayerAirPlane::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward", this, &APlayerAirPlane::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &APlayerAirPlane::MoveRight);
PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &APlayerAirPlane::Attack);
}
void APlayerAirPlane::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
Move(DeltaTime);
}
//玩家移动的方法:获取虚拟轴的输入来确定移动
void APlayerAirPlane::Move(float DeltaTime)
{
AddActorWorldOffset(ConsumeMovementInputVector() * moveSpeed * DeltaTime, true);
}
void APlayerAirPlane::Attack()
{
UE_LOG(LogTemp, Log, TEXT("Plane Attacking!"));
TmpWeapon->Attack();
//if (TmpWeapon1)
//{
// //TmpWeapon->FindFunctionByName();
// TmpWeapon1->Attack();
//}
}
//获取用户输入的虚拟轴方法
void APlayerAirPlane::MoveForward(float Value)
{
AddMovementInput(FVector::UpVector,Value);
//UE_LOG(LogTemp, LOG, Error, TEXT("LogTemp->上下移动"));
//UE_LOG(123, LOG, Error, TEXT("123的上下移动"));
//UE_LOG(LogTemp, Log, TEXT("LogTemp->上下移动"));
}
void APlayerAirPlane::MoveRight(float Value)
{
AddMovementInput(FVector::RightVector, Value);
}
void APlayerAirPlane::BeginPlay()
{
Super::BeginPlay();
}
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 PLANEWAR_API ABullet : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ABullet();
//声明默认根组件 和 Mesh
UPROPERTY(VisibleAnywhere, Category = "Component")
USceneComponent* RootComp;
UPROPERTY(VisibleAnywhere, Category = "Component")
UStaticMeshComponent* BulletSM;
// 声明Tick,Move方法
virtual void Tick(float DeltaTime) override;
void Move(float DeltaTime);
//声明变量
UPROPERTY(EditAnywhere, Category = "Value")
bool bIsEnemy;
UPROPERTY(EditAnywhere, Category = "Value")
float Speed;
UPROPERTY(EditAnywhere, Category = "Value")
float Damage;
UPROPERTY(EditAnywhere, Category = "Value")
float EfectX;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
};
Bullet.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "Bullet.h"
#include "PlayerAirPlane.h"
#include "UObject/ConstructorHelpers.h"
#include "Engine/CollisionProfile.h"
#include "Components/StaticMeshComponent.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;
//初始化
static ConstructorHelpers::FObjectFinder<UStaticMesh>BulletMesh(TEXT("/Game/Weapon_Pack/Mesh/Weapons/Weapons_Kit/SM_Mace.SM_Mace"));
RootComp = CreateDefaultSubobject<USceneComponent>(TEXT("RootComp"));
RootComponent = RootComp;
BulletSM = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BulletSM"));
BulletSM ->SetupAttachment(RootComponent);
BulletSM->SetCollisionProfileName(UCollisionProfile::Pawn_ProfileName);
BulletSM->SetStaticMesh(BulletMesh.Object);
/*ProjectileMovementComp = CreateDefaultSubobject<UProjectileMovementComponent>(TEXT("ProjectileMovementComp"));*/
}
// Called when the game starts or when spawned
void ABullet::BeginPlay()
{
Super::BeginPlay();
Speed = 0.3;
Damage = 1;
bIsEnemy = false;
EfectX = 1;
}
// Called every frame
void ABullet::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
Move(DeltaTime);
}
void ABullet::Move(float DeltaTime)
{
AddActorWorldOffset(FVector::ForwardVector * DeltaTime * Speed * EfectX);
}
Weapon.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Bullet.h"
#include "Weapon.generated.h"
UCLASS()
class PLANEWAR_API AWeapon : public AActor
{
GENERATED_BODY()
public:
//武器类中的子弹类
UPROPERTY(EditAnywhere, Category = "Fire")
TSubclassOf<ABullet> TmpBullet;
//子弹发射点
UPROPERTY(VisibleAnywhere, Category = "Component")
USceneComponent* SpawnPoint;
//武器的Transform
//
// Sets default values for this actor's properties
AWeapon();
virtual void Tick(float DeltaTime) override;
void Attack();
//属性
UPROPERTY(EditAnywhere, Category = "Fire")
int BulletNum;
UPROPERTY(EditAnywhere, Category = "Fire")
float AttackPower;
UPROPERTY(EditAnywhere, Category = "Fire")
float FireInterval;
//float FireInterval;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
};
Weapon.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "Weapon.h"
#include "Bullet.h"
#include "Engine.h"
#include "Engine/World.h"
// Sets default values
AWeapon::AWeapon()
{
// 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;
//开火位置
SpawnPoint = CreateDefaultSubobject<USceneComponent>(TEXT("SceneComp"));
RootComponent = SpawnPoint;
AttackPower = 1;
BulletNum = 1;
FireInterval = 0.5;
}
// Called when the game starts or when spawned
void AWeapon::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AWeapon::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AWeapon::Attack()
{
UE_LOG(LogTemp, Log, TEXT("Weapon Attacking!"));
FActorSpawnParameters SpawnParams;
GetWorld()->SpawnActor<ABullet>(TmpBullet, SpawnPoint->GetComponentLocation(), SpawnPoint->GetComponentRotation(), SpawnParams);
}