官术网_书友最值得收藏!

The Gladiator header (.h) file

Now let's jump back to the header file again, and let's start adding some more functions and variables to it so we can start building gameplay logic for the character and make something that fits our target.

Considering that a class based on FTableRowBase and called AGameDataTables will be used later to read the gameplay data from Excel tables; here is the header file code I ended up with.

To make it easier to understand the code, I would like to breakdown all the variable components and methods into a set of chunks; that way it will be very easy to understand them.

Everything starts with the includes, just like any form of C++ coding you are used to making, including the header files that are going to be used or referenced and must be done at the top of the code.

#pragma once
#include "GameDataTables.h"
#include "GameFramework/Character.h"
#include "Gladiator.generated.h"

Defining the class itself is essentially a step directly after the include statements.

UCLASS(config = Game)
class AGladiator : public ACharacter
{
  GENERATED_BODY()

BeginPlay: This is the virtual void of the override BeginPlay from the Character class base and this one will be called once the game is started.

virtual void BeginPlay() override;

CameraBoom: This is a USpringArmComponent that will be added to the character blueprint that is based on that class. This component will be used to control the camera.

//Camera boom positioning the camera behind the character
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class USpringArmComponent* CameraBoom;

FollowCamera: This is the camera itself that will be viewing the game and following the player. This one will also be added to the blueprints.

//Follow camera
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
class UCameraComponent* FollowCamera;

EffectSprite: This is a Paper2D sprite component (Paper 2D is the main 2D framework for Unreal Engine 4.x). There are lots of ways we can use this to achieve an on-screen draw texture, but this one is the easiest and most flexible. I managed to add a sprite component that is very close to the camera, and then we can use it to draw whatever effect we need.

//The sprite used to draw effect, better and more contrallable than using the HUD or Textures
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Effects, meta = (AllowPrivateAccess = "true"))
class UPaperSpriteComponent* EffectSprite;

AGladiator: This is the constructor, and as mentioned earlier, it is used to build the object in edit mode.

public:
AGladiator();

BaseTurnRate: This is a float variable in degrees to control the camera turn rate.

//Base turn rate, in deg/sec. Other scaling may affect final turn rate.
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
float BaseTurnRate;

BaseLookUpRate: Another float variable to control the camera, but this time it's for lookups. This one is also in degrees.

//Base look up/down rate, in deg/sec. Other scaling may affect final rate.
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera)
float BaseLookUpRate;

JumpingVelocity: This is a float variable to determine the jump velocity.

//Base Jump velocity
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Player Attributes")
float jumppingVelocity;

IsStillAlive: This is a Boolean variable to tell us what the current state of the layer is. It is a very important variable, as most of player behavior and inputs will be based on it.

//Is the player dead or not
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Player Attributes")
bool IsStillAlive;

IsAttacking: Another Boolean variable to report if the player is attacking now or not. It is important for animations.

//is the player attacking right now?
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Player Attributes")
bool IsAttacking;

WeaponIndex: This is an integer to determine the current active weapon index. The player could have several weapons; to be able to load the weapon's data, it is a good idea to give each weapon its own index.

//the index of the current active weapon.
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Player Attributes")
int32 WeaponIndex;

IsControlable: Sometimes the player is not dead, but you also need to take the control out of his hands, maybe because the character is carrying out an attack, or maybe because the player paused the game. So this is a bool variable meant to tell us if the player is in control now or not.

//To be able to disable the player during cutscenes, menus, death....etc
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Player Attributes")
bool IsControlable;

TablesInstance: A datatable variable to hold the active instance of the game tables. It is just here to load some data.

UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Game DataTables")
AGameDataTables* TablesInstance;

GetIsStillAlive: A getter method to return the value of the IsStillAlive Boolean variable.

//Return if the player dead or alive
UFUNCTION(BlueprintCallable, Category = "Player Attributes")
bool GetIsStillAlive() const { return IsStillAlive; }

OnSetPlayerController: A method that takes a parameter of true or false, and uses it to set the status of the player controller. So it is here we take the control from the player, or give it to him at a certain moment.

//Enable or disable inputs
UFUNCTION(BlueprintCallable, Category = "Player Attributes")
void OnSetPlayerController(bool status);

OnChangeHealthByAmount: A method that takes a float value, and reduces the total player health using it. It is usually used when the player gets damaged.

