박하의 나날

[14]Battery Collector_player 상태 설정

프로그래밍/Unreal

18.Setting Up the Play States 

 

BatteryCollectorGameMode.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "GameFramework/GameMode.h"
#include "BatteryCollectorGameMode.generated.h"
 
 
//enum to store the current state of gameplay
UENUM(BlueprintType)
enum class EBatteryPlayState{
    EPlaying,
    EGameOver,
    EWon,
    EUnknown
};
 
UCLASS(minimalapi)
class ABatteryCollectorGameMode : public AGameMode
{
    GENERATED_BODY() 
public:
/**Returns the current playing state*/
    UFUNCTION(Blueprintpure, Category = "Power")
    EBatteryPlayState GetCurrentState() const;
    //Sets a new playing state
    void SetCurrentState(EBatteryPlayState NewState)
private:
    /**Keeps track of the current playing state*/
    EBatteryPlayState CurrentState;
};
 
cs

8~14)상태. EUnknown = default 

23) 현재상태 리턴

25) 새로운 상태 설정

 

BatteryCollectorGameMode.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
 
#include "BatteryCollector.h"
#include "BatteryCollectorGameMode.h"
#include "BatteryCollectorCharacter.h"
#include "Kismet/GameplayStatics.h"
#include "Blueprint/UserWidget.h"
 
void ABatteryCollectorGameMode::Tick(float DeltaTime){
 
    Super::Tick(DeltaTime); 
    //Check that we are using the battery collector character
    ABatteryCollectorCharacter* MyCharacter = Cast<ABatteryCollectorCharacter>(UGameplayStatics::GetPlayerPawn(this0));
    if (MyCharacter){
        //If our power is greater than needed to win, set the game's state to won
        if (MyCharacter->GetCurrentPower() > PowerToWin){
            SetCurrentState(EBatteryPlayState::EWon);
        }
        //if the character's power is positive
        else if (MyCharacter->GetCurrentPower() > 0){
            //decrease the character's power using the decay rate
            MyCharacter->UpdatePower(-DeltaTime*DecayRate*(MyCharacter->GetInitialPower()));
        }
        else{
            SetCurrentState(EBatteryPlayState::EGameOver);
        }
    }
}
 
 
EBatteryPlayState ABatteryCollectorGameMode::GetCurrentState() const{
    return CurrentState;
}
 
void ABatteryCollectorGameMode::SetCurrentState(EBatteryPlayState NewState){
    CurrentState = NewState;
}
 
 
cs

state text binding