Add single player HUD

This commit is contained in:
Sara Montecino 2022-11-17 01:40:04 -08:00
parent f538a55384
commit b7ac77f7c1
13 changed files with 126 additions and 17 deletions

Binary file not shown.

Binary file not shown.

BIN
Content/HUDWidget.uasset Executable file

Binary file not shown.

BIN
Content/PlayerHUD.uasset Executable file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -8,7 +8,6 @@
#include "Kismet/KismetSystemLibrary.h" #include "Kismet/KismetSystemLibrary.h"
#include "Util.h" #include "Util.h"
#define LogInfo(Msg) Util::log_info(TEXT("ABasePawn"), Msg)
#define LogError(Msg) Util::log_error(TEXT("ABasePawn"), Msg) #define LogError(Msg) Util::log_error(TEXT("ABasePawn"), Msg)
// Sets default values // Sets default values
@ -71,7 +70,6 @@ void ABasePawn::handle_hit(
{ {
// Update current health. // Update current health.
current_health -= damage_class_map[other_class]; current_health -= damage_class_map[other_class];
LogInfo(FString::Printf(TEXT("Total health %d"), current_health));
} }
} }
@ -102,10 +100,10 @@ void ABasePawn::Tick(float DeltaTime)
/* /*
* Handle death. * Handle death.
*/ */
if (current_health <= 0) if (current_health <= 0 && !death_handled)
{ {
handle_death(); handle_death();
current_health = max_health; death_handled = true;
} }
} }
@ -155,10 +153,9 @@ void ABasePawn::shoot()
if (use_aim_assist) if (use_aim_assist)
{ {
float assist_yaw; float assist_yaw;
const bool succeess = aim_assist(player_location, rotator, assist_yaw); const bool success = aim_assist(player_location, rotator, assist_yaw);
if (assist_yaw) if (success)
{ {
LogInfo("Aim assist activated!");
rotator.Yaw = assist_yaw; rotator.Yaw = assist_yaw;
} }
} }
@ -272,4 +269,4 @@ void ABasePawn::destroy_self()
* Remove thyself from this world. * Remove thyself from this world.
*/ */
Destroy(); Destroy();
} }

View File

@ -8,6 +8,8 @@
#include "Kismet/KismetMathLibrary.h" #include "Kismet/KismetMathLibrary.h"
#include "BasePawn.generated.h" #include "BasePawn.generated.h"
#define LogInfo(Msg) Util::log_info(TEXT("ABasePawn"), Msg)
class UFloatingPawnMovement; class UFloatingPawnMovement;
UCLASS() UCLASS()
@ -21,6 +23,16 @@ public:
FVector get_location() const; FVector get_location() const;
FORCEINLINE float get_current_health_percentage() const
{
if (max_health == 0)
{
return 0;
}
return (current_health * 1.0f) / max_health;
}
// Called every frame // Called every frame
virtual void Tick(float DeltaTime) override; virtual void Tick(float DeltaTime) override;
virtual void handle_move_right(float axis); virtual void handle_move_right(float axis);
@ -141,6 +153,11 @@ protected:
*/ */
int32 current_health; int32 current_health;
/**
* Death handled.
*/
bool death_handled;
/** /**
* Current ammo. * Current ammo.
*/ */

View File