//the attack effect on health
UFUNCTION(BlueprintCallable, Category = "Player Attributes")
void OnChangeHealthByAmount(float usedAmount);

OnGetHealthAmount: This is a getter function that returns the TotalHealth value of the player as a float value.

UFUNCTION(BlueprintCallable, Category = "Player Attributes")
float OnGetHealthAmount() const {return TotalHealth;}

OnPostAttack: A method that holds some procedurals after the player has done an attack.

UFUNCTION(BlueprintCallable, Category = "Player Actions")
void OnPostAttack();

GetCameraBoom: A getter method to return the CameraBoom component variable.

//Returns CameraBoom subobject
FORCEINLINE class USpringArmComponent* GetCameraBoom() const { return CameraBoom; }

GetFollowCamera: Another getter method to return the FollowCamera component variable.

//Returns FollowCamera subobject
FORCEINLINE class UCameraComponent* GetFollowCamera() const { return FollowCamera; }

MoveForward: A method that holds the code responsible for player movement to the forward and backward. Notice that it is a BlueprintCallable in order to be able to use it from the Gladiator class blueprint instances.

protected:
UFUNCTION(BlueprintCallable, Category = "Player Actions")
void MoveForward(float Value);

MoveRight: A method that holds the code responsible for player movement to the left and right.

UFUNCTION(BlueprintCallable, Category = "Player Actions")
void MoveRight(float Value);

Jump: A method that is responsible for applying the jump action to the character based on the base character class.

UFUNCTION(BlueprintCallable, Category = "Player Actions")
void Jump();

StopJumping: A method that is responsible for stopping the jump, and resuming the idle/run animation.

UFUNCTION(BlueprintCallable, Category = "Player Actions")
void StopJumping();

OnAttack: A method that is responsible for attacking.

UFUNCTION(BlueprintCallable, Category = "Player Actions")
void OnAttack();

OnChangeWeapon: A method that is responsible for switching between weapons.

UFUNCTION(BlueprintCallable, Category = "Player Actions")
void OnChangeWeapon();

TurnAtRate: A method that is responsible for applying turns to the following camera.

//Called via input to turn at a given rate.
void TurnAtRate(float Rate);

LookUpAtRate: A method that is responsible for applying the camera look-up rate to the follow camera.

//Called via input to turn look up/down at a given rate.
void LookUpAtRate(float Rate);

TotalHealth: A float variable that holds the player's total health (the current health), as at any moment the player's health gets reduced, that will be the final value. Some people like the approach of creating two variables:

  • TotalHealth: This is the base and default health
  • CurrentHealth: This represents the current health status

Feel free to use any approach you want, but I like to make one health variable for such a small game.

//The health of the enemy
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player Attributes")
float TotalHealth;

AttackRange: A float variable that holds the current weapon attacking range.

//The range for the enemy attack
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Player Attributes")
float AttackRange;

SetupPlayerInputComponent: Another override of a virtual function from the base class, and this one is called to set up and map the key inputs (the ones we made in Chapter 1, Preparing for a Big Project) to the class.

protected:
//APawn interface
virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
  //End of APawn interface
};

As you can see, there are some stylings that have been applied to the code. It looks weird, strange, and unique at first glance, but once you get used to writing Unreal code you'll get very familiar with these things.

The styling I used involved some access specifiers and macros, which include:

  • Public: Any variables or methods listed underneath this access specifier will be public
  • Private: Any variable or method listed underneath this access specifier will be private
  • UPROPERTY: This is used before a variable and is a macro to define attributes for the variable, such as how and where it should or could be changed or seen, and its category to make sure it is listed in the correct place
  • UFUNCTION: This is used only before a function and is a macro to define attributes for a function, such as how and where it should or could be called, and its category to make sure it is listed in the correct place
主站蜘蛛池模板: 洛南县| 金乡县| 海南省| 崇阳县| 中宁县| 屯门区| 韩城市| 乐山市| 道孚县| 剑河县| 修水县| 霍山县| 松阳县| 时尚| 哈巴河县| 札达县| 红原县| 东台市| 大姚县| 老河口市| 拉孜县| 林西县| 若尔盖县| 丹东市| 平原县| 古蔺县| 容城县| 临湘市| 温宿县| 上饶县| 南溪县| 吴堡县| 海宁市| 德庆县| 子洲县| 兴国县| 凤冈县| 西青区| 太湖县| 宁乡县| 涟源市|