#region File Description
//-----------------------------------------------------------------------------
// PowerUp.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
#endregion
namespace NetRumble
{
///
/// Base public class for all power-ups that exist in the game.
///
abstract public class PowerUp : GameplayObject
{
#region Constant Data
///
/// The speed of the rotation of the power-up, in radians/sec.
///
const float rotationSpeed = 2f;
///
/// The amplitude of the pulse
///
const float pulseAmplitude = 0.1f;
///
/// The rate of the pulse.
///
const float pulseRate = 0.1f;
public const float PowerUpRadius = 20f;
#endregion
#region Graphics Data
///
/// The time accumulator for the power-up pulse.
///
private float pulseTime = 0f;
#endregion
#region Initialization Methods
///
/// Constructs a new power-up.
///
protected PowerUp()
: base()
{
// set the collision data
this.radius = PowerUpRadius;
this.mass = Int32.MaxValue;
}
///
/// Initialize the power-up to it's default gameplay states.
///
public override void Initialize()
{
if (!active)
{
// play the spawn sound effect
AudioManager.PlaySoundEffect("powerup_spawn");
}
base.Initialize();
}
#endregion
#region Drawing Methods
///
/// Draw the triple-laser power-up.
///
/// The amount of elapsed time, in seconds.
/// The SpriteBatch object used to draw.
public abstract void Draw(float elapsedTime, SpriteBatch spriteBatch);
///
/// Draw the power-up.
///
/// The amount of elapsed time, in seconds.
/// The SpriteBatch object used to draw.
/// The texture used to draw this object.
/// The source rectangle in the texture.
/// The color of the sprite, ignored here.
public override void Draw(float elapsedTime, SpriteBatch spriteBatch,
Texture2D sprite, Rectangle? sourceRectangle, Color color)
{
// update the rotation
rotation += rotationSpeed * elapsedTime;
// adjust the radius to affect the scale
float oldRadius = radius;
pulseTime += elapsedTime;
radius *= 1f + pulseAmplitude * (float)Math.Sin(pulseTime / pulseRate);
base.Draw(elapsedTime, spriteBatch, sprite, sourceRectangle, color);
radius = oldRadius;
}
#endregion
#region Interaction Methods
///
/// Defines the interaction between this power-up and a target GameplayObject
/// when they touch.
///
/// The GameplayObject that is touching this one.
/// True if the objects meaningfully interacted.
public override bool Touch(GameplayObject target)
{
// if it touched a ship, then create a particle system and play a sound
Ship ship = target as Ship;
if (ship != null)
{
// play the "power-up picked up" sound effect
AudioManager.PlaySoundEffect("powerup_touch");
// kill the power-up
Die(target, false);
// the ship keeps going as if it didn't hit anything
return false;
}
return base.Touch(target);
}
#endregion
}
}