⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unittemplateclasses.cpp

📁 <B>很多DirectX 9.0游戏编程源码例子</B>
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			if( szTempBuffer[ iCurPos ] == ',' ) {
				iEnd = iCurPos;
				memset( &szValue[ iCurValue ][ 0 ], 0x00, 32 );
				memcpy( &szValue[ iCurValue ], &szTempBuffer[ iStart ], iEnd-iStart );
				iStart = iEnd+1;
				iCurValue++;
			}
			iCurPos++;
		};
		// Import the last column
		iEnd = iCurPos;
		memset( &szValue[ iCurValue ][ 0 ], 0x00, 32 );
		memcpy( &szValue[ iCurValue ], &szTempBuffer[ iStart ], iEnd-iStart );
		iStart = iEnd+1;
		iCurValue++;

		//
		// Store data
		//

		// ID Type
		m_DefenseObjs[ m_iTotalDefObjs ].m_iType = m_iTotalDefObjs;
		// Name
		strcpy( m_DefenseObjs[ m_iTotalDefObjs ].m_szName, &szValue[ 0 ][ 0 ] );
		// Bullet armor rating
		m_DefenseObjs[ m_iTotalDefObjs ].m_iBulletArmorRating = atoi( &szValue[ 1 ][ 0 ] );
		// Missile armor rating
		m_DefenseObjs[ m_iTotalDefObjs ].m_iMissileArmorRating = atoi( &szValue[ 2 ][ 0 ] );
		// Laser armor rating
		m_DefenseObjs[ m_iTotalDefObjs ].m_iLaserArmorRating = atoi( &szValue[ 3 ][ 0 ] );
		// Melee armor rating
		m_DefenseObjs[ m_iTotalDefObjs ].m_iMeleeArmorRating = atoi( &szValue[ 4 ][ 0 ] );
		// Hit points
		m_DefenseObjs[ m_iTotalDefObjs ].m_iMeleeArmorRating = atoi( &szValue[ 5 ][ 0 ] );
		// Regen rate
		m_DefenseObjs[ m_iTotalDefObjs ].m_iMeleeArmorRating = atoi( &szValue[ 6 ][ 0 ] );

		// Increment objects
		m_iTotalDefObjs++;
	}
	fclose( fp );

	//------------------------------------------
	//
	// OFFENSE TYPES
	//
	//------------------------------------------
	// Open the base type file
	fp = fopen( szOffFileName, "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_iTotalOffObjs = 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
			if( szTempBuffer[ iCurPos ] == ',' ) {
				iEnd = iCurPos;
				memset( &szValue[ iCurValue ][ 0 ], 0x00, 32 );
				memcpy( &szValue[ iCurValue ], &szTempBuffer[ iStart ], iEnd-iStart );
				iStart = iEnd+1;
				iCurValue++;
			}
			iCurPos++;
		};
		// Import the last column
		iEnd = iCurPos;
		memset( &szValue[ iCurValue ][ 0 ], 0x00, 32 );
		memcpy( &szValue[ iCurValue ], &szTempBuffer[ iStart ], iEnd-iStart );
		iStart = iEnd+1;
		iCurValue++;

		//
		// Store data
		//

		// ID Type
		m_OffenseObjs[ m_iTotalOffObjs ].m_iType = m_iTotalOffObjs;
		// Name
		memset( m_OffenseObjs[ m_iTotalOffObjs ].m_szName, 0x00, 64 );
		strcpy( m_OffenseObjs[ m_iTotalOffObjs ].m_szName, &szValue[ 0 ][ 0 ] );
		// Bullet damage rating
		m_OffenseObjs[ m_iTotalOffObjs ].m_iBulletDamageRating = atoi( &szValue[ 1 ][ 0 ] );
		// Missile damage rating
		m_OffenseObjs[ m_iTotalOffObjs ].m_iMissileDamageRating = atoi( &szValue[ 2 ][ 0 ] );
		// Laser damage rating
		m_OffenseObjs[ m_iTotalOffObjs ].m_iLaserDamageRating = atoi( &szValue[ 3 ][ 0 ] );
		// Melee damage rating
		m_OffenseObjs[ m_iTotalOffObjs ].m_iMeleeDamageRating = atoi( &szValue[ 4 ][ 0 ] );
		// Proj speed
		m_OffenseObjs[ m_iTotalOffObjs ].m_fProjectileSpeed = (float)atof( &szValue[ 5 ][ 0 ] );
		// Range
		m_OffenseObjs[ m_iTotalOffObjs ].m_iRange = atoi( &szValue[ 6 ][ 0 ] );
		// ROF
		m_OffenseObjs[ m_iTotalOffObjs ].m_iRateOfFire = atoi( &szValue[ 7 ][ 0 ] );
		// Splash Radius
		m_OffenseObjs[ m_iTotalOffObjs ].m_iSplashRadius = atoi( &szValue[ 8 ][ 0 ] );

		// Increment objects
		m_iTotalOffObjs++;
	}
	fclose( fp );

	//------------------------------------------
	//
	// MOVEMENT TYPES
	//
	//------------------------------------------
	// Open the base type file
	fp = fopen( szMovFileName, "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_iTotalMovObjs = 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
			if( szTempBuffer[ iCurPos ] == ',' ) {
				iEnd = iCurPos;
				memset( &szValue[ iCurValue ][ 0 ], 0x00, 32 );
				memcpy( &szValue[ iCurValue ], &szTempBuffer[ iStart ], iEnd-iStart );
				iStart = iEnd+1;
				iCurValue++;
			}
			iCurPos++;
		};
		// Import the last column
		iEnd = iCurPos;
		memset( &szValue[ iCurValue ][ 0 ], 0x00, 32 );
		memcpy( &szValue[ iCurValue ], &szTempBuffer[ iStart ], iEnd-iStart );
		iStart = iEnd+1;
		iCurValue++;

		//
		// Store data
		//

		// ID Type
		m_MovementObjs[ m_iTotalMovObjs ].m_iType = m_iTotalMovObjs;
		// Name
		memset( m_MovementObjs[ m_iTotalOffObjs ].m_szName, 0x00, 64 );
		strcpy( m_MovementObjs[ m_iTotalMovObjs ].m_szName, &szValue[ 0 ][ 0 ] );
		// Acceleration
		m_MovementObjs[ m_iTotalMovObjs ].m_fAcceleration = (float)atof( &szValue[ 1 ][ 0 ] );
		// Deacceleration
		m_MovementObjs[ m_iTotalMovObjs ].m_fDeacceleration = (float)atof( &szValue[ 2 ][ 0 ] );
		// Speed
		m_MovementObjs[ m_iTotalMovObjs ].m_fMovementSpeed = (float)atof( &szValue[ 3 ][ 0 ] );
		// Move type
		m_MovementObjs[ m_iTotalMovObjs ].m_iMovementType = atoi( &szValue[ 4 ][ 0 ] );
		// Turn speed
		m_MovementObjs[ m_iTotalMovObjs ].m_fTurnSpeed = (float)atof( &szValue[ 5 ][ 0 ] );

		// Increment objects
		m_iTotalMovObjs++;
	}
	fclose( fp );

	//------------------------------------------
	//
	// ANIMATION TYPES
	//
	//------------------------------------------
	// Open the base type file
	fp = fopen( szAnimFileName, "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_iTotalAnimationObjs = 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
			if( szTempBuffer[ iCurPos ] == ',' ) {
				iEnd = iCurPos;
				memset( &szValue[ iCurValue ][ 0 ], 0x00, 32 );
				memcpy( &szValue[ iCurValue ], &szTempBuffer[ iStart ], iEnd-iStart );
				iStart = iEnd+1;
				iCurValue++;
			}
			iCurPos++;
		};
		// Import the last column
		iEnd = iCurPos;
		memset( &szValue[ iCurValue ][ 0 ], 0x00, 32 );
		memcpy( &szValue[ iCurValue ], &szTempBuffer[ iStart ], iEnd-iStart );
		iStart = iEnd+1;
		iCurValue++;

		//
		// Store data
		//

		// ID Type
		m_AnimationObjs[ m_iTotalAnimationObjs ].m_iType = m_iTotalAnimationObjs;
		// Name
		memset( m_AnimationObjs[ m_iTotalAnimationObjs ].m_szName, 0x00, 64 );
		strcpy( m_AnimationObjs[ m_iTotalAnimationObjs ].m_szName, &szValue[ 0 ][ 0 ] );
		// Prefix
		memset( m_AnimationObjs[ m_iTotalAnimationObjs ].m_szBitmapPrefix, 0x00, 64 );
		strcpy( m_AnimationObjs[ m_iTotalAnimationObjs ].m_szBitmapPrefix, &szValue[ 1 ][ 0 ] );
		// Number of still frames
		m_AnimationObjs[ m_iTotalAnimationObjs ].m_iNumStillFrames = atoi( &szValue[ 2 ][ 0 ] );
		// Number of move frames
		m_AnimationObjs[ m_iTotalAnimationObjs ].m_iNumMoveFrames = atoi( &szValue[ 3 ][ 0 ] );
		// Number of attack frames
		m_AnimationObjs[ m_iTotalAnimationObjs ].m_iNumAttackFrames = atoi( &szValue[ 4 ][ 0 ] );
		// Number of die frames
		m_AnimationObjs[ m_iTotalAnimationObjs ].m_iNumDieFrames = atoi( &szValue[ 5 ][ 0 ] );
		// Set the render device
		m_AnimationObjs[ m_iTotalAnimationObjs ].vSetRenderDevice( m_pd3dDevice );
		// Load the textures
		m_AnimationObjs[ m_iTotalAnimationObjs ].vLoadTextures();

		// Increment objects
		m_iTotalAnimationObjs++;
	}
	fclose( fp );

	//------------------------------------------
	//
	// UNIT TYPES
	//
	//------------------------------------------

	// Open the base type file
	fp = fopen( szUnitFileName, "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_iTotalUnitBaseObjs = 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
			if( szTempBuffer[ iCurPos ] == ',' ) {
				iEnd = iCurPos;
				memset( &szValue[ iCurValue ][ 0 ], 0x00, 32 );
				memcpy( &szValue[ iCurValue ], &szTempBuffer[ iStart ], iEnd-iStart );
				iStart = iEnd+1;
				iCurValue++;
			}
			iCurPos++;
		};
		// Import the last column
		iEnd = iCurPos;
		memset( &szValue[ iCurValue ][ 0 ], 0x00, 32 );
		memcpy( &szValue[ iCurValue ], &szTempBuffer[ iStart ], iEnd-iStart );
		iStart = iEnd+1;
		iCurValue++;

		//
		// Store data
		//

		// ID Type
		m_UnitBaseObjs[ m_iTotalUnitBaseObjs ].m_iType = m_iTotalUnitBaseObjs;
		// Name
		memset( m_UnitBaseObjs[ m_iTotalUnitBaseObjs ].m_szName, 0x00, 64 );
		strcpy( m_UnitBaseObjs[ m_iTotalUnitBaseObjs ].m_szName, &szValue[ 0 ][ 0 ] );
		// Defense Type
		ptrDefense = ptrGetDefenseType( &szValue[ 1 ][ 0 ] );
		// Offense Type1
		ptrOffense1 = ptrGetOffenseType( &szValue[ 2 ][ 0 ] );
		// Offense Type2
		ptrOffense2 = ptrGetOffenseType( &szValue[ 3 ][ 0 ] );
		// Offense Type3
		ptrOffense3 = ptrGetOffenseType( &szValue[ 4 ][ 0 ] );
		// Move Type
		ptrMovement = ptrGetMoveType( &szValue[ 5 ][ 0 ] );
		// Animation Type
		ptrAnimation = ptrGetAnimType( &szValue[ 6 ][ 0 ] );
		// Set the base types
		m_UnitBaseObjs[ m_iTotalUnitBaseObjs ].vSetBaseValues(	
			ptrDefense, 
			ptrOffense1,
			ptrOffense2,
			ptrOffense3,
			ptrMovement,
			ptrAnimation );

		// Increment objects
		m_iTotalUnitBaseObjs++;
	}
	fclose( fp );

	return( 1 );	
}

//-------------------------------------------------------
// Given the defense type name, return a pointer to the
// type.
//-------------------------------------------------------
CUnitDefense* CUnitManager::ptrGetDefenseType( char *szName )
{
	int				i;
	CUnitDefense	*ptrUnitDefense = NULL;

	for( i = 0; i < m_iTotalDefObjs; i++ ) {
		if( !stricmp( szName, m_DefenseObjs[ i ].m_szName ) ) {
			ptrUnitDefense = &m_DefenseObjs[ i ];
			return( ptrUnitDefense );	
		}
	}

	return( ptrUnitDefense );
}

//-------------------------------------------------------
// Given the offense type name, return a pointer to the
// type.
//-------------------------------------------------------
CUnitOffense* CUnitManager::ptrGetOffenseType( char *szName )
{
	int				i;
	CUnitOffense	*ptrUnitOffense = NULL;

	for( i = 0; i < m_iTotalOffObjs; i++ ) {
		if( !stricmp( szName, m_OffenseObjs[ i ].m_szName ) ) {
			ptrUnitOffense = &m_OffenseObjs[ i ];
			return( ptrUnitOffense );	
		}
	}

	return( ptrUnitOffense );
}

//-------------------------------------------------------
// Given the movement type name, return a pointer to the
// type.
//-------------------------------------------------------
CUnitMovement* CUnitManager::ptrGetMoveType( char *szName )
{
	int				i;
	CUnitMovement	*ptrUnitMovement = NULL;

	for( i = 0; i < m_iTotalMovObjs; i++ ) {
		if( !stricmp( szName, m_MovementObjs[ i ].m_szName ) ) {
			ptrUnitMovement = &m_MovementObjs[ i ];
			return( ptrUnitMovement );	
		}
	}

	return( ptrUnitMovement );
}

//-------------------------------------------------------
// Given the anim type name, return a pointer to the
// type.
//-------------------------------------------------------
CUnitAnimation* CUnitManager::ptrGetAnimType( char *szName )
{
	int				i;
	CUnitAnimation	*ptrUnitAnim = NULL;

	for( i = 0; i < m_iTotalAnimationObjs; i++ ) {
		if( !stricmp( szName, m_AnimationObjs[ i ].m_szName ) ) {
			ptrUnitAnim = &m_AnimationObjs[ i ];
			return( ptrUnitAnim );	
		}
	}

	return( ptrUnitAnim );
}

//-------------------------------------------------------
// Activate a unit for use in the game
//-------------------------------------------------------
int CUnitManager::iAddUnit( char *szName, int iOwner )
{
	int		i;
	int		iFoundID = -1;

	// Find a matching type
	for( i = 0; i < m_iTotalUnitBaseObjs; i++ ) {
		if( !stricmp( szName, m_UnitBaseObjs[ i ].m_szName ) ) {
			iFoundID = i;
			break;
		}
	}

	// Return out of base type not found
	if( iFoundID == -1 ) {
		return( -1 );
	}

	// Find free unit slot
	for( i = 0; i < m_iTotalUnitObjs; i++ ) {
		// Check for inactive unit
		if( !m_UnitObjs[ i ].m_bActive ) {
			// Activate the unit
			m_UnitObjs[ i ].m_bActive	= 1;
			// Set its internal types
			m_UnitObjs[ i ].vSetBaseValues( m_UnitBaseObjs[ iFoundID ].m_Defense,
											m_UnitBaseObjs[ iFoundID ].m_Offense1,
											m_UnitBaseObjs[ iFoundID ].m_Offense2,
											m_UnitBaseObjs[ iFoundID ].m_Offense3,
											m_UnitBaseObjs[ iFoundID ].m_Movement,
											m_UnitBaseObjs[ iFoundID ].m_Animation );

			// Set the unit type
			m_UnitObjs[ i ].m_iType		= iFoundID;
			// Set the owner of the unit
			m_UnitObjs[ i ].m_iOwner	= iOwner;
			// Increment how many the owner owns
			m_iOwnerTotal[ iOwner ]++;

			return( i );
		}
	}

	return( -1 );
}

//-------------------------------------------------------
// Sets the Direct3D device necessary for loading
// textures.
//-------------------------------------------------------
void CUnitManager::vSetRenderDevice( LPDIRECT3DDEVICE9 pd3d )
{
	m_pd3dDevice = pd3d;
}

//-------------------------------------------------------
// Deactivates a unit and resets its internal values.
//-------------------------------------------------------
void CUnitManager::vRemoveUnit( int iUnitID )
{
	// Deactivate the unit if it is active
	if( m_UnitObjs[ iUnitID ].m_bActive ) {
		// Decrement how many the owner owns
		m_iOwnerTotal[ m_UnitObjs[ iUnitID ].m_iOwner ]--;
		m_UnitObjs[ iUnitID ].m_bActive = 0;
		m_UnitObjs[ iUnitID ].vReset();
	}
}

int CUnitManager::iCountTotalTextures( void )
{
	int iCount = 0;

	for( int i = 0; i < m_iTotalAnimationObjs; i++ ) {
		iCount += m_AnimationObjs[ i ].m_iTotalTextures;
	}
	
	return( iCount );
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -