1
5个状态:
①当我们刚进入游戏的时候,给我们一个提示说,欢迎来到PacMan,menu菜单
②游戏中 的状态,
③游戏暂停,
④游戏胜利,
⑤GameOver
状态之间可以切换
按下P键,暂停
按下N键,重新开始游戏
状态放到 GameMode 的里面 PacManGameModeBase.cpp
GameMode 用来处理游戏规则的
在 PacManGaemModeBase.h 文件中新增
enum class EGameState : short {
EMenu,
EPlaying,
EPause,
EWin,
EGameOver
};
要知道当前状态,才能做出不同的反应
private:
// 要知道当前状态,才能做出不同的反应
EGameState currentState;
2个方法:获得当前状态,给当前状态设值
public:
// 获得当前状态 加 const 不能修改我们的成员
EGameState GetCurrentState() const;
// 设置当前状态
void SetCurrentState(EGameState value);
// 为了程序更加快捷,定义一个 内联
// APcManGameModeBase 和 命名的项目名称一致,视频中的AMyPacManGameModeBase,项目名称为:MyPacMan
FORCEINLINE EGameState APacManGameModeBase::GetCurrentState() const
{
return currentState;
};
去PacManGameModeBase.cpp 中实现
void APacManGameModeBase::SetCurrentState(EGameState value)
{
currentState = value;
}
在PacManCharacter.h 中
public:
void ReStart();
void NewGame();
void Pause();
在PacManCharacter.cpp 中
// 重启游戏
void APacManCharacter :: ReStart()
{
}
// 重来一局游戏
void APacManCharacter :: NewGame()
{
}
// 暂停游戏
void APacManCharacter :: Pause()
{
}
PacManGameModeBase.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "PacManGameModeBase.generated.h"
/**
*
*/
enum class EGameState : short {
EMenu,
EPlaying,
EPause,
EWin,
EGameOver
};
UCLASS()
class PACMAN_API APacManGameModeBase : public AGameModeBase
{
GENERATED_BODY()
public:
// 获得当前状态 加 const 不能修改我们的成员
EGameState GetCurrentState() const;
// 设置当前状态
void SetCurrentState(EGameState value);
private:
// 要知道当前状态,才能做出不同的反应
EGameState currentState;
};
// 为了程序更加快捷,定义一个 内联
// APcManGameModeBase 和 命名的项目名称一致,视频中的AMyPacManGameModeBase,项目名称为:MyPacMan
FORCEINLINE EGameState APacManGameModeBase::GetCurrentState() const
{
return currentState;
};
PacManGameModeBase.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "PacManGameModeBase.h"
// 设置当前状态
void APacManGameModeBase::SetCurrentState(EGameState value)
{
currentState = value;
}
PacManCharacter.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.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();
// 定义一个向量
FVector CurrentVelocity;
};
PacManCharacter.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "Public/PacManCharacter.h"
#include "Components/InputComponent.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();
}
// 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);
}
void APacManCharacter::MoveXAxis(float AxisValue)
{
CurrentVelocity.X = AxisValue;
AddMovementInput(CurrentVelocity);
}
void APacManCharacter::MoveYAxis(float AxisValue)
{
CurrentVelocity.Y = AxisValue;
AddMovementInput(CurrentVelocity);
}
// 重启游戏
void APacManCharacter::ReStart()
{
}
// 重来一局游戏
void APacManCharacter::NewGame()
{
}
// 暂停游戏
void APacManCharacter::Pause()
{
}