I'm currently trying to learn the ins and outs of XNA/C# but I'm currently stuck on a problem: I want to add optional functionality to a class and I have no idea how to even start. For clarity, here is my current code:
public class Actor
{
//Fields
private Vector3 m_location;
//Properties
public Vector3 Location
{
get { return m_location; }
set { m_location = value; }
}
//Constructors
public Actor(float x, float y, float z)
{
Location = new Vector3(x, y, z);
}
public Actor(Vector3 location)
{
Location = location;
}
}
public class Actor2D : Actor
{
//Fields
private Texture2D m_texture;
//Properties
public Texture2D Texture
{
get { return m_texture; }
set { m_texture = value; }
}
//Constructors
public Actor2D(float x, float y, float z) : base(x, y, z) {}
public Actor2D(Vector3 vector) : base(vector) {}
//Methods
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, new Vector2(Location.X, Location.Y), Color.White);
}
public void Load(ContentManager content, string asset)
{
Texture = content.Load<Texture2D>(asset);
}
}
The logic here is that all Actors have to have a location but they don't necessarily have to have a texture as an Actor could be a light source, trigger, etc etc. Actor2D is similar in this except that it's designated as an Actor that uses 2D texture. What I want to do is have the ability to add functionality to either of these classes on an as needed basis. Say I want the Actor2D to be animated or as mentioned earlier I want to add a trigger or light source to an untextured Actor... What are some ideas that would help me accomplish these goals?
You could try taking a look at the decorator pattern, which allows you to 'tack on' behaviour to an object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With