Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@danielkirwan
Created December 6, 2016 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielkirwan/1a9bd31439f78c3e3d9d5cc959cb475d to your computer and use it in GitHub Desktop.
Save danielkirwan/1a9bd31439f78c3e3d9d5cc959cb475d to your computer and use it in GitHub Desktop.
// Fill out your copyright notice in the Description page of Project Settings.
#include "BuildingEscape.h"
#include "Grabber.h"
#define OUT
// Sets default values for this component's properties
UGrabber::UGrabber()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
bWantsBeginPlay = true;
PrimaryComponentTick.bCanEverTick = true;
}
// Called when the game starts
void UGrabber::BeginPlay()
{
Super::BeginPlay();
FindPhysicsHandleComponent();
SetupInputComponent();
}
///Look for attched handle
void UGrabber::FindPhysicsHandleComponent() {
PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
if (PhysicsHandle == nullptr) {
UE_LOG(LogTemp, Error, TEXT("No component found on %s"), *GetOwner()->GetName());
}
}
/// Lok for attache input component (Only appears at run time)
void UGrabber::SetupInputComponent()
{
InputComponent = GetOwner()->FindComponentByClass<UInputComponent>();
if (InputComponent) {
UE_LOG(LogTemp, Warning, TEXT("component found on %s"), *GetOwner()->GetName());
InputComponent->BindAction("Grab", IE_Pressed, this, &UGrabber::Grab);
InputComponent->BindAction("Grab", IE_Released, this, &UGrabber::Release);
}
else {
UE_LOG(LogTemp, Error, TEXT("No input component found on %s"), *GetOwner()->GetName());
}
}
void UGrabber::Grab() {
///Line trace and see if we reach any actors with physics body collison channel set
auto HitResult = GetFirstPhysicsBodyInReach();
auto ComponentToGrab = HitResult.GetComponent();// point of object to grab
auto ActorHit = HitResult.GetActor();
///If we hit something then attach a physics handle
if (ActorHit) {
// attach physics handle
PhysicsHandle->GrabComponent(
ComponentToGrab,
NAME_None, //no bones needed
ComponentToGrab->GetOwner()->GetActorLocation(),
true); // allow rotation
}
}
void UGrabber::Release() {
//TODO release physics handle
PhysicsHandle->ReleaseComponent();
}
/// Called every frame
void UGrabber::TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction )
{
Super::TickComponent( DeltaTime, TickType, ThisTickFunction );
//if the physics handle is attached
if (PhysicsHandle->GrabbedComponent) {
//then move the object we're holding
PhysicsHandle->SetTargetLocation(GetReachLineEnd());
}
}
const FHitResult UGrabber::GetFirstPhysicsBodyInReach()
{
///Draw a red trace in the world to visualize the players sight
/*DrawDebugLine(
GetWorld(),
PlayerViewpointLocation,
LineTraceEnd,
FColor(255, 0, 0),
false,
0.f,
0,
10.f
);*/
/// Ray-cast/Line trace out to reach distance
FHitResult HitResult;
FCollisionQueryParams TraceParameters(FName(TEXT("")), false, GetOwner());
GetWorld()->LineTraceSingleByObjectType(
OUT HitResult,
GetReachLineStart(),
GetReachLineEnd(),
FCollisionObjectQueryParams(ECollisionChannel::ECC_PhysicsBody),
TraceParameters
);
///// see what we hit
//AActor * ActorHit = Hit.GetActor();
//if (ActorHit) {
// UE_LOG(LogTemp, Warning, TEXT("Line trace hit: %s"), *(ActorHit->GetName()));
//}
return HitResult;
}
FVector UGrabber::GetReachLineStart() {
FVector PlayerViewpointLocation;
FRotator PlayerViewpointRotation;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
OUT PlayerViewpointLocation,
OUT PlayerViewpointRotation);
return PlayerViewpointLocation;
}
FVector UGrabber::GetReachLineEnd(){
FVector PlayerViewpointLocation;
FRotator PlayerViewpointRotation;
GetWorld()->GetFirstPlayerController()->GetPlayerViewPoint(
OUT PlayerViewpointLocation,
OUT PlayerViewpointRotation);
return PlayerViewpointLocation + PlayerViewpointRotation.Vector() * Reach;
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "Components/ActorComponent.h"
#include "Grabber.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BUILDINGESCAPE_API UGrabber : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UGrabber();
// Called when the game starts
virtual void BeginPlay() override;
// Called every frame
virtual void TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) override;
private:
// How far the player can see
float Reach = 100.f;
UPhysicsHandleComponent* PhysicsHandle = nullptr;
UInputComponent* InputComponent = nullptr;
//RayCast and Grab whats in reach
void Grab();
// called when grab is released
void Release();
// Find attached physics
void FindPhysicsHandleComponent();
//Setup (assumed) input component
void SetupInputComponent();
// Return hit for first physics body in reach
const FHitResult GetFirstPhysicsBodyInReach();
// returns current end of reach line
FVector GetReachLineEnd();
// returns current start of reach line
FVector GetReachLineStart();
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "BuildingEscape.h"
#include "OpenDoor.h"
#define OUT
// Sets default values for this component's properties
UOpenDoor::UOpenDoor()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
bWantsBeginPlay = true;
PrimaryComponentTick.bCanEverTick = true;
// ...
}
// Called when the game starts
void UOpenDoor::BeginPlay()
{
Super::BeginPlay();
Owner = GetOwner();
// Finds the default pawn that you move
// ActorThatOpens = GetWorld()->GetFirstPlayerController()->GetPawn();
}
void UOpenDoor::OpenDoor()
{
//Set the door rotation
Owner->SetActorRotation(FRotator(0.f, OpenAngle, 0.f));
}
void UOpenDoor::CloseDoor()
{
//Set the door rotation back to closed
Owner->SetActorRotation(FRotator(0.f, -90.f, 0.f));
}
// Called every frame
void UOpenDoor::TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction )
{
Super::TickComponent( DeltaTime, TickType, ThisTickFunction );
// Poll the trigger volume every frame
if (GetTotalMassOfActorsOnPlate() > 30.f) //TODO make into a parameter
{
OpenDoor();
LastDoorOpenTime = GetWorld()->GetTimeSeconds();
}
// Check if its time to close the door
if (GetWorld()->GetTimeSeconds() - LastDoorOpenTime > DoorCloseDelay) {
CloseDoor();
}
}
float UOpenDoor::GetTotalMassOfActorsOnPlate() {
float TotalMass = 0.f;
//Find all the overlapping actors
TArray<AActor*> OverlappingActors;
PressurePlate->GetOverlappingActors(OUT OverlappingActors);
//iterate through them adding their masses
for (const auto& Actor : OverlappingActors) {
TotalMass += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();
UE_LOG(LogTemp, Warning, TEXT("%s on Pressure plate"), *Actor->GetName());
}
return TotalMass;
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "Components/ActorComponent.h"
#include "OpenDoor.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BUILDINGESCAPE_API UOpenDoor : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UOpenDoor();
// Called when the game starts
virtual void BeginPlay() override;
void OpenDoor();
void CloseDoor();
// Called every frame
virtual void TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) override;
private:
UPROPERTY(VisibleAnywhere)
float OpenAngle = -160.f;
UPROPERTY(EditAnywhere)
ATriggerVolume* PressurePlate;
UPROPERTY(EditAnywhere)
float DoorCloseDelay = 1.0f;
float LastDoorOpenTime;
// AActor* ActorThatOpens; // rememmber pawns inherits from actor
AActor* Owner = GetOwner(); //The owning door
//retuurns total mass in kg
float GetTotalMassOfActorsOnPlate();
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "BuildingEscape.h"
#include "PositionReport.h"
// Sets default values for this component's properties
UPositionReport::UPositionReport()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don't need them.
bWantsBeginPlay = true;
PrimaryComponentTick.bCanEverTick = true;
}
// Called when the game starts
void UPositionReport::BeginPlay()
{
Super::BeginPlay();
FString ObjectName = GetOwner()->GetName();
FString ObjectPos = GetOwner()->GetTransform().GetLocation().ToString();
UE_LOG(LogTemp, Warning, TEXT("%s is at %s"), *ObjectName, *ObjectPos);
}
// Called every frame
void UPositionReport::TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction )
{
Super::TickComponent( DeltaTime, TickType, ThisTickFunction );
// ...
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "Components/ActorComponent.h"
#include "PositionReport.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BUILDINGESCAPE_API UPositionReport : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UPositionReport();
// Called when the game starts
virtual void BeginPlay() override;
// Called every frame
virtual void TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) override;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment