//-----------------------------------------------------------------------------
// Torque Game Engine 
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Machinegun weapon. This file contains all the items related to this weapon
// including explosions, ammo, the item and the weapon item image.
// These objects rely on the item & inventory support system defined
// in item.cs and inventory.cs
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Weapon fire emitter

datablock AudioProfile(grenadeReloadSound)
{
filename = "~/data/shapes/XM8/machinegun_sound_reload.wav";
description = AudioClose3d;
preload = true;
};

datablock AudioProfile(grenadeFireSound)
{
filename = "~/data/shapes/XM8/machinegun_sound_fire.wav";
description = AudioClose3d;
preload = true;
};

datablock AudioProfile(grenadeFireEmptySound)
{
filename = "~/data/shapes/XM8/machinegun_sound_empty.wav";
description = AudioClose3d;
preload = true;
};

datablock AudioProfile(grenadeExplosionSound)
{
filename = "~/data/shapes/XM8/explosion.wav";
description = AudioClose3d;
preload = true;
};

datablock ParticleData(grenadeFireParticle) {
   dragCoefficient = "0";
   windCoefficient = "0";
   gravityCoefficient = "-0.20";
   inheritedVelFactor = "0";
   constantAcceleration = "0";
   lifetimeMS = "231";
   lifetimeVarianceMS = "0";
   spinSpeed = "0";
   spinRandomMin = "-30";
   spinRandomMax = "0.5";
   useInvAlpha = "0";
   animateTexture = "0";
   framesPerSec = "1";
   textureName = "~/data/shapes/particles/smoke";
   animTexName[0] = "~/data/shapes/particles/smoke";
   colors[0] = "0.0 0.0 0.0 1.000000";
   colors[1] = "1.000000 1.000000 1.000000 0.096063";
   colors[2] = "0.000000 0.000000 0.000000 0.000000";
   colors[3] = "1.000000 1.000000 1.000000 1.000000";
   sizes[0] = "0.1";
   sizes[1] = "0.1";
   sizes[2] = "0.2";
   sizes[3] = "0.1";
   times[0] = "0";
   times[1] = "0.3";
   times[2] = "1";
   times[3] = "1";
   sgAllowLighting = "0";
};

datablock ParticleEmitterData(grenadeFireEmitter) {
   className = "ParticleEmitterData";
   ejectionPeriodMS = "60";
   periodVarianceMS = "0";
   ejectionVelocity = "2";
   velocityVariance = "0";
   ejectionOffset = "0";
   thetaMin = "0";
   thetaMax = "50.4";
   phiReferenceVel = "0";
   phiVariance = "360";
   overrideAdvance = "0";
   orientParticles = "0";
   orientOnVelocity = "1";
   particles = "grenadeFireParticle";
   lifetimeMS = "0";
   lifetimeVarianceMS = "0";
   useEmitterSizes = "0";
   useEmitterColors = "0";
};

//-----------------------------------------------------------------------------
// Projectile Explosion

datablock ParticleData(grenadeExplosionParticle)
{
   dragCoefficient      = 2;
   gravityCoefficient   = 0.2;
   inheritedVelFactor   = 0.2;
   constantAcceleration = 0.0;
   lifetimeMS           = 800;
   lifetimeVarianceMS   = 150;
   textureName          = "~/data/shapes/particles/smoke";
   colors[0]     = "1 1 1 0.5";
   colors[1]     = "1 1 1 0.0";
   sizes[0]      = 0.2;
   sizes[1]      = 0.4;
};

datablock ParticleEmitterData(grenadeExplosionEmitter)
{
   ejectionPeriodMS = 7;
   periodVarianceMS = 0;
   ejectionVelocity = 1;
   velocityVariance = 1.0;
   ejectionOffset   = 0.0;
   thetaMin         = 0;
   thetaMax         = 60;
   phiReferenceVel  = 0;
   phiVariance      = 360;
   overrideAdvance = false;
   particles = "grenadeExplosionParticle";
};

datablock ExplosionData(grenadeExplosion)
{
   soundProfile = grenadeExplosionSound;
   lifeTimeMS = 800;

   particleEmitter = grenadeExplosionEmitter;
   particleDensity = 1;
   particleRadius = 0.2;

   faceViewer     = true;
   explosionScale = "1 1 1";

   shakeCamera = false;
   camShakeFreq = "10.0 11.0 10.0";
   camShakeAmp = "1.0 1.0 1.0";
   camShakeDuration = 0.5;
   camShakeRadius = 10.0;

};


