void ASHUD::DrawCenterDot() { float CenterX = Canvas->ClipX / 2; float CenterY = Canvas->ClipY / 2; float CenterDotScale = 0.07f; ASPlayerController* PCOwner = Cast<ASPlayerController>(PlayerOwner); if (PCOwner) { ASCharacter* Pawn = Cast<ASCharacter>(PCOwner->GetPawn()); if (Pawn && Pawn->IsAlive()) { // Boost size when hovering over a switchable object. ASUsableActor* usable = Pawn->GetUsableInView(); if (usable) CenterDotScale *= 1.5f; Canvas->SetDrawColor(255, 255, 255, 255); Canvas->DrawIcon(CenterDotIcon, CenterX - CenterDotIcon.UL*CenterDotScale / 2.0f, CenterY - CenterDotIcon.VL*CenterDotScale / 2.0f, CenterDotScale); } } }
void ASWeaponPickup::OnUsed(APawn* InstigatorPawn) { ASCharacter* MyPawn = Cast<ASCharacter>(InstigatorPawn); if (MyPawn) { /* Fetch the default variables of the class we are about to pick up and check if the storage slot is available on the pawn. */ if (MyPawn->WeaponSlotAvailable(WeaponClass->GetDefaultObject<ASWeapon>()->GetStorageSlot())) { FActorSpawnParameters SpawnInfo; SpawnInfo.bNoCollisionFail = true; ASWeapon* NewWeapon = GetWorld()->SpawnActor<ASWeapon>(WeaponClass, SpawnInfo); MyPawn->AddWeapon(NewWeapon); Super::OnUsed(InstigatorPawn); } else { ASPlayerController* PC = Cast<ASPlayerController>(MyPawn->GetController()); if (PC) { PC->ClientMessageReceived("Weapon slot already taken."); } } } }
void ASCharacter::RestoreCondition(float HealthRestored, float HungerRestored) { // Reduce Hunger, ensure we do not go outside of our bounds Hunger = FMath::Clamp(Hunger - HungerRestored, 0.0f, GetMaxHunger()); // Restore Hitpoints Health = FMath::Clamp(Health + HealthRestored, 0.0f, GetMaxHealth()); ASPlayerController* PC = Cast<ASPlayerController>(Controller); if (PC) { PC->ClientHUDMessage(EHUDMessage::Character_EnergyRestored); } }
/* As with Server side functions, NetMulticast functions have a _Implementation body */ void ASGameState::BroadcastGameMessage_Implementation(const FString& NewMessage) { for (FConstPlayerControllerIterator It = GetWorld()->GetPlayerControllerIterator(); It; It++) { ASPlayerController* MyController = Cast<ASPlayerController>(*It); if (MyController && MyController->IsLocalController()) { ASHUD* MyHUD = Cast<ASHUD>(MyController->GetHUD()); if (MyHUD) { MyHUD->MessageReceived(NewMessage); } } } }
FVector ASWeapon::GetCameraDamageStartLocation(const FVector& AimDir) const { ASPlayerController* PC = MyPawn ? Cast<ASPlayerController>(MyPawn->Controller) : nullptr; FVector OutStartTrace = FVector::ZeroVector; if (PC) { FRotator DummyRot; PC->GetPlayerViewPoint(OutStartTrace, DummyRot); // Adjust trace so there is nothing blocking the ray between the camera and the pawn, and calculate distance from adjusted start OutStartTrace = OutStartTrace + AimDir * (FVector::DotProduct((Instigator->GetActorLocation() - OutStartTrace), AimDir)); } return OutStartTrace; }
FVector ASWeapon::GetCameraDamageStartLocation(const FVector& AimDir) const { ASPlayerController* PC = MyPawn ? Cast<ASPlayerController>(MyPawn->Controller) : nullptr; FVector OutStartTrace = FVector::ZeroVector; if (PC) { FRotator dummyRot; PC->GetPlayerViewPoint(OutStartTrace, dummyRot); // Adjust trace so there is nothing blocking the ray between the camera and the pawn, and calculate distance from adjusted start // TODO: Break down into easy to understand code (copied from ShooterGame) OutStartTrace = OutStartTrace + AimDir * ((Instigator->GetActorLocation() - OutStartTrace) | AimDir); } return OutStartTrace; }
void ASGameMode::FinishMatch() { ASGameState* const MyGameState = Cast<ASGameState>(GameState); if (IsMatchInProgress()) { EndMatch(); /* Stop spawning bots */ GetWorldTimerManager().ClearTimer(TimerHandle_BotSpawns); for (FConstPlayerControllerIterator It = GetWorld()->GetPlayerControllerIterator(); It; It++) { ASPlayerController* MyController = Cast<ASPlayerController>(*It); if (MyController) { MyController->ClientHUDStateChanged(EHUDState::MatchEnd); } } } }
void ASHUD::DrawCenterDot() { float lCenterX = Canvas->ClipX * 0.5f; float lCenterY = Canvas->ClipY * 0.5f; float lCenterDotScale = 0.07f; ASPlayerController* lOwner = Cast<ASPlayerController>(PlayerOwner); if (lOwner) { ASCharacter* lPawn = Cast<ASCharacter>(lOwner->GetPawn()); if (lPawn && lPawn->IsAlive()) { ASUsableActor* lUsable = lPawn->GetUsableInView(); if (lUsable) lCenterDotScale *= 1.5f; Canvas->SetDrawColor(255, 255, 255, 255); Canvas->DrawIcon(CenterDotIcon, lCenterX - CenterDotIcon.UL * lCenterDotScale * 0.5f, lCenterY - CenterDotIcon.VL * lCenterDotScale * 0.5f, lCenterDotScale); } } }
void ASGameMode::DefaultTimer() { /* This function is called every 1 second. */ Super::DefaultTimer(); /* Immediately start the match while playing in editor */ //if (GetWorld()->IsPlayInEditor()) { if (GetMatchState() == MatchState::WaitingToStart) { StartMatch(); } } /* Only increment time of day while game is active */ if (IsMatchInProgress()) { ASGameState* MyGameState = Cast<ASGameState>(GameState); if (MyGameState) { /* Increment our time of day */ MyGameState->ElapsedGameMinutes += MyGameState->GetTimeOfDayIncrement(); /* Determine our state */ MyGameState->GetAndUpdateIsNight(); /* Trigger events when night starts or ends */ bool CurrentIsNight = MyGameState->GetIsNight(); if (CurrentIsNight != LastIsNight) { FString MessageText = CurrentIsNight ? "SURVIVE!" : "You Survived! Now prepare for the coming night!"; ASGameState* MyGameState = Cast<ASGameState>(GameState); if (MyGameState) { MyGameState->BroadcastGameMessage(MessageText); } /* The night just ended, respawn all dead players */ if (!CurrentIsNight) { /* Respawn spectating players that died during the night */ for (FConstPlayerControllerIterator It = GetWorld()->GetPlayerControllerIterator(); It; It++) { /* Look for all players that are spectating */ ASPlayerController* MyController = Cast<ASPlayerController>(*It); if (MyController) { if (MyController->PlayerState->bIsSpectator) { RestartPlayer(MyController); MyController->ClientHUDStateChanged(EHUDState::Playing); } else { /* Player still alive, award him some points */ ASCharacter* MyPawn = Cast<ASCharacter>(MyController->GetPawn()); if (MyPawn && MyPawn->IsAlive()) { ASPlayerState* PS = Cast<ASPlayerState>(MyController->PlayerState); if (PS) { PS->ScorePoints(NightSurvivedScore); } } } } } } /* Update bot states */ if (CurrentIsNight) { WakeAllBots(); } else { PassifyAllBots(); } } LastIsNight = MyGameState->bIsNight; } } }