박하의 나날

[22]npc 대화상자

프로그래밍/Unreal

NPC.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
31
// Fill out your copyright notice in the Description page of Project Settings.
 
#pragma once
 
#include "GameFramework/Character.h"
#include "NPC.generated.h"
 
UCLASS()
class BOOK_0528_API ANPC : public ACharacter
{
    GENERATED_BODY()
 
public:
    // Sets default values for this character's properties
    ANPC();
 
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCmsg)
    FString NpcMessage;
 
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Collision)
    class USphereComponent* ProxSphere;
 
 
    UFUNCTION(BlueprintNativeEvent, Category = Collision)
    void Prox(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult);
 
    virtual void ANPC::Prox_Implementation(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, 
bool bFromSweep, const FHitResult & SweepResult);
 
};
 
cs

 

NPC.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
// Fill out your copyright notice in the Description page of Project Settings.
 
#include "Book_0528.h"
#include "NPC.h"
#include "MyHUD.h"
#include "MyCharacter.h"
 
// Sets default values
ANPC::ANPC()
{
    // Set this character to call Tick() every frame.  
    PrimaryActorTick.bCanEverTick = false;
 
    ProxSphere = CreateDefaultSubobject<USphereComponent>(TEXT("Proximity Sphere"), false);
    ProxSphere->AttachTo(RootComponent);
    ProxSphere->SetSphereRadius(100.0f);
 
    ProxSphere->OnComponentBeginOverlap.AddDynamic(this&ANPC::Prox);
    
    //NpcMessage = TEXT("hello");
}
 
void ANPC::Prox_Implementation(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult){
    if (Cast<AMyCharacter>(OtherActor) == nullptr){
        return;
    }
    APlayerController* PCon = GetWorld()->GetFirstPlayerController();
    if (PCon){
        AMyHUD* hud = Cast<AMyHUD>(PCon->GetHUD());
        hud->addMessage(Message(NpcMessage, 5.f, FColor::Red));
    }    
}
 
 
cs

 

MyHUD.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
31
32
33
34
35
36
37
38
39
40
41
42
// Fill out your copyright notice in the Description page of Project Settings.
 
#pragma once
 
#include "GameFramework/HUD.h"
#include "MyHUD.generated.h"
 
struct Message{
    FString message;
    float time;
    FColor color;
    Message(){
        time = 5.f;
        color = FColor::White;
    }
    Message(FString iMessage, float iTime, FColor iColor){ 
        message = iMessage;
        time = iTime;
        color = iColor;
    }
};
 
UCLASS()
class BOOK_0528_API AMyHUD : public AHUD
{
    GENERATED_BODY()
 
public:
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = HUDFont)
    UFont* hudFont;
    virtual void DrawHUD() override;
 
    TArray<Message> messages;
 
    void addMessage(Message msg);
 
    //These are the ims of the screen
    FVector2D dims;
 
    void DrawMessages();    
};
 
cs

 

MyHUD.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
// Fill out your copyright notice in the Description page of Project Settings.
 
#include "Book_0528.h"
#include "MyHUD.h"
#include "MyCharacter.h"
 
void AMyHUD::DrawHUD(){
    Super::DrawHUD();
    dims.X = Canvas->SizeX;
    dims.Y = Canvas->SizeY;
        //DrawLine(200, 300, 400, 500, FLinearColor::Blue);
        //DrawText(TEXT("its ok??"), FVector2D(0, 0), hudFont, FVector2D(1, 1), FColor::Red);
    DrawMessages();
}
 
void AMyHUD::DrawMessages(){
    for (int c = messages.Num() - 1; c >= 0; c--){
        float ow, oh, pad = 10.f;
        GetTextSize(messages[c].message, ow, oh, hudFont, 1.f);
        float messageH = oh + 2.* pad;
        float x = 0.f, y = c * messageH;
 
        //draw background to black
        DrawRect(FLinearColor::Black, x, y, dims.X, messageH);
        //draw message use to hudFont
        DrawText(messages[c].message, messages[c].color, x + pad, y + pad, hudFont);
 
        messages[c].time -= GetWorld()->GetDeltaSeconds();
        if (messages[c].time < 0){
            messages.RemoveAt(c);
        }
    }
}
void AMyHUD::addMessage(Message msg){
    messages.Add(msg);
}
 
cs

 

 

 

'프로그래밍 > Unreal' 카테고리의 다른 글

[24]템플릿과 자주 사용되는 컨테이너들_2  (0) 2017.06.06
[23] 템플릿과 자주 사용되는 컨테이너들  (0) 2017.06.04
[21]언리얼 오브젝트 처리  (0) 2017.05.22
[20] Object  (0) 2017.05.22
0521  (0) 2017.05.21