diff --git a/SeagullGame/Source/SeagullGame/SeagullGameCharacter.cpp b/SeagullGame/Source/SeagullGame/SeagullGameCharacter.cpp index eebbbab..86ccf31 100644 --- a/SeagullGame/Source/SeagullGame/SeagullGameCharacter.cpp +++ b/SeagullGame/Source/SeagullGame/SeagullGameCharacter.cpp @@ -144,30 +144,11 @@ void ASeagullGameCharacter::StartGame() } GetWorld()->GetTimerManager().SetTimer(GameTimerHandle, this, &ASeagullGameCharacter::EndGame, GameTime, false); - FTimerHandle CravingTimerHandle; - GetWorld()->GetTimerManager().SetTimer(CravingTimerHandle, [this]() + if (GetWorld()->GetTimerManager().IsTimerActive(CravingTimerHandle)) { - TSubclassOf ItemActor = ItemActors[FMath::RandRange(0, ItemActors.Num() - 1)]; - if (IsValid(CravingItemActor)) CravingItemActor->Destroy(); - CravingItemActor = GetWorld()->SpawnActor(ItemActor); - CurrentCraving = Cast(CravingItemActor)->ItemType; - CravingItemActor->SetActorEnableCollision(false); - if (!IsValid(John)) return; - UStaticMeshComponent* MeshComponent = Cast(John->GetComponentByClass(UStaticMeshComponent::StaticClass())); - if (!MeshComponent->HasAnySockets()) return; - CravingItemActor->AttachToComponent(MeshComponent, FAttachmentTransformRules::SnapToTargetNotIncludingScale, "CravingSocket"); - FString CravingString = UEnum::GetValueAsString(CurrentCraving); - CravingString.Split(FString("::"), nullptr, &CravingString); - for (int i = 0; i < CravingString.Len(); i++) - { - if (CravingString[i] >= 'A' && CravingString[i] <= 'Z') - { - CravingString.InsertAt(i, " "); - i++; - } - } - CravingItemName = CravingString; - }, 60, true, 2); + GetWorld()->GetTimerManager().ClearTimer(CravingTimerHandle); + } + GetWorld()->GetTimerManager().SetTimer(CravingTimerHandle, this, &ASeagullGameCharacter::SetCravingItem, 60, true, 2); } void ASeagullGameCharacter::EndGame() @@ -205,6 +186,30 @@ void ASeagullGameCharacter::Scroll(const FInputActionValue& Value) } } +void ASeagullGameCharacter::SetCravingItem() +{ + const TSubclassOf ItemActor = ItemActors[FMath::RandRange(0, ItemActors.Num() - 1)]; + if (IsValid(CravingItemActor)) CravingItemActor->Destroy(); + CravingItemActor = GetWorld()->SpawnActor(ItemActor); + CurrentCraving = Cast(CravingItemActor)->ItemType; + CravingItemActor->SetActorEnableCollision(false); + if (!IsValid(John)) return; + UStaticMeshComponent* MeshComponent = Cast(John->GetComponentByClass(UStaticMeshComponent::StaticClass())); + if (!MeshComponent->HasAnySockets()) return; + CravingItemActor->AttachToComponent(MeshComponent, FAttachmentTransformRules::SnapToTargetNotIncludingScale, "CravingSocket"); + FString CravingString = UEnum::GetValueAsString(CurrentCraving); + CravingString.Split(FString("::"), nullptr, &CravingString); + for (int i = 0; i < CravingString.Len(); i++) + { + if (CravingString[i] >= 'A' && CravingString[i] <= 'Z') + { + CravingString.InsertAt(i, " "); + i++; + } + } + CravingItemName = CravingString; +} + void ASeagullGameCharacter::PickupItem() { OnPlayerPickupItem.Broadcast(); diff --git a/SeagullGame/Source/SeagullGame/SeagullGameCharacter.h b/SeagullGame/Source/SeagullGame/SeagullGameCharacter.h index c15fb90..c70f951 100644 --- a/SeagullGame/Source/SeagullGame/SeagullGameCharacter.h +++ b/SeagullGame/Source/SeagullGame/SeagullGameCharacter.h @@ -100,6 +100,8 @@ public: UFUNCTION(Category = "General") void Scroll(const FInputActionValue& Value); + UFUNCTION(BlueprintCallable, Category = "Item") + void SetCravingItem(); UFUNCTION(BlueprintCallable, Category = "Item") void PickupItem(); UFUNCTION(BlueprintCallable, Category = "Item") @@ -126,6 +128,8 @@ public: FString CravingItemName; UPROPERTY(BlueprintReadOnly, Category = "Item") FString HeldItemName; + UPROPERTY(BlueprintReadOnly, Category = "Item") + FTimerHandle CravingTimerHandle; UPROPERTY(BlueprintReadOnly, Category = "John") AActor* John;