#region File Description
//-----------------------------------------------------------------------------
// Marble.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 Marblets
{
///
/// Marble represents a single marble
///
public class Marble
{
///
/// Width of a marble in HD resolution screen coordinates
///
public const int Width = 45;
///
/// Height of a marble in HD resolution screen coordinates
///
public const int Height = 45;
///
/// How long it takes a marble to break
///
public const double BreakTime = 0.5;
///
/// Animation fields
///
private const int breakFrameCount = 12;
private static float pulseFactor;
private double animationStart;
private int frame;
private Animation animation = Animation.None;
///
/// Initialize the random number generator
///
private static Random random = new Random();
///
/// Marble textures
///
private static Texture2D breakTexture;
private static Texture2D glowRing1Texture;
private static Texture2D glowRing2Texture;
private static Texture2D marble;
private Color color;
///
/// Marble position
///
private Vector2 position2D;
///
/// Current state of the animation
///
public Animation Animation
{
get
{
return animation;
}
}
///
/// Color of the marble
///
public Color Color
{
get
{
return color;
}
}
///
/// Is this marble currently selected
///
public bool Selected;
///
/// The HD screen based coordinates of the marble
///
public Vector2 Position
{
get
{
return position2D;
}
set
{
position2D = value;
}
}
///
/// Constructs a new marble
///
public Marble()
{
//Set a random color
color = MarbletsGame.Settings.MarbleColors[
random.Next(MarbletsGame.Settings.MarbleColors.GetLength(0))];
}
///
/// Initializes the marble
///
/// The game object
public static void Initialize()
{
}
///
/// Updates the animation for marbles
///
/// Total game time
public void Update(GameTime time)
{
if(time != null)
{
if(Animation == Animation.Breaking)
{
if((time.TotalGameTime.TotalSeconds - animationStart) > BreakTime)
{
animation = Animation.Gone;
frame = breakFrameCount;
}
else
{
frame = (int)((time.TotalGameTime.TotalSeconds - animationStart)
/ BreakTime * breakFrameCount);
}
}
}
}
///
/// Updates the static parts of the class - only needs to be done once rather
/// than for 144 marbles each frame
///
/// Total game time
public static void UpdateStatic(GameTime time)
{
if(time != null)
pulseFactor = (float)Math.Sin(time.TotalGameTime.TotalSeconds * 6.0);
}
///
/// Draws the specified marble
///
/// The sprite batch to use
public void Draw(RelativeSpriteBatch spriteBatch)
{
Draw2DMarble(spriteBatch);
}
private void Draw2DMarble(RelativeSpriteBatch spriteBatch)
{
//Draw the 2d marble
if(Animation == Animation.Breaking || Animation == Animation.Gone)
{
//Draw the correct frame.
spriteBatch.Draw(breakTexture, Position, new Rectangle(frame * Width, 0,
Width, Height), Color);
}
else
{
spriteBatch.Draw(marble, Position, Color);
//Highlight selected marbles
if(Selected)
{
if(pulseFactor < 0.0)
{
//Make a new color with the correct transparency.
Color fade = Color.White * -pulseFactor;
//pulse the single ring
spriteBatch.Draw(glowRing1Texture, Position, fade);
}
else
{
//pulse the double ring
//Make a new color with the correct transparency.
Color fade = Color.White * pulseFactor;
spriteBatch.Draw(glowRing2Texture, Position, fade);
}
}
}
}
///
/// Signal a marble to start breaking
///
/// Start time for the animation
public void Break(GameTime time)
{
if(time != null)
{
animationStart = time.TotalGameTime.TotalSeconds;
animation = Animation.Breaking;
}
}
///
/// Load graphics content.
///
public static void LoadContent()
{
breakTexture =
MarbletsGame.Content.Load("Textures/marble_burst");
glowRing1Texture =
MarbletsGame.Content.Load("Textures/marble_glow_1ring");
glowRing2Texture =
MarbletsGame.Content.Load("Textures/marble_glow_2rings");
marble = MarbletsGame.Content.Load("Textures/marble");
}
}
}