Skip to content

Commit

Permalink
#13 Added Extent to Linear Door Actor
Browse files Browse the repository at this point in the history
  • Loading branch information
xthebat committed Nov 15, 2023
1 parent cbf853f commit 014a8c6
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 8 deletions.
41 changes: 34 additions & 7 deletions Source/Cloud9/Environment/Cloud9LinearDoor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@
#include "Cloud9LinearDoor.h"

#include "Cloud9/Cloud9.h"
#include "Cloud9/Tools/Cloud9ToolsLibrary.h"
#include "Cloud9/Tools/Extensions/AActor.h"

ACloud9LinearDoor::ACloud9LinearDoor()
{
PrimaryActorTick.bCanEverTick = true;

bIsOpen = false;
Direction = FVector::ZeroVector;
Speed = 1.0f;
Distance = 0.0f;
Direction = EDirection::Right;

bIsMoving = false;
DirectionVector = FVector::ZeroVector;

SetMobility(EComponentMobility::Movable);
}
Expand Down Expand Up @@ -65,15 +69,38 @@ void ACloud9LinearDoor::BeginPlay()
{
Super::BeginPlay();

let Sign = bIsOpen ? -1 : 1;
OriginPosition = GetActorLocation();
TargetPosition = OriginPosition + Sign * Direction * Distance;
let Transform = GetActorTransform();
let LocalVector = this | EAActor::ToDirectionVector(Direction);
DirectionVector = Transform.InverseTransformVector(LocalVector);
DirectionVector.Normalize();

if (Speed == 0.0f || Distance == 0.0f || Direction.IsZero())
if (DirectionVector.IsZero())
{
SetActorTickEnabled(false);
UE_LOG(LogCloud9, Warning, TEXT("Door %s can't moved due to incorrect properties"), *GetName());
UE_LOG(LogCloud9, Error, TEXT("Door '%s' can't moved due to incorrect direction vector"), *GetName());
}

if (Speed <= 0.0f)
{
SetActorTickEnabled(false);
UE_LOG(LogCloud9, Warning, TEXT("Door '%s' can't moved due to speed <= 0.0f"), *GetName());
}

if (Distance <= 0.0f)
{
var Origin = FVector::ZeroVector;
var Extents = FVector::ZeroVector;
GetActorBounds(true, Origin, Extents, true);
Distance = (2.0f * Extents * DirectionVector).Size();
}

let ActualDistance = Distance - Extent;

UE_LOG(LogCloud9, Display, TEXT("Door '%s' distance = '%f'"), *GetName(), ActualDistance);

let Sign = bIsOpen ? -1 : 1;
OriginPosition = Transform.GetLocation();
TargetPosition = OriginPosition + Sign * DirectionVector * ActualDistance;
}

void ACloud9LinearDoor::Tick(float DeltaTime)
Expand All @@ -84,7 +111,7 @@ void ACloud9LinearDoor::Tick(float DeltaTime)
{
var NewPosition = GetActorLocation() + Shift * DeltaTime;

if (FVector::DistSquared(OriginPosition, GetActorLocation()) > FMath::Square(Distance))
if (FVector::DistSquared(OriginPosition, GetActorLocation()) > FMath::Square(Distance - Extent))
{
bIsOpen = !bIsOpen;
bIsMoving = false;
Expand Down
11 changes: 10 additions & 1 deletion Source/Cloud9/Environment/Cloud9LinearDoor.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
#pragma once

#include "CoreMinimal.h"
#include "Cloud9/Tools/Cloud9Direction.h"
#include "Engine/StaticMeshActor.h"
#include "Cloud9LinearDoor.generated.h"


UCLASS()
class CLOUD9_API ACloud9LinearDoor : public AStaticMeshActor
{
Expand Down Expand Up @@ -56,6 +58,7 @@ class CLOUD9_API ACloud9LinearDoor : public AStaticMeshActor
FVector OriginPosition;
FVector TargetPosition;
FVector Shift;
FVector DirectionVector;

/**
* Whether door is currently open or closed
Expand All @@ -67,7 +70,7 @@ class CLOUD9_API ACloud9LinearDoor : public AStaticMeshActor
* Direction where to move door
*/
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Door, meta = (AllowPrivateAccess = "true"))
FVector Direction;
EDirection Direction;

/**
* Door moving speed when open or close
Expand All @@ -80,4 +83,10 @@ class CLOUD9_API ACloud9LinearDoor : public AStaticMeshActor
*/
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Door, meta = (AllowPrivateAccess = "true"))
float Distance;

/**
* Extent when move distance to make door not open on full size (if distance not specified)
*/
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Door, meta = (AllowPrivateAccess = "true"))
float Extent;
};
35 changes: 35 additions & 0 deletions Source/Cloud9/Tools/Cloud9Direction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2023 Alexei Gladkikh
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#pragma once

