T3D/Tutorials/SimpleFPSTutorial/Part3

From TDN

Simple FPS Tutorial for Torque3D



Back to Part Two: Custom Weapons Art Scripts - SemiAuto

Part THREE




Custom Weapons Art Scripts - fullauto:

Now on to the other custom weapon script. Make a new file called "fullauto.cs", you could copy the current file instead, change it's name and then open it and replace every instance of "semiauto" for "fullauto". You'll still need to do some edits.

Firstly, the "Projectile" wants to be slightly less powerful and with much shorter range, think like a submachine-gun.

datablock ProjectileData(fullautoProjectile)
{
   projectileShapeName = "art/shapes/weapons/SwarmGun/rocket.dts";
   directDamage = 15;
   radiusDamage = 0;
   damageRadius = 0;
   areaImpulse = 2500;

   explosion = bulletImpact;
   waterExplosion = bulletWaterImpact;

   decal = bulletHoleDecal;
   splash = bulletSplashRingEmitter;

   muzzleVelocity = 100;
   velInheritFactor = 0.3;

   armingDelay = 0;
   lifetime = 500; //(50 units range)
   fadeDelay = 400;

   bounceElasticity = 0;
   bounceFriction = 0;
   isBallistic = false;
   gravityMod = 0.80;

   damageType = "autobulletDamage";
};

With directDamage of 15, the player can take 7 hits before death. The range is reduced to 50 units (100 * 500 /1000).

datablock ItemData(fullautoAmmo)
{
   // 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 = "art/shapes/weapons/SwarmGun/rocket.dts";
   mass = 2;
   elasticity = 0.2;
   friction = 0.6;

   // Dynamic properties defined by the scripts
   pickUpName = "fullauto Ammo";
   maxInventory = 30;
};

Whilst it may have a shorter range and less damage than the "semiauto" weapon, maxInventory is now 30, a third more than "semiauto".

datablock ItemData(fullauto)
{
   // 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 = "art/shapes/weapons/SwarmGun/swarmgun.dts";
   mass = 5;
   elasticity = 0.2;
   friction = 0.6;
   emap = true;

   // Dynamic properties defined by the scripts
   pickUpName = "fullauto Rifle";
   description = "fullauto Rifle";
   image = fullautoImage;

   // weaponHUD
   previewImage = 'swarmer.png';
   reticle = 'reticle_rocketlauncher';
};

For the physical item that can be picked up by the player, we are using the "swarmgun" model and it's associated weaponHUD artwork. That way the 2 weapons look different from each other.

Finally in this "fullauto.cs" file, we have the ImageData which links it all together into a working gun.

datablock ShapeBaseImageData(fullautoImage)
{
   // Basic Item properties
   shapefile = "art/shapes/weapons/SwarmGun/swarmgun.dts";
   emap = true;

   // Specify mount point & offset for 3rd person, and eye offset
   // for first person rendering.
   mountPoint = 0;
   offset = "0.0 0.15 0.025";
   eyeOffset = "0.25 0.6 -0.4"; // 0.25=right/left 0.5=forward/backward, -0.5=up/down

   // 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 = fullauto;
   ammo = fullautoAmmo;
   projectile = fullautoProjectile;
   projectileType = Projectile;


   // Let there be light - NoLight, ConstantLight, PulsingLight, WeaponFireLight.
   lightType = "WeaponFireLight";
   lightColor = "1.0 1.0 0.9";
   lightDuration = 200;
   lightRadius = 10;

   // 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.

   // Initial start up state
   stateName[0] = "Preactivate";
   stateTransitionOnLoaded[0] = "Activate";
   stateTransitionOnNoAmmo[0] = "NoAmmo";

   // Activating the gun.
   // Called when the weapon is first mounted and there is ammo.
   stateName[1] = "Activate";
   stateTransitionOnTimeout[1] = "Ready";
   stateTimeoutValue[1] = 0.6;
   stateSequence[1] = "Activate";

   // Ready to fire, just waiting for the trigger
   stateName[2] = "Ready";
   stateTransitionOnNoAmmo[2] = "NoAmmo";
   stateTransitionOnTriggerDown[2]  = "Fire";
   stateSequence[2] = "Ready";

   // Fire the weapon. Calls the fire script which does the actual work.
   stateName[3] = "Fire";
   stateTransitionOnTimeout[3] = "PostFire";
   stateTimeoutValue[3] = 0.1;
   stateFire[3] = true;
   stateRecoil[3] = LightRecoil;
   stateAllowImageChange[3] = false;
   stateSequence[3] = "Fire";
   stateScript[3] = "onFire";
   stateSound[3] = fullautoFireSound;
   stateEmitter[3] = fullautofiring1Emitter;
   stateEmitterTime[3] = 0.1;

   // Check ammo
   stateName[4] = "PostFire";
   stateTransitionOnAmmo[4] = "Reload";
   stateTransitionOnNoAmmo[4] = "NoAmmo";

   // Play the reload animation, and transition into
   stateName[5] = "Reload";
   stateTransitionOnTimeout[5] = "Ready";
   stateTimeoutValue[5] = 0.01;
   stateAllowImageChange[5] = false;
   stateSequence[5] = "Reload";
   stateEjectShell[5] = false; // set to true to enable shell casing eject
 //  stateSound[5] = bulletReloadSound;

   // No ammo in the weapon, just idle until something shows up.
   // Play the dry fire sound if the trigger iS pulled.
   stateName[6] = "NoAmmo";
   stateTransitionOnAmmo[6] = "Reload";
   stateSequence[6] = "NoAmmo";
   stateTransitionOnTriggerDown[6] = "DryFire";

   // No ammo dry fire
   stateName[7] = "DryFire";
   stateTimeoutValue[7] = 1.0;
   stateTransitionOnTimeout[7] = "NoAmmo";
   stateSound[7] = bulletFireEmptySound;
};

Notice that this time there is no WaitForRelease on the trigger, this weapon is "fully automatic" and will continue to fire as long as the fire button is held down and there is available ammunition. Checking the timeouts in States "Fire" and "Reload" show that the rate of fire is much higher than in first custom weapon script we made.



Part Four: Sharing Weapon Audio and Effects