Game Development Community

Weapon.cs

by Levi Putna · in Torque Game Engine · 04/04/2006 (12:11 am) · 5 replies

Does anyone have a copy of rifle.cs or some code for a simple weapon that I can work from. I am new to toque and am having some trouble creating a simple weapon. (like a handgun or rifle) I am finding the crossbow.cs a little confusing with all the particle emitters.

If anyone has some simple code they are willing to share that would be great.

#1
04/04/2006 (4:24 am)
Just ignore the particle information or remove it if that makes life easier. Weapon behaviour is defined in the state system, that's where you should be looking.

To help you out I'll give you some basic information to get you going.

- Create your rifle dts model, ammo dts model, textures and sound. Create a new folder called rifle and place it in the data/shapes directory.

- Create a new *.cs file called 'rifle.cs'. Place the file in the server/scripts directory. Just use notepad if you don't have a proper script editor.

- Execute this script from server/scripts/game.cs like so (you will find a whole bunch of these near top of file)
exec("./rifle.cs");

Ok, now in your newly created rifle script lets define some sounds for the rifle like so
// Sounds profiles

datablock AudioProfile(RifleFireSound)
{
filename = "~/data/sound/rifle_firing.ogg";
description = AudioClose3d;
preload = true;
};

datablock AudioProfile(RifleReloadSound)
{
filename = "~/data/sound/rifle_reload.ogg";
description = AudioClose3d;
preload = true;
};

datablock AudioProfile(RifleDryFireSound)
{
filename = "~/data/sound/rifle_dryfire.ogg";
description = AudioClose3d;
preload = true;
};

Naturally you'll have to find / create your own sfx and include them in the data/sound directory

We'll skip all particle and explosion stuff for now, we can go into that some other time.

Lets create the projectile information now, under your sound profiles put the following

//-----------------------------------------------------------------------------
// Projectile Object

datablock ProjectileData(RifleProjectile)
{
   projectileShapeName = "~/data/shapes/rifle/projectile.dts";
   directDamage        = 20;
   radiusDamage        = 20;
   damageRadius        = 1.5;
   areaImpulse         = 200;

   muzzleVelocity      = 100;
   velInheritFactor    = 0.3;

   armingDelay         = 0;
   lifetime            = 5000;
   fadeDelay           = 5000;
   bounceElasticity    = 0;
   bounceFriction      = 0;
   isBallistic         = false;
   gravityMod = 0.80;
};

function RifleProjectile::onCollision(%this,%obj,%col,%fade,%pos,%normal)
{
   // Apply damage to the object all shape base objects
   if (%col.getType() & $TypeMasks::ShapeBaseObjectType)
      %col.damage(%obj,%pos,%this.directDamage,"RifleProjectile");

   // Radius damage is a support scripts defined in radiusDamage.cs
   // Push the contact point away from the contact surface slightly
   // along the contact normal to derive the explosion center. -dbs
   radiusDamage(%obj, %pos, %this.damageRadius, %this.radiusDamage, "Radius", %this.areaImpulse);
}

...to be continued in next thread
#2
04/04/2006 (4:35 am)
Now for the ammo item

//-----------------------------------------------------------------------------
// Ammo Item

datablock ItemData(RifleAmmo)
{
   // Mission editor category
   category = "Ammo";

   // Add the Ammo namespace as a parent.  The ammo namespace provides
   // common ammo related functions and hooks into the inventory system.
   className = "Ammo";

   // Basic Item properties
   shapeFile = "~/data/shapes/rifle/ammo.dts";
   mass = 1;
   elasticity = 0.2;
   friction = 0.6;

   // Dynamic properties defined by the scripts
   pickUpName = "rifle ammo";
   maxInventory = 30;
};

And the weapon item

//--------------------------------------------------------------------------
// Weapon Item.  This is the item that exists in the world, i.e. when it's
// been dropped, thrown or is acting as re-spawnable item.  When the weapon
// is mounted onto a shape, the RifleImage is used.

datablock ItemData(Rifle)
{
   // Mission editor category
   category = "Weapon";

   // Hook into Item Weapon class hierarchy. The weapon namespace
   // provides common weapon handling functions in addition to hooks
   // into the inventory system.
   className = "Weapon";

   // Basic Item properties
   shapeFile = "~/data/shapes/rifle/rifle.dts";
   mass = 1;
   elasticity = 0.2;
   friction = 0.6;
   emap = true;

   // Dynamic properties defined by the scripts
   pickUpName = "a rifle";
   image = RifleImage;
};

And finally, the image and firing function

//--------------------------------------------------------------------------
// Rifle image which does all the work.  Images do not normally exist in
// the world, they can only be mounted on ShapeBase objects.