UENUM(BlueprintType)
enum class EDirection : uint8
{
Up UMETA(DisplayName = "Up"),
Down UMETA(DisplayName = "Down"),
Left UMETA(DisplayName = "Left"),
Right UMETA(DisplayName = "Right"),
Forward UMETA(DisplayName = "Forward"),
Backward UMETA(DisplayName = "Backward"),
};
34 changes: 34 additions & 0 deletions Source/Cloud9/Tools/Cloud9ToolsLibrary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,38 @@ FVector UCloud9ToolsLibrary::VInterpTo(
};
}

FVector UCloud9ToolsLibrary::DirectionToActorVector(const AActor* Actor, const EDirection Direction)
{
if (Direction == EDirection::Right)
{
return Actor->GetActorRightVector();
}

if (Direction == EDirection::Left)
{
return -Actor->GetActorRightVector();
}

if (Direction == EDirection::Up)
{
return Actor->GetActorUpVector();
}

if (Direction == EDirection::Down)
{
return -Actor->GetActorUpVector();
}

if (Direction == EDirection::Forward)
{
return Actor->GetActorForwardVector();
}

if (Direction == EDirection::Backward)
{
return -Actor->GetActorForwardVector();
}

UE_LOG(LogCloud9, Fatal, TEXT("Invalid value for Actor = '%s' Direction = '%d'"), *Actor->GetName(), Direction);
return {};
}
3 changes: 3 additions & 0 deletions Source/Cloud9/Tools/Cloud9ToolsLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#pragma once

#include "CoreMinimal.h"
#include "Cloud9Direction.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Kismet/GameplayStatics.h"
#include "Cloud9ToolsLibrary.generated.h"
Expand Down Expand Up @@ -91,4 +92,6 @@ class CLOUD9_API UCloud9ToolsLibrary : public UBlueprintFunctionLibrary
float DeltaTime,
const FVector InterpSpeed);

UFUNCTION(BlueprintCallable)
static FVector DirectionToActorVector(const AActor* Actor, const EDirection Direction);
};
31 changes: 31 additions & 0 deletions Source/Cloud9/Tools/Extensions/AActor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2023 Alexei Gladkikh
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#include "AActor.h"

#include "Cloud9/Tools/Cloud9ToolsLibrary.h"

FVector EAActor::ToDirectionVector::operator()(const AActor* Actor) const
{
return UCloud9ToolsLibrary::DirectionToActorVector(Actor, Direction);
}
37 changes: 37 additions & 0 deletions Source/Cloud9/Tools/Extensions/AActor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2023 (c)
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

#pragma once
#include "Cloud9/Cloud9.h"

enum class EDirection : uint8;

namespace EAActor
{
struct ToDirectionVector : TOperator<ToDirectionVector>
{
explicit ToDirectionVector(EDirection Direction) : Direction(Direction) { }

FVector operator()(const AActor* Actor) const;

private:
EDirection Direction;
};
}

0 comments on commit 014a8c6

Please sign in to comment.