Game Development Community

HELP with GuiVehicleHealthBar.cc

by Anthony Rosenbaum · in Torque Game Engine · 06/12/2002 (11:24 am) · 1 replies

This file is a collaberations of the guiHealthBarHud.cc found in /engine/game/fps. Mixed with Labrat vert early vehicle health/energy bars. The Idea is to make a new gui element that will display the current vehicles health . Anyway I am lost this is what I have so far
//-----------------------------------------------------------------------------
// Torque Game Engine
// 
// Copyright (c) 2001 GarageGames.Com
//-----------------------------------------------------------------------------

#include "dgl/dgl.h"
#include "gui/guiControl.h"
#include "console/consoleTypes.h"
#include "game/gameConnection.h"
#include "game/shapeBase.h"

//-----------------------------------------------------------------------------
/**
   A basic health bar control.
   This gui displays the damage value of the current PlayerObjectType
   control object.  The gui can be set to pulse if the health value
   drops below a set value. This control only works if a server
   connection exists and it's control object is a PlayerObjectType. If
   either of these requirements is false, the control is not rendered.
*/
class GuiVehicleHealthBar : public GuiControl
{
   typedef GuiControl Parent;

   bool     mShowFrame;
   bool     mShowFill;
   bool     mDisplayEnergy;

   ColorF   mFillColor;
   ColorF   mFrameColor;
   ColorF   mDamageFillColor;

   S32      mPulseRate;
   F32      mPulseThreshold;

   F32      mValue;
   F32      mMaxDamage;
   F32      mDamageTaken;

public:
   GuiVehicleHealthBar();

   void onRender( Point2I, const RectI &);
   static void initPersistFields();
   DECLARE_CONOBJECT( GuiVehicleHealthBar );
};


//-----------------------------------------------------------------------------

IMPLEMENT_CONOBJECT( GuiVehicleHealthBar );

GuiVehicleHealthBar::GuiVehicleHealthBar()
{
   mShowFrame = mShowFill = true;
   mDisplayEnergy = false;
   mFillColor.set(0, 0, 0, 0.5);
   mFrameColor.set(0, 1, 0, 1);
   mDamageFillColor.set(1, 0, 1, 1);
   mMaxDamage = 100;
   mDamageTaken = 0;
   mPulseRate = 0;
   mPulseThreshold = 0.3f;
   mValue = 0.2f;
}

void GuiVehicleHealthBar::initPersistFields()
{
   Parent::initPersistFields();

   addField( "showFill", TypeBool, Offset( mShowFill, GuiVehicleHealthBar ) );
   addField( "displayEnergy", TypeBool, Offset( mDisplayEnergy, GuiVehicleHealthBar ) );
   addField( "showFrame", TypeBool, Offset( mShowFrame, GuiVehicleHealthBar ) );
   addField( "fillColor", TypeColorF, Offset( mFillColor, GuiVehicleHealthBar ) );
   addField( "frameColor", TypeColorF, Offset( mFrameColor, GuiVehicleHealthBar ) );
   addField( "damageFillColor", TypeColorF, Offset( mDamageFillColor, GuiVehicleHealthBar ) );
   addField( "pulseRate", TypeS32, Offset( mPulseRate, GuiVehicleHealthBar ) );
   addField( "pulseThreshold", TypeF32, Offset( mPulseThreshold, GuiVehicleHealthBar ) );
}


//-----------------------------------------------------------------------------
/**
   Gui onRender method.
   Renders a  vehicle health bar with filled background and border.
*/
void GuiVehicleHealthBar::onRender(Point2I offset, const RectI &updateRect)
{
   // Must have a connection and player control object and vehicle
   GameConnection * con = GameConnection::getServerConnection();
	if(con != NULL)
	return;
		ShapeBase * obj = con->getControlObject();
		if(obj != NULL)
		return;
			ShapeBase * obj2 = obj->getControlObject();
			if(obj2 != NULL)
			return;

	if(mDisplayEnergy)
	{
		mMaxDamage = obj2->maxDamage;
		mDamageTaken = obj2->getDamageLevel();
		mValue =  mMaxDamage - mDamageTaken;
	
	}
	else
	{
      // We'll just grab the damage right off the control object.
      // Damage value 0 = no damage.
	  mValue = 1 - obj2->getDamageValue();
	}
   // Background first
   if (mShowFill)
      dglDrawRectFill(updateRect, mFillColor);
   
   // Pulse the damage fill if it's below the threshold
   if (mPulseRate != 0)
      if (mValue < mPulseThreshold) {
         F32 time = Platform::getVirtualMilliseconds();
         F32 alpha = mFmod(time,mPulseRate) / (mPulseRate / 2.0);
         mDamageFillColor.alpha = (alpha > 1.0)? 2.0 - alpha: alpha;
      }
      else
         mDamageFillColor.alpha = 1;

   // Render damage fill %
   RectI rect(updateRect);
   if(mBounds.extent.x > mBounds.extent.y)
      rect.extent.x *= mValue;
   else
   {
      S32 bottomY = rect.point.y + rect.extent.y;
      rect.extent.y *= mValue;
      rect.point.y = bottomY - rect.extent.y;
   }
   dglDrawRectFill(rect, mDamageFillColor);
   
   // Border last
   if (mShowFrame)
      dglDrawRect(updateRect, mFrameColor);
}
This way it will not compile
I added a new getShapeDataBlock() function that will return the mDataBlock variable (cause it wasn't public) this function was copy and paste of the GameBase::getDataBlock(). this is the change with the new function
if(mDisplayEnergy)
	{
		mMaxDamage = obj2->getShapeDataBlock()->maxDamage;
		mDamageTaken = obj2->getDamageLevel();
		mValue =  mMaxDamage - mDamageTaken;
	
	}
But I can't get it working. Maybe I am confused on how the bar is drawn. Could someone please explain what I am missing!!!

#1
06/12/2002 (4:44 pm)
is this right
if(mDisplayEnergy)
	{
		mValue =  obj2->getEnergyValue();
	
	}
	else
	{
      // We'll just grab the damage right off the control object.
      // Damage value 0 = no damage.
	  mValue = 1 - obj2->getDamageLevel();
	}