Game Development Community

GUIVehicleHealthBar for TGEA

by CodingChris · 02/19/2008 (6:33 am) · 3 comments

Adding the code is very simple.

1. Add this to your copy of TGEA:
//-----------------------------------------------------------------------------
// Torque Game Engine
// 
// Copyright (c) 2001 GarageGames.Com
//-----------------------------------------------------------------------------

#include "gui/core/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;

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);

   mPulseRate = 0;
   mPulseThreshold = 0.3f;
   mValue = 0.0f;
}

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* conn = GameConnection::getConnectionToServer();
   if (!conn)
      return;

   
  ShapeBase* vehicle = conn->getControlObject();
if (!vehicle || !(vehicle->getType() & VehicleObjectType))
return;

		if(mDisplayEnergy)
		{
		mValue =  vehicle->getEnergyValue();
		}
		else
		{
        // We'll just grab the damage right off the control object.
	    // Damage value 0 = no damage.
        mValue =  1 - vehicle->getDamageValue();
		}


   // Background first
   if (mShowFill)
 GFX->drawRectFill(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;
   }
  GFX->drawRectFill(rect, mDamageFillColor);
   
   // Border last
   if (mShowFrame)
       GFX->drawRect(updateRect, mFrameColor);
   
   
}
2. Recompile!
3. Add this to your vehicle datablock(if not already there):
// Energy
    jetForce = 3000;
    minJetEnergy = 30;
    maxEnergy = 200;
    jetEnergyDrain = 2;
    rechargeRate = 0.8;
4. Add this to your vehicle::onAdd function:
%obj.setEnergyLevel(%this.MaxEnergy);
 %obj.setRechargeRate(%this.rechargeRate);
5. Add the control to your PlayGUI.

That's it!
I tested it with TGEA 1.0.3

#1
01/12/2008 (11:25 pm)
Great! ;-)

Thank you for your efforts. I'll try it later by myself.
#2
02/22/2008 (5:11 am)
Thanks.
#3
02/22/2008 (7:04 am)
Always good to know that people like it:)