Cpp:Building A Simple Tile Engine:Part 5

From GPWiki

Files:GUITutorial_warn.gif The Game Programming Wiki has moved! Files:GUITutorial_warn.gif

The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server.

However, the GPWiki forums are still active! Come say hello.

Part 5: Creating units


I haven't got the time to finish this article right now, so until I have here's a stub box for you to look at:

This page is far from complete, go ahead and write some more!
You can extend this page by editing it.

File: Unit.h

#ifndef UNIT_H
#define UNIT_H
 
#include "Entity.h"
 
/** Unit entity */
class CUnit: public CEntity
{
private:
	int m_iPlayer;
 
public:
	CUnit();
	virtual ~CUnit();
 
	virtual void			VThink		( const int& iElapsedTime );
	virtual void			VRender		( SDL_Surface* pDestSurface	);
 
	virtual bool			VLoad		( TiXmlElement* pXMLData );
	virtual TiXmlElement	VGetSaveData();
};
 
#endif // UNIT_H 

File: Unit.cpp

#include "Unit.h"
 
CUnit::CUnit()
{
 
}
 
CUnit::~CUnit()
{
 
}
 
bool CUnit::VLoad( TiXmlElement* pXMLData )
{
	CEntity::VLoad( pXMLData );
 
	m_iPlayer = atoi( pXMLData->Attribute( "player" ) );
 
	TiXmlElement* XMLPosition = pXMLData->FirstChildElement( "position" );
	int iTileX = atoi( XMLPosition->FirstChild( "x" )->FirstChild()->Value() );
	int iTileY = atoi( XMLPosition->FirstChild( "y" )->FirstChild()->Value() );
	SetTile( iTileX, iTileY );
	
	return true;
}
 
TiXmlElement CUnit::VGetSaveData()
{
	TiXmlElement UnitElm( "unit" );
	UnitElm.SetAttribute( "player", m_iPlayer );
	UnitElm.InsertEndChild( CEntity::VGetSaveData() );
	TiXmlElement PositionElm( "position" );
 
	char buf[8];
 
	TiXmlElement PosXElm( "x" );
	sprintf( buf, "%d", GetTileX() );
	TiXmlText PosXTxt( buf );
	PosXElm.InsertEndChild( PosXTxt );
	PositionElm.InsertEndChild( PosXElm );
 
	TiXmlElement PosYElm( "y" );
	sprintf( buf, "%d", GetTileY() );
	TiXmlText PosYTxt( buf );
	PosYElm.InsertEndChild( PosYTxt );
	PositionElm.InsertEndChild( PosYElm );
 
	UnitElm.InsertEndChild( PositionElm );
	
	return UnitElm;
}
 
void CUnit::VThink( const int& iElapsedTime )
{
	CEntity::VThink( iElapsedTime );
}
 
void CUnit::VRender( SDL_Surface* pDestSurface )
{
	CEntity::VRender( pDestSurface );
}

Proceed to Part 6: Creating the unit manager


Table of Content - Part 1 - Part 2 - Part 3 - Part 4 - Part 5 - Part 6 - Part 7 - Part 8 - Part X