Game Development Community

dev|Pro Game Development Curriculum

Drop-in Grenades.

by FruitBatInShades · 01/30/2005 (1:08 am) · 43 comments

This little resource started life out as this thread but did not work for me. With the help of Craig Ball, Anthony Rosenbaum(who also wrote the original code) and David Barr I got the basic stuff working. This is my first attempt at torquescript so bear with me but it works well apart from the two issues mentioned at the end.

Save this as Grenades.cs in your server scripts directory:-
//-----------------------------------------------------------
//  Grenades.cs - Class to add grenades to torque via script
//-----------------------------------------------------------
// This script file is an easy way to add grenades to your
// FPS.  Just exec this script from game.cs and bind a key 
// to call the throwGrenadeDB function
//-----------------------------------------------------------

datablock ItemData(Grenade)
{
   category = "Grenades";
   shapeFile = "starter.fps/data/shapes/items/healthkit.dts";
   mass = 0.7;
   friction = 1;
   elasticity = 0.3; //how much bounce 0.1 = none, 1 = funny
   repairAmount = 50;
   maxDamage = 0.2;
   directDamage = 1;
   damageRadius = 20;
   radiusDamage = 1;
   areaImpulse = 1000;
   dynamicType = $TypeMasks::DamagableItemObjectType;
   explosion = CrossbowExplosion;
   fadeIn = 0;
};

function Grenade::Explode(%dataBlock, %obj)
{
   echo("Grenade::Explode called");
   %pos = %obj.getPosition();
   echo("Grenade::Explode Doing Damaged");
   radiusDamage(%obj,%pos,%dataBlock.damageRadius,%dataBlock.radiusDamage,"Grenade",%dataBlock.areaImpulse);
   %obj.setDamageState(Destroyed);
   %obj.schedule(99, "delete");
}

function Grenade::Damage(%this,%obj,%sourceObject, %position, %damage, %damageType)
{
   echo("Grenade::Damage called");
   //Grenade has recieved damage so detonate it
   cancel(%obj.ExplodeEventID);//Cancel previous explode timer
   %obj.setDamageState(Destroyed);
   %obj.schedule(99,"delete");
}

function Grenade::onDestroyed(%data,%obj)
{
   echo("Grenade::onDestroyed called");
}

function ShapeBase::throwGrenadeDB(%this,%GrenadeTypeDB,%client,%DelayToExplode)
{
	//%GrenadeTypeDB 	= the datablock type for this grenade
	//%client			= client object
	//%time				= time in milliseconds before explode
	
	//Check time is not silly or negative
	if (%DelayToExplode < 1)
		%DelayToExplode = 500;
	
	echo("ShapeBase::throwGrenade grenade, creating item");
	%item = new Item()
	{
		dataBlock 	= %GrenadeTypeDB;
		rotation 	= "1 0 0 " @ (getRandom() * 360);
		sourceObject= %client.player;
		client		= %client;
		ExplodeEventID=0;
	};
	//call the more general throwObject
	%this.throwObject(%item);
	//set detonantion delay
	%item.ExplodeEventID = %GrenadeTypeDB.schedule(%DelayToExplode, "Explode",%item);
	//make sure we tidy up :)
	MissionCleanup.add(%item);
}

function ShapeBase::throwObject(%this,%obj){
   // Throw the given object in the direction the shape is
   // looking.  The force values are hardcoded...
   echo("ShapeBase::throwObject grenade, getting direction");
   %eye = %this.getEyeVector();
   %vec = vectorScale(%eye, 20);
   // Add a vertical component to give the item a better arc
   %dot = vectorDot("0 0 1",%eye);
      if (%dot < 0)
            %dot = -%dot;
      %vec = vectorAdd(%vec,vectorScale("0 0 7",1 - %dot));
      // Add the shape's velocity
      %vec = vectorAdd(%vec,%this.getVelocity());
      // Set the object's position and initial velocity
      %pos = getBoxCenter(%this.getWorldBox());
      %obj.setTransform(%pos);
      echo("ShapeBase::throwObject grenade, Applying impulse");
      // Since the object is thrown from the center of the
      // shape, the object needs to avoid colliding with it's
      // thrower.
      %obj.setCollisionTimeout(%this);    
      %obj.applyImpulse(%pos,%vec);
}
If you know what your doing, put the binding stuff anywhere you like :)
Known issues
There are a few issues, if you can solve them please let me know so I can update this resource.

1. Grenades just sometimes disappear. Usually the first one thrown and then randomly after that. Still trying to figuire out why.
2.
Page«First 1 2 3 Next»
#41
01/10/2007 (12:18 pm)
This is a great resource
#42
08/09/2008 (5:22 am)
cool... cool...
#43
02/23/2009 (7:56 pm)
I want to show a timer above the grenade before explode,
how do I do that?
any idea?
Page«First 1 2 3 Next»