박하의 나날

[09]Battery Collector _ power 일정수준씩 감소시키기

프로그래밍/Unreal

 

13.Powering Down the Character 

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
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "GameFramework/GameMode.h"
#include "BatteryCollectorGameMode.generated.h"
 
UCLASS(minimalapi)
class ABatteryCollectorGameMode : public AGameMode
{
    GENERATED_BODY()
 
public:
    ABatteryCollectorGameMode();
 
    virtual void Tick(float DeltaTime) override;
 
protected:
    /*The rate at which the character loswes power    */
    UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Power",

Meta = (BlueprintProtected = "true"));

    float DecayRate;
};
 
 
 
 
cs

캐릭터가 아닌 게임모드에서 게임의 룰을 설정하는데 그런 룰 중 하나에 캐릭터가 얼마나 빠르게
 파워가 줄어드느냐 가 들어있어서 여기서 설정.

파워가 줄어드는 정도는 예를 들면 난이도에 따라서라던가.

19)캐릭터의 파워가 줄어드는 정도

 

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
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
 
#include "BatteryCollector.h"
#include "BatteryCollectorGameMode.h"
#include "BatteryCollectorCharacter.h"
#include "Kismet/GameplayStatics.h"  
ABatteryCollectorGameMode::ABatteryCollectorGameMode()
{
    // set default pawn class to our Blueprinted character
    static ConstructorHelpers::FClassFinder<APawn> 
PlayerPawnBPClass(TEXT("/Game/ThirdPersonCPP/Blueprints/ThirdPersonCharacter"));
    if (PlayerPawnBPClass.Class != NULL)
    {
        DefaultPawnClass = PlayerPawnBPClass.Class;
    }
 
    //base decay rate = test
    DecayRate = 0.01f;
}
 
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 the character's power is positive
        if (MyCharacter->GetCurrentPower() > 0){
            //decrease the character's power using the decay rate
            MyCharacter->UpdatePower(-DeltaTime*DecayRate*(MyCharacter->GetInitialPower()));
        }
    }
}
 
 
 
cs

6,7)캐릭터에 엑세스하고 캐릭터 사용

23)Tick에서 반드시 써줘야하는 부분

26) 기본 폰(default pawn)등을 사용할 경우를 대비해서 안전하게 가져오기위한 형변환

배터리 코렉터 캐릭터를 사용하고 있는지 확인

28~32) 캐릭터의 power를 decay rate 만큼 감소시킨다.