📄 unittemplateclasses.cpp
字号:
#include "UnitTemplateClasses.h"
void FileDebug( char *msg, ... )
{
FILE *fp;
va_list arglist;
char msgbuf[32768];
va_start( arglist, msg );
vsprintf( &msgbuf[0], msg, arglist );
va_end( arglist );
fp = fopen("debug.txt","a+");
fprintf( fp, msgbuf );
fflush( fp );
fclose( fp );
}
//-------------------------------------------------------
//-------------------------------------------------------
//
// DEFENSE CLASS
//
//-------------------------------------------------------
//-------------------------------------------------------
CUnitDefense::CUnitDefense()
{
// Set internal vars
vReset();
}
CUnitDefense::~CUnitDefense()
{
}
void CUnitDefense::vReset( void )
{
m_iType = 0;
m_iMissileArmorRating = 0;
m_iBulletArmorRating = 0;
m_iLaserArmorRating = 0;
m_iMeleeArmorRating = 0;
m_iHitPoints = 0;
m_iRegenRate = 0;
strcpy( m_szName, "N/A" );
}
//-------------------------------------------------------
//-------------------------------------------------------
//
// OFFENSE CLASS
//
//-------------------------------------------------------
//-------------------------------------------------------
CUnitOffense::CUnitOffense()
{
// Set internal vars
vReset();
}
CUnitOffense::~CUnitOffense()
{
}
//-------------------------------------------------------
// Set internal variables their default states. This
// is good for resetting a unit for reuse.
//-------------------------------------------------------
void CUnitOffense::vReset( void )
{
m_iType = 0;
m_iMissileDamageRating = 0;
m_iBulletDamageRating = 0;
m_iLaserDamageRating = 0;
m_iMeleeDamageRating = 0;
m_iSplashRadius = 0;
m_iRateOfFire = 0;
m_fProjectileSpeed = 0.0f;
m_iRange = 0;
strcpy( m_szName, "N/A" );
}
//-------------------------------------------------------
//-------------------------------------------------------
//
// MOVEMENT CLASS
//
//-------------------------------------------------------
//-------------------------------------------------------
CUnitMovement::CUnitMovement()
{
// Set internal vars
vReset();
}
CUnitMovement::~CUnitMovement()
{
}
//-------------------------------------------------------
// Set internal variables their default states. This
// is good for resetting a unit for reuse.
//-------------------------------------------------------
void CUnitMovement::vReset( void )
{
m_iType = 0;
m_fMovementSpeed = 0.0f;
m_iMovementType = 0;
m_fAcceleration = 0.0f;
m_fDeacceleration = 0.0f;
m_fTurnSpeed = 0.0f;
strcpy( m_szName, "N/A" );
}
//-------------------------------------------------------
//-------------------------------------------------------
//
// UNIT CLASS
//
//-------------------------------------------------------
//-------------------------------------------------------
CUnit::CUnit()
{
// Set internal vars
vReset();
}
CUnit::~CUnit()
{
}
//-------------------------------------------------------
// Set internal variables their default states. This
// is good for resetting a unit for reuse.
//-------------------------------------------------------
void CUnit::vReset( void )
{
m_Defense = NULL;
m_Offense1 = NULL;
m_Offense2 = NULL;
m_Offense3 = NULL;
m_Movement = NULL;
m_Animation = NULL;
m_iCurHitPoints = 0;
m_fXPos = 0.0f;
m_fYPos = 0.0f;
m_fRot = 0.0f;
m_fScale = 1.0f;
m_iUnitID = -1;
m_iParentID = -1;
strcpy( m_szName, "N/A" );
m_bActive = 0;
m_iOwner = 0;
m_iType = 0;
m_fCurSpeed = 0.0f;
m_iCurAnimFrame = 0;
m_iCurAttackFrame = 0;
m_iCurStillFrame = 0;
m_iCurMoveFrame = 0;
m_iCurDieFrame = 0;
}
//-------------------------------------------------------
// Set internal pointers to loaded types.
//-------------------------------------------------------
void CUnit::vSetBaseValues( CUnitDefense* ptrDef,
CUnitOffense* ptrOff1,
CUnitOffense* ptrOff2,
CUnitOffense* ptrOff3,
CUnitMovement* ptrMove,
CUnitAnimation* ptrAnimation )
{
// Point to the passed in class objects
m_Defense = ptrDef;
m_Offense1 = ptrOff1;
m_Offense2 = ptrOff2;
m_Offense3 = ptrOff3;
m_Movement = ptrMove;
m_Animation = ptrAnimation;
}
//-------------------------------------------------------
// Set the global position of the unit
//-------------------------------------------------------
void CUnit::vSetPosition( float fX, float fY )
{
m_fXPos = fX;
m_fYPos = fY;
}
//-------------------------------------------------------
//-------------------------------------------------------
//
// TEXTURE CLASS
//
//-------------------------------------------------------
//-------------------------------------------------------
CTexture::CTexture()
{
// Set the name to null
memset( m_szName, 0x00, 64 );
// Set texture to unloaded state
m_pTexture = NULL;
}
CTexture::~CTexture()
{
// Release any memory
vRelease();
}
void CTexture::vRelease( void )
{
// Release the texture if it exists in memory
if( m_pTexture ) {
m_pTexture->Release();
m_pTexture = NULL;
}
}
//-------------------------------------------------------
// Sets the Direct3D device necessary for loading
// textures.
//-------------------------------------------------------
void CTexture::vSetRenderDevice( LPDIRECT3DDEVICE9 pd3d )
{
m_pd3dDevice = pd3d;
}
//-------------------------------------------------------
// Load the texture from the specified file name
//-------------------------------------------------------
void CTexture::vLoad( char *szName )
{
// Store the filename
strcpy( m_szName, szName );
// Load the texture
D3DXCreateTextureFromFile( m_pd3dDevice, m_szName, &m_pTexture );
}
//-------------------------------------------------------
//-------------------------------------------------------
//
// UNIT ANIMATION CLASS
//
//-------------------------------------------------------
//-------------------------------------------------------
CUnitAnimation::CUnitAnimation()
{
// Set # textures to 0
m_iTotalTextures = 0;
// Reset internal vars
vReset();
}
CUnitAnimation::~CUnitAnimation()
{
vReset();
}
void CUnitAnimation::vReset( void )
{
memset( m_szName, 0x00, 64 );
memset( m_szBitmapPrefix, 0x00, 64 );
if( m_iTotalTextures ) {
delete [] m_Textures;
m_Textures = NULL;
m_iTotalTextures = 0;
}
m_iNumStillFrames = 0;
m_iNumMoveFrames = 0;
m_iNumAttackFrames = 0;
m_iNumDieFrames = 0;
m_iType = 0;
m_iStartStillFrames = 0;
m_iStartMoveFrames = 0;
m_iStartAttackFrames = 0;
m_iStartDieFrames = 0;
}
//-------------------------------------------------------
// Sets the Direct3D device necessary for loading
// textures.
//-------------------------------------------------------
void CUnitAnimation::vSetRenderDevice( LPDIRECT3DDEVICE9 pd3d )
{
m_pd3dDevice = pd3d;
}
void CUnitAnimation::vLoadTextures( void )
{
//
// Load the animations
//
int i, j;
int iLocalCount = 0;
char szBitmapFileName[ 128 ];
//
// Allocate memory for the textures
//
m_Textures = new CTexture[
(m_iNumStillFrames*(UNITMANAGER_MAXOWNERS+1))+
(m_iNumMoveFrames*(UNITMANAGER_MAXOWNERS+1))+
(m_iNumAttackFrames*(UNITMANAGER_MAXOWNERS+1))+
(m_iNumDieFrames*(UNITMANAGER_MAXOWNERS+1)) ];
//
// Still graphics (fidgit)
//
m_iStartStillFrames = 0;
for( i = 0; i < m_iNumStillFrames; i++ ) {
for( j = 0; j < UNITMANAGER_MAXOWNERS+1; j++ ) {
sprintf( szBitmapFileName, "UnitData\\%s%d_%d.tga",
m_szBitmapPrefix, iLocalCount, j );
// Set the render device
m_Textures[ m_iTotalTextures ].vSetRenderDevice( m_pd3dDevice );
// Load the texture
m_Textures[ m_iTotalTextures ].vLoad( szBitmapFileName );
// Increment total # of textures
m_iTotalTextures++;
}
iLocalCount++;
}
//
// Move graphics
//
m_iStartMoveFrames = m_iTotalTextures;
for( i = 0; i < m_iNumMoveFrames; i++ ) {
for( j = 0; j < UNITMANAGER_MAXOWNERS+1; j++ ) {
sprintf( szBitmapFileName, "UnitData\\%s%d_%d.tga",
m_szBitmapPrefix, iLocalCount, j );
// Set the render device
m_Textures[ m_iTotalTextures ].vSetRenderDevice( m_pd3dDevice );
// Load the texture
m_Textures[ m_iTotalTextures ].vLoad( szBitmapFileName );
// Increment total # of textures
m_iTotalTextures++;
}
iLocalCount++;
}
//
// Attack graphics
//
m_iStartAttackFrames = m_iTotalTextures;
for( i = 0; i < m_iNumAttackFrames; i++ ) {
for( j = 0; j < UNITMANAGER_MAXOWNERS+1; j++ ) {
sprintf( szBitmapFileName, "UnitData\\%s%d_%d.tga",
m_szBitmapPrefix, iLocalCount, j );
// Set the render device
m_Textures[ m_iTotalTextures ].vSetRenderDevice( m_pd3dDevice );
// Load the texture
m_Textures[ m_iTotalTextures ].vLoad( szBitmapFileName );
// Increment total # of textures
m_iTotalTextures++;
}
iLocalCount++;
}
//
// Die graphics
//
m_iStartDieFrames = m_iTotalTextures;
for( i = 0; i < m_iNumDieFrames; i++ ) {
for( j = 0; j < UNITMANAGER_MAXOWNERS+1; j++ ) {
sprintf( szBitmapFileName, "UnitData\\%s%d_%d.tga",
m_szBitmapPrefix, iLocalCount, j );
// Set the render device
m_Textures[ m_iTotalTextures ].vSetRenderDevice( m_pd3dDevice );
// Load the texture
m_Textures[ m_iTotalTextures ].vLoad( szBitmapFileName );
// Increment total # of textures
m_iTotalTextures++;
}
iLocalCount++;
}
}
//-------------------------------------------------------
//-------------------------------------------------------
//
// UNIT MANAGER CLASS
//
//-------------------------------------------------------
//-------------------------------------------------------
CUnitManager::CUnitManager()
{
m_DefenseObjs = NULL;
m_OffenseObjs = NULL;
m_MovementObjs = NULL;
m_UnitBaseObjs = NULL;
m_UnitObjs = NULL;
m_AnimationObjs = NULL;
m_iTotalDefObjs = 0;
m_iTotalOffObjs = 0;
m_iTotalMovObjs = 0;
m_iTotalAnimationObjs = 0;
m_iTotalUnitBaseObjs = 0;
m_iTotalUnitObjs = 0;
vReset();
}
CUnitManager::~CUnitManager()
{
vClearMem();
}
//-------------------------------------------------------
// Free memory allocated for unit base types
//-------------------------------------------------------
void CUnitManager::vClearMem( void )
{
if( m_DefenseObjs ) {
delete [] m_DefenseObjs;
m_DefenseObjs = NULL;
}
if( m_OffenseObjs ) {
delete [] m_OffenseObjs;
m_OffenseObjs = NULL;
}
if( m_MovementObjs ) {
delete [] m_MovementObjs;
m_MovementObjs = NULL;
}
if( m_AnimationObjs ) {
delete [] m_AnimationObjs;
m_AnimationObjs = NULL;
}
if( m_UnitBaseObjs ) {
delete [] m_UnitBaseObjs;
m_UnitBaseObjs = NULL;
}
if( m_UnitObjs ) {
delete [] m_UnitObjs;
m_UnitObjs = NULL;
}
m_iTotalDefObjs = 0;
m_iTotalOffObjs = 0;
m_iTotalMovObjs = 0;
m_iTotalAnimationObjs = 0;
m_iTotalUnitBaseObjs = 0;
m_iTotalUnitObjs = 0;
}
//-------------------------------------------------------
// Set internal variables their default states. This
// is good for resetting a unit for reuse.
//-------------------------------------------------------
void CUnitManager::vReset( void )
{
vClearMem();
m_DefenseObjs = new CUnitDefense[ UNITMANAGER_MAXBASEOBJS ];
m_OffenseObjs = new CUnitOffense[ UNITMANAGER_MAXBASEOBJS ];
m_MovementObjs = new CUnitMovement[ UNITMANAGER_MAXBASEOBJS ];
m_AnimationObjs = new CUnitAnimation[ UNITMANAGER_MAXBASEOBJS ];
m_UnitBaseObjs = new CUnit[ UNITMANAGER_MAXBASEOBJS ];
m_UnitObjs = new CUnit[ UNITMANAGER_MAXUNITS ];
m_iTotalUnitObjs = UNITMANAGER_MAXUNITS;
// Set how many each player owns
for( int i = 0; i < UNITMANAGER_MAXOWNERS; i++ ) {
m_iOwnerTotal[ i ] = 0;
}
}
//-------------------------------------------------------
// Import unit defense, offense, move, and base types
// from a csv (comma delimited) file.
//-------------------------------------------------------
int CUnitManager::iLoadBaseTypes( char *szDefFileName,
char *szOffFileName,
char *szMovFileName,
char *szUnitFileName,
char *szAnimFileName )
{
FILE *fp;
char szTempBuffer[ 512 ];
char szValue[ 32 ][ 32 ];
//char szBitmapFileName[ 128 ];
int iStart, iEnd, iCurPos;
int iCurValue = 0;
CUnitDefense *ptrDefense;
CUnitOffense *ptrOffense1;
CUnitOffense *ptrOffense2;
CUnitOffense *ptrOffense3;
CUnitMovement *ptrMovement;
CUnitAnimation *ptrAnimation;
//------------------------------------------
//
// DEFENSE TYPES
//
//------------------------------------------
// Open the base type file
fp = fopen( szDefFileName, "r" );
if( fp == NULL ) {
return( -1 );
}
// Pull the header first and toss it out
fgets( szTempBuffer, 512, fp );
szTempBuffer[strlen(szTempBuffer)-1] = '\0';
// Set total objects to 0
m_iTotalDefObjs = 0;
// Loop through and read every line
while( !feof( fp ) ) {
// Get next line
fgets( szTempBuffer, 512, fp );
if( feof( fp ) ) {
break;
}
// Add terminator
szTempBuffer[strlen(szTempBuffer)-1] = '\0';
iStart = 0;
iEnd = 0;
iCurPos = 0;
iCurValue = 0;
// Pull out the values
while( szTempBuffer[ iCurPos ] != '\0' && iCurPos < 512 ) {
// Check for end of value
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -