door opens!!!

This commit is contained in:
Tyrel Souza 2018-02-23 00:23:47 -05:00
parent b7be0494d7
commit 1c59557023
3 changed files with 32 additions and 5 deletions

View File

@ -2,8 +2,11 @@
#include "OpenDoor.h"
#include "Engine/World.h"
#include "Components/PrimitiveComponent.h"
#include "GameFramework/Actor.h" // See auto completes
#define OUT
// Sets default values for this component's properties
UOpenDoor::UOpenDoor()
@ -21,17 +24,37 @@ void UOpenDoor::BeginPlay()
Super::BeginPlay();
OwningDoor = GetOwner();
ActorThatOpens = GetWorld()->GetFirstPlayerController()->GetPawn();
}
//Returns total mass in Kg
float UOpenDoor::GetTotalMassOfActorsOnPlate()
{
float TotalMass = 0.0f;
TArray<AActor*> OverlappingActors;
//Find all overlapping actors.
PressurePlate->GetOverlappingActors(
OUT OverlappingActors
);
//iterate through finding masses.
for (const auto *Actor : OverlappingActors) {
UPrimitiveComponent *Component = Actor->FindComponentByClass<UPrimitiveComponent>();
TotalMass += Component->GetMass();
UE_LOG(LogTemp, Warning, TEXT("MASS %s"), *Actor->GetName());
}
return TotalMass;
}
void UOpenDoor::OpenDoor() {
FString Rot = OwningDoor->GetTransform().GetRotation().GetAxisZ().ToString();
OwningDoor->SetActorRotation(FRotator(0.0f, OpenAngle, 0.0f));
}
void UOpenDoor::CloseDoor() {
FString Rot = OwningDoor->GetTransform().GetRotation().GetAxisZ().ToString();
OwningDoor->SetActorRotation(FRotator(0.0f, 0.0f, 0.0f));
}
@ -42,7 +65,7 @@ void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorCompon
float CurSeconds = GetWorld()->GetTimeSeconds();
// poll the TriggerVolume every frame
// if ActorThatOpens is in the volume, open door
if (PressurePlate->IsOverlappingActor(ActorThatOpens)) {
if (GetTotalMassOfActorsOnPlate() > TriggerMass) {
OpenDoor();
LastDoorOpenTime = CurSeconds;
}

View File

@ -29,6 +29,9 @@ private:
UPROPERTY(EditAnywhere)
float OpenAngle = -90.f;
UPROPERTY(EditAnywhere)
float TriggerMass = 50.f;
UPROPERTY(EditAnywhere)
ATriggerVolume* PressurePlate;
@ -37,8 +40,9 @@ private:
float LastDoorOpenTime;
AActor* ActorThatOpens;
AActor* OwningDoor;
float GetTotalMassOfActorsOnPlate();
};