Ok I've given the Soul Cube a Gravity Gun ability while in the Zoomed in mode. Press fire to grab. Press alt fire to throw grabbed objects. It all works very well but I cant seem to keep the weapon from wanting to holster and switch back to the last weapon when grabbing stuff. Is there a way to keep the weapon from wanting to switch back to the previous weapon when you press the "fire" key? It'll stay in the zoomed mode and allow you to use the gravity feature as long as you don't zoom out but the instant you zoom out it switches back to the previous weapon. The hud also displays the previous weapon and 1 round of ammo as soon as you press the fire key while zoomed in. Its like its just waiting for you to zoom out so it can switch back to the previous weapon. Here is my script file. Please help if you can. Also if you can figure out how to allow this gravity gun feature without any ammo having to be available that would be awesome.
Code Select
/***********************************************************************
weapon_soulcube.script
***********************************************************************/
// blend times
#define SOULCUBE_IDLE_TO_LOWER 4
#define SOULCUBE_IDLE_TO_FIRE 4
#define SOULCUBE_RAISE_TO_IDLE 4
#define SOULCUBE_SPIN_TO_IDLE 0
#define SOULCUBE_IDLE_TO_SPIN 0
#define GG_RANGE 1150 //objects can be captured within this range
#define GG_HOVER 125 //captured objects hover in front of player at this distance
#define GG_AMMO_STRENGTH 10 //one ammo unit strength
#define GG_TARGET_SPEED 1100 //captured objects move to hover position with this speed
#define GG_LAUNCH_TIME 1 //caputred objects drop (after fire button is released) within this time, launch after
#define GG_LAUNCH_SPEED 24000 //captured objects launch away from player (after fire button is released) with this speed
#define GG_KICKBACK_SPEED 30 //player push back after captured object launch
#define GG_LAUNCH_AMMO 5 //ammo units used after captured object launch
object weapon_soulcube : weapon_base {
//entity beam1;
entity gg_owner;
//entity emit;
entity target;
float x;
float y;
float z;
float e;
float z_angle;
float xy_angle;
float target_health;
float justhurt;
float Parmangle;
float ammo_clip;
string target_class;
vector gg_angles;
vector gg_start;
vector gg_end;
vector gg_owner_size;
entity hand_emit;
entity weapon_ent;
entity owner;
float dragging;
vector target_pos;
vector move_dir;
vector push_dir;
float distance;
float frac;
float launch_time;
float current_time;
vector target_size;
float ftime;
float adjustorigin;
void init();
void UpdateShaderParms();
void Lower();
void Raise();
void Idle();
void Fire();
void Gravity();
void Grab();
void Dragging();
void Throw();
void Launch();
void ExitCinematic();
void FireSecondary();
};
void weapon_soulcube::init() {
cacheSoundShader( "player_drag" );
cacheSoundShader( "player_stopdrag" );
cacheSoundShader( "player_startdrag" );
cacheSoundShader( "player_drag_hum" );
weaponState( "Raise", 0 );
}
void weapon_soulcube::UpdateShaderParms() {
setShaderParm( SHADERPARM_MODE, totalAmmoCount() );
}
void weapon_soulcube::Raise() {
weaponRising();
playAnim( ANIMCHANNEL_ALL, "raise" );
while( !animDone( ANIMCHANNEL_ALL, SOULCUBE_RAISE_TO_IDLE ) ) {
UpdateShaderParms();
waitFrame();
}
weaponState( "Idle", SOULCUBE_RAISE_TO_IDLE );
}
void weapon_soulcube::Lower() {
weaponLowering();
playAnim( ANIMCHANNEL_ALL, "putaway" );
while( !animDone( ANIMCHANNEL_ALL, 0 ) ) {
UpdateShaderParms();
waitFrame();
}
weaponHolstered();
waitUntil( WEAPON_RAISEWEAPON );
weaponState( "Raise", 0 );
}
void weapon_soulcube::Idle() {
float count;
weaponReady();
count = 8 + sys.random( 8 );
while( 1 ) {
count--;
if ( count <= 0 ) {
playAnim( ANIMCHANNEL_ALL, "spin" );
count = 8 + sys.random( 8 );
} else {
playAnim( ANIMCHANNEL_ALL, "idle" );
}
while( !animDone( ANIMCHANNEL_ALL, 0 ) ) {
UpdateShaderParms();
if ( WEAPON_LOWERWEAPON ) {
weaponState( "Lower", SOULCUBE_IDLE_TO_LOWER );
}
if ( WEAPON_ATTACK && !isLowered() && !BUTTON_ZOOM ) {
weaponState( "Fire", SOULCUBE_IDLE_TO_FIRE );
}
if ( BUTTON_ALTFIRE && !isLowered() && !BUTTON_ZOOM && ( ammoAvailable() > 0 ) ) {
weaponState( "FireSecondary", SOULCUBE_IDLE_TO_FIRE );
}
if ( BUTTON_ALTFIRE && !isLowered() && ( !ammoAvailable() ) ) {
weaponState( "Lower", SOULCUBE_IDLE_TO_LOWER );
}
if ( BUTTON_ZOOM && ( ammoAvailable() > 0 ) ) {
weaponState( "Gravity", SOULCUBE_IDLE_TO_SPIN );
}
if ( BUTTON_ZOOM && ( !ammoAvailable() ) ) {
weaponState( "Lower", SOULCUBE_IDLE_TO_LOWER );
}
waitFrame();
}
}
}
void weapon_soulcube::Fire() {
WEAPON_START_FIRING = true;
playAnim( ANIMCHANNEL_ALL, "fire" );
while( !animDone( ANIMCHANNEL_ALL, 0 ) ) {
UpdateShaderParms();
waitFrame();
}
WEAPON_START_FIRING = false;
weaponHolstered();
}
void weapon_soulcube::FireSecondary() {
WEAPON_START_FIRING = true;
playAnim( ANIMCHANNEL_ALL, "fire" );
launchProjectiles( 0, 0, 0, 1.0, 1.0 ); // don't launch any projectiles, we need this to fire off the soul's ammo
$player1.fireProjectile( "projectile_SKspawner", getJointPos(1), $player1.getViewAngles() );
WEAPON_START_FIRING = false;
weaponHolstered();
}
void weapon_soulcube::Gravity() {
float gg_trace;
string target_name;
dragging = 0;
weaponReady();
while ( !WEAPON_RELOAD && BUTTON_ZOOM ) {
if ( WEAPON_ATTACK ) {
//ammo_clip = ammoInClip();
gg_owner = getOwner();
gg_start = gg_owner.getWorldOrigin();
gg_owner_size = gg_owner.getSize();
gg_start_z = gg_start_z + (gg_owner_size_z - 6);
gg_angles = gg_owner.getViewAngles();
z_angle = gg_angles_x * -1;
xy_angle = gg_angles_y;
z = sys.sin(z_angle) * GG_RANGE;
e = sys.cos(z_angle) * GG_RANGE;
y = sys.sin(xy_angle) * e;
x = sys.cos(xy_angle) * e;
gg_end_x = x + gg_start_x;
gg_end_y = y + gg_start_y; // //CONTENTS_PROJECTILE|
gg_end_z = z + gg_start_z;
gg_trace = sys.tracePoint(gg_start, gg_end, CONTENTS_CORPSE|MASK_MONSTERSOLID|CONTENTS_RENDERMODEL, gg_owner);
target = sys.getTraceEntity();
target_class = target.getKey("spawnclass");
//target_health = target.getHealth();
target_name = target.getName();
//sys.print( "Class: " + target_class + "\n" );
if (target_class == "idAFEntity_Generic" || target_class == "idAFEntity_WithAttachedHead" || target_class == "idAI" || target_class == "idPlayer" || target_class == "idExplodingBarrel" || target_class == "idMoveable" || target_class == "idMoveableItem") {
weaponState( "Grab", SOULCUBE_IDLE_TO_SPIN );
}
}
waitFrame();
}
weaponState( "Idle", SOULCUBE_SPIN_TO_IDLE );
}
void weapon_soulcube::Grab() {
float th;
justhurt = 0;
adjustorigin = 0;
weapon_ent = gg_owner.getWeaponEntity();
playAnim( ANIMCHANNEL_ALL, "idle" );
sys.setSpawnArg( "model", "hand_glow_drag.prt" );
sys.setSpawnArg( "origin", target.getOrigin() );
hand_emit = sys.spawn( "func_emitter" );
//startSoundShader( "player_startdrag", SND_CHANNEL_ANY );
startSoundShader( "player_drag", SND_CHANNEL_ANY );
launch_time = (sys.getTime() + GG_LAUNCH_TIME);
if ( target_class == "idAI" || target_class == "idPlayer" ) { //just hurt monsters if their not dead yet
if ( target.getHealth() > 0 ) {
justhurt = 1;
//emit.setOrigin( '0 0 32' );
//startSoundShader( "player_startdrag", SND_CHANNEL_ANY );
playAnim( ANIMCHANNEL_ALL, "idle" );
target.allowMovement( false );
// useAmmo( 5 );
while ( WEAPON_ATTACK ) {
// sys.radiusDamage( target.getOrigin(), self, gg_owner, gg_owner, "damage_oneshot", 1.0 );
th = target.getHealth();
//SHOW THE MONSTERS HEALTH IN THE METERS
// setShaderParm( 2, (th / 750 ));
ftime = sys.getTime() + 0.1; //pain every 0.3 seconds
while ( ftime > current_time ) {
current_time = sys.getTime();
hand_emit.setWorldOrigin(weapon_ent.getJointPos(weapon_ent.getJointHandle( "Barrel" )));
waitFrame();
}
if ( target.getHealth() < 1 ) {
justhurt = 0;
weaponState( "Dragging", 1 );
}
waitFrame();
}
stopSound( SND_CHANNEL_ANY, false );
target.allowMovement( true );
}
}
if ( justhurt != 1 ) {
weaponState( "Dragging", 0 );
}
hand_emit.remove();
weaponState( "Gravity", 4 );
}
void weapon_soulcube::Dragging() {
// ammo_clip = ammoInClip();
// if ( ammo_clip < 1 ) {
// hand_emit.remove();
// weaponState( "Reload", 2 );
// }
dragging = 1;
// weapon_ent.setSkin( "skins/gg_active" );
// useAmmo( 1 );
while ( WEAPON_ATTACK ) { //following math continously calcs hover & captured object positions
gg_start = gg_owner.getWorldOrigin();
gg_owner_size = gg_owner.getSize();
gg_start_z = gg_start_z + (gg_owner_size_z - 6);
gg_angles = gg_owner.getViewAngles();
z_angle = gg_angles_x * -1;
xy_angle = gg_angles_y;
z = sys.sin(z_angle) * GG_HOVER;
e = sys.cos(z_angle) * GG_HOVER;
y = sys.sin(xy_angle) * e;
x = sys.cos(xy_angle) * e;
gg_end_x = x + gg_start_x;
gg_end_y = y + gg_start_y;
if ( target_class == "idAFEntity_WithAttachedHead" || target_class == "idAI" || target_class == "idPlayer" ) {
gg_end_z = z + gg_start_z + 16;
} else {
gg_end_z = z + gg_start_z;
}
target_pos = target.getWorldOrigin();
move_dir = gg_end - target_pos; //direction from captured object towards hover postion
distance = sys.vecLength(move_dir);
setShaderParm( 2, 0.02 + (distance / 60) + sys.random(0.05) );
move_dir = sys.vecNormalize(move_dir);
frac = (distance / GG_HOVER);
target.setLinearVelocity(move_dir * frac * GG_TARGET_SPEED); //move captured object towards hover position
hand_emit.setWorldOrigin(weapon_ent.getJointPos(weapon_ent.getJointHandle( "barrel" )));
if ( BUTTON_ALTFIRE && !isLowered() ) {
stopSound( SND_CHANNEL_ANY, false );
weaponState( "Throw", 0 );
}
waitFrame();
}
current_time = sys.getTime();
setShaderParm( 2, 0.01 );
stopSound( SND_CHANNEL_ANY, false );
waitFrame();
dragging = 0;
hand_emit.remove();
// weapon_ent.setSkin( "" );
weaponState( "Gravity", 4 );
}
void weapon_soulcube::Throw() {
// ammo_clip = ammoInClip();
// if ( ammo_clip < 3 ) {
// stopSound( SND_CHANNEL_ANY, false );
// hand_emit.remove();
// weaponState( "Idle", 2 );
// }
// useAmmo( 3 );
// weapon_ent.setSkin( "" );
hand_emit.remove();
startSoundShader( "player_stopdrag", SND_CHANNEL_ANY );
push_dir = gg_start - target_pos;
push_dir = sys.vecNormalize(push_dir);
playAnim( ANIMCHANNEL_ALL, "Idle" );
if ( target_class == "idExplodingBarrel" || target_class == "idMoveable" || target_class == "idMoveableItem") {
target.setLinearVelocity(push_dir * (GG_LAUNCH_SPEED/20) * -1);
} else {
target.setLinearVelocity(push_dir * (GG_LAUNCH_SPEED/1.5) * -1); //objects such as monsters get a higher speed due to higher mass
}
gg_owner.setLinearVelocity(push_dir * GG_KICKBACK_SPEED);
ftime = sys.getTime() + 1; //hold pain for 1.5 seconds
while ( ftime > current_time ) {
current_time = sys.getTime();
sys.radiusDamage( target.getOrigin(), self, gg_owner, target, "damage_gravitygun", 2.0 );
setShaderParm( 2, 0.01 + sys.random(0.02) );
waitFrame();
}
weaponState( "Gravity", 3 );
}
void weapon_soulcube::Launch() {
launchProjectiles( 1, 0, 0, 1.0, 1.0 );
}
void weapon_soulcube::ExitCinematic() {
weaponState( "Idle", 0 );
}