commit 7f4fd950df147df131d3a90cd5da0d100c3d8d56 Author: Maximilian Fajnberg Date: Tue Jan 11 11:07:17 2022 +0100 initial commit diff --git a/AdventureCameraPawn.cpp b/AdventureCameraPawn.cpp new file mode 100644 index 0000000..cb59f5d --- /dev/null +++ b/AdventureCameraPawn.cpp @@ -0,0 +1,150 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "AdventureCameraPawn.h" + +#include "AdventureMap.h" +#include "AdventurePlayerController.h" +#include "AdventureCharacter.h" + +#include "GameFramework/PlayerController.h" +#include "Kismet/GameplayStatics.h" + +#include "math.h" + +// Sets default values +AAdventureCameraPawn::AAdventureCameraPawn() +{ + // Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don't need it. + PrimaryActorTick.bCanEverTick = true; + + ESASize = .01; + BaseScrollSpeed = 15; + ScrollAccelleration = BaseScrollSpeed * 2; + + ESAMaxBoundSlope = 1 / (1 - (1-ESASize)); + ESAMaxBoundIntercept = 1 - ESAMaxBoundSlope; + + ESAMinBoundSlope = -1 / ESASize; + ESAMinBoundIntercept = 0 - (ESAMinBoundSlope * ESASize); + +} + +// Called when the game starts or when spawned +void AAdventureCameraPawn::BeginPlay() +{ + Super::BeginPlay(); + ControllerRef = (AAdventurePlayerController*)UGameplayStatics::GetPlayerController(GetWorld(), 0); + + Viewport = GetWorld()->GetGameViewport(); + ViewSize = Viewport->Viewport->GetSizeXY(); + +} + +// Called every frame +void AAdventureCameraPawn::Tick(float DeltaTime) +{ + Super::Tick(DeltaTime); + +} + +// Called to bind functionality to input +void AAdventureCameraPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) +{ + Super::SetupPlayerInputComponent(PlayerInputComponent); + if (PlayerInputComponent) + { + PlayerInputComponent->BindAxis("Mouse X", this, &AAdventureCameraPawn::EdgeScrollSide); + PlayerInputComponent->BindAxis("Mouse Y", this, &AAdventureCameraPawn::EdgeScrollVert); + } +} + + +void AAdventureCameraPawn::EdgeScrollSide(float AxisValue) +{ + float mouseX; + float mouseY; + ControllerRef->GetMousePosition(mouseX, mouseY); + float mousePosX = mouseX / ViewSize.X; + float VertAccelleration = GetInputAxisValue("Mouse Y") * (ScrollAccelleration * .75); + + if (mousePosX <= ESASize) // if in LEFT area + { + ////// Scroll LEFT with Vert Accelleration + float SpeedX = (mousePosX * ESAMinBoundSlope + ESAMinBoundIntercept) * BaseScrollSpeed; + AddActorLocalOffset(FVector(VertAccelleration, -SpeedX, 0)); + + if (AxisValue < .0 && mousePosX <= .005) // if mouse moving LEFT and touching EDGE + { + ////// Add LEFT accelleration + SpeedX = AxisValue * ScrollAccelleration; + AddActorLocalOffset(FVector(0, SpeedX, 0)); + } + } + + if (mousePosX >= 1-ESASize) // if in RIGHT area + { + ////// Scroll RIGHT + float SpeedX = (mousePosX * ESAMaxBoundSlope + ESAMaxBoundIntercept) * BaseScrollSpeed; + AddActorLocalOffset(FVector(VertAccelleration, SpeedX, 0)); + + if (AxisValue > .0 && mousePosX >= .995) // if mouse moving RIGHT and touching EDGE + { + ////// Add RIGHT accelleration + SpeedX = AxisValue * ScrollAccelleration; + AddActorLocalOffset(FVector(0, SpeedX, 0)); + } + } +} + +void AAdventureCameraPawn::EdgeScrollVert(float AxisValue) +{ + float mouseX; + float mouseY; + ControllerRef->GetMousePosition(mouseX, mouseY); + float mousePosY = mouseY / ViewSize.Y; + float SideAccelleration = GetInputAxisValue("Mouse X") * (ScrollAccelleration * .75); + + if (mousePosY <= ESASize) // if in TOP area + { + ////// Scroll TOP + float SpeedY = (mousePosY * ESAMinBoundSlope + ESAMinBoundIntercept) * BaseScrollSpeed; + AddActorLocalOffset(FVector(SpeedY, SideAccelleration, 0)); + + if (AxisValue > .0 && mousePosY <= .005) // if mouse moving TOP and touching EDGE + { + ////// Add TOP accelleration + SpeedY = AxisValue * ScrollAccelleration; + AddActorLocalOffset(FVector(SpeedY, 0, 0)); + } + } + + if (mousePosY >= 1-ESASize) // if in BOTTOM area + { + ////// Scroll BOTTOM + float SpeedY = (mousePosY * ESAMaxBoundSlope + ESAMaxBoundIntercept) * BaseScrollSpeed; + AddActorLocalOffset(FVector(-SpeedY, SideAccelleration, 0)); + + if (AxisValue < .0 && mousePosY >= .995) // if mouse moving BOTTOM and touching EDGE + { + ////// add BOTTOM accelleration + SpeedY = AxisValue * ScrollAccelleration; + AddActorLocalOffset(FVector(SpeedY, 0, 0)); + } + + } +} + +void AAdventureCameraPawn::FollowAdvPawn() +{ + UE_LOG(LogTemp, Warning, TEXT("following...")); + + AdvPawnLocationCurrent = AdvPawnRef->GetActorLocation(); + AdvPawnLocationDelta = AdvPawnLocationCurrent - AdvPawnLocationPrevious; + + UE_LOG(LogTemp, Warning, TEXT("%s"), *AdvPawnLocationDelta.ToString()); + + AddActorLocalOffset(AdvPawnLocationDelta); + + AdvPawnLocationPrevious = AdvPawnLocationCurrent; +} \ No newline at end of file diff --git a/AdventureCameraPawn.h b/AdventureCameraPawn.h new file mode 100644 index 0000000..d0ac78f --- /dev/null +++ b/AdventureCameraPawn.h @@ -0,0 +1,75 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "GameFramework/Pawn.h" +#include "AdventureCameraPawn.generated.h" + +UCLASS() +class FRAY_API AAdventureCameraPawn : public APawn +{ + GENERATED_BODY() + +public: + // Sets default values for this pawn's properties + AAdventureCameraPawn(); + + UPROPERTY() + UGameViewportClient* Viewport; + UPROPERTY() + FIntPoint ViewSize; + + UPROPERTY(BlueprintReadWrite, Category = "Config") + class AAdventureMap* AdvMapRef; + UPROPERTY(BlueprintReadWrite, Category = "Config") + class AAdventurePlayerController* ControllerRef; + UPROPERTY(BlueprintReadWrite, Category = "Config") + class AAdventureCharacter* AdvPawnRef; + + + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Input") + float ESASize; + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Input") + float ScrollAccelleration; + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Input") + float BaseScrollSpeed; + + UPROPERTY() + FVector AdvPawnLocationPrevious; + UPROPERTY() + FVector AdvPawnLocationCurrent; + UPROPERTY() + FVector AdvPawnLocationDelta; + UPROPERTY(BlueprintReadWrite, Category = "Runtime") + bool bAdvPawnIsMoving; + + UPROPERTY(BlueprintReadWrite, Category = "Runtime") + class AHexTile* SelectedHex; + +protected: + // Called when the game starts or when spawned + virtual void BeginPlay() override; + + float ESAMaxBoundSlope; + float ESAMaxBoundIntercept; + + float ESAMinBoundSlope; + float ESAMinBoundIntercept; + + +public: + // Called every frame + virtual void Tick(float DeltaTime) override; + + // Called to bind functionality to input + virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; + + UFUNCTION(BlueprintCallable) + void EdgeScrollSide(float AxisValue); + UFUNCTION(BlueprintCallable) + void EdgeScrollVert(float AxisValue); + + UFUNCTION(BlueprintCallable) + void FollowAdvPawn(); +}; diff --git a/AdventureCharacter.cpp b/AdventureCharacter.cpp new file mode 100644 index 0000000..4a37af6 --- /dev/null +++ b/AdventureCharacter.cpp @@ -0,0 +1,34 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "AdventureCharacter.h" + +// Sets default values +AAdventureCharacter::AAdventureCharacter() +{ + // Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it. + PrimaryActorTick.bCanEverTick = true; + +} + +// Called when the game starts or when spawned +void AAdventureCharacter::BeginPlay() +{ + Super::BeginPlay(); + +} + +// Called every frame +void AAdventureCharacter::Tick(float DeltaTime) +{ + Super::Tick(DeltaTime); + +} + +// Called to bind functionality to input +void AAdventureCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) +{ + Super::SetupPlayerInputComponent(PlayerInputComponent); + +} + diff --git a/AdventureCharacter.h b/AdventureCharacter.h new file mode 100644 index 0000000..42793a8 --- /dev/null +++ b/AdventureCharacter.h @@ -0,0 +1,29 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "GameFramework/Character.h" +#include "AdventureCharacter.generated.h" + +UCLASS() +class FRAY_API AAdventureCharacter : public ACharacter +{ + GENERATED_BODY() + +public: + // Sets default values for this character's properties + AAdventureCharacter(); + +protected: + // Called when the game starts or when spawned + virtual void BeginPlay() override; + +public: + // Called every frame + virtual void Tick(float DeltaTime) override; + + // Called to bind functionality to input + virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; + +}; diff --git a/AdventureMap.cpp b/AdventureMap.cpp new file mode 100644 index 0000000..9f5daf8 --- /dev/null +++ b/AdventureMap.cpp @@ -0,0 +1,88 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "AdventureMap.h" +#include "HexTile.h" +#include "AdventureCameraPawn.h" +#include "AdventureCharacter.h" +#include "Kismet/GameplayStatics.h" + + +// Sets default values +AAdventureMap::AAdventureMap() +{ +} + +// Called when the game starts or when spawned +void AAdventureMap::BeginPlay() +{ + Super::BeginPlay(); + UWorld* World = GetWorld(); + if (IsValid(BaseTileClass)) + { + MakeGrid(); + } +} + +// Every Hex Tile's index within the Grid Array can be derived from its Q and R coordinates +int32 AAdventureMap::GridIndex(int32 qAxial, int32 rAxial) +{ + /* + * The Q axis is (i.e. columns are) oriented diagonally. + * The Hex Grid has a rough square shape, hence the Q coordinates must be offset by -1 every other row. + */ + int32 column = qAxial + FMath::FloorToInt(rAxial / 2); + return (rAxial * GridSize) + column; +} + +// Called once on Begin Play +void AAdventureMap::MakeGrid() +{ + UWorld* World = GetWorld(); + FVector NextHexAt = FVector(); + float HexWidth = sqrt(3) * TileSize; + int QOffset = 0; + + for (int r = 1; r <= GridSize; r++) + { + float XOffset = 0.f; + + if (r % 2 != 0) + { + if (r > 1) + { + QOffset--; // The Q axis is (i.e. columns are) oriented diagonally. + } + } + else + { + XOffset = HexWidth / 2; + } + + for (int q = 1; q <= GridSize; q++) + { + NextHexAt.X = XOffset + (HexWidth * q); + NextHexAt.Y = TileSize * 1.5f * r; + NextHexAt.Z = 0.f; + + FTransform SpawnTransform = FTransform(NextHexAt); + AHexTile* Tile = World->SpawnActor(BaseTileClass, SpawnTransform); + + Tile->Q = q - 1 + QOffset; + Tile->R = r - 1; + + Grid.Add(Tile); + } + } + + for (auto& tile : Grid) + { + tile->Index = GridIndex(tile->Q, tile->R); + } +} + +AHexTile* AAdventureMap::RandomHex() +{ + int32 SpawnHex = GridIndex(FMath::RandRange(0, GridSize), FMath::RandRange(0, GridSize)); + return Grid[SpawnHex]; +} \ No newline at end of file diff --git a/AdventureMap.h b/AdventureMap.h new file mode 100644 index 0000000..31333b4 --- /dev/null +++ b/AdventureMap.h @@ -0,0 +1,51 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "GameFramework/Actor.h" +#include "AdventureMap.generated.h" + +class AHexTile; +class AAdventureCharacter; + +UCLASS() +class FRAY_API AAdventureMap : public AActor +{ + GENERATED_BODY() + +public: + // Sets default values for this actor's properties + AAdventureMap(); + + UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config") + TSubclassOf BaseTileClass; + UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config") + TSubclassOf BasePartyCharacterClass; + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Config") + int32 GridSize = 100; // squared is the number of Tiles + UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Config") + int32 TileSize = 100; + UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category = "MapData") + TArray Grid; + UFUNCTION(BlueprintCallable) + int32 GridIndex(int32 q, int32 r); + + // Player spawn section + UPROPERTY(VisibleAnywhere, BlueprintReadWrite) + APawn* CameraPawn; + UPROPERTY(VisibleAnywhere, BlueprintReadWrite) + AAdventureCharacter* PlayerCharacter; + + UFUNCTION(BlueprintCallable) + void MakeGrid(); + UFUNCTION(BlueprintCallable) + AHexTile* RandomHex(); + +protected: + // Called when the game starts or when spawned + virtual void BeginPlay() override; + + + +}; \ No newline at end of file diff --git a/AdventurePlayerController.cpp b/AdventurePlayerController.cpp new file mode 100644 index 0000000..56f926a --- /dev/null +++ b/AdventurePlayerController.cpp @@ -0,0 +1,16 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "AdventurePlayerController.h" +#include "AdventureMap.h" +#include "HexTile.h" +#include "AdventureCameraPawn.h" +#include "AdventureCharacter.h" + + +AAdventurePlayerController::AAdventurePlayerController() +{ + PrimaryActorTick.bCanEverTick = true; + PrimaryActorTick.bStartWithTickEnabled = true; + +} \ No newline at end of file diff --git a/AdventurePlayerController.h b/AdventurePlayerController.h new file mode 100644 index 0000000..682fd59 --- /dev/null +++ b/AdventurePlayerController.h @@ -0,0 +1,27 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "GameFramework/PlayerController.h" + +#include "AdventurePlayerController.generated.h" + +class AAdventureMap; +class AHexTile; +class AAdventureCameraPawn; +class AAdventureCharacter; + +/** + * + */ +UCLASS() +class FRAY_API AAdventurePlayerController : public APlayerController +{ + GENERATED_BODY() + +public: + AAdventurePlayerController(); + +public: +}; diff --git a/Clickable.cpp b/Clickable.cpp new file mode 100644 index 0000000..45ba5b3 --- /dev/null +++ b/Clickable.cpp @@ -0,0 +1,34 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "Clickable.h" + +// Sets default values for this component's properties +UClickable::UClickable() +{ + // 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. + PrimaryComponentTick.bCanEverTick = true; + + // ... +} + + +// Called when the game starts +void UClickable::BeginPlay() +{ + Super::BeginPlay(); + + // ... + +} + + +// Called every frame +void UClickable::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) +{ + Super::TickComponent(DeltaTime, TickType, ThisTickFunction); + + // ... +} + diff --git a/Clickable.h b/Clickable.h new file mode 100644 index 0000000..9fb69f9 --- /dev/null +++ b/Clickable.h @@ -0,0 +1,28 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "Components/ActorComponent.h" +#include "Clickable.generated.h" + + +UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) ) +class FRAY_API UClickable : public UActorComponent +{ + GENERATED_BODY() + +public: + // Sets default values for this component's properties + UClickable(); + +protected: + // Called when the game starts + virtual void BeginPlay() override; + +public: + // Called every frame + virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override; + + +}; diff --git a/FRAY.Build.cs b/FRAY.Build.cs new file mode 100644 index 0000000..4b61e6a --- /dev/null +++ b/FRAY.Build.cs @@ -0,0 +1,23 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +using UnrealBuildTool; + +public class FRAY : ModuleRules +{ + public FRAY(ReadOnlyTargetRules Target) : base(Target) + { + PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; + + PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore" }); + + PrivateDependencyModuleNames.AddRange(new string[] { }); + + // Uncomment if you are using Slate UI + // PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" }); + + // Uncomment if you are using online features + // PrivateDependencyModuleNames.Add("OnlineSubsystem"); + + // To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true + } +} diff --git a/FRAY.Target.cs b/FRAY.Target.cs new file mode 100644 index 0000000..6e67e0e --- /dev/null +++ b/FRAY.Target.cs @@ -0,0 +1,14 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +using UnrealBuildTool; +using System.Collections.Generic; + +public class FRAYTarget : TargetRules +{ + public FRAYTarget( TargetInfo Target) : base(Target) + { + Type = TargetType.Game; + DefaultBuildSettings = BuildSettingsVersion.V2; + ExtraModuleNames.AddRange( new string[] { "FRAY" } ); + } +} diff --git a/FRAY.cpp b/FRAY.cpp new file mode 100644 index 0000000..8c09297 --- /dev/null +++ b/FRAY.cpp @@ -0,0 +1,6 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#include "FRAY.h" +#include "Modules/ModuleManager.h" + +IMPLEMENT_PRIMARY_GAME_MODULE( FDefaultGameModuleImpl, FRAY, "FRAY" ); diff --git a/FRAY.h b/FRAY.h new file mode 100644 index 0000000..677c8e2 --- /dev/null +++ b/FRAY.h @@ -0,0 +1,6 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#pragma once + +#include "CoreMinimal.h" + diff --git a/FRAYEditor.Target.cs b/FRAYEditor.Target.cs new file mode 100644 index 0000000..8fb4455 --- /dev/null +++ b/FRAYEditor.Target.cs @@ -0,0 +1,14 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +using UnrealBuildTool; +using System.Collections.Generic; + +public class FRAYEditorTarget : TargetRules +{ + public FRAYEditorTarget( TargetInfo Target) : base(Target) + { + Type = TargetType.Editor; + DefaultBuildSettings = BuildSettingsVersion.V2; + ExtraModuleNames.AddRange( new string[] { "FRAY" } ); + } +} diff --git a/FRAYGameModeBase.cpp b/FRAYGameModeBase.cpp new file mode 100644 index 0000000..552cd52 --- /dev/null +++ b/FRAYGameModeBase.cpp @@ -0,0 +1,5 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + + +#include "FRAYGameModeBase.h" + diff --git a/FRAYGameModeBase.h b/FRAYGameModeBase.h new file mode 100644 index 0000000..4423c86 --- /dev/null +++ b/FRAYGameModeBase.h @@ -0,0 +1,17 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#pragma once + +#include "CoreMinimal.h" +#include "GameFramework/GameModeBase.h" +#include "FRAYGameModeBase.generated.h" + +/** + * + */ +UCLASS() +class FRAY_API AFRAYGameModeBase : public AGameModeBase +{ + GENERATED_BODY() + +}; diff --git a/HexTile.cpp b/HexTile.cpp new file mode 100644 index 0000000..3856383 --- /dev/null +++ b/HexTile.cpp @@ -0,0 +1,44 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "HexTile.h" +#include "AdventurePlayerController.h" + +#include "Kismet/KismetMathLibrary.h" + +// Sets default values +AHexTile::AHexTile() +{ + TileSize = 100.f; + + RootComponent = CreateDefaultSubobject(TEXT("Scene")); + + this->FillCornersArray(); + + // OnClicked.AddDynamic(this, ) +} + +void AHexTile::BeginPlay() +{ + Super::BeginPlay(); +} + +FVector AHexTile::Corner(int32 i) +{ + FVector TileCenter = this->GetActorTransform().GetLocation(); + + int32 Angle_Deg = 60 * i - 30; + float Angle_Rad = UKismetMathLibrary::GetPI()/180 * Angle_Deg; + float X = TileCenter.X + TileSize * cos(Angle_Rad); + float Y = TileCenter.Y + TileSize * sin(Angle_Rad); + + return FVector(X, Y, 0.f); +} +void AHexTile::FillCornersArray() +{ + for (int32 i = 0 ; i < 6; i++) + { + Corners.Emplace(Corner(i + 1)); + } +} + diff --git a/HexTile.h b/HexTile.h new file mode 100644 index 0000000..9169796 --- /dev/null +++ b/HexTile.h @@ -0,0 +1,48 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "GameFramework/Actor.h" + +#include "HexTile.generated.h" + +class USceneComponent; +class UStaticMeshComponent; + +class AAdventurePlayerController; + +UCLASS() +class FRAY_API AHexTile : public AActor +{ + GENERATED_BODY() + +public: + // Sets default value for this actor's properties + AHexTile(); + + UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config") + float TileSize; + UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components") + USceneComponent* SceneComponent; + + UFUNCTION(BlueprintCallable) + FVector Corner(int32 i); + UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Runtime") + TArray Corners; + UFUNCTION() + void FillCornersArray(); + + UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Runtime") + int32 Q; + UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Runtime") + int32 R; + UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Runtime") + int32 Index; + +protected: + // Called when the game starts or when spawned + virtual void BeginPlay() override; + +}; + diff --git a/WalkAnimInstance.cpp b/WalkAnimInstance.cpp new file mode 100644 index 0000000..edb352d --- /dev/null +++ b/WalkAnimInstance.cpp @@ -0,0 +1,28 @@ +// Fill out your copyright notice in the Description page of Project Settings. + + +#include "WalkAnimInstance.h" +#include "GameFramework/Pawn.h" +#include "GameFramework/PawnMovementComponent.h" + + +void UWalkAnimInstance::NativeInitializeAnimation() +{ + if (Pawn == nullptr) + { + Pawn = TryGetPawnOwner(); + } + + if (IsValid(Pawn)) + { + MovementComponent = Pawn->GetMovementComponent(); + } +} + +void UWalkAnimInstance::NativeUpdateAnimation(float DeltaSeconds) +{ + if (Pawn && MovementComponent) + { + Speed = Pawn->GetVelocity().Size(); + } +} diff --git a/WalkAnimInstance.h b/WalkAnimInstance.h new file mode 100644 index 0000000..c4173ba --- /dev/null +++ b/WalkAnimInstance.h @@ -0,0 +1,34 @@ +// Fill out your copyright notice in the Description page of Project Settings. + +#pragma once + +#include "CoreMinimal.h" +#include "Animation/AnimInstance.h" +#include "WalkAnimInstance.generated.h" + +class APawn; +class UPawnMovementComponent; + +/** + * + */ +UCLASS() +class FRAY_API UWalkAnimInstance : public UAnimInstance +{ + GENERATED_BODY() + +public: + UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Movement") + float Speed; + + UPROPERTY(VisibleInstanceOnly, BlueprintReadOnly, Category = "Movement") + APawn* Pawn; + + UPROPERTY(VisibleInstanceOnly, BlueprintReadOnly, Category = "Movement") + UPawnMovementComponent* MovementComponent; + + virtual void NativeInitializeAnimation() override; + + virtual void NativeUpdateAnimation(float DeltaSeconds) override; + +};