using System;
using System.Collections;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace LD19
{
public class PrimitiveLine
{
Texture2D pixel;
ArrayList vectors;
///
/// Gets/sets the colour of the primitive line object.
///
public Color Colour;
///
/// Gets/sets the position of the primitive line object.
///
public Vector2 Position;
///
/// Gets/sets the render depth of the primitive line object (0 = front, 1 = back)
///
public float Depth;
///
/// Gets the number of vectors which make up the primtive line object.
///
public int CountVectors
{
get
{
return vectors.Count;
}
}
///
/// Creates a new primitive line object.
///
/// The Graphics Device object to use.
public PrimitiveLine(GraphicsDevice graphicsDevice)
{
// create pixels
pixel = new Texture2D(graphicsDevice, 1, 1, true, SurfaceFormat.Color);
Color[] pixels = new Color[1];
pixels[0] = Color.White;
pixel.SetData(pixels);
Colour = Color.White;
Position = new Vector2(0, 0);
Depth = 0;
vectors = new ArrayList();
}
///
/// Called when the primive line object is destroyed.
///
~PrimitiveLine()
{
}
///
/// Adds a vector to the primive live object.
///
/// The vector to add.
public void AddVector(Vector2 vector)
{
vectors.Add(vector);
}
///
/// Insers a vector into the primitive line object.
///
/// The index to insert it at.
/// The vector to insert.
public void InsertVector(int index, Vector2 vector)
{
vectors.Insert(index, vectors);
}
///
/// Removes a vector from the primitive line object.
///
/// The vector to remove.
public void RemoveVector(Vector2 vector)
{
vectors.Remove(vector);
}
///
/// Removes a vector from the primitive line object.
///
/// The index of the vector to remove.
public void RemoveVector(int index)
{
vectors.RemoveAt(index);
}
///
/// Clears all vectors from the primitive line object.
///
public void ClearVectors()
{
vectors.Clear();
}
///
/// Renders the primtive line object.
///
/// The sprite batch to use to render the primitive line object.
public void Render(SpriteBatch spriteBatch)
{
if (vectors.Count < 2)
return;
for (int i = 1; i < vectors.Count; i++)
{
Vector2 vector1 = (Vector2)vectors[i - 1];
Vector2 vector2 = (Vector2)vectors[i];
// calculate the distance between the two vectors
float distance = Vector2.Distance(vector1, vector2);
// calculate the angle between the two vectors
float angle = (float)Math.Atan2((double)(vector2.Y - vector1.Y),
(double)(vector2.X - vector1.X));
// stretch the pixel between the two vectors
spriteBatch.Draw(pixel,
Position + vector1,
null,
Colour,
angle,
Vector2.Zero,
new Vector2(distance, 1),
SpriteEffects.None,
Depth);
}
}
///
/// Creates a circle starting from 0, 0.
///
/// The radius (half the width) of the circle.
/// The number of sides on the circle (the more the detailed).
public void CreateCircle(float radius, int sides)
{
vectors.Clear();
float max = 2 * (float)Math.PI;
float step = max / (float)sides;
for (float theta = 0; theta < max; theta += step)
{
vectors.Add(new Vector2(radius * (float)Math.Cos((double)theta),
radius * (float)Math.Sin((double)theta)));
}
// then add the first vector again so it's a complete loop
vectors.Add(new Vector2(radius * (float)Math.Cos(0),
radius * (float)Math.Sin(0)));
}
}
}