diff --git a/RoomEscape/Content/DefaultPawn_BP.uasset b/RoomEscape/Content/DefaultPawn_BP.uasset index c75d02a..1b7cdd0 100644 Binary files a/RoomEscape/Content/DefaultPawn_BP.uasset and b/RoomEscape/Content/DefaultPawn_BP.uasset differ diff --git a/RoomEscape/Source/RoomEscape/OpenDoor.cpp b/RoomEscape/Source/RoomEscape/OpenDoor.cpp index f558f84..74efcb0 100644 --- a/RoomEscape/Source/RoomEscape/OpenDoor.cpp +++ b/RoomEscape/Source/RoomEscape/OpenDoor.cpp @@ -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 OverlappingActors; + + //Find all overlapping actors. + PressurePlate->GetOverlappingActors( + OUT OverlappingActors + ); + //iterate through finding masses. + for (const auto *Actor : OverlappingActors) { + UPrimitiveComponent *Component = Actor->FindComponentByClass(); + 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; } diff --git a/RoomEscape/Source/RoomEscape/OpenDoor.h b/RoomEscape/Source/RoomEscape/OpenDoor.h index b9e07da..419c067 100644 --- a/RoomEscape/Source/RoomEscape/OpenDoor.h +++ b/RoomEscape/Source/RoomEscape/OpenDoor.h @@ -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(); + };