@ -6,6 +6,7 @@
#include "GameFramework/FloatingPawnMovement.h" #include "GameFramework/FloatingPawnMovement.h"
#include "Math/UnrealMathUtility.h" #include "Math/UnrealMathUtility.h"
#include "Util.h" #include "Util.h"
#include "EngineUtils.h"
#define LogInfo(Msg) Util::log_info(TEXT("APlayerPawn"), Msg) #define LogInfo(Msg) Util::log_info(TEXT("APlayerPawn"), Msg)
#define LogError(Msg) Util::log_error(TEXT("APlayerPawn"), Msg) #define LogError(Msg) Util::log_error(TEXT("APlayerPawn"), Msg)
@ -68,24 +69,29 @@ void APlayerPawn::handle_overlap(
} }
other_actor->Destroy(); other_actor->Destroy();
LogInfo(FString::Printf(TEXT("Total ammo %d"), current_ammo));
} }
} }
void APlayerPawn::Tick(float delta_time) void APlayerPawn::Tick(float delta_time)
{ {
Super::Tick(delta_time); Super::Tick(delta_time);
if (cooldown_wait_s <= cooldown_threshold_s) if (!init)
{
president_ref = get_president();
init = true;
}
if (cooldown_wait_s < cooldown_threshold_s)
{ {
cooldown_wait_s += delta_time; cooldown_wait_s += delta_time;
} }
if (reset_wait_s <= reset_threshold_s) if (reset_wait_s < reset_threshold_s)
{ {
reset_wait_s += delta_time; reset_wait_s += delta_time;
} }
if (reset_wait_s > reset_threshold_s) if (reset_wait_s >= reset_threshold_s)
{ {
reset_boost(); reset_boost();
} }
@ -102,10 +108,9 @@ void APlayerPawn::Tick(float delta_time)
void APlayerPawn::boost() void APlayerPawn::boost()
{ {
const bool boost_allowed = cooldown_wait_s > cooldown_threshold_s; const bool boost_allowed = cooldown_wait_s >= cooldown_threshold_s;
if (!boost_allowed) if (!boost_allowed)
{ {
LogError("Cannot boost! You're on cooldown");
return; return;
} }
@ -180,4 +185,39 @@ void APlayerPawn::respawn()
EnableInput(controller); EnableInput(controller);
player_mesh->SetHiddenInGame(false); player_mesh->SetHiddenInGame(false);
collision->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics); collision->SetCollisionEnabled(ECollisionEnabled::QueryAndPhysics);
current_health = max_health;
death_handled = false;
}
/**
* Helper function to find president in level.
*/
ABasePawn* APlayerPawn::get_president() const
{
ABasePawn* found = nullptr;
for (TActorIterator<AActor> iterator(GetWorld()); iterator; ++iterator)
{
AActor* actor = *iterator;
if (actor && actor != this)
{
UClass* actor_class = actor->GetClass();
if (actor_class == (*president_class))
{
if (found)
{
LogError("Multiple presidents found!");
break;
}
found = Cast<ABasePawn>(actor);
}
}
}
if (!found)
{
// LogError("No president found!");
}
return found;
} }

View File

@ -23,6 +23,46 @@ public:
// Called to bind functionality to input // Called to bind functionality to input
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override; virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
UFUNCTION(BlueprintCallable)
FORCEINLINE float get_president_health() const
{
if (president_ref)
{
return president_ref->get_current_health_percentage();
}
return 0;
}
UFUNCTION(BlueprintCallable)
FORCEINLINE float get_boost_cooldown_percentage() const
{
if (reset_wait_s < reset_threshold_s)
{
return 0;
}
if (cooldown_wait_s >= cooldown_threshold_s)
{
return 1.0f;
}
return cooldown_wait_s / cooldown_threshold_s;
}
UFUNCTION(BlueprintCallable)
FORCEINLINE int32 get_current_ammo() const
{
return current_ammo;
}
UFUNCTION(BlueprintCallable)
FORCEINLINE int32 get_max_ammo() const
{
return max_ammo;
}
protected: protected:
virtual void BeginPlay() override; virtual void BeginPlay() override;
virtual void destroy_self() override; virtual void destroy_self() override;
@ -42,6 +82,9 @@ protected:
UPROPERTY(EditAnywhere, Category = "Player") UPROPERTY(EditAnywhere, Category = "Player")
TMap<UClass*, int> ammo_class_map; TMap<UClass*, int> ammo_class_map;
UPROPERTY(EditAnywhere, Category = "Player")
TSubclassOf<ABasePawn> president_class;
/** /**
* Multiplier to apply to current speed while boosting. Applies to max speed and accleration. * Multiplier to apply to current speed while boosting. Applies to max speed and accleration.
*/ */
@ -74,6 +117,7 @@ private:
void boost(); void boost();
void reset_boost(); void reset_boost();
void respawn(); void respawn();
ABasePawn* get_president() const;
/** /**
* Default max speed. Cached when boost is called. * Default max speed. Cached when boost is called.
@ -99,4 +143,14 @@ private:
* Time waiting for respawn. * Time waiting for respawn.
*/ */
float respawn_wait_s; float respawn_wait_s;
/**
* True after post tick initialization.
*/
bool init;
/**
* Cached reference to the president.
*/
ABasePawn* president_ref;
}; };

View File

@ -18,10 +18,8 @@ xAdd auto aim
xAdd art xAdd art
x Car models x Car models
x Street shader x Street shader
Add HUD UI xAdd HUD UI for single player.
Add sounds Add sounds
Add win condition
- Time based. Keep president alive until he reaches destination.
Add juice Add juice
Add muzzle on shoot Add muzzle on shoot
Camera zoom out based on screen size? Looks too small when I shrink the window Camera zoom out based on screen size? Looks too small when I shrink the window
@ -29,8 +27,11 @@ Add juice
Improve street shader Improve street shader
Screen shake on zoom Screen shake on zoom
Particle effect on brake Particle effect on brake
Add win condition
- Time based. Keep president alive until he reaches destination.
Add menu Add menu
Add local multiplayer Add local multiplayer
x Blueprint x Blueprint
- C++ - C++
- HUD UI for multiplayer
Add netcode Add netcode