datablock ShapeBaseImageData(RifleImage)
{
   // Basic Item properties
   shapeFile = "~/data/shapes/rifle/rifle.dts";
   emap = true;

   // Specify mount point & offset for 3rd person, and eye offset
   // for first person rendering.
   mountPoint = 0;
   eyeOffset = "0.1 0.4 -0.6";

   // When firing from a point offset from the eye, muzzle correction
   // will adjust the muzzle vector to point to the eye LOS point.
   // Since this weapon doesn't actually fire from the muzzle point,
   // we need to turn this off.  
   correctMuzzleVector = false;

   // Add the WeaponImage namespace as a parent, WeaponImage namespace
   // provides some hooks into the inventory system.
   className = "WeaponImage";

   // Projectile && Ammo.
   item = Rifle;
   ammo = RifleAmmo;
   projectile = RifleProjectile;
   projectileType = Projectile;

   // Images have a state system which controls how the animations
   // are run, which sounds are played, script callbacks, etc. This
   // state system is downloaded to the client so that clients can
   // predict state changes and animate accordingly.  The following
   // system supports basic ready->fire->reload transitions as
   // well as a no-ammo->dryfire idle state.

   stateName[0]                     = "Activate";
   stateTransitionOnTimeout[0]      = "ActivateReady";
   stateTimeoutValue[0]             = 0.5;
   stateSequence[0]                 = "Activate";

   stateName[1]                     = "ActivateReady";
   stateTransitionOnLoaded[1]       = "Ready";
   stateTransitionOnNoAmmo[1]       = "NoAmmo";

   stateName[2]                     = "Ready";
   stateTransitionOnNoAmmo[2]       = "NoAmmo";
   stateTransitionOnTriggerDown[2]  = "CheckWet";

   stateName[3]                     = "Fire";
   stateTransitionOnTimeout[3]      = "Reload";
   stateTimeoutValue[3]             = 0.5;
   stateFire[3]                     = true;
   stateAllowImageChange[3]         = false;
   stateSequence[3]                 = "Fire";
   stateScript[3]                   = "onFire";
   stateSound[3]                    = RifleFireSound;

   stateName[4]                     = "Reload";
   stateTransitionOnTimeout[4]      = "Ready";
   stateTimeoutValue[4]             = 0.5;
   stateAllowImageChange[4]         = false;
   stateSound[4]                    = RifleReloadSound;

   stateName[5]                     = "CheckWet";
   stateTransitionOnWet[5]          = "DryFire";
   stateTransitionOnNotWet[5]       = "Fire";
   
   stateName[6]                     = "NoAmmo";
   stateTransitionOnAmmo[6]         = "Reload";
   stateTransitionOnTriggerDown[6]  = "DryFire";
   stateSequence[6]                 = "NoAmmo";
   
   stateName[7]                     = "DryFire";
   stateSound[7]                    = RifleDryFireSound;
   stateTimeoutValue[7]             = 0.5;
   stateTransitionOnTimeout[7]      = "Ready";
};


//-----------------------------------------------------------------------------

function RifleImage::onFire(%this, %obj, %slot)
{
   %projectile = %this.projectile;

   // Decrement inventory ammo. The image's ammo state is update
   // automatically by the ammo inventory hooks.
   %obj.decInventory(%this.ammo,1);

   // Determin initial projectile velocity based on the 
   // gun's muzzle point and the object's current velocity
   %muzzleVector = %obj.getMuzzleVector(%slot);
   %objectVelocity = %obj.getVelocity();
   %muzzleVelocity = VectorAdd(
      VectorScale(%muzzleVector, %projectile.muzzleVelocity),
      VectorScale(%objectVelocity, %projectile.velInheritFactor));

   // Create the projectile object
   %p = new (%this.projectileType)() {
      dataBlock        = %projectile;
      initialVelocity  = %muzzleVelocity;
      initialPosition  = %obj.getMuzzlePoint(%slot);
      sourceObject     = %obj;
      sourceSlot       = %slot;
      client           = %obj.client;
   };
   MissionCleanup.add(%p);
   return %p;
}

That's a very basic rifle setup. There are many more functions available to weapons like projectile spread, using looping sounds for an even fire sound, impulse effects and ofcourse particle effects. I haven't gone into these as to not overload you with information.

As you can see the characteristics of a weapon are defined in the state system, see if you can alter the one i've included above to suit a shotgun, or maybe a rapid fire weapon like a machine gun.

NB the setup above will not let you fire underwater, just thought i'd chuck that in there incase you didn't know about it.

Good luck!!!
#3
04/04/2006 (6:29 pm)
Thanks that was a great help,
I just have one little problem, I creates some code to pickup the gun when the player walk over it but it doesn't seem to be doing anything.

Am I going about doing this the correct way?

function Rifle::onCollision( %this, %obj, %col )
{
%col.incinventory(Rifle,1);
%col.mountImage(RifleImage, 0 );
%col.setImageAmmo( 0, 1 );

}
#4
04/04/2006 (8:10 pm)
I wouldn't be doing it that way. You'll want a form of weapon selection, check out this resource to get you started.

Weapon Cycling in Torque


and these may also be helpful

Three Weapons for Torque

Ammo Hud Tutorial

Bullet Hole Decals

Weapon Reload Tutorial

How to add Ammo Clips and reload functionality
#5
04/06/2006 (4:15 am)
Thanks that helped a lot, I think I finally understand how the gun and projectiles work. I noticed that the built hole tutorial is one of yours. I am in the middle of exams at the moment but I will try the code out when I get a chance.

Thanks