#region File Description
//-----------------------------------------------------------------------------
// Weapon.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using Microsoft.Xna.Framework;
#endregion
namespace NetRumble
{
///
/// Base public class for all weapons that exist in the game.
///
abstract public class Weapon
{
#region Gameplay Data
///
/// The ship that owns this weapon.
///
protected Ship owner = null;
///
/// The amount of time remaining before this weapon can fire again.
///
protected float timeToNextFire = 0f;
///
/// The minimum amount of time between each firing of this weapon.
///
protected float fireDelay = 0f;
#endregion
#region Audio Data
///
/// The name of the sound effect played when this weapon fires.
///
protected string fireSoundEffect = String.Empty;
#endregion
#region Initialization Methods
///
/// Constructs a new weapon.
///
/// The ship that owns this weapon.
protected Weapon(Ship owner)
{
if (owner == null)
{
throw new ArgumentNullException("owner");
}
this.owner = owner;
}
#endregion
#region Updating Methods
///
/// Update the weapon.
///
/// The amount of elapsed time, in seconds.
public virtual void Update(float elapsedTime)
{
// count down to when the weapon can fire again
if (timeToNextFire > 0f)
{
timeToNextFire = MathHelper.Max(timeToNextFire - elapsedTime, 0f);
}
}
#endregion
#region Interaction Methods
///
/// Fire the weapon in the direction given.
///
/// The direction that the weapon is firing in.
public virtual void Fire(Vector2 direction)
{
// if we can't fire yet, then we're done
if (timeToNextFire > 0f)
{
return;
}
// the owner is no longer safe from damage
owner.Safe = false;
// set the timer
timeToNextFire = fireDelay;
// create and spawn the projectile
CreateProjectiles(direction);
// play the sound effect for firing
if (String.IsNullOrEmpty(fireSoundEffect) == false)
{
AudioManager.PlaySoundEffect(fireSoundEffect);
}
}
///
/// Create and spawn the projectile(s) from a firing from this weapon.
///
/// The direction that the projectile will move.
protected abstract void CreateProjectiles(Vector2 direction);
#endregion
}
}