-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemy.cpp
More file actions
69 lines (55 loc) · 1.93 KB
/
Copy pathEnemy.cpp
File metadata and controls
69 lines (55 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Fill out your copyright notice in the Description page of Project Settings.
#include "Enemy.h"
#include "Components/SphereComponent.h"
#include "Kismet/GameplayStatics.h"
#include "SpaceShip.h"
#include "Kismet/KismetMathLibrary.h"
#include "Particles/ParticleSystem.h"
#include "ShipGameMode.h"
#include "EnemySpawner.h"
// Sets default values
AEnemy::AEnemy()
{
// 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 = CreateAbstractDefaultSubobject<USphereComponent>(TEXT("CollisionComp"));
RootComponent = CollisionComp;
ShipMS = CreateAbstractDefaultSubobject<UStaticMeshComponent>(TEXT("ShipMS"));
ShipMS->SetupAttachment(RootComponent);
}
// Called when the game starts or when spawned
void AEnemy::BeginPlay()
{
Super::BeginPlay();
SpaceShip = Cast<ASpaceShip>(UGameplayStatics::GetPlayerPawn(this, 0));
SetColor();
MyGameMode= Cast<AShipGameMode>( UGameplayStatics::GetGameMode(this));
TArray<AActor*>EnemySpawnerArray;
UGameplayStatics::GetAllActorsOfClass(this, AEnemySpawner::StaticClass(), EnemySpawnerArray);
EnemySpawner = Cast<AEnemySpawner>(EnemySpawnerArray[0]);
}
void AEnemy::MoveTowardsPlayer()
{
FVector Direction = (SpaceShip->GetActorLocation() - GetActorLocation()).GetSafeNormal();
AddActorWorldOffset(Direction * Speed * FApp::GetDeltaTime(), true);
SetActorRotation(UKismetMathLibrary::FindLookAtRotation(GetActorLocation(), SpaceShip->GetActorLocation()));
}
void AEnemy::OnDeath()
{
MyGameMode->IncreaseScore();
EnemySpawner->DecreaseEnemyCount();
SpawnExplosion();
Destroy();
}
// Called every frame
void AEnemy::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if(!SpaceShip->GetBDead())
MoveTowardsPlayer();
}
// Called to bind functionality to input
void AEnemy::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}