TArray<AActor*> EnemyBulletArr;
AEnemyBullet* Bullet;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), AEnemyBullet::StaticClass(), EnemyBulletArr);
for (AActor* TmpActor : EnemyBulletArr)
{
Bullet = Cast<AEnemyBullet>(TmpActor);
Bullet->BulletDead();
}
Weapon.h
//Weapon Attack......
virtual void AttackLevelOne()override;
virtual void AttackLevelTwo()override;
virtual void AttackLevelMax()override;
void UWeaponNormalComponent::AttackLevelOne()
{
InstantBullet(this->GetComponentLocation(), this->GetComponentRotation());
InstantBullet(FVector(this->GetComponentLocation().X, this->GetComponentLocation().Y - 15, this->GetComponentLocation().Z - 20), FRotator(75, 90, 90));
InstantBullet(FVector(this->GetComponentLocation().X, this->GetComponentLocation().Y + 15, this->GetComponentLocation().Z - 20), FRotator(105, 90, 90));
}
void UWeaponNormalComponent::AttackLevelTwo()
{
InstantBullet(this->GetComponentLocation(), this->GetComponentRotation());
InstantBullet(FVector(this->GetComponentLocation().X, this->GetComponentLocation().Y - 15, this->GetComponentLocation().Z - 20), FRotator(75, 90, 90));
InstantBullet(FVector(this->GetComponentLocation().X, this->GetComponentLocation().Y + 15, this->GetComponentLocation().Z - 20), FRotator(105, 90, 90));
InstantBullet(FVector(this->GetComponentLocation().X, this->GetComponentLocation().Y - 30, this->GetComponentLocation().Z - 20), FRotator(60, 90, 90));
InstantBullet(FVector(this->GetComponentLocation().X, this->GetComponentLocation().Y + 30, this->GetComponentLocation().Z - 20), FRotator(120, 90, 90));
}
void UWeaponNormalComponent::AttackLevelMax()
{
InstantBullet(this->GetComponentLocation(), this->GetComponentRotation());
InstantBullet(FVector(this->GetComponentLocation().X, this->GetComponentLocation().Y - 15, this->GetComponentLocation().Z - 20), FRotator(75, 90, 90));
InstantBullet(FVector(this->GetComponentLocation().X, this->GetComponentLocation().Y + 15, this->GetComponentLocation().Z - 20), FRotator(105, 90, 90));
InstantBullet(FVector(this->GetComponentLocation().X, this->GetComponentLocation().Y - 30, this->GetComponentLocation().Z - 20), FRotator(60, 90, 90));
InstantBullet(FVector(this->GetComponentLocation().X, this->GetComponentLocation().Y + 30, this->GetComponentLocation().Z - 20), FRotator(120, 90, 90));
InstantBullet(FVector(this->GetComponentLocation().X, this->GetComponentLocation().Y - 45, this->GetComponentLocation().Z - 20), FRotator(45, 90, 90));
InstantBullet(FVector(this->GetComponentLocation().X, this->GetComponentLocation().Y + 45, this->GetComponentLocation().Z - 20), FRotator(135, 90, 90));
InstantBullet(FVector(this->GetComponentLocation().X, this->GetComponentLocation().Y - 5, this->GetComponentLocation().Z - 20), FRotator(85, 90, 90));
InstantBullet(FVector(this->GetComponentLocation().X, this->GetComponentLocation().Y + 5, this->GetComponentLocation().Z - 20), FRotator(95, 90, 90));
}
PROP.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "PlayerAirPlane.h"
#include "PROP.generated.h"
class USphereComponent;
UENUM(BlueprintType)
enum class PROPEnum :uint8 //设置uint8类型
{
None = 0,
WeaponNormal =1 UMETA(DisplayName = "WeaponNormal"),
WeaponHelix=2 UMETA(DisplayName = "WeaponHelix"),
WeaponTrack = 3 UMETA(DisplayName = "WeaponTrack"),
Health = 4 UMETA(DisplayName = "Health"),
Shield = 5 UMETA(DisplayName = "Shield"),
Boom = 6 UMETA(DisplayName = "Boom")
};
UCLASS()
class PLANEWAR_API APROP : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
APROP();
//Declare BeforeDestroy......
void PickUp(APlayerAirPlane* tmpPlayerAirPlane);
void AfterPickUp();
void ChangeNext();
// Called every frame
virtual void Tick(float DeltaTime) override;
//Declare CollisionComponent......
UPROPERTY(VisibleAnywhere, Category = "Component")
USphereComponent* CollisionComp;
//Declare MeshComponent......
UPROPERTY(VisibleAnywhere, Category = "Component")
UStaticMeshComponent* PROPSM;
UPROPERTY(VisibleAnywhere, Category = "Component")
UStaticMeshComponent* PROPShieldSM;
//Declare PROP disappear time......
UPROPERTY(EditAnywhere, Category = "PROPValue")
float PROPTimer;
//Declare PROP Health......
UPROPERTY(EditAnywhere, Category = "PROPValue")
float AddHealth;
//Declare PROP Health......
UPROPERTY(EditAnywhere, Category = "PROPValue")
float AddShield;
//Declare PORP type......
UPROPERTY(EditAnywhere, Category = "PROPValue")
PROPEnum CurrentPROPType;
UStaticMesh* Mesh1;
UStaticMesh* Mesh2;
UStaticMesh* Mesh3;
UStaticMesh* Mesh4;
UStaticMesh* Mesh5;
UStaticMesh* Mesh6;
UStaticMesh* Mesh7;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
virtual void NotifyActorBeginOverlap(AActor* OtherActor) override;
// Delay calling function......
FTimerHandle PROPTimerHandle;
};
PROP.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "PROP.h"
#include "Components/StaticMeshComponent.h"
#include "Components/SphereComponent.h"
#include "PlayerAirPlane.h"
#include "math.h"
// Sets default values
APROP::APROP()
{
// 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;
//一个Sphere碰撞体
CollisionComp = CreateDefaultSubobject<USphereComponent>(TEXT("CollisionComp"));
RootComponent = CollisionComp;
//PROP Shield Mesh
PROPShieldSM = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PROPShieldSM"));
PROPShieldSM->SetupAttachment(RootComponent);
//PROP Mesh
PROPSM = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PROPSM"));
PROPSM->SetupAttachment(RootComponent);
static ConstructorHelpers::FObjectFinder<UStaticMesh> propMesh(TEXT("/Game/PlaneWar/Meshes/AirPlane"));
PROPSM->SetStaticMesh(propMesh.Object);
static ConstructorHelpers::FObjectFinder<UStaticMesh> propShieldMesh(TEXT("/Game/PlaneWar/Meshes/Sphere"));
PROPShieldSM->SetStaticMesh(propShieldMesh.Object);
Initialize properties......
CurrentPROPType = PROPEnum::Health;
AddHealth = -50;
AddShield = -50;
static ConstructorHelpers::FObjectFinder<UStaticMesh>StaticMesh1(TEXT("/Game/PlaneWar/Meshes/PROP/PROP_1"));
Mesh1 = StaticMesh1.Object;
static ConstructorHelpers::FObjectFinder<UStaticMesh>StaticMesh2(TEXT("/Game/PlaneWar/Meshes/PROP/PROP_2"));
Mesh2 = StaticMesh2.Object;
static ConstructorHelpers::FObjectFinder<UStaticMesh>StaticMesh3(TEXT("/Game/PlaneWar/Meshes/PROP/PROP_3"));
Mesh3 = StaticMesh3.Object;
static ConstructorHelpers::FObjectFinder<UStaticMesh>StaticMesh4(TEXT("/Game/PlaneWar/Meshes/PROP/PROP_4"));
Mesh4= StaticMesh4.Object;
static ConstructorHelpers::FObjectFinder<UStaticMesh>StaticMesh5(TEXT("/Game/PlaneWar/Meshes/PROP/PROP_5"));
Mesh5 = StaticMesh5.Object;
static ConstructorHelpers::FObjectFinder<UStaticMesh>StaticMesh6(TEXT("/Game/PlaneWar/Meshes/PROP/PROP_6"));
Mesh6 = StaticMesh6.Object;
static ConstructorHelpers::FObjectFinder<UStaticMesh>StaticMesh7(TEXT("/Game/PlaneWar/Meshes/PROP/PROP_7"));
Mesh7 = StaticMesh7.Object;
}
// Called when the game starts or when spawned
void APROP::BeginPlay()
{
Super::BeginPlay();
GetWorld()->GetTimerManager().SetTimer(PROPTimerHandle, [=]() {ChangeNext(); }, 4.0f, true, 4.0f);
//static ConstructorHelpers::FObjectFinder<UStaticMesh> WeaponNormalMesh(TEXT("/Game/PlaneWar/Meshes/Bullet/PlayerBullet_2"));
//static ConstructorHelpers::FObjectFinder<UStaticMesh> WeaponHelixMesh(TEXT("/Game/PlaneWar/Meshes/Bullet/PlayerBullet_3"));
}
void APROP::NotifyActorBeginOverlap(AActor* OtherActor)
{
if (Cast<APlayerAirPlane>(OtherActor))
{
UE_LOG(LogTemp, Log, TEXT("PROP Overlap...."));
PickUp(Cast<APlayerAirPlane>(OtherActor));
//Destroy();
}
}
void APROP::PickUp(APlayerAirPlane* tmpPlayerAirPlane)
{
switch (CurrentPROPType)
{
case PROPEnum::None:
break;
case PROPEnum::WeaponNormal:tmpPlayerAirPlane->LevelUpNormal();
break;
case PROPEnum::WeaponHelix:tmpPlayerAirPlane->LevelUpHelix();
break;
case PROPEnum::WeaponTrack:tmpPlayerAirPlane->LevelUpTrack();
break;
case PROPEnum::Health:tmpPlayerAirPlane->UnderAttack(AddHealth);
break;
case PROPEnum::Shield:tmpPlayerAirPlane->ShieldUnderAttack(AddShield);
break;
default:
break;
}
AfterPickUp();
}
void APROP::ChangeNext()
{
int i = FMath::RandRange(1, 6);
UE_LOG(LogTemp, Log, TEXT("Rand PROP :%d...."), i);
CurrentPROPType = (PROPEnum)i;
switch (CurrentPROPType)
{
case PROPEnum::None:
break;
case PROPEnum::WeaponNormal:
PROPSM->SetStaticMesh( Mesh1);
break;
case PROPEnum::WeaponHelix:
PROPSM->SetStaticMesh(Mesh2);
break;
case PROPEnum::WeaponTrack:
PROPSM->SetStaticMesh(Mesh3);
break;
case PROPEnum::Health:
PROPSM->SetStaticMesh(Mesh4);
break;
case PROPEnum::Shield:
PROPSM->SetStaticMesh(Mesh5);
break;
case PROPEnum::Boom:
PROPSM->SetStaticMesh(Mesh6);
break;
default:
break;
}
//switch (CurrentPROPType)
//{
//case PROPEnum::None:
// break;
//case PROPEnum::WeaponNormal:
//{//PROPSM->SetStaticMesh(WeaponNormalMesh);
//}
//break;
//case PROPEnum::WeaponHelix:
// //PROPSM->SetStaticMesh(WeaponHelixMesh);
// break;
//case PROPEnum::WeaponTrack: {
// static ConstructorHelpers::FObjectFinder<UStaticMesh> WeaponTrackMesh(TEXT("/Game/PlaneWar/Meshes/Bullet/PlayerBullet_4"));
// PROPSM->SetStaticMesh(WeaponTrackMesh.Object); }
// break;
//case PROPEnum::Health: {
// static ConstructorHelpers::FObjectFinder<UStaticMesh> HealthMesh(TEXT("/Game/PlaneWar/Meshes/Bullet/PlayerBullet_5"));
// PROPSM->SetStaticMesh(HealthMesh.Object); }
// break;
//case PROPEnum::Shield: {
// static ConstructorHelpers::FObjectFinder<UStaticMesh> ShieldMesh(TEXT("/Game/PlaneWar/Meshes/Bullet/PlayerBullet_6"));
// PROPSM->SetStaticMesh(ShieldMesh.Object); }
// break;
//case PROPEnum::Boom: {
// static ConstructorHelpers::FObjectFinder<UStaticMesh> BoomMesh(TEXT("/Game/PlaneWar/Meshes/Bullet/PlayerBullet_7"));
// PROPSM->SetStaticMesh(BoomMesh.Object); }
// break;
/*default:
break;
}*/
}
void APROP::AfterPickUp()
{
Destroy();
}
// Called every frame
void APROP::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
PROPTimer += DeltaTime;
this->AddActorLocalRotation(FRotator(60 * DeltaTime, 60 * DeltaTime, 60 * DeltaTime));
this->AddActorWorldOffset(FVector(0, cos(2 * PROPTimer), sin(2 * PROPTimer)) * DeltaTime * 300);
}
EnemyWeaponAim.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "EnemyWeaponAim.h"
#include "PlayerAirPlane.h"
#include <Engine.h>
#include <Runtime/Engine/Classes/Kismet/KismetMathLibrary.h>
UEnemyWeaponAim::UEnemyWeaponAim()
{
}
void UEnemyWeaponAim::BeginPlay()
{
PlayerShip = Cast<APlayerAirPlane>(UGameplayStatics::GetPlayerPawn(this, 0));
MyGameMode = Cast<APlaneWarGameMode>(UGameplayStatics::GetGameMode(this));
}
void UEnemyWeaponAim::AttackLevelZero()
{
//SetActorRotation(UKismetMathLibrary::FindLookAtRotation(GetActorLocation(), SpaceShip->GetActorLocation()));
/*if (PlayerShip)
{
UE_LOG(LogTemp, Log, TEXT("Yes..."));
FRotator Tmp_rotator = UKismetMathLibrary::FindLookAtRotation(this->GetComponentLocation(), PlayerShip->GetActorLocation().GetSafeNormal());
UE_LOG(LogTemp, Warning, TEXT("The vector value is: %s"), *Tmp_rotator.ToString());
InstantBullet(this->GetComponentLocation(), Tmp_rotator);
}
else
{
UE_LOG(LogTemp, Log, TEXT("No..."));
InstantBullet(this->GetComponentLocation(), this->GetComponentRotation());
}*/
InstantBullet(this->GetComponentLocation(), this->GetComponentRotation());
}
void UEnemyWeaponAim::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
if (PlayerShip)
{
//UE_LOG(LogTemp, Log, TEXT("yes..."));
this->SetWorldRotation(UKismetMathLibrary::FindLookAtRotation(this->GetComponentLocation(),PlayerShip->GetActorLocation()));
}
else
{
UE_LOG(LogTemp, Log, TEXT("No..."));
}
if (bIsAutoAttack)
{
ManualAttack();
}
}
.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "PlayerAirPlane.h"
#include "PlaneWarGameMode.h"
#include "EnemyWeaponComponent.h"
#include "EnemyWeaponAim.generated.h"
/**
*
*/
UCLASS(ClassGroup = (Custom), meta = (BlueprintSpawnableComponent))
class PLANEWAR_API UEnemyWeaponAim : public UEnemyWeaponComponent
{
GENERATED_BODY()
public:
UEnemyWeaponAim();
APlayerAirPlane* PlayerShip;
APlaneWarGameMode* MyGameMode;
virtual void AttackLevelZero() override;
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
protected:
// Called when the game starts
virtual void BeginPlay() override;
};
AutoGenerate.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "EnemySpawner.generated.h"
class AEnemy;
class UBoxComponent;
class ASpaceShip;
UCLASS()
class SPACESHIPBATTLE_API AEnemySpawner : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AEnemySpawner();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UPROPERTY(EditAnywhere,Category="Enemy")
TSubclassOf<AEnemy> Enemy;
UPROPERTY(VisibleAnywhere,Category="Component")
UBoxComponent* SpawnArea;
FVector GetGenerateLocation();
float MinimumDistanceToPlayer = 1200.0f;
ASpaceShip* SpaceShip;
void SpawnEnemy();
FTimerHandle TimerHandle_Spawn;
float SpawnInterval;
UPROPERTY(EditAnywhere,Category="Spawn")
int MaxEnemyNum;
int CurrentEnemyCount;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
void DecreaseEnemyCount();
};
AutoGenerate.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "Public/EnemySpawner.h"
#include "Components/BoxComponent.h"
#include "Kismet/KismetMathLibrary.h"
#include "Kismet/GameplayStatics.h"
#include "Public/SpaceShip.h"
#include "Engine/World.h"
#include "Public/Enemy.h"
#include "TimerManager.h"
// Sets default values
AEnemySpawner::AEnemySpawner()
{
// 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;
SpawnArea = CreateDefaultSubobject<UBoxComponent>(TEXT("SpawnArea"));
RootComponent = SpawnArea;
SpawnInterval = 2.0f;
MaxEnemyNum = 30;
CurrentEnemyCount = 0;
}
// Called when the game starts or when spawned
void AEnemySpawner::BeginPlay()
{
Super::BeginPlay();
SpaceShip=Cast<ASpaceShip>( UGameplayStatics::GetPlayerPawn(this,0));
GetWorldTimerManager().SetTimer(TimerHandle_Spawn,this,&AEnemySpawner::SpawnEnemy,SpawnInterval,true,0.0f);
}
FVector AEnemySpawner::GetGenerateLocation()
{
float Distance=0;
FVector Location;
while (Distance<MinimumDistanceToPlayer)
{
Location = UKismetMathLibrary::RandomPointInBoundingBox(SpawnArea->Bounds.Origin, SpawnArea->Bounds.BoxExtent);
Distance = (Location - SpaceShip->GetActorLocation()).Size();
}
return Location;
}
void AEnemySpawner::SpawnEnemy()
{
if (SpaceShip->GetBDead() == false&&CurrentEnemyCount<MaxEnemyNum)
{
FActorSpawnParameters SpawnParameters;
GetWorld()->SpawnActor<AEnemy>(Enemy, GetGenerateLocation(), FRotator::ZeroRotator, SpawnParameters);
CurrentEnemyCount++;
}
}
// Called every frame
void AEnemySpawner::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AEnemySpawner::DecreaseEnemyCount()
{
if (CurrentEnemyCount > 0)
{
CurrentEnemyCount--;
UE_LOG(LogTemp,Warning,TEXT("%s"),*FString::SanitizeFloat(CurrentEnemyCount));
}
}
Boom.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Boom.generated.h"
class USphereComponent;
class UParticleSystemComponent;
UCLASS()
class PLANEWAR_API ABoom : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ABoom();
// Called every frame
virtual void Tick(float DeltaTime) override;
UPROPERTY(VisibleAnywhere, Category = "Component")
USphereComponent* CollisionComp;
UPROPERTY(VisibleAnywhere, Category = "Component")
UStaticMeshComponent* BoomSM;
UPROPERTY(VisibleAnywhere, Category = "Component")
UParticleSystem* BoomFx;
FVector StartBoomLocation;
UPROPERTY(EditAnywhere, Category = "Value")
float BoomSpeed;
UPROPERTY(EditAnywhere, Category = "Value")
float ExplotionDistance;
void Move(float DeltaTime);
void StartBoom();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
};
Boom.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "Boom.h"
#include "Components/StaticMeshComponent.h"
#include "Components/SphereComponent.h"
#include "Particles/ParticleSystem.h"
// Sets default values
ABoom::ABoom()
{
// 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;
//
CollisionComp = CreateDefaultSubobject<USphereComponent>(TEXT("CollisionComp"));
RootComponent = CollisionComp;
//
BoomSM = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BoomSM"));
BoomSM->SetupAttachment(RootComponent);
static ConstructorHelpers::FObjectFinder<UStaticMesh> BoomMesh(TEXT("/Game/PlaneWar/Meshes/Bullet/Boom"));
BoomSM->SetStaticMesh(BoomMesh.Object);
//BoomFx = CreateDefaultSubobject<UParticleSystem>(TEXT("BoomFx"));
ExplotionDistance = 500;
BoomSpeed = 200;
}
// Called when the game starts or when spawned
void ABoom::BeginPlay()
{
Super::BeginPlay();
StartBoomLocation= this->GetActorTransform().GetLocation();
}
// Called every frame
void ABoom::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
Move(DeltaTime);
if (FVector::Distance(this->GetActorTransform().GetLocation(), StartBoomLocation)>ExplotionDistance)
{
StartBoom();
}
}
void ABoom::Move(float DeltaTime)
{
AddActorWorldOffset(FVector(0,0,BoomSpeed) * DeltaTime);
}
void ABoom::StartBoom()
{
Destroy();
}
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()
{
}
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/TwinStick/Meshes/TwinStickProjectile.TwinStickProjectile"));
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 = 3000;
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::UpVector * DeltaTime * Speed * EfectX);
}
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 "Camera/CameraComponent.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 = "Fire")
UCameraComponent* CameraComp;
/*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 "Camera/CameraComponent.h"
#include "GameFramework/PlayerController.h"
APlayerAirPlane::APlayerAirPlane()
{
//指定Mesh材质
static ConstructorHelpers::FObjectFinder<UStaticMesh> ShipMesh(TEXT("/Game/TwinStick/Meshes/TwinStickUFO.TwinStickUFO"));
ShipSM->SetStaticMesh(ShipMesh.Object);
// Create a camera...
CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("TopDownCamera"));
CameraComp->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
//实例化一个武器类
//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!"));
if (TmpWeapon)
{
TmpWeapon->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();
}
EnemyAirPlane.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "AirPlane.h"
#include "EnemyWeaponNormal.h"
#include "EnemyAirPlane.generated.h"
/**
*
*/
UCLASS()
class PLANEWAR_API AEnemyAirPlane : public AAirPlane
{
GENERATED_BODY()
public:
AEnemyAirPlane();
virtual void Tick(float DeltaTime) override;
void UnderAttack(float Damage);
void Attack();
void Dead();
UPROPERTY(EditAnywhere, Category = "Fire")
AEnemyWeaponNormal* TmpWeapon;
float EnemyTimer;
private:
void Move(float DeltaTime);
};
EnemyAirPlane.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "math.h"
#include "EnemyWeaponNormal.h"
#include "EnemyAirPlane.h"
AEnemyAirPlane::AEnemyAirPlane()
{
//初始化
EnemyTimer = 0;
moveSpeed = 800;
//指定Mesh材质
static ConstructorHelpers::FObjectFinder<UStaticMesh> ShipMesh(TEXT("/Game/TwinStick/Meshes/EnemyAirPlane.EnemyAirPlane"));
ShipSM->SetStaticMesh(ShipMesh.Object);
TmpWeapon = CreateDefaultSubobject<AEnemyWeaponNormal>(TEXT("Fire"));
}
//敌人移动的方法:获取虚拟轴的输入来确定移动
void AEnemyAirPlane::Move(float DeltaTime)
{
//UE_LOG(LogTemp, Log, TEXT("Enemy Moving..."));
EnemyTimer += DeltaTime;
AddActorLocalOffset(FVector(0, sin(EnemyTimer*2) * moveSpeed, 0) * DeltaTime);
}
void AEnemyAirPlane::Tick(float DeltaTime)
{
//UE_LOG(LogTemp, Log, TEXT("Enemy Ticking..."));
Super::Tick(DeltaTime);
Move(DeltaTime);
}
void AEnemyAirPlane::Attack()
{
UE_LOG(LogTemp, Log, TEXT("AEnemyAirPlane Attacking................"));
if (TmpWeapon)
{
TmpWeapon->Attack();
}
}
void AEnemyAirPlane::Dead()
{
Attack();
this->Destroy();
}
void AEnemyAirPlane::UnderAttack(float Damage)
{
hp -= Damage;
if (hp <= 0)
{
Dead();
}
Attack();
}
Weapon.h
// Fill out your copyright notice in the Description page of Project Settings.
#include "PlayerAirPlane.h"
#include "Engine.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/PlayerController.h"
APlayerAirPlane::APlayerAirPlane()
{
//指定Mesh材质
static ConstructorHelpers::FObjectFinder<UStaticMesh> ShipMesh(TEXT("/Game/TwinStick/Meshes/TwinStickUFO.TwinStickUFO"));
ShipSM->SetStaticMesh(ShipMesh.Object);
// Create a camera...
CameraComp = CreateDefaultSubobject<UCameraComponent>(TEXT("TopDownCamera"));
CameraComp->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
//实例化一个武器类
//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!"));
if (TmpWeapon)
{
TmpWeapon->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();
}
Weapon.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "Weapon.h"
#include "Bullet.h"
#include "BulletHelix.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!"));
APawn* ThePawn = GetWorld()->GetFirstPlayerController()->GetPawn();
GetWorld()->SpawnActor<ABulletHelix>(TmpBullet, ThePawn->GetActorLocation(), FRotator(180, 180, 180));
GetWorld()->SpawnActor<ABulletHelix>(TmpBullet, ThePawn->GetActorLocation(), FRotator(180, 0, 180));
//GetWorld()->SpawnActor<ABulletHelix>(TmpBullet, ThePawn->GetActorLocation(), ThePawn->GetActorRotation());
//GetWorld()->SpawnActor<ABulletHelix>(TmpBullet, ThePawn->GetActorLocation(),FRotator(ThePawn->GetActorRotation().Euler().X, ThePawn->GetActorRotation().Euler().Y,180));
}
BulletHelix.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Bullet.h"
#include "BulletHelix.generated.h"
/**
*
*/
UCLASS()
class PLANEWAR_API ABulletHelix : public ABullet
{
GENERATED_BODY()
public:
void Move(float DeltaTime);
float Timer;
// 声明Tick方法
virtual void Tick(float DeltaTime) override;
};
BulletHelix.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "BulletHelix.h"
#include<math.h>
void ABulletHelix::Move(float DeltaTime)
{
Timer += 8*DeltaTime;
//AddActorWorldOffset(FVector(0,1,1) * DeltaTime * Speed * EffectX);
//AddActorWorldOffset(FVector(0,EffectX * sin(Timer), 1) *DeltaTime* Speed* EffectX);
AddActorLocalOffset(FVector(0, EffectX * sin(Timer), 1) * DeltaTime * Speed * EffectX);
}
// Called every frame
void ABulletHelix::Tick(float DeltaTime)
{
Move(DeltaTime);
}
EnemyBullet.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Bullet.h"
#include "EnemyBullet.generated.h"
/**
*
*/
UCLASS()
class PLANEWAR_API AEnemyBullet : public ABullet
{
GENERATED_BODY()
public:
AEnemyBullet();
// 声明Tick方法
virtual void Tick(float DeltaTime) override;
void Move(float DeltaTime);
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
};
EnemyBullet.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "EnemyBullet.h"
AEnemyBullet::AEnemyBullet()
{
}
void AEnemyBullet::Move(float DeltaTime)
{
AddActorWorldOffset(FVector::UpVector * DeltaTime * Speed * EffectX*-1);
}
void AEnemyBullet::BeginPlay()
{
Super::BeginPlay();
Speed = 100;
Damage = 1;
bIsEnemy = true;
EffectX = 1;
}
// Called every frame
void AEnemyBullet::Tick(float DeltaTime)
{
Move(DeltaTime);
//UE_LOG(LogTemp, Log, TEXT("The Damage: %f"),this->Damage);
}