5654人加入学习
(18人评价)
【旧版】Unreal初级课程 - 3D吃豆人

旧版课程,制作完成于2018-03-29,基于Unreal 4.18

价格 免费

 

使用 UGameplayStatics :: GetGameMode,需要引入的头文件是:#include "Kismet/GameplayStatics.h"

 

PacManCharacter.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "PacManGameModeBase.h"
#include "PacManCharacter.generated.h"

UCLASS()
class PACMAN_API APacManCharacter : public ACharacter
{
	GENERATED_BODY()

public:
	// Sets default values for this character's properties
	APacManCharacter();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

	void MoveXAxis(float AxisValue);
	void MoveYAxis(float AxisValue);
	// 重启游戏
	void ReStart();
	// 重玩一局游戏
	void NewGame();
	// 暂停游戏
	void Pause();
private:
	// 定义一个向量
	FVector CurrentVelocity;

	APacManGameModeBase* GameMode;
};

private:
    APacManGameModeBase* GameMode;

 

PacManCharacter.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "Public/PacManCharacter.h"
#include "Components/InputComponent.h"
#include "Kismet/GameplayStatics.h"
#include "Engine/World.h"
// Sets default values
APacManCharacter::APacManCharacter()
{
 	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void APacManCharacter::BeginPlay()
{
	Super::BeginPlay();
	// 获得当前的游戏模式
	// 使用 UGameplayStatics 需要引入的头文件:#include "Kismet/GameplayStatics.h"
	// 需要强制转换,使用 Cast <APacManGameModeBase>()
	GameMode = Cast<APacManGameModeBase>(UGameplayStatics::GetGameMode(this));
}

// Called every frame
void APacManCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

// Called to bind functionality to input
void APacManCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);
	// 第一个参数,绑定的轴的名字,与编辑器里面对应的;
	// 第二个参数,this,绑定这个类上;
	// 第三个参数,绑定的哪个函数
	// InputComponent 报红,引入#include "Components/InputComponent.h"头文件解决
	// 传递的引用,需要加上 & 符号
	PlayerInputComponent->BindAxis("MoveX", this, &APacManCharacter::MoveXAxis);
	PlayerInputComponent->BindAxis("MoveY", this, &APacManCharacter::MoveYAxis);

	// 绑定行为,多了一个参数,IE_Pressed 按下的时候触发
	PlayerInputComponent->BindAction("NewGame", IE_Pressed, this, &APacManCharacter::NewGame);
	PlayerInputComponent->BindAction("ReStart", IE_Pressed, this, &APacManCharacter::ReStart);
	PlayerInputComponent->BindAction("Pause", IE_Pressed, this, &APacManCharacter::Pause);
}

void APacManCharacter::MoveXAxis(float AxisValue)
{
	CurrentVelocity.X = AxisValue;
	AddMovementInput(CurrentVelocity);
}

void APacManCharacter::MoveYAxis(float AxisValue)
{
	CurrentVelocity.Y = AxisValue;
	AddMovementInput(CurrentVelocity);
}
// 重启游戏
void APacManCharacter::ReStart()
{
	// 使用GetWorld()需要引入头文件:#include "Engine/World.h"
	GetWorld()->GetFirstPlayerController()->ConsoleCommand(TEXT("RestartLevel"));
}
// 重来一局游戏
void APacManCharacter::NewGame()
{
	if (GameMode->GetCurrentState() == EGameState::EMenu)
	{
		GameMode->SetCurrentState(EGameState::EPlaying);
	}
}
// 暂停游戏
void APacManCharacter::Pause()
{
	if (GameMode->GetCurrentState() == EGameState::EPlaying)
	{
		GameMode->SetCurrentState(EGameState::EPause);
	}
	else if (GameMode->GetCurrentState() == EGameState::EPause)
	{
		GameMode->SetCurrentState(EGameState::EPlaying);
	}
}

#include "Kismet/GameplayStatics.h"
#include "Engine/World.h"

 

void APacManCharacter::BeginPlay()
{
    Super::BeginPlay();
    // 获得当前的游戏模式
    // 使用 UGameplayStatics 需要引入的头文件:#include "Kismet/GameplayStatics.h"
    // 需要强制转换,使用 Cast <APacManGameModeBase>()
    GameMode = Cast<APacManGameModeBase>(UGameplayStatics::GetGameMode(this));
}

 

void APacManCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);
    // 第一个参数,绑定的轴的名字,与编辑器里面对应的;
    // 第二个参数,this,绑定这个类上;
    // 第三个参数,绑定的哪个函数
    // InputComponent 报红,引入#include "Components/InputComponent.h"头文件解决
    // 传递的引用,需要加上 & 符号
    PlayerInputComponent->BindAxis("MoveX", this, &APacManCharacter::MoveXAxis);
    PlayerInputComponent->BindAxis("MoveY", this, &APacManCharacter::MoveYAxis);

    // 绑定行为,多了一个参数,IE_Pressed 按下的时候触发
    PlayerInputComponent->BindAction("NewGame", IE_Pressed, this, &APacManCharacter::NewGame);
    PlayerInputComponent->BindAction("ReStart", IE_Pressed, this, &APacManCharacter::ReStart);
    PlayerInputComponent->BindAction("Pause", IE_Pressed, this, &APacManCharacter::Pause);
}

 

 

// 重启游戏
void APacManCharacter::ReStart()
{
    // 使用GetWorld()需要引入头文件:#include "Engine/World.h"
    GetWorld()->GetFirstPlayerController()->ConsoleCommand(TEXT("RestartLevel"));
}
// 重来一局游戏
void APacManCharacter::NewGame()
{
    if (GameMode->GetCurrentState() == EGameState::EMenu)
    {
        GameMode->SetCurrentState(EGameState::EPlaying);
    }
}
// 暂停游戏
void APacManCharacter::Pause()
{
    if (GameMode->GetCurrentState() == EGameState::EPlaying)
    {
        GameMode->SetCurrentState(EGameState::EPause);
    }
    else if (GameMode->GetCurrentState() == EGameState::EPause)
    {
        GameMode->SetCurrentState(EGameState::EPlaying);
    }
}

 

 

引擎- 输入

Actiong Mappings

NewGame 按键N

ReStart 按键 R

Pause 按键 P

[展开全文]

授课教师

SIKI学院老师

课程特色

图文(1)
下载资料(1)
视频(30)

学员动态