-
Notifications
You must be signed in to change notification settings - Fork 0
/
UFO.cpp
111 lines (90 loc) · 3.46 KB
/
UFO.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
// Fill out your copyright notice in the Description page of Project Settings.
#include "UFO.h"
#include "Components/StaticMeshComponent.h"
#include "Components/SphereComponent.h"
#include "../../Characters/Aliens/Character/AlienCharacter.h"
#include "../../Game_Rules/Hivemind_GameState.h"
#include "Kismet/GameplayStatics.h"
// Sets default values
AUFO::AUFO()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
PrimaryActorTick.bStartWithTickEnabled = false;
//Setup trigger
TriggerEscape = CreateDefaultSubobject<USphereComponent>("TriggerEscape");
TriggerEscape->SetSphereRadius(500.f);
TriggerEscape->SetCollisionEnabled(ECollisionEnabled::NoCollision);
TriggerEscape->SetCollisionResponseToAllChannels(ECR_Ignore);
TriggerEscape->SetCollisionResponseToChannel(ECC_Pawn, ECR_Overlap);
SetRootComponent(TriggerEscape);
//Setup mesh
MeshComp = CreateDefaultSubobject<UStaticMeshComponent>("StaticMesh");
MeshComp->SetupAttachment(TriggerEscape);
MeshComp->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
MeshComp->SetCollisionResponseToAllChannels(ECR_Block);
MeshComp->SetCollisionResponseToChannel(ECC_Pawn, ECR_Block);
//Setup enter
{
FScriptDelegate ScriptDelegate{};
ScriptDelegate.BindUFunction(this, L"OnTriggerEnter");
TriggerEscape->OnComponentBeginOverlap.AddUnique(ScriptDelegate);
}
//Setup exit
{
FScriptDelegate ScriptDelegate{};
ScriptDelegate.BindUFunction(this, L"OnTriggerExit");
TriggerEscape->OnComponentEndOverlap.AddUnique(ScriptDelegate);
}
}
// Called when the game starts or when spawned
void AUFO::BeginPlay()
{
Super::BeginPlay();
}
void AUFO::OnTriggerEnter_Implementation(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
//When a player enters, increase amount
AAlienCharacter* const AlienChar = Cast<AAlienCharacter>(OtherActor);
if (AlienChar)
{
UE_LOG(LogTemp, Warning, L"Alien entered the UFO trigger");
AHivemind_GameState* const State = Cast<AHivemind_GameState>(UGameplayStatics::GetGameState(this));
if (State)
{
State->IncreaseCurrentAmountEscapingPlayers(1);
}
}
}
void AUFO::OnTriggerExit_Implementation(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
//When a player exits, decrease amount
AAlienCharacter* const AlienChar = Cast<AAlienCharacter>(OtherActor);
if (AlienChar)
{
UE_LOG(LogTemp, Warning, L"Alien left the UFO trigger");
AHivemind_GameState* const State = Cast<AHivemind_GameState>(UGameplayStatics::GetGameState(this));
if (State)
{
State->IncreaseCurrentAmountEscapingPlayers(-1);
}
}
}
// Called every frame
void AUFO::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
FVector AUFO::GetBeamLocation() const
{
//Determine the offset transform (only location change)
FTransform OffsetTransform = FTransform::Identity;
OffsetTransform.SetLocation(BeamLocation);
//Combine transforms to obtain final beam location
return (OffsetTransform * GetActorTransform()).GetLocation();
}
void AUFO::ToggleTriggerEscape(bool bValue)
{
//Enable/disable depending on input
TriggerEscape->SetCollisionEnabled(bValue ? ECollisionEnabled::QueryOnly : ECollisionEnabled::NoCollision);
}