Laser Beams!

  • Thread starter Thread starter Vage
  • Start date Start date
V

Vage

Guest
Sh'ok... I've got a question, hopefully someone cool over here can help me answer it. I'm trying to learn the SDK so I fooled around with the MP5 in the stripped down MP SDK, to only get a myraid of errors.

I'm wondering how you can get a gun to shoot lasers, maybe using te_beamlaser or some such thing? ...Help... -.-
 
Ah, thanks anyways ...

I've got it all sorted out I believe! I've created a lame laser sight on the MP5 as test. I used the MP SDK. I only have one problem with it: It updates funky. It looks almost like 2 laserbeams. Any help with that would be great! Here is some code for anyone else that has this trouble:

Code:
#include "cbase.h"
#include "mathlib.h"
#include "beam_shared.h"
#include "IEffects.h"
#include "weapon_sdkbase.h"
#include "sdk_fx_shared.h"
#include "effect_color_tables.h"
#include "effect_dispatch_data.h"

#include "collisionutils.h"

#if defined( CLIENT_DLL )

	#define CWeaponMP5 C_WeaponMP5
	#include "c_sdk_player.h"

#else

	#include "sdk_player.h"

#endif

#undef MAX_TRACE_LENGTH
#define	MAX_TRACE_LENGTH 4096

class CWeaponMP5 : public CWeaponSDKBase
{
public:
	DECLARE_CLASS( CWeaponMP5, CWeaponSDKBase );
	DECLARE_NETWORKCLASS(); 
	DECLARE_PREDICTABLE();
	
	CWeaponMP5();

	virtual void PrimaryAttack();
	virtual bool Deploy();
	virtual bool Reload();
	virtual void WeaponIdle();
	virtual void ItemPostFrame();
	virtual void Drop(const Vector & );
	virtual bool Holster( CBaseCombatWeapon * );
	//virtual void Precache();

	virtual SDKWeaponID GetWeaponID( void ) const		{ return WEAPON_MP5; }


private:

	CWeaponMP5( const CWeaponMP5 & );
	
	CBeam *pBeam;

	virtual void ActivateLaser( const Vector &, const Vector & );
	virtual void DeactivateLaser();
	virtual void UpdateLaser( const Vector &, const Vector & );

	void Fire( float flSpread );
};

IMPLEMENT_NETWORKCLASS_ALIASED( WeaponMP5, DT_WeaponMP5 )

BEGIN_NETWORK_TABLE( CWeaponMP5, DT_WeaponMP5 )
END_NETWORK_TABLE()

BEGIN_PREDICTION_DATA( CWeaponMP5 )
END_PREDICTION_DATA()

LINK_ENTITY_TO_CLASS( weapon_mp5, CWeaponMP5 );
PRECACHE_WEAPON_REGISTER( weapon_mp5 );

CWeaponMP5::CWeaponMP5()
{}

//Updates the gun...
void CWeaponMP5::ItemPostFrame( void )
{
	CSDKPlayer *pPlayer = GetPlayerOwner();
	
	if ( pPlayer == NULL )
		return;

	Vector	forward;
	pPlayer->EyeVectors( &forward );
	Vector vecMuzzlePos = pPlayer->Weapon_ShootPosition() + Vector(0, -3, -3);
	Vector vecEndPos = vecMuzzlePos + ( forward * MAX_TRACE_LENGTH );

	trace_t	tr;
	// Trace out for the endpoint
	UTIL_TraceLine( vecMuzzlePos, vecEndPos, (MASK_SHOT & ~CONTENTS_WINDOW), this, COLLISION_GROUP_NONE, &tr );

	Vector laserPos = tr.endpos;

	if (!pBeam)
	{
		// Beam is out for first time.
		ActivateLaser( laserPos, vecMuzzlePos );
	}
	else
	{
		// Beam seems to be on, update the mutha!
		UpdateLaser( laserPos, vecMuzzlePos );
	}

	BaseClass::ItemPostFrame();
}

void CWeaponMP5::UpdateLaser( const Vector &laserPos, const Vector &vecMuzzlePos )
{
	pBeam->SetEndPos(vecMuzzlePos);
	pBeam->SetStartPos(laserPos);
	pBeam->RelinkBeam();
}

void CWeaponMP5::DeactivateLaser( )
{
		if (!pBeam) { return; }

        pBeam->TurnOff();
        pBeam->RelinkBeam();
        pBeam->SUB_Remove();

        pBeam = NULL;
		// Clear our pointer to the beam.
}

void CWeaponMP5::ActivateLaser( const Vector &vecLaserPos, const Vector &vecMuzzlePos )
{
	if (pBeam) { return; }
	pBeam = CBeam::BeamCreate( "sprites/physbeam.vmt", 0.2 ); 

	if ( pBeam == NULL )
	{
		// We were unable to create the beam
		Assert(0);
		return;
	}

	pBeam->PointsInit( vecLaserPos, vecMuzzlePos );
	pBeam->SetBrightness( 255 );
	//pBeam->SetNoise( 0 );
	pBeam->SetHaloTexture( NULL );
	pBeam->SetHaloScale( 0 );
	pBeam->SetWidth( 0.5 );
	pBeam->SetEndWidth( 0 );
	//pBeam->SetScrollRate( 0 );
	//pBeam->SetFadeLength( 0 );
	pBeam->SetColor( 255, 0, 0 );
	pBeam->RelinkBeam();
}

bool CWeaponMP5::Holster( CBaseCombatWeapon *pSwitchingTo )
{
	CWeaponMP5::DeactivateLaser( );
	return BaseClass::Holster( pSwitchingTo );
}

void CWeaponMP5::Drop( const Vector &vecVelocity )
{
	CWeaponMP5::DeactivateLaser( );

	BaseClass::Drop( vecVelocity );
}

bool CWeaponMP5::Deploy( )
{
	CSDKPlayer *pPlayer = GetPlayerOwner();
	pPlayer->m_iShotsFired = 0;

	return BaseClass::Deploy();
}

bool CWeaponMP5::Reload( )
{
	CSDKPlayer *pPlayer = GetPlayerOwner();

	if (pPlayer->GetAmmoCount( GetPrimaryAmmoType() ) <= 0)
		return false;

	int iResult = DefaultReload( GetMaxClip1(), GetMaxClip2(), ACT_VM_RELOAD );
	if ( !iResult )
		return false;

	pPlayer->SetAnimation( PLAYER_RELOAD );

#ifndef CLIENT_DLL
	if ((iResult) && (pPlayer->GetFOV() != pPlayer->GetDefaultFOV()))
	{
		pPlayer->SetFOV( pPlayer, pPlayer->GetDefaultFOV() );
	}
#endif

	pPlayer->m_iShotsFired = 0;

	return true;
}

void CWeaponMP5::PrimaryAttack( void )
{
	const CSDKWeaponInfo &pWeaponInfo = GetSDKWpnData();
	CSDKPlayer *pPlayer = GetPlayerOwner();

	float flCycleTime = pWeaponInfo.m_flCycleTime;

	bool bPrimaryMode = true;

	float flSpread = 0.01f;

	// more spread when jumping
	if ( !FBitSet( pPlayer->GetFlags(), FL_ONGROUND ) )
		flSpread = 0.05f;
	
	pPlayer->m_iShotsFired++;

	// Out of ammo?
	if ( m_iClip1 <= 0 )
	{
		if (m_bFireOnEmpty)
		{
			PlayEmptySound();
			m_flNextPrimaryAttack = gpGlobals->curtime + 0.2;
		}
	}

	SendWeaponAnim( ACT_VM_PRIMARYATTACK );

	m_iClip1--;

	// player "shoot" animation
	pPlayer->SetAnimation( PLAYER_ATTACK1 );

	FX_FireBullets(
		pPlayer->entindex(),
		pPlayer->Weapon_ShootPosition(),
		pPlayer->EyeAngles() + pPlayer->GetPunchAngle(),
		GetWeaponID(),
		bPrimaryMode?Primary_Mode:Secondary_Mode,
		CBaseEntity::GetPredictionRandomSeed() & 255,
		flSpread );

	pPlayer->DoMuzzleFlash();
	
	m_flNextPrimaryAttack = m_flNextSecondaryAttack = gpGlobals->curtime + flCycleTime;

	if (!m_iClip1 && pPlayer->GetAmmoCount( m_iPrimaryAmmoType ) <= 0)
	{
		// HEV suit - indicate out of ammo condition
		pPlayer->SetSuitUpdate("!HEV_AMO0", false, 0);
	}

	// start idle animation in 5 seconds
	SetWeaponIdleTime( gpGlobals->curtime + 5.0 );
}

void CWeaponMP5::WeaponIdle()
{
	if (m_flTimeWeaponIdle > gpGlobals->curtime)
		return;

	// only idle if the slid isn't back
	if ( m_iClip1 != 0 )
	{
		SetWeaponIdleTime( gpGlobals->curtime + 5.0f );
		SendWeaponAnim( ACT_VM_IDLE );
	}
}
 
Back
Top