//-----------------------------------------------------------------------------
// Projectile Object

datablock ProjectileData(grenadeProjectile)
{
   projectileShapeName = "~/data/shapes/cratea/cratea4.dts";
   directDamage        = 90;
   radiusDamage        = 90;
   damageRadius        = 4.0;
   explosion           = grenadeExplosion;

   muzzleVelocity      = 30;
   velInheritFactor    = 1;

   armingDelay         = 4000;
   lifetime            = 10000;
   fadeDelay           = 1300;
   bounceElasticity    = 20;
   bounceFriction      = 0;
   isBallistic         = true;
   gravityMod = 0.5;

   hasLight    = false;
   lightRadius = 3;
   lightColor  = "0.5 0.5 0.25";

};

function grenadeProjectile::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,"grenadeBullet");

}


//-----------------------------------------------------------------------------
// Ammo Item

datablock ItemData(grenadeAmmo)
{
   // 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/cratea/cratea4.dts";
   mass = 1;
   elasticity = 0.2;
   friction = 0.6;

	// Dynamic properties defined by the scripts
	pickUpName = "a frag grenade";
   maxInventory = 1;
};


//image which does all the work.  Images do not normally exist in
// the world, they can only be mounted on ShapeBase objects.

datablock ShapeBaseImageData(grenadeImage)
{
   // Basic Item properties
   shapeFile = "~/data/shapes/cratea/cratea4.dts";
   emap = true;

   // Specify mount point & offset for 3rd person, and eye offset
   // for first person rendering.
   mountPoint = 0;
   offset = "0 0 0";
   eyeOffset = "0.14 0.24 -0.25";

   // 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 = grenadeAmmo;
   ammo = grenadeAmmo;
   projectile = grenadeProjectile;
   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.

   // 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";
   stateScript[1]                   = "setHud";
   stateTimeoutValue[1]             = 0.0;
   
   // Ready to fire, just waiting for the trigger
   stateName[2]                     = "Ready";
   stateTransitionOnNoAmmo[2]       = "NoAmmo";
   stateTransitionOnTriggerDown[2]  = "Fire";

   // Fire the weapon. Calls the fire script which does 
   // the actual work.  Since we're a machinegun, skip the
   // reload animation...
   stateName[3]                     = "Fire";
   stateTransitionOnTimeout[3]      = "ReFire";
   stateTimeoutValue[3]             = 0.1;
   stateFire[3]                     = true;
   stateRecoil[3]                   = LightRecoil;
   stateAllowImageChange[3]         = false;
   stateSequence[3]                 = "Fire";
   stateScript[3]                   = "onFire";
   stateEmitter[3]                  = grenadeFireEmitter;
   stateEmitterTime[3]              = 0.1;
   stateEjectShell[3]               = true;
   stateSound[3]                    = grenadeFireSound ;
   stateTransitionOnTriggerUp[3]    = "ShellDrop";

   // Reset the trigger by playing the activate sequence, then
   // if trigger is still down, loop back to firing.  This makes
   // the weapon fire continuously, like a machinegun
   stateName[4]                     = "ReFire";
   stateTransitionOnNoAmmo[4]       = "NoAmmo";
   stateTransitionOnTriggerDown[4]  = "Fire";
   stateScript[4]                   = "setHud";
   stateTransitionOnTimeout[4]      = "Activate";
   stateTimeoutValue[4]             = 0.1;
   stateAllowImageChange[4]         = false;

   // No ammo in the weapon, just idle until something
   // shows up. Play the dry fire sound if the trigger is
   // pulled.
   stateName[5]                     = "NoAmmo";
   stateTransitionOnAmmo[5]         = "Reload";
   stateSequence[5]                 = "NoAmmo";
   stateTransitionOnTriggerDown[5]  = "DryFire";

   // No ammo dry fire
   stateName[6]                     = "DryFire";
   stateTimeoutValue[6]             = 1.0;
   stateTransitionOnTimeout[6]      = "NoAmmo";
   stateSound[6]                    = grenadeFireEmptySound;

};


//-----------------------------------------------------------------------------


function grenadeImage::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);
   %currentAmmo = %obj.getInventory(%this.ammo);

   %obj.client.setGrenadeHud(%currentAmmo);

   // 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;
}

function grenadeImage::setHud(%this, %obj, %slot)

{ 

   %currentAmmo = %obj.getInventory(%this.ammo);

   %obj.client.SetGrenadeHud(%currentAmmo);

}
