#region File Description //----------------------------------------------------------------------------- // GameplayObject.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 { /// /// A base public class for all gameplay objects. /// abstract public class GameplayObject { #region Status Data /// /// If true, the object is active in the world. /// protected bool active = false; public bool Active { get { return active; } } #endregion #region Graphics Data protected Vector2 position = Vector2.Zero; public Vector2 Position { get { return position; } set { position = value; } } protected Vector2 velocity = Vector2.Zero; public Vector2 Velocity { get { return velocity; } set { if ((value.X == Single.NaN) || (value.Y == Single.NaN)) { throw new ArgumentException("Velocity was NaN"); } velocity = value; } } protected float rotation = 0f; public float Rotation { get { return rotation; } set { rotation = value; } } #endregion #region Collision Data protected float radius = 1f; public float Radius { get { return radius; } set { radius = value; } } protected float mass = 1f; public float Mass { get { return mass; } } protected bool collidedThisFrame = false; public bool CollidedThisFrame { get { return collidedThisFrame; } set { collidedThisFrame = value; } } #endregion #region Initialization Methods /// /// Constructs a new gameplay object. /// protected GameplayObject() { } /// /// Initialize the object to it's default gameplay states. /// public virtual void Initialize() { if (!active) { active = true; CollisionManager.Collection.Add(this); } } #endregion #region Updating Methods /// /// Update the gameplay object. /// /// The amount of elapsed time, in seconds. public virtual void Update(float elapsedTime) { collidedThisFrame = false; } #endregion #region Drawing Methods /// /// Draw the gameplay object. /// /// The amount of elapsed time, in seconds. /// The SpriteBatch object used to draw. /// The texture used to draw this object. /// The source rectangle. /// The color of the sprite. public virtual void Draw(float elapsedTime, SpriteBatch spriteBatch, Texture2D sprite, Rectangle? sourceRectangle, Color color) { if ((spriteBatch != null) && (sprite != null)) { spriteBatch.Draw(sprite, position, sourceRectangle, color, rotation, new Vector2(sprite.Width / 2f, sprite.Height / 2f), 2f * radius / MathHelper.Min(sprite.Width, sprite.Height), SpriteEffects.None, 0f); } } #endregion #region Interaction Methods /// /// Defines the interaction between this GameplayObject and /// a target GameplayObject when they touch. /// /// The GameplayObject that is touching this one. /// True if the objects meaningfully interacted. public virtual bool Touch(GameplayObject target) { return true; } /// /// Damage this object by the amount provided. /// /// /// This function is provided in lieu of a Life mutation property to allow /// classes of objects to restrict which kinds of objects may damage them, /// and under what circumstances they may be damaged. /// /// The GameplayObject responsible for the damage. /// The amount of damage. /// If true, this object was damaged. public virtual bool Damage(GameplayObject source, float damageAmount) { return false; } /// /// Kills this object, in response to the given GameplayObject. /// /// The GameplayObject responsible for the kill. /// /// If true, the object dies without any further effects. /// public virtual void Die(GameplayObject source, bool cleanupOnly) { // deactivate the object if (active) { active = false; CollisionManager.Collection.QueuePendingRemoval(this); } } #endregion } }