20 - Handling New Play States

在gameMode中添加 电池生成开关 根据当前的不同状态 开启生成
最终BatteryCollectorGameMode.h
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.

pragma once

include "GameFramework/GameModeBase.h"

include "BatteryCollectorGameMode.generated.h"

UENUM(BlueprintType)
enum class EBatteryPlayState:uint8
{
EPlaying,
EGameOver,
EWon,
EUnknown
};

UCLASS(minimalapi)
class ABatteryCollectorGameMode : public AGameModeBase
{
GENERATED_BODY()

public:
ABatteryCollectorGameMode();
virtual void Tick(float DeltaSeconds) override;

UFUNCTION(BlueprintPure, Category = "Power")
float GetPowerToWin() const;

virtual void BeginPlay() override;

UFUNCTION(BlueprintPure, Category = "Power")
EBatteryPlayState GetCurrentState() const;
UFUNCTION(BlueprintCallable, Category = "Power")
void SetCurrentState(EBatteryPlayState state);

protected:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Power")
float DecayRate;

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Power")
float PowerToWin;

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Power")
TSubclassOf HUDWidgetClass;

UPROPERTY()
class UUserWidget* CurrentWidget;

private:

EBatteryPlayState CurrentState;

TArray SpawnVolumActors;

void HandleNewState(EBatteryPlayState NewState);

};

最终BatteryCollectorGameMode.cpp
// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.

include "BatteryCollector.h"

include "BatteryCollectorGameMode.h"

include "BatteryCollectorCharacter.h"

include "Kismet/GameplayStatics.h"

include "Blueprint/UserWidget.h"

include "SpawnVolume.h"

ABatteryCollectorGameMode::ABatteryCollectorGameMode()
{
// set default pawn class to our Blueprinted character
static ConstructorHelpers::FClassFinder PlayerPawnBPClass(TEXT("/Game/ThirdPersonCPP/Blueprints/ThirdPersonCharacter"));
if (PlayerPawnBPClass.Class != NULL)
{
DefaultPawnClass = PlayerPawnBPClass.Class;
}
DecayRate = 0.01f;
PrimaryActorTick.bCanEverTick = true;

}

void ABatteryCollectorGameMode::BeginPlay()
{
Super::BeginPlay();

TArray FoundActors;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), ASpawnVolume::StaticClass(), FoundActors);
for (auto Actor : FoundActors)
{
    ASpawnVolume* SpawnVolumActor = Cast(Actor);
    if (SpawnVolumActor)
    {
        SpawnVolumActors.AddUnique(SpawnVolumActor);
    }
}

SetCurrentState(EBatteryPlayState::EPlaying);

ABatteryCollectorCharacter* MyCharacter = Cast(UGameplayStatics::GetPlayerPawn(this, 0));
if (MyCharacter)
{
    PowerToWin = MyCharacter->GetInitialPower()*1.25f;
}
if (HUDWidgetClass != NULL)
{
    CurrentWidget = CreateWidget(GetWorld(), HUDWidgetClass);
    if(CurrentWidget != NULL)
    {
        CurrentWidget->AddToViewport();
    }
}

}

void ABatteryCollectorGameMode::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
ABatteryCollectorCharacter* MyCharacter = Cast(UGameplayStatics::GetPlayerPawn(this, 0));
if (MyCharacter)
{
if (MyCharacter->GetCurrentPower() > PowerToWin)
SetCurrentState(EBatteryPlayState::EWon);
else if (MyCharacter->GetCurrentPower() > 0)
{
MyCharacter->UpdatePower(-DeltaSecondsDecayRate(MyCharacter->GetInitialPower()));
}
else
{
SetCurrentState(EBatteryPlayState::EGameOver);
}
}
}

float ABatteryCollectorGameMode::GetPowerToWin()const
{
return PowerToWin;
}

EBatteryPlayState ABatteryCollectorGameMode::GetCurrentState() const
{
return CurrentState;
}

void ABatteryCollectorGameMode::SetCurrentState(EBatteryPlayState state)
{
CurrentState = state;
HandleNewState(CurrentState);
}

void ABatteryCollectorGameMode::HandleNewState(EBatteryPlayState NewState)
{
switch (NewState)
{
case EBatteryPlayState::EPlaying:
for (ASpawnVolume* Volume:SpawnVolumActors)
{
Volume->SetSpawningPointInVolume(true);
}
break;
case EBatteryPlayState::EGameOver:
{
for (ASpawnVolume* Volume : SpawnVolumActors)
{
Volume->SetSpawningPointInVolume(false);
}
APlayerController* PlayerController = UGameplayStatics::GetPlayerController(this, 0);
if (PlayerController)
{
PlayerController->SetCinematicMode(true, false, false, true, true); //屏蔽移动和旋转输入
}
ACharacter* MyCharacter = UGameplayStatics::GetPlayerCharacter(this, 0);
if (MyCharacter)
{
MyCharacter->GetMesh()->SetSimulatePhysics(true); //开启角色物理模拟 瘫痪效果
MyCharacter->GetMovementComponent()->MovementState.bCanJump = false; //屏蔽跳跃输入
}
break;
}

case EBatteryPlayState::EWon:
    for (ASpawnVolume* Volume : SpawnVolumActors)
    {
        Volume->SetSpawningPointInVolume(false);
    }
    break;
case EBatteryPlayState::EUnknown:
    break;
default:
    break;
}

}

临时调整一下GameMode_BP Rate时间 方便测试

20 - Handling New Play States_第1张图片
Paste_Image.png

编辑ThirdPersonCharacter 修改他的默认Collision属性 选择Mesh 修改Detail中的Collision CollisionEnabled

20 - Handling New Play States_第2张图片
Paste_Image.png

你可能感兴趣的:(20 - Handling New Play States)