--
[ Picked text/plain from multipart/alternative ]
Hello all.
After completing ep2 i wanted to recreate the flashlight. So i got the hud
element but only set it for the suit.
//========= Copyright © 1996-2005, Valve Corporation, All rights reserved.
============//
//
// Purpose:
//
//=============================================================================//
#include "cbase.h"
#include "hud.h"
#include "text_message.h"
#include "hud_macros.h"
#include "iclientmode.h"
#include "view.h"
#include "KeyValues.h"
#include "vgui_controls/AnimationController.h"
#include "vgui/ILocalize.h"
#include "vgui/ISurface.h"
#include "vguimatsurface/IMatSystemSurface.h"
#include "materialsystem/IMaterial.h"
#include "materialsystem/IMesh.h"
#include "materialsystem/imaterialvar.h"
#include "ieffects.h"
#include "hudelement.h"
#include "c_basehlplayer.h"
using namespace vgui;
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// Purpose: HDU Damage indication
//-----------------------------------------------------------------------------
class CHudFlashPower : public CHudElement, public vgui::Panel
{
DECLARE_CLASS_SIMPLE( CHudFlashPower, vgui::Panel );
public:
CHudFlashPower( const char *pElementName );
private:
bool ShouldDraw( void );
virtual void Paint();
virtual void OnThink();
virtual void Init( void );
private:
CPanelAnimationVarAliasType( float, m_flBarInsetX, "BarInsetX", "8",
"proportional_float" );
CPanelAnimationVarAliasType( float, m_flBarInsetY, "BarInsetY", "8",
"proportional_float" );
CPanelAnimationVarAliasType( float, m_flBarWidth, "BarWidth", "80",
"proportional_float" );
CPanelAnimationVarAliasType( float, m_flBarHeight, "BarHeight", "10",
"proportional_float" );
CPanelAnimationVarAliasType( float, m_flBarChunkWidth, "BarChunkWidth", "10",
"proportional_float" );
CPanelAnimationVarAliasType( float, m_flBarChunkGap, "BarChunkGap", "2",
"proportional_float" );
CPanelAnimationVar( int, m_iAuxPowerDisabledAlpha, "AuxPowerDisabledAlpha",
"70" );
CPanelAnimationVar( Color, m_AuxPowerColor, "AuxPowerColor", "255 220 0 220" );
CPanelAnimationVar( vgui::HFont, m_hTextFont, "TextFont", "WeaponIconsSmall" );
CPanelAnimationVar( Color, m_TextColor, "TextColor", "FgColor" );
CPanelAnimationVarAliasType( float, text_xpos, "text_xpos", "4",
"proportional_float" );
CPanelAnimationVarAliasType( float, text_ypos, "text_ypos", "4",
"proportional_float" );
CPanelAnimationVarAliasType( float, text_ygap, "text_ygap", "14",
"proportional_float" );
float m_flSuitPower;
int m_nSuitPowerLow;
int m_iActiveSuitDevices;
};
DECLARE_HUDELEMENT( CHudFlashPower );
#define SUITPOWER_INIT -1
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CHudFlashPower::CHudFlashPower( const char *pElementName ) : CHudElement(
pElementName ), BaseClass(NULL, "HudFlashPower")
{
vgui::Panel *pParent = g_pClientMode->GetViewport();
SetParent( pParent );
SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudFlashPower::Init( void )
{
m_flSuitPower = SUITPOWER_INIT;
m_nSuitPowerLow = -1;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudFlashPower::OnThink()
{
float flCurrentPower = 0;
C_BaseHLPlayer *pPlayer = (C_BaseHLPlayer *)C_BasePlayer::GetLocalPlayer();
if ( !pPlayer )
return;
flCurrentPower = pPlayer->m_HL2Local.m_flSuitPower;
// Only update if we've changed suit power
if ( flCurrentPower == m_flSuitPower )
return;
if ( flCurrentPower >= 100.0f && m_flSuitPower < 100.0f )
{
// we've reached max power
g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("SuitAuxPowerMax");
}
else if ( flCurrentPower < 100.0f && (m_flSuitPower >= 100.0f || m_flSuitPower
== SUITPOWER_INIT) )
{
// we've lost power
g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("SuitAuxPowerNotMax");
}
m_flSuitPower = flCurrentPower;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CHudFlashPower::ShouldDraw()
{
C_BaseHLPlayer *pPlayer = (C_BaseHLPlayer *)C_BasePlayer::GetLocalPlayer();
if ( !pPlayer )
return false;
//if flashlight is active or power isnt full
if (pPlayer->IsFlashlightActive() || (pPlayer->m_HL2Local.m_flSuitPower <
100.0f) )
{
return true;
}
else
{
return false;
}
}
//-----------------------------------------------------------------------------
// Purpose: Paints the flashlight menu
//-----------------------------------------------------------------------------
void CHudFlashPower::Paint()
{
C_BaseHLPlayer *pPlayer = (C_BaseHLPlayer *)C_BasePlayer::GetLocalPlayer();
if ( !pPlayer )
return;
// get bar chunks
int chunkCount = m_flBarWidth / (m_flBarChunkWidth + m_flBarChunkGap);
int enabledChunks = (int)((float)chunkCount * (m_flSuitPower * 1.0f/100.0f) +
0.5f );
// see if we've changed power state
int lowPower = 0;
if (enabledChunks <= (chunkCount / 4))
{
lowPower = 1;
}
if (m_nSuitPowerLow != lowPower)
{
if (m_flSuitPower < 100.0f)
{
if (lowPower)
{
g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("SuitAuxPowerDecreasedBelow25");
}
else
{
g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("SuitAuxPowerIncreasedAbove25");
}
m_nSuitPowerLow = lowPower;
}
}
// draw the suit power bar
surface()->DrawSetColor( m_AuxPowerColor );
int xpos = m_flBarInsetX, ypos = m_flBarInsetY;
for (int i = 0; i < enabledChunks; i++)
{
surface()->DrawFilledRect( xpos, ypos, xpos + m_flBarChunkWidth, ypos +
m_flBarHeight );
xpos += (m_flBarChunkWidth + m_flBarChunkGap);
}
// draw the exhausted portion of the bar.
surface()->DrawSetColor( Color( m_AuxPowerColor[0], m_AuxPowerColor[1],
m_AuxPowerColor[2], m_iAuxPowerDisabledAlpha ) );
for (int i = enabledChunks; i < chunkCount; i++)
{
surface()->DrawFilledRect( xpos, ypos, xpos + m_flBarChunkWidth, ypos +
m_flBarHeight );
xpos += (m_flBarChunkWidth + m_flBarChunkGap);
}
// draw the text
surface()->DrawSetTextFont( m_hTextFont );
surface()->DrawSetTextColor( m_TextColor );
surface()->DrawSetTextPos(text_xpos, text_ypos);
const wchar_t *labelText = localize()->Find("Valve_HudFlashPower");
Assert( labelText );
for (const wchar_t *wch = labelText; wch && *wch != 0; wch++)
{
if (*wch == '\n')
{
ypos += text_ygap;
surface()->DrawSetTextPos(text_xpos, ypos);
}
else
{
surface()->DrawUnicodeChar(*wch);
}
}
}
and a power supply:
int m_iMaxPower = 100;
float flRechargeRate = recharge_rate;
float flChargeAmount = flRechargeRate * gpGlobals->frametime;
if ( m_iPower < 20 ) { recharge_rate = 9.0f; }
if ( m_iPower > 20 && m_iPower < 80 ) { recharge_rate = 5.5f; }
if ( m_iPower > 80 ) { recharge_rate = 2.5f; }
if ( m_flDrainRemainder != 0.0f )
{
if ( m_flDrainRemainder >= flChargeAmount )
{
m_flDrainRemainder -= flChargeAmount;
return;
}
else
{
flChargeAmount -= m_flDrainRemainder;
m_flDrainRemainder = 0.0f;
}
}
m_flChargeRemainder += flChargeAmount;
int nPowerToAdd = (int)m_flChargeRemainder;
m_flChargeRemainder -= nPowerToAdd;
//---------------------------------------------------------------
//---------------------------------------------------------------
if ( FlashlightIsOn() )
{
m_iPower -= nPowerToAdd;
if ( m_iPower == 0 )
{
m_flChargeRemainder = 0.0f;
}
if ( m_iPower <= 5 )
{
FlashlightTurnOff();
}
}
else
{
if ((m_iPower < m_iMaxPower) )
{
m_iPower += nPowerToAdd;
if ( m_iPower == m_iMaxPower )
{
m_flChargeRemainder = 0.0f;
}
}
}
but im having some problems linking the power supply to the hud element. What
exactly should i do about it?
Thanks in advance,
Adrian
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
--
_______________________________________________
To unsubscribe, edit your list preferences, or view the list archives, please
visit:
http://list.valvesoftware.com/mailman/listinfo/hlcoders