Skip to content

Instantly share code, notes, and snippets.

@eriksk
Created August 7, 2014 05:56
Show Gist options
  • Save eriksk/9f6ac9f9d54810ce4758 to your computer and use it in GitHub Desktop.
Save eriksk/9f6ac9f9d54810ce4758 to your computer and use it in GitHub Desktop.
Generic GameObject for MonoGame
public class GameObject
{
private Vector2 originInPixels;
public Vector2 position, scale, origin;
public Color color;
public Texture2D texture;
public Rectangle source;
public SpriteEffects flip;
public float rotation;
public GameObject()
:this(null)
{
}
public GameObject(Texture2D texture)
{
this.texture = texture;
color = Color.White;
position = new Vector2();
scale = new Vector2(1f, 1f);
origin = new Vector2(0.5f, 0.5f);
rotation = 0f;
source = texture == null ? new Rectangle() : new Rectangle(0, 0, texture.Width, texture.Height);
flip = SpriteEffects.None;
originInPixels = new Vector2();
}
public virtual GameObject SetPosition(float x, float y)
{
position.X = x;
position.Y = y;
return this;
}
public virtual GameObject SetScale(float scalar)
{
scale.X = scalar;
scale.Y = scalar;
return this;
}
public virtual GameObject SetScale(float x, float y)
{
scale.X = x;
scale.Y = y;
return this;
}
public virtual GameObject SetRotation(float rotation)
{
this.rotation = rotation;
return this;
}
public virtual GameObject SetSource(int x, int y, int width, int height)
{
source.X = x;
source.Y = y;
source.Width = width;
source.Height = height;
return this;
}
public virtual GameObject SetFlip(SpriteEffects flip)
{
this.flip = flip;
return this;
}
public virtual GameObject SetOrigin(float x, float y)
{
origin.X = x;
origin.Y = y;
return this;
}
public virtual void Update(float dt)
{
}
public virtual void Draw(SpriteBatch spriteBatch)
{
originInPixels.X = source.Width * origin.X;
originInPixels.Y = source.Height * origin.Y;
spriteBatch.Draw(texture, position, source, color, rotation, originInPixels, scale, flip, 0f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment