Game Development Community

Throwing grenates only if you have ....

by Michael Schaumburg · in Torque Game Engine · 02/16/2005 (10:05 am) · 2 replies

I added the nice tutorial on adding grenates to throw. All is working fine but
I like that the player can throw them only if he has grenates.
I added in function in weapon.cs to get grenates into the inventory :

//-----------------------------------------------------------------------------
// Grenade Class 
//-----------------------------------------------------------------------------
function Grenade::onPickup(%this, %obj, %shape, %amount)
{
   // The parent Item method performs the actual pickup.
   if (Parent::onPickup(%this, %obj, %shape, %amount)) {
      serverPlay3D(GrenadePickupSound,%obj.getTransform());
   }
}

In player.cs I added maxInv[Grenade] = 10; to get the maximum of 10 grenates.

The rest of the throwing code is this :

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;  
	}; 
	
	//make sure we tidy up :)  
	MissionCleanup.add(%item);

	//call the more general throwObject  
	%this.throwObject(%item);  

	
	//set detonantion delay   
	%item.ExplodeEventID = %GrenadeTypeDB.schedule(%DelayToExplode, "Explode",%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);
	
}

function serverCmdthrowGrenadeNow(%client)
{
   	%client.player.throwGrenadeDB(Grenade,%client,5000);
}

moveMap.bindCmd(keyboard, "g", "commandToServer(\'throwGrenadeNow\');", "");

What I have to do, that I only can throw grenates if they are in the inventory ???
Any help will be great, I tried a lot but nothing works......

#1
02/16/2005 (11:59 am)
Something else I can not understand. If I add :

function Grenade::onUse(%this,%user);
{
     %user.decInventory(%this,1);	
}

I get in the console ' unknown command throwGrenadeNow' . Why ???????
#2
02/16/2005 (1:08 pm)
Because you not using it.
Its only in the inventory , try to make a grenade weaponimage.
When you pick it up add grenadeammo.
When you throw it decAmmo , unmount grenade.

I dont have things like this in my game so this is an idea only.