Files
WizardingHub/Source/WizardingCentral/StickySlime/Hammer.cpp
T
2025-05-23 18:26:37 -04:00

115 lines
2.3 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "Hammer.h"
#include "StickySlimeComponent.h"
// Sets default values
AHammer::AHammer()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("HammerMesh"));
RootComponent = MeshComponent;
MeshComponent->SetSimulatePhysics(true);
MeshComponent->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
MeshComponent->SetCollisionObjectType(ECC_PhysicsBody);
MeshComponent->OnComponentHit.AddDynamic(this, &AHammer::OnHit);
MeshComponent->SetNotifyRigidBodyCollision(true);
MeshComponent->SetGenerateOverlapEvents(true);
GroundIdleTime = 1.0f;
AirIdleTime = 0.5f;
LiftSpeed = 500.0f;
bIsSlimed = false;
bIsMovingUp = false;
IdleTimer = 0.0f;
}
// Called when the game starts or when spawned
void AHammer::BeginPlay()
{
Super::BeginPlay();
InitialLocation = GetActorLocation();
}
// Called every frame
void AHammer::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (bIsSlimed)
{
return;
}
if (bIsMovingUp && IdleTimer <= 0.0f)
{
FVector NewLocation = GetActorLocation();
NewLocation.Z += LiftSpeed * DeltaTime;
if (NewLocation.Z >= InitialLocation.Z)
{
NewLocation.Z = InitialLocation.Z;
bIsMovingUp = false;
IdleTimer = AirIdleTime;
}
SetActorLocation(NewLocation);
}
else if (IdleTimer > 0.0f)
{
IdleTimer -= DeltaTime;
if (IdleTimer <= 0.0f)
{
IdleTimer = 0.0f;
if (!bIsMovingUp)
{
MeshComponent->SetSimulatePhysics(true);
}
}
}
}
void AHammer::OnSlimed_Implementation(AActor* Slime)
{
ISlimableInterface::OnSlimed_Implementation(Slime);
bIsSlimed = true;
MeshComponent->SetSimulatePhysics(false);
}
void AHammer::OnUnslimed_Implementation()
{
ISlimableInterface::OnUnslimed_Implementation();
bIsSlimed = false;
IdleTimer = GroundIdleTime;
}
void AHammer::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent,
FVector NormalImpulse, const FHitResult& Hit)
{
UE_LOG(LogTemp, Warning, TEXT("Hit"));
if (bIsSlimed)
{
return;
}
if (!OtherActor || OtherActor == this)
{
return;
}
MeshComponent->SetSimulatePhysics(false);
IdleTimer = GroundIdleTime;
bIsMovingUp = true;
}