Game Development Reference
In-Depth Information
Next, we have to create the player's Weapon class that uses the ammunition discussed previously.
The Weapon class is derived from the Object3d class.
public class Weapon extends Object3d
The
MAX_DEFAULTAMMO
variable indicates the maximum number of projectiles that this weapon can
hold at one time.
private int MAX_DEFAULTAMMO = 20;
The
m_WeaponClip
variable array actually holds the weapon's ammunition.
private Ammunition[] m_WeaponClip = new Ammunition[MAX_DEFAULTAMMO];
The
m_TimeLastFired
variable holds the time this weapon was last fired in milliseconds.
private long m_TimeLastFired = 0;
The
m_TimeReadyToFire
variable is the time that this weapon will next be able to fire ammunition in
milliseconds.
private long m_TimeReadyToFire = 0;
The
m_FireDelay
variable is the minimum time in milliseconds between the firing of projectiles.
private long m_FireDelay = 500;
The Weapon constructor calls the Object3d's constructor. (See Listing 7-21.)
Listing 7-21. Weapon Constructor
Weapon(Context iContext, Mesh iMesh, MeshEx iMeshEx, Texture[] iTextures, Material iMaterial,
Shader iShader)
{
super(iContext, iMesh, iMeshEx, iTextures, iMaterial, iShader );
}
The
TurnOnOffSFX()
function turns on or off the sound effects associated with the weapon's
ammunition. (See Listing 7-22.)
Listing 7-22. Turning On/Off Ammunition Sound Effects
void TurnOnOffSFX(boolean value)
{
for (int i = 0; i < MAX_DEFAULTAMMO; i++)
{
m_WeaponClip[i].SetSFXOnOff(value);
}
}