/******************************************************************************/ /*! \file Object.cs \author Kevin Carey Represents a 3D game object Copyright © 2007 DigiPen(USA) Corporation. All rights reserved. */ /******************************************************************************/ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Content; using System; namespace Shooting_Range { // ObjectType enum enum ObjectType { TARGET, BULLET, RANGE, GUN }; // Object Class // Represents a 3D game object class Object : ICloneable, IComparable { // Constructors public Object(Model model, ObjectType type) { m_ObjectType = type; m_Model = model; m_Scale = new Vector3(1.0f, 1.0f, 1.0f); m_RotationXYZ = Vector3.Zero; m_Position = Vector3.Zero; m_PhysicsData = new Physics(Vector3.Zero, 0.0f); m_IsActive = false; } public Object(Model model, ObjectType type, Vector3 scale, Vector3 rotationYPR, Vector3 position) { m_ObjectType = type; m_Model = model; m_Scale = scale; m_RotationXYZ = rotationYPR; m_Position = position; m_PhysicsData = new Physics(Vector3.Zero, 0.0f); m_IsActive = false; } public Object(Model model, ObjectType type, Vector3 scale, Vector3 rotationYPR, Vector3 position, Vector3 velocityDirection) { m_ObjectType = type; m_Model = model; m_Scale = scale; m_RotationXYZ = rotationYPR; m_Position = position; m_PhysicsData = new Physics(velocityDirection); m_IsActive = false; } public Object(Model model, ObjectType type, Vector3 scale, Vector3 rotationYPR, Vector3 position, Vector3 velocityDirection, float gravity) { m_ObjectType = type; m_Model = model; m_Scale = scale; m_RotationXYZ = rotationYPR; m_Position = position; m_PhysicsData = new Physics(velocityDirection, gravity); m_IsActive = false; } public Object(Model model, ObjectType type, Vector3 scale, Vector3 rotationYPR, Vector3 position, Vector3 velocityDirection, float gravity, bool active) { m_ObjectType = type; m_Model = model; m_Scale = scale; m_RotationXYZ = rotationYPR; m_Position = position; m_PhysicsData = new Physics(velocityDirection, gravity); m_IsActive = active; } // ICloneable Clone function. I think that being forced to cast from the object type is a pain public object Clone() { Object clone = new Object(m_Model, m_ObjectType, m_Scale, m_RotationXYZ, m_Position); clone.IsActive = m_IsActive; clone.PhysicsData = m_PhysicsData.ClonePhysics(); return clone; } // So I fixed it with this function public Object CloneObject() { return (Object)Clone(); } // IComparable CompareTo function // Used for sorting public int CompareTo(object obj) { // If the object is the right type if (obj is Object) { // Convert to an Object Object rhs = (Object)obj; // Compare and return the depth value return rhs.PositionZ.CompareTo(m_Position.Z); } // If the object is the wrong type, throw an exception throw new Exception("Object CompareTo: Parameter is not of type Object"); } // Update function public void Update() { // If the object is active if (m_IsActive) { // Update it's downward velocity m_PhysicsData.VelocityDirectionY -= m_PhysicsData.Gravity; // Update it's position Move(m_PhysicsData.VelocityDirection); } } // Move function // Moves the objects position, as well as all the collision data positions public void Move(Vector3 delta) { // Update the position m_Position += delta; // For each piece of collision data for (int i = 0; i < m_PhysicsData.CollisionData.Count; ++i) { // Update the collision data's position m_PhysicsData.CollisionData[i].circle.Center += new Vector2(delta.X, delta.Y); } } // AddCollisionData function public void AddCollisionData(CirclePlane circlePlane) { // Add the CirclePlane to the physics m_PhysicsData.AddCollisionData(circlePlane); } // ObjectType get and set public ObjectType Type { get { return m_ObjectType; } set { m_ObjectType = value; } } // Model get public Model ModelData { get { return m_Model; } set { m_Model = value; } } // IsActive flag public bool IsActive { get { return m_IsActive; } set { m_IsActive = value; } } // Position get and set public Vector3 Position { get { return m_Position; } set { m_Position = value; } } // X component of the position get and set public float PositionX { get { return m_Position.X; } set { m_Position.X = value; } } // Y component of the position get and set public float PositionY { get { return m_Position.Y; } set { m_Position.Y = value; } } // Z component of the position get and set public float PositionZ { get { return m_Position.Z; } set { m_Position.Z = value; } } // Scale get and set public Vector3 Scale { get { return m_Scale; } set { m_Scale = value; } } // X component of the scale get and set public float ScaleX { get { return m_Scale.X; } set { m_Scale.X = value; } } // Y component of the scale get and set public float ScaleY { get { return m_Scale.Y; } set { m_Scale.Y = value; } } // Z component of the scale get and set public float ScaleZ { get { return m_Scale.Z; } set { m_Scale.Z = value; } } // Rotation get and set public Vector3 RotationXYZ { get { return m_RotationXYZ; } set { m_RotationXYZ = value; } } // Y component of the rotation get and set public float YawY { get { return m_RotationXYZ.Y; } set { m_RotationXYZ.Y = value; } } // X component of the rotation get and set public float PitchX { get { return m_RotationXYZ.X; } set { m_RotationXYZ.X = value; } } // Z component of the rotation get and set public float RollZ { get { return m_RotationXYZ.Z; } set { m_RotationXYZ.Z = value; } } // Physics get public Physics PhysicsData { get { return m_PhysicsData; } set { m_PhysicsData = value; } } // Object type private ObjectType m_ObjectType; // Model the object uses private Model m_Model; // Whether the object is currently in use or not private bool m_IsActive; // Position of the object (translation) private Vector3 m_Position; // Scale factors of the object private Vector3 m_Scale; // Rotation of the object private Vector3 m_RotationXYZ; // Physics data private Physics m_PhysicsData; } }