Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #pragma once
- #include "GameFramework/Actor.h"
- #include "TargetComponent.h"
- #include "SensorMeshComponent.h"
- #include "DataNode.h"
- #include "ActionManagerInterface.h"
- #include "RRNodeTarget.h"
- #include "RRLevelDataAsset.h"
- #include "RRStaticFigureNode.generated.h"
- UENUM(BlueprintType)// The way in which the node becomes activated in game
- enum class ENodeActivationType : uint8
- {
- // Becomes active as soon as its on the players screen, no matter how far away
- OnScreen UMETA(DisplayName = "OnScreen"),
- // Becomes active when its on screen and in range of the player
- OnScreenInRange UMETA(DisplayName = "OnScreenInRange"),
- // Becomes active when the player looks straight at it and its in range
- LookedAtInRange UMETA(DisplayName = "LookedAtInRange"),
- // Becomes active when the player is a certain distance away, regardless of whether its looked at or on screen
- Distance UMETA(DisplayName = "Distance")
- };
- UCLASS()
- class RRDEVELOP_API ARRStaticFigureNode : public ACharacter, public IActionManagerInterface
- {
- GENERATED_BODY()
- public:
- // Sets default values for this actor's properties
- ARRStaticFigureNode(const FObjectInitializer& ObjectInitializer);
- UPROPERTY(EditAnywhere, Category = "StaticFigureNode")
- class UBehaviorTree* NodeBehavior;
- // Called when the game starts or when spawned
- virtual void BeginPlay() override;
- // Called every frame
- virtual void Tick( float DeltaSeconds ) override;
- UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "StaticFigureNode", meta = (ToolTip = "SpringArm component used to raise or lower the sensorcomponent if necessary"))
- class USpringArmComponent* SensorPositionComponent;
- UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "StaticFigureNode", meta = (ToolTip = "Large interaction sphere used to register the node with an approaching player, or activate the node"))
- class USphereComponent* InteractionSphere;
- UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "StaticFigureNode", meta = (ToolTip = "Optional subobject where the BodyMeshes are loaded/made visible"))
- class USensorMeshComponent* BodyMesh;
- static FName StaticBodyMeshName;
- UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "StaticFigureNode", meta = (ToolTip = "Resizeable sensor that is used to activate node by lookAt"))
- class USensorMeshComponent* StaticFigureSensor;
- UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "StaticFigureNode", meta = (ToolTip = "The next target that the node should progress to"))
- class ARRNodeTarget* NextTargetInSequence;
- UFUNCTION(BlueprintCallable, Category = "StaticFigureNode")
- virtual FName GetNameViaInterface() override;
- virtual void SpeakerIsTooFar(AActor* Speaker) override;
- UFUNCTION(BlueprintCallable, Category = "StaticFigureNode")
- virtual EObjectType GetObjectType() override;
- UFUNCTION(BlueprintCallable, Category = "StaticFigureNode")
- virtual void UpdateCharacterData(FStringAssetReference NewSoundCue, FStringAssetReference NewAnimMontage) override;
- UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "StaticFigureNode")
- FName NodeName;
- virtual void PostInitializeComponents() override;
- UFUNCTION(BlueprintCallable, Category = "StaticFigureNode")
- void MinimizeSensorMesh();
- // Specific function to destroy figure and cue sequences etc, called from Act
- UFUNCTION(BlueprintCallable, Category = "StaticFigureNode")
- void CallDestroyFigureNode();
- UFUNCTION(BlueprintCallable, Category = "StaticFigureNode")
- bool IsInView();
- UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "StaticFigureNode", meta = (ToolTip = "The way in which the node becomes active"))
- ENodeActivationType ActivationType;
- UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "StaticFigureNode", meta = (ToolTip = "If activation type is distance, this value must be set"))
- float ActivationDistance;
- UFUNCTION(BlueprintCallable, Category = "StaticFigureNode")
- int32 GetNumInRangePlayer();
- UFUNCTION(BlueprintCallable, Category = "StaticFigureNode")
- void LoadNodeMesh();
- UFUNCTION(BlueprintCallable, Category = "StaticFigureNode")
- void ExecuteCurrentNodeTarget();
- UFUNCTION(BlueprintCallable, Category = "StaticFigureNode")
- void AddToInRange(APawn* Incoming);
- UFUNCTION(BlueprintCallable, Category = "StaticFigureNode")
- void RemoveFromInRange(APawn* Outgoing);
- UFUNCTION(BlueprintCallable, Category = "StaticFigureNode")
- void LoadNodeMeshData();
- private:
- UFUNCTION(BlueprintCallable, Category = "StaticFigureNode")
- void StartSensorLookAt(class USensorMeshComponent* HitSensor);
- UFUNCTION(BlueprintCallable, Category = "StaticFigureNode")
- void FinishSensorLookAt(class USensorMeshComponent* HitSensor);
- UFUNCTION(BlueprintCallable, Category = "StaticFigureNode")
- void StartFigureMeshLookAt(class USensorMeshComponent* HitSensor);
- UFUNCTION(BlueprintCallable, Category = "StaticFigureNode")
- void FinishFigureMeshLookAt(class USensorMeshComponent* HitSensor);
- FTimerHandle Load_Asset_Data;
- TArray<FQueryKVPair> NodeHistory;
- TArray<APawn*> InRangePlayers;
- EObjectType ObjectType;
- // Node target bool activates per tick events such as delta counting etc
- bool bFigureMeshIsLoaded;
- // Mesh is loaded set by async loading delegate
- bool bMeshTimerIsOn;
- bool bRunSequenceIsOn;
- float LifeTime;// when spawned time == lifetime the figure is unloaded
- //bool IsCounting;// Sets whether or not the spawned time is being updated
- float RunTime;// How long the figure has been spawned (loaded) for
- EStaticFigureType CurrentFigureType;
- // Array to hold mesh data for all of the meshes loaded by the current NodeTarget
- TArray<FRRLevelStaticMeshInfo> NodeTargetMeshData;
- // Array to hold mesh data for all of the meshes that can be loaded by this node
- TArray<FRRLevelStaticMeshInfo> NodeMeshData;
- FStringAssetReference BodyMeshToLoad;
- FStringAssetReference MinimizedMeshToLoad;
- FStringAssetReference MaximizedMeshToLoad;
- void DoAsyncMeshLoad();
- void InitStaticMesh(class UStaticMeshComponent* StaticMeshPointer, bool AttachToParent);
- void UnloadNodeMesh();
- UStaticMesh* MinimizedSensor;
- };
- // Fill out your copyright notice in the Description page of Project Settings.
- #include "RRDevelop.h"
- #include "RRDevelopGameMode.h"
- #include "RRGameSingleton.h"
- #include "RRStreamingLevelScriptActor.h"
- #include "RRDevelopGameMode.h"
- #include "RRFigureNodeAIController.h"
- #include "RRStaticFigureNode.h"
- FName ARRStaticFigureNode::StaticBodyMeshName = FName(TEXT("StaticBodyMesh"));
- ARRStaticFigureNode::ARRStaticFigureNode(const class FObjectInitializer& PCIP)
- : Super(PCIP.DoNotCreateDefaultSubobject(TEXT("CharacterMesh0")))
- {
- SensorPositionComponent = CreateDefaultSubobject<USpringArmComponent>(TEXT("BodyMeshPositionComponent"));
- SensorPositionComponent->AttachParent = GetCapsuleComponent();
- SensorPositionComponent->TargetArmLength = 0.0f; // if the camera is in fps mode, the boom length is zero
- SensorPositionComponent->SetRelativeLocation(FVector(0.f, 0.f, 0.f));
- InteractionSphere = PCIP.CreateDefaultSubobject<USphereComponent>(this, TEXT("InteractionSphere"));
- // InteractionSphere->OnComponentBeginOverlap.AddDynamic(this, &ARRStaticFigureNode::OnBeginOverlap);
- // InteractionSphere->OnComponentEndOverlap.AddDynamic(this, &ARRStaticFigureNode::OnEndOverlap);
- InteractionSphere->AttachParent = SensorPositionComponent;;
- InteractionSphere->SetCollisionObjectType(ECC_WorldDynamic);
- InteractionSphere->SetSphereRadius(400.f);
- BodyMesh = CreateOptionalDefaultSubobject<USensorMeshComponent>(ARRStaticFigureNode::StaticBodyMeshName);
- BodyMesh->OnSensorStartHit.AddDynamic(this, &ARRStaticFigureNode::StartFigureMeshLookAt);
- BodyMesh->OnSensorEndHit.AddDynamic(this, &ARRStaticFigureNode::FinishFigureMeshLookAt);
- BodyMesh->SensorType = ESensorType::StaticFigure;
- BodyMesh->AttachParent = GetCapsuleComponent();
- BodyMesh->SetRelativeLocation(FVector(0.f, 0.f, -75.f));
- BodyMesh->SetRelativeRotation(FRotator(0.f, -90.f, 0.f));
- BodyMesh->SensorName = "static_figure_mesh";
- BodyMesh->SensorType = ESensorType::StaticFigure;
- StaticFigureSensor = PCIP.CreateOptionalDefaultSubobject<USensorMeshComponent>(this, TEXT("StaticFigureSensor"));
- StaticFigureSensor->OnSensorStartHit.AddDynamic(this, &ARRStaticFigureNode::StartSensorLookAt);
- StaticFigureSensor->OnSensorEndHit.AddDynamic(this, &ARRStaticFigureNode::FinishSensorLookAt);
- StaticFigureSensor->AttachParent = SensorPositionComponent;
- StaticFigureSensor->bHiddenInGame = true;
- StaticFigureSensor->SetCollisionObjectType(INTERACTION_CHANNEL);
- StaticFigureSensor->SetCollisionEnabled(ECollisionEnabled::QueryOnly);
- StaticFigureSensor->SetCollisionResponseToChannel(ECC_GameTraceChannel1, ECR_Block);
- StaticFigureSensor->SensorName = "static_figure_sensor";
- StaticFigureSensor->SensorType = ESensorType::StaticFigure;
- bRunSequenceIsOn = false;
- bMeshTimerIsOn = false;
- RunTime = 0.f;
- ObjectType = EObjectType::StaticFigureNode;
- }
- // Called when the game starts or when spawned
- void ARRStaticFigureNode::BeginPlay()
- {
- Super::BeginPlay();
- //GetWorldTimerManager().SetTimer(Load_Asset_Data, this, &ARRStaticFigureNode::LoadNodeMeshData, 0.5f, true);
- }
- // Called every frame
- void ARRStaticFigureNode::Tick( float DeltaTime )
- {
- Super::Tick( DeltaTime );
- if (bMeshTimerIsOn)
- {
- RunTime = RunTime + DeltaTime;
- if (RunTime >= LifeTime)
- {
- UnloadNodeMesh();
- }
- }
- }
- void ARRStaticFigureNode::PostInitializeComponents()
- {
- Super::PostInitializeComponents();
- //ThisCharRace = FCharacterRace::RACESEX_ElfMale; //<- This could be coming from a save file overriding
- // Picks the right Database Path and name;
- // switch (ThisCharRace)
- // {
- // case FCharacterRace::RACESEX_ElfMale:
- // DatabasePath = "/Game/SubDirectory/ElfMaleData.ElfMaleData"; // <- You need databases to load from!
- // break;
- //
- // default:
- // DatabasePath = "/Game/SubDirectory/ElfFemaleData.ElfFemaleData"; // <- You need databases to load from!
- // break
- // }
- // Call to load library and setup meshes
- // InitDefaultMeshes(0, 1, 2, 3, 4);
- }
- void ARRStaticFigureNode::CallDestroyFigureNode()
- {
- // if ()
- // {
- ARRDevelopGameMode* GameMode = Cast<ARRDevelopGameMode>(UGameplayStatics::GetGameMode(this));
- GameMode->SelectAndDestroy(this);
- //}
- }
- void ARRStaticFigureNode::InitStaticMesh(class UStaticMeshComponent* StaticMeshPointer, bool AttachToParent)
- {
- StaticMeshPointer->AlwaysLoadOnClient = true;
- StaticMeshPointer->AlwaysLoadOnServer = true;
- StaticMeshPointer->bOwnerNoSee = false;
- //StaticMeshPointer->MeshComponentUpdateFlag = EMeshComponentUpdateFlag::AlwaysTickPose;
- StaticMeshPointer->bCastDynamicShadow = true;
- StaticMeshPointer->bAffectDynamicIndirectLighting = true;
- StaticMeshPointer->PrimaryComponentTick.TickGroup = TG_PrePhysics;
- //StaticMeshPointer->bChartDistanceFactor = false;
- //StaticMeshPointer->AttachParent = RootComponent;
- if (AttachToParent)
- {
- StaticMeshPointer->AttachParent = GetCapsuleComponent();
- // StaticMeshPointer->SetMasterPoseComponent(HeadMSHComponent);
- // StaticMeshPointer->bUseBoundsFromMasterPoseComponent = true;
- }
- }
- void ARRStaticFigureNode::StartSensorLookAt(class USensorMeshComponent* HitSensor)
- {
- GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::White, TEXT("StaticFigureNode LOOKAT Fired"));
- ARRFigureNodeAIController* NodeController = Cast<ARRFigureNodeAIController>(this->GetController());
- if (ActivationType == ENodeActivationType::LookedAtInRange)
- {
- if (NodeController->GetNodeBehaviorType() == ENodeBehaviorType::Inactive)
- {
- NodeController->SetNodeBehaviorType(ENodeBehaviorType::ExecuteSequence);
- MinimizeSensorMesh();
- }
- }
- //LoadNewMesh();
- }
- void ARRStaticFigureNode::FinishSensorLookAt(class USensorMeshComponent* HitSensor)
- {
- GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::White, TEXT("StaticFigureNode FINISHLOOKAT Fired"));
- }
- void ARRStaticFigureNode::StartFigureMeshLookAt(class USensorMeshComponent* HitSensor)
- {
- switch (CurrentFigureType)
- {
- case EStaticFigureType::ProximityOrLookAt:
- {
- UnloadNodeMesh();
- }
- break;
- case EStaticFigureType::LookAtTimeout:
- {
- bMeshTimerIsOn = true;
- }
- break;
- }
- GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::White, TEXT("StaticFigureNode LOOKAT Fired"));
- }
- void ARRStaticFigureNode::FinishFigureMeshLookAt(class USensorMeshComponent* HitSensor)
- {
- if (CurrentFigureType == EStaticFigureType::LookAtTimeout)
- {
- bMeshTimerIsOn = false;
- }
- GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::White, TEXT("StaticFigureNode FINISHLOOKAT Fired"));
- }
- void ARRStaticFigureNode::AddToInRange(APawn* Incoming)
- {
- if (!InRangePlayers.Contains(Incoming))
- {
- InRangePlayers.Add(Incoming);
- if (bRunSequenceIsOn)
- {
- switch (CurrentFigureType)
- {
- case EStaticFigureType::Proximity:
- {
- UnloadNodeMesh();
- }
- break;
- case EStaticFigureType::ProximityOrLookAt:
- {
- UnloadNodeMesh();
- }
- break;
- }
- }
- }
- }
- void ARRStaticFigureNode::RemoveFromInRange(APawn* Outgoing)
- {
- if (InRangePlayers.Contains(Outgoing))
- {
- InRangePlayers.Remove(Outgoing);
- }
- }
- // void ARRStaticFigureNode::OnBeginOverlap(AActor* Other, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
- // {
- // GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::White, TEXT("StaticFigureNode ONBEGINOVERLAP Fired"));
- // APlayerController* Controller = UGameplayStatics::GetPlayerController(GetWorld(), 0);
- // APawn* PlayerPawn = Controller->GetPawn();
- // APawn* IncomingPawn = Cast<APawn>(Other);
- //
- // if (PlayerPawn == Other)
- // {
- // InRangePlayers.Add(IncomingPawn);
- // }
- // }
- // void ARRStaticFigureNode::OnEndOverlap(AActor* Other, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
- // {
- // GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::White, TEXT("StaticFigureNode ONENDOVERLAP Fired"));
- //
- // APawn* OutgoingPawn = Cast<APawn>(Other);
- //
- // if (InRangePlayers.Contains(OutgoingPawn))
- // {
- // InRangePlayers.Remove(OutgoingPawn);
- // }
- // }
- void ARRStaticFigureNode::ExecuteCurrentNodeTarget()
- {
- NodeTargetMeshData.Empty();
- if (NextTargetInSequence != NULL && NodeMeshData.Num() > 0)
- {
- for (int32 i = 0; i < NextTargetInSequence->NumFiguresToSpawn; i++)
- {
- if (NodeMeshData.IsValidIndex(0))
- {
- NodeTargetMeshData.Add(NodeMeshData[0]);
- NodeMeshData.RemoveAt(0);
- }
- }
- }
- bRunSequenceIsOn = true;
- LoadNodeMesh();
- }
- void ARRStaticFigureNode::LoadNodeMeshData()
- {
- ARRDevelopGameMode* GameMode = Cast<ARRDevelopGameMode>(UGameplayStatics::GetGameMode(this));
- ARRStreamingLevelScriptActor* Level = GameMode->GetCurrentStreamingLevel();
- NodeMeshData = Level->GetStaticFigureInfo(NodeName);
- MinimizedMeshToLoad = Level->GetMinimizedSensorRef();
- }
- void ARRStaticFigureNode::LoadNodeMesh()
- {
- if (NodeTargetMeshData.Num() > 0)
- {
- if (NodeTargetMeshData.IsValidIndex(0))
- {
- BodyMeshToLoad = NodeTargetMeshData[0].FRRLevelStaticMeshInfo::BodyMeshAsset.ToStringReference();
- LifeTime = NodeTargetMeshData[0].Lifetime;
- CurrentFigureType = NodeTargetMeshData[0].StaticFigureType;
- FStreamableManager& BaseLoader = URRGameSingleton::Get().AssetLoader;
- BaseLoader.RequestAsyncLoad(BodyMeshToLoad, FStreamableDelegate::CreateUObject(this, &ARRStaticFigureNode::DoAsyncMeshLoad));
- NodeTargetMeshData.RemoveAt(0);
- }
- }
- else
- {
- bRunSequenceIsOn = false;
- bMeshTimerIsOn = false;
- }
- }
- void ARRStaticFigureNode::UnloadNodeMesh()
- {
- BodyMesh->SetStaticMesh(NULL);
- LoadNodeMesh();
- }
- void ARRStaticFigureNode::DoAsyncMeshLoad()
- {
- check(BodyMeshToLoad.ResolveObject() != nullptr)
- {
- UObject* NewMesh = BodyMeshToLoad.ResolveObject(); // Creates a pointer to store the loaded object
- BodyMesh->SetStaticMesh(Cast<UStaticMesh>(NewMesh));
- RunTime = 0;
- switch (CurrentFigureType)
- {
- case EStaticFigureType::Timeout:
- {
- bMeshTimerIsOn = true;
- }
- break;
- case EStaticFigureType::TimeoutOrProximity:
- {
- bMeshTimerIsOn = true;
- }
- break;
- case EStaticFigureType::Proximity:
- {
- bMeshTimerIsOn = false;
- }
- break;
- case EStaticFigureType::ProximityOrLookAt:
- {
- bMeshTimerIsOn = false;
- }
- break;
- case EStaticFigureType::LookAtTimeout:
- {
- bMeshTimerIsOn = false;
- }
- break;
- case EStaticFigureType::Interact:
- {
- bMeshTimerIsOn = false;
- }
- break;
- }
- }
- }
- void ARRStaticFigureNode::MinimizeSensorMesh()
- {
- StaticFigureSensor->SetRelativeScale3D(FVector(0.001, 0.001, 0.001));
- }
- FName ARRStaticFigureNode::GetNameViaInterface()
- {
- return NodeName;
- }
- void ARRStaticFigureNode::SpeakerIsTooFar(AActor* Speaker)
- {
- }
- void ARRStaticFigureNode::UpdateCharacterData(FStringAssetReference NewSoundCue, FStringAssetReference NewAnimMontage)
- {
- }
- EObjectType ARRStaticFigureNode::GetObjectType()
- {
- return ObjectType;
- }
- int32 ARRStaticFigureNode::GetNumInRangePlayer()
- {
- return InRangePlayers.Num();
- }
- bool ARRStaticFigureNode::IsInView()
- {
- // TArray<UPrimitiveComponent*> Components;
- // GetComponents(Components);
- // for (int32 i = 0; i < Components.Num(); i++)
- // {
- // UPrimitiveComponent* Component = Components[i];
- // if (Component->IsA(USphereComponent::StaticClass()))
- // {
- //float RenderTime = Component->UPrimitiveComponent::LastRenderTime;
- float WorldTime = GetWorld()->TimeSeconds;
- float RenderTime = this->GetLastRenderTime();
- float Difference = WorldTime - RenderTime;
- FString Rendertimestring = FString::SanitizeFloat(RenderTime);
- FString worldtimestring = FString::SanitizeFloat(WorldTime);
- GEngine->AddOnScreenDebugMessage(2, 10.0f, FColor::Red, TEXT("Rendertime: " + Rendertimestring));
- GEngine->AddOnScreenDebugMessage(3, 10.0f, FColor::Red, TEXT("Worldtime: " + worldtimestring));
- if (Difference < 0.2) {
- return true;
- }
- // }
- //
- // }
- else return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement