Posted by: DarkNight
Date posted: Apr 14 2003 User Rating: 5 out of 5.0 | Number of views: 1639 Number of comments: 0 | Description: Creating the Capture Point |
This part of the tutorial will show you how to create the Capture Point for the CTF Gamerules.
NOTE: This code will need modifying to suit your team system,ect so don't take this as a complete C&P (which it basically is).
Open up ctf_gameplay.h and put the following at the bottom:
| |
// Capture point for Team 1 class CCaptureTeam1 : public CBaseEntity { public: void Spawn( ); void Precache( ); void EXPORT Touch(CBaseEntity *); void EXPORT Think( ); void KeyValue( KeyValueData* ); };
// Capture point for Team 2 class CCaptureTeam2 : public CBaseEntity { public: void Spawn( ); void Precache( ); void EXPORT Touch( CBaseEntity *); void EXPORT Think( ); void KeyValue( KeyValueData* ); };
|
In player.h, put the following in class CBasePlayer : public CBaseMonster in the publics under bool m_fHasObject;
Open up ctf_gameplay.cpp and put the following at the bottom: | |
LINK_ENTITY_TO_CLASS( capture_team1, CCaptureTeam1 );
void CCaptureTeam1 :: Spawn( ) {
// Calls the precache function to precache any models, sounds,ect // you need before it spawns Precache( ); SET_MODEL( ENT(pev), STRING(pev->model) ); UTIL_SetOrigin( pev, pev->origin ); pev->movetype = MOVETYPE_FLY;
// This allows you to trigger it by walking through it pev->solid = SOLID_TRIGGER;
// This just sets our Touch to Touch and our Think to Think // which is down a little further. SetTouch(Touch); SetThink(Think);
// Sets next think time pev->nextthink = gpGlobals->time + 0.1;
// Make the entity invisible pev->rendermode = kRenderTransTexture; pev->renderamt = 0; }
// This is the Precache function which is called from the Spawn function.
void CCaptureTeam1 :: Precache( ) { PRECACHE_MODEL( (char*)STRING(pev->model) ); } void CCaptureTeam1 :: Think( ) { // loop through every player and check if they are in the area for(int i=0; i<gpGlobals->maxClients; i++) { CBasePlayer *pPlayer = (CBasePlayer *)UTIL_PlayerByIndex(i); if(pPlayer && pPlayer->m_iTeam == 1) { if((pPlayer->pev->origin.x >= pev->mins.x) && (pPlayer->pev->origin.x <= pev->maxs.x) && (pPlayer->pev->origin.y >= pev->mins.y) && (pPlayer->pev->origin.y <= pev->maxs.y) && (pPlayer->pev->origin.z >= pev->mins.z) && (pPlayer->pev->origin.z <= pev->maxs.z)) pPlayer->m_bInCapture = true; else pPlayer->m_bInCapture = false; } } // Set next think time pev->nextthink = gpGlobals->time + 0.1; }
// This is the Touch function which was set above, in the Spawn function, // its basically the stuff that runs when the capture point has been touched by a player
void CCaptureTeam1 :: Touch( CBaseEntity* pOther ) { // Determine if the object that touches it, is a player // and check if the player is alive. if(!pOther) return; if(!pOther->IsPlayer()) return; if(!pOther->IsAlive()) return;
CBasePlayer *pPlayer = (CBasePlayer *)pOther;
if(pPlayer && pPlayer->m_iTeam == 1) { // Check to see if they have the object if( !pPlayer->m_fHasObject ) return; else { // Print to HUD who has captured the flag UTIL_ClientPrintAll( HUD_PRINTCENTER, UTIL_VarArgs ( "%s has captured the Flag!\n", STRING( pPlayer->pev->netname )));
// Remove the flag pPlayer->m_fHasObject = false;
PLAYBACK_EVENT_FULL(0, pPlayer->edict(), g_usObject, 0, (float *)&g_vecZero, (float *)&g_vecZero, 0, 0, FLAG_CAPTURE, 0, 0, 0);
// Reset the flag CObjectFlag *pFlag = (CObjectFlag *)UTIL_FindEntityByClassname(NULL, "object_flag"); if(pFlag) { pFlag->m_fIsInPlay = false;
// Do a funky effect on the flag when it gets reset pFlag->pev->effects = EF_BRIGHTFIELD; } } } else return; // wrong team }
void CCaptureTeam1 :: KeyValue( KeyValueData* Data ) { // This will store the points but its not fully implemented. I'll leave it for you to do. if( FStrEq( Data->szKeyName, "points" ) ) { int m_iPoints = atoi(Data->szValue); Data->fHandled = true; }
CBaseEntity::KeyValue(Data); // call the parent function }
|
| |
LINK_ENTITY_TO_CLASS( capture_team2, CCaptureTeam2 );
void CCaptureTeam2 :: Spawn( ) { // Calls the precache function to precache any models, sounds,ect // you need before it spawns. Precache( ); SET_MODEL( ENT(pev), STRING(pev->model) ); UTIL_SetOrigin( pev, pev->origin ); pev->movetype = MOVETYPE_FLY;
// This allows you to trigger it by walking through it pev->solid = SOLID_TRIGGER;
// This just sets our Touch to Touch and our Think to Think // which is down a little further. SetTouch(Touch); SetThink(Think);
// Set next think time pev->nextthink = gpGlobals->time + 0.1;
// Make the entity invisible pev->rendermode = kRenderTransTexture; pev->renderamt = 0; }
void CCaptureTeam2 :: Precache( ) { PRECACHE_MODEL( (char*)STRING(pev->model) ); } void CCaptureTeam2 :: Think( ) { // loop through every player and check if they are in the area for(int i=0; i<gpGlobals->maxClients; i++) { CBasePlayer *pPlayer = (CBasePlayer *)UTIL_PlayerByIndex(i); if(pPlayer && pPlayer->m_iTeam == 2) { if((pPlayer->pev->origin.x >= pev->mins.x) && (pPlayer->pev->origin.x <= pev->maxs.x) && (pPlayer->pev->origin.y >= pev->mins.y) && (pPlayer->pev->origin.y <= pev->maxs.y) && (pPlayer->pev->origin.z >= pev->mins.z) && (pPlayer->pev->origin.z <= pev->maxs.z)) pPlayer->m_bInCapture = true; else pPlayer->m_bInCapture = false; } } // Set the next think time pev->nextthink = gpGlobals->time + 0.1; } void CCaptureTeam2 :: Touch( CBaseEntity* pOther ) { // Determine if the object that touches it, is a player // and check if the player is alive. if(!pOther) return; if(!pOther->IsPlayer()) return; if(!pOther->IsAlive()) return;
CBasePlayer *pPlayer = (CBasePlayer *)pOther;
if(pPlayer && pPlayer->m_iTeam == 2) { // Check to see if they have the object if( !pPlayer->m_fHasObject ) return; else { // Print to HUD who has captured the flag UTIL_ClientPrintAll( HUD_PRINTCENTER, UTIL_VarArgs ( "%s has captured the Flag!\n", STRING( pPlayer->pev->netname ))); // Remove the object pPlayer->m_fHasObject = false;
PLAYBACK_EVENT_FULL(0, pPlayer->edict(), g_usObject, 0, (float *)&g_vecZero, (float *)&g_vecZero, 0, 0, FLAG_CAPTURE, 0, 0, 0);
// Reset the object CObjectFlag *pFlag = (CObjectFlag *)UTIL_FindEntityByClassname(NULL, "object_flag"); if(pFlag) { pFlag->m_fIsInPlay = false;
// Do a funky effect on the flag when it gets reset pFlag->pev->effects = EF_BRIGHTFIELD; } } } else return; // Wrong team }
void CCaptureTeam2 :: KeyValue( KeyValueData* Data ) { // This will store the points but its not fully implemented. I'll leave it for you to do. if( FStrEq( Data->szKeyName, "points" ) ) { int m_iPoints = atoi(Data->szValue); Data->fHandled = true; }
CBaseEntity::KeyValue(Data); // call the parent function }
|
That just about raps it up for this part of the tutorial. If you have suggestions,complaints or need help with something from this tutorial, please email me at the_dark_wonderer@hotmail.com |
|
User Comments
No User Comments
You must register to post a comment. If you have already registered, you must login.
|
Any money that is donated will go towards the server costs that I incur for running our server. Don't feel you HAVE to donate, but the link is there if you want to help us out a little. Thanks :)
|
296 Approved Articless
3 Pending Articles
3940 Registered Members
0 People Online (7 guests)
|
|