Skip to content

Commit

Permalink
Functioning status-bars
Browse files Browse the repository at this point in the history
  • Loading branch information
ataulien committed Dec 19, 2016
1 parent 5988c44 commit d168d78
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 40 deletions.
9 changes: 8 additions & 1 deletion src/engine/BaseEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
#include <zenload/zCModelPrototype.h>
#include <components/Vob.h>
#include <fstream>
#include <ui/Hud.h>

using namespace Engine;

BaseEngine::BaseEngine()
{

m_pHUD = nullptr;
}

BaseEngine::~BaseEngine()
{
getRootUIView().removeChild(m_pHUD);
delete m_pHUD;
}

void BaseEngine::initEngine(int argc, char** argv)
Expand Down Expand Up @@ -92,6 +95,10 @@ void BaseEngine::initEngine(int argc, char** argv)
else
LogWarn() << "Unknown game files, could not find world.zen or newworld.zen!";
}

// Init HUD
m_pHUD = new UI::Hud(*this);
getRootUIView().addChild(m_pHUD);
}

Handle::WorldHandle BaseEngine::addWorld(const std::string & worldFile, const std::string& savegame)
Expand Down
12 changes: 12 additions & 0 deletions src/engine/BaseEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
#include <ui/View.h>
#include <bx/commandline.h>

namespace UI
{
class Hud;
}

namespace Engine
{
const int MAX_NUM_WORLDS = 4;
Expand Down Expand Up @@ -73,6 +78,12 @@ namespace Engine
*/
UI::View& getRootUIView() { return m_RootUIView; }

/**
* // TODO: Move to GameEngine, or pass GameEngine to world!
* @return HUD
*/
UI::Hud& getHud(){ return *m_pHUD; }

/**
* Sets the path the engine is looking for files
* @param path New path
Expand Down Expand Up @@ -131,6 +142,7 @@ namespace Engine
* Base UI-View
*/
UI::View m_RootUIView;
UI::Hud* m_pHUD;

/**
* Allocator for always present textures
Expand Down
31 changes: 19 additions & 12 deletions src/logic/PlayerController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <engine/Input.h>
#include "ItemController.h"
#include <json.hpp>
#include <ui/Hud.h>

using json = nlohmann::json;
using namespace Logic;
Expand Down Expand Up @@ -155,18 +156,11 @@ void PlayerController::onUpdate(float deltaTime)
}
}

// TODO: HACK, take this out!
// Make following NPCs a bit faster...
/*if(m_RoutineState.entityTarget.isValid())
{
float defSpeed = 7.0f;
if (inputGetKeyState(entry::Key::Space))
m_NPCProperties.moveSpeed = defSpeed * 4;
else if (inputGetKeyState(entry::Key::KeyB))
m_NPCProperties.moveSpeed = defSpeed * 8;
else
m_NPCProperties.moveSpeed = defSpeed;
}*/
// Run extra stuff if this is the controlled character
if(isPlayerControlled())
{
onUpdateForPlayer(deltaTime);
}
}

void PlayerController::continueRoutine()
Expand Down Expand Up @@ -2305,6 +2299,19 @@ void PlayerController::checkUnconscious()
setBodyState(EBodyState::BS_UNCONSCIOUS);
}

void PlayerController::onUpdateForPlayer(float deltaTime)
{
// Nofity hud about current stats
UI::Hud& hud = m_World.getEngine()->getHud();
auto& stats = getScriptInstance();

hud.setHealth(stats.attribute[Daedalus::GEngineClasses::C_Npc::EATR_HITPOINTS] /
(float)stats.attribute[Daedalus::GEngineClasses::C_Npc::EATR_HITPOINTSMAX]);

hud.setMana(stats.attribute[Daedalus::GEngineClasses::C_Npc::EATR_MANA] /
(float)stats.attribute[Daedalus::GEngineClasses::C_Npc::EATR_MANAMAX]);
}




Expand Down
6 changes: 6 additions & 0 deletions src/logic/PlayerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ namespace Logic
*/
void onUpdateByInput(float deltaTime);

/**
* Update routine for the NPC currently controlled by the player
* @param deltaTime Time since last frame
*/
void onUpdateForPlayer(float deltaTime);

/**
* Called at rendertime
*/
Expand Down
46 changes: 28 additions & 18 deletions src/target/REGoth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <logic/PlayerController.h>
#include <ui/ImageView.h>
#include <ui/BarView.h>
#include <ui/Hud.h>

using json = nlohmann::json;

Expand Down Expand Up @@ -565,26 +566,35 @@ class ExampleCubes : public /*entry::AppI*/ PLATFORM_CLASS
return "Interrupted player, cleared EM";
});

imguiCreate(nullptr, 0, fontSize);
m_Console.registerCommand("hurtself", [this](const std::vector<std::string>& args) -> std::string {

auto& alloc = m_pEngine->getEngineTextureAlloc();
Handle::TextureHandle backh = alloc.loadTextureVDF("BAR_BACK.TGA");
Handle::TextureHandle healthh = alloc.loadTextureVDF("BAR_HEALTH.TGA");
VobTypes::NpcVobInformation player = VobTypes::asNpcVob(m_pEngine->getMainWorld().get(),
m_pEngine->getMainWorld().get().getScriptEngine().getPlayerEntity());

if(backh.isValid() && healthh.isValid())
{
Textures::Texture& back = alloc.getTexture(backh);
Textures::Texture& health = alloc.getTexture(healthh);

UI::BarView* bar = new UI::BarView();
bar->setAlignment(UI::A_BottomLeft);
bar->setBackgroundImage(back);
bar->setBarImage(health);
bar->setValue(0.7f);
bar->setTranslation(Math::float2(0.01f, 0.99f));
bar->setSize(Math::float2(0.6f, 0.6f));
m_pEngine->getRootUIView().addChild(bar);
}
if(args.size() < 2)
return "Missing argument. Usage: hurtself <damage>";

int dmg = std::stoi(args[1]);
player.playerController->changeAttribute(Daedalus::GEngineClasses::C_Npc::EATR_HITPOINTS, -dmg);

return "Hurt player by " + std::to_string(dmg) + " HP";
});

m_Console.registerCommand("usemana", [this](const std::vector<std::string>& args) -> std::string {

VobTypes::NpcVobInformation player = VobTypes::asNpcVob(m_pEngine->getMainWorld().get(),
m_pEngine->getMainWorld().get().getScriptEngine().getPlayerEntity());

if(args.size() < 2)
return "Missing argument. Usage: usemana <mana>";

int dmg = std::stoi(args[1]);
player.playerController->changeAttribute(Daedalus::GEngineClasses::C_Npc::EATR_MANA, -dmg);

return "Used " + std::to_string(dmg) + " mana";
});

imguiCreate(nullptr, 0, fontSize);

m_scrollArea = 0;
}
Expand Down
16 changes: 7 additions & 9 deletions src/ui/BarView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extern bgfx::ProgramHandle imguiGetImageProgram(uint8_t _mip);

UI::BarView::BarView()
{

m_Value = 1.0f;
}

UI::BarView::~BarView()
Expand All @@ -33,6 +33,9 @@ void UI::BarView::update(double dt, Engine::Input::MouseState& mstate, Render::R
int px = (int) (absTranslation.x * config.state.viewWidth + 0.5f);
int py = (int) (absTranslation.y * config.state.viewHeight + 0.5f);

int sx = (int) (m_Background.m_Width * absSize.x + 0.5f);
int sy = (int) (m_Background.m_Height * absSize.y + 0.5f);

px += offset.x;
py += offset.y;

Expand All @@ -50,17 +53,12 @@ void UI::BarView::update(double dt, Engine::Input::MouseState& mstate, Render::R
int pxBorderX = (int)((INNER_OFFSET_X * m_Background.m_Width) + 0.5f);
int pxBorderY = (int)((INNER_OFFSET_Y * m_Background.m_Height) + 0.5f);

drawTexture(BGFX_VIEW, px + pxBorderX * absSize.x, py + pxBorderY * absSize.x,
(int)((m_Background.m_Width - pxBorderX * 2) * m_Value * absSize.x + 0.5f),
(int)((m_Background.m_Height - pxBorderY * 2) * absSize.x + 0.5f),
drawTexture(BGFX_VIEW, px + pxBorderX * absSize.x, py + pxBorderY * absSize.y,
(int)((sx - pxBorderX * 2 * absSize.x) * m_Value + 0.5f),
(int)((sy - pxBorderY * 2 * absSize.y) + 0.5f),
config.state.viewWidth, config.state.viewHeight, m_Bar.m_TextureHandle,
program, config.uniforms.diffuseTexture);


imguiBeginArea("", 20, 20, 100, 100);
imguiImage(m_Background.m_TextureHandle, 1, 100, 100);
imguiEndArea();

View::update(dt, mstate, config);
}

Expand Down
1 change: 1 addition & 0 deletions src/ui/BarView.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "View.h"
#include <content/Texture.h>

namespace UI
{
Expand Down
83 changes: 83 additions & 0 deletions src/ui/Hud.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// Created by desktop on 17.12.16.
//

#include "Hud.h"
#include "BarView.h"
#include <engine/BaseEngine.h>

UI::Hud::Hud(Engine::BaseEngine& engine) : m_Engine(engine)
{
Textures::TextureAllocator& alloc = engine.getEngineTextureAlloc();

m_pHealthBar = new BarView();
m_pManaBar = new BarView();
m_pEnemyHealthBar = new BarView();

addChild(m_pHealthBar);
addChild(m_pManaBar);
addChild(m_pEnemyHealthBar);

// Initialize status bars
{
Handle::TextureHandle hBarBackground = alloc.loadTextureVDF("BAR_BACK.TGA");
Handle::TextureHandle hBarHealth = alloc.loadTextureVDF("BAR_HEALTH.TGA");
Handle::TextureHandle hBarMana = alloc.loadTextureVDF("BAR_MANA.TGA");

// Images
m_pHealthBar->setBackgroundImage(alloc.getTexture(hBarBackground));
m_pManaBar->setBackgroundImage(alloc.getTexture(hBarBackground));
m_pEnemyHealthBar->setBackgroundImage(alloc.getTexture(hBarBackground));

m_pHealthBar->setBarImage(alloc.getTexture(hBarHealth));
m_pManaBar->setBarImage(alloc.getTexture(hBarMana));
m_pEnemyHealthBar->setBarImage(alloc.getTexture(hBarHealth));

// Alignment
m_pHealthBar->setAlignment(A_BottomLeft);
m_pManaBar->setAlignment(A_BottomRight);
m_pEnemyHealthBar->setAlignment(A_TopCenter);

// Size
Math::float2 barSize = Math::float2(0.6f, 0.6f);
m_pHealthBar->setSize(barSize);
m_pManaBar->setSize(barSize);
m_pEnemyHealthBar->setSize(barSize);

// Position
m_pHealthBar->setTranslation(Math::float2(0.01f, 0.99f));
m_pManaBar->setTranslation(Math::float2(0.99f, 0.99f));
m_pEnemyHealthBar->setTranslation(Math::float2(0.5f, 0.01f));
}
}

UI::Hud::~Hud()
{
removeChild(m_pHealthBar);
removeChild(m_pManaBar);
removeChild(m_pEnemyHealthBar);

delete m_pManaBar;
delete m_pHealthBar;
delete m_pEnemyHealthBar;
}

void UI::Hud::update(double dt, Engine::Input::MouseState& mstate, Render::RenderConfig& config)
{
View::update(dt, mstate, config);
}

void UI::Hud::setHealth(float value)
{
m_pHealthBar->setValue(value);
}

void UI::Hud::setMana(float value)
{
m_pManaBar->setValue(value);
}

void UI::Hud::setEnemyHealth(float value)
{
m_pEnemyHealthBar->setValue(value);
}
50 changes: 50 additions & 0 deletions src/ui/Hud.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once
#include "View.h"

namespace Engine
{
class BaseEngine;
}

namespace UI
{
class BarView;
class Hud : public View
{
public:
Hud(Engine::BaseEngine& engine);
~Hud();

/**
* Updates/draws the UI-Views
* @param dt time since last frame
* @param mstate mouse-state
*/
void update(double dt, Engine::Input::MouseState &mstate, Render::RenderConfig &config) override;

/**
* @param value Value for health-bar in 0..1 range
*/
void setHealth(float value);

/**
* @param value Value for mana-bar in 0..1 range
*/
void setMana(float value);

/**
* @param value Value for enemy health-bar in 0..1 range
*/
void setEnemyHealth(float value);
protected:

BarView* m_pHealthBar;
BarView* m_pManaBar;
BarView* m_pEnemyHealthBar;

/**
* Engine access, ie. for main texture allocator and VDFS-index
*/
Engine::BaseEngine& m_Engine;
};
}

0 comments on commit d168d78

Please sign in to comment.