-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefender.cpp
137 lines (113 loc) · 3.72 KB
/
Defender.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "Defender.h"
#include "GameFramework/FloatingPawnMovement.h"
#include "Components/StaticMeshComponent.h"
#include "Components/BoxComponent.h"
#include "Bullet.h"
#include "math.h"
ADefender::ADefender()
{
PrimaryActorTick.bCanEverTick = false;
SM_Defender = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("SM Defender"));
RootComponent = SM_Defender;
SM_Defender->SetEnableGravity(false);
SM_Defender->SetConstraintMode(EDOFMode::XZPlane);
SM_Defender->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
SM_Defender->SetCollisionProfileName(TEXT("PhysicsActor"));
Box_Collision = CreateDefaultSubobject<UBoxComponent>(TEXT("Box Component"));
Box_Collision->SetBoxExtent(FVector(15.0f, 15.0f, 15.0f));
FloatingMovement = CreateDefaultSubobject<UFloatingPawnMovement>(TEXT("Floating Pawn Movement"));
}
void ADefender::BeginPlay()
{
Super::BeginPlay();
Reset();
Box_Collision->OnComponentBeginOverlap.AddDynamic(this, &ADefender::OnOverlapBegin);
}
void ADefender::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void ADefender::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
}
void ADefender::MoveCircular(float AxisValue)
{
CurrentAngle += RotationSpeed * AxisValue * GetWorld()->GetDeltaSeconds();;
Angle = CurrentAngle * RadToDeg - 90.0f;
if ((CurrentAngle > 2 * M_PI) || (CurrentAngle < -2 * M_PI)) {
CurrentAngle = 0.0f;
Angle = 180.0f;
}
float xPosition = CenterPointX + cos(CurrentAngle) * 90.0;
float yPosition = CenterPointY + sin(CurrentAngle) * 90.0;
if (AxisValue != 0) {
SetActorLocationAndRotation(FVector(xPosition, yPosition, -110.0f), FRotator(0.0f, Angle, 0.0f));
}
}
void ADefender::Shoot()
{
if (ProjectileClass)
{
FVector CameraLocation;
FRotator CameraRotation;
GetActorEyesViewPoint(CameraLocation, CameraRotation);
MuzzleOffset.Set(420.0f, 0.0f, -140.0f);
FVector MuzzleLocation = CameraLocation + FTransform(FRotator(0.0f, (Angle + 90.0f), 0.0f)).TransformVector(MuzzleOffset);
FRotator MuzzleRotation = FRotator(0.0f, (Angle + 90.0f), 0.0f);
UWorld* World = GetWorld();
if (World)
{
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = GetInstigator();
ABullet* Bullet = World->SpawnActor<ABullet>(ProjectileClass, MuzzleLocation, MuzzleRotation, SpawnParams);
if (Bullet)
{
FVector LaunchDirection = MuzzleRotation.Vector();
Bullet->FireInDirection(LaunchDirection);
}
}
}
}
void ADefender::Reset()
{
CurrentAngle = InitialRotationAngle;
DefenderPoints = 0;
Angle = 180.0f;
SetActorLocationAndRotation(FVector(50.0f, 140.0f, -110.0f), FRotator(0.0f, Angle, 0.0f));
}
void ADefender::AddDefenderPoints()
{
DefenderPoints++;
UE_LOG(LogTemp, Warning, TEXT("Defender Points: %d"), DefenderPoints);
}
void ADefender::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor,
UPrimitiveComponent* OtherComp, int32 OtherBodyIndexType, bool bFromSweep,
const FHitResult& SweepResult)
{
if (OtherActor->ActorHasTag("Bullet")) {
UE_LOG(LogTemp, Warning, TEXT("Defender hitten!"));
DamageDefender(25);
OtherActor->Destroy();
}
if (OtherActor->ActorHasTag("Invader") || OtherActor->ActorHasTag("Invader2") || OtherActor->ActorHasTag("Asteroid")) {
UE_LOG(LogTemp, Warning, TEXT("Defender crushed!"));
DamageDefender(100);
OtherActor->Destroy();
}
}
void ADefender::DamageDefender(int damagePoints)
{
DefenderLifePoints -= damagePoints;
if (DefenderLifePoints <= 0) {
UE_LOG(LogTemp, Warning, TEXT("You lose! Your Points: %d"), DefenderPoints);
}
else {
UE_LOG(LogTemp, Warning, TEXT("Defender Life Points: %d"), DefenderLifePoints);
}
}
int ADefender::GetDefenderLifePoints()
{
return DefenderLifePoints;
}