[C#] Creating new instances using Polymorphism

Started by
11 comments, last by ChaosEngine 14 years, 10 months ago
Quote:

However if every type of bullet must be a sub-class then use abstract.



Thanks for your help. Just wanted to clarify one thing.

Here is the class, as it stands at the moment:

    public class Bullet : IQuad, IOcTree    {        public int damage;              // How much damage the weapon inflicts (Multiply it by a factor for different parts of the body [E.G. HeadShot = damage * 3])              public CD_Ray ray;              // The path the ammunition will follow (used for collision)        public float speed;        // IQuad related parameters        string IQuad.Effect { get { return effect_Type; } }        float IQuad.Height { get { return height; } }        int IQuad.Index_Indices { get { return index_Indices; } }        int IQuad.Index_Vertices { get { return index_Vertices; } }        ushort IQuad.Texture { get { return texture; } }        float IQuad.Width { get { return width; } }        public string effect_Type;        public float height;        public int index_Indices;        public int index_Vertices;        public ushort texture;        public float width;        // IOcTree related parameters        CD_AxisAlignedBoundingBox? IOcTree.Collision_AABB { get { return collision_AABB; } }        CD_BoundingSphere? IOcTree.Collision_BoundingSphere { get { return collision_BoundingSphere; } }        bool IOcTree.IsAlive { get { return isAlive; } }        bool IOcTree.IsOcTreeUpdatable { get { return isOcTreeUpdatable; } }        Vector3 IOcTree.Position_Current { get { return position_Current; } }        Vector3 IOcTree.Position_Last { get { return position_Last; } }        public CD_AxisAlignedBoundingBox? collision_AABB;        public CD_BoundingSphere? collision_BoundingSphere = null;        public bool isAlive;        public bool isOcTreeUpdatable;        public Vector3 position_Current;        public Vector3 position_Last;        public virtual void Spawn(Vector3 position_FiredFrom, Vector3 direction_FiredTo)        {        }        public virtual void Update()        {        }    }    class PistolBulletNormal : Bullet    {        private QuadManager quad_Manager;        public PistolBulletNormal(QuadManager quad_Manager)        {            this.damage = 10;                     this.quad_Manager = quad_Manager;            this.ray = new CD_Ray(Vector3.Zero, Vector3.Zero);            this.speed = 1.0f;            // IQuad related parameters            this.effect_Type = "effect_Billboarding";            this.height = 2.0f;            this.texture = 0;            this.width = 5.0f;        }        public override void Spawn(Vector3 position_FiredFrom, Vector3 direction_FiredTo)        {            // Update Ray            this.ray.Origin = position_FiredFrom;            this.ray.Direction = direction_FiredTo;            this.isAlive = true;            this.isOcTreeUpdatable = true;            this.position_Current = position_FiredFrom;            this.position_Last = this.position_Current;            this.collision_AABB = new CD_AxisAlignedBoundingBox(this.position_Current, this.width, this.height, this.width);            this.index_Vertices = this.quad_Manager.SpawnQuad(this.position_Current, this);            this.index_Indices = (this.index_Vertices / GameConstants.QUAD_NUMBER_OF_VERTICES) * 6;        }        public override void Update()        {            this.UpdatePosition();        }        public void UpdatePosition()        {            this.isOcTreeUpdatable = true;            this.position_Current += this.ray.Direction * this.speed;            this.position_Last = this.position_Current;            this.quad_Manager.UpdateQuad(this.position_Current, this);        }    }


I will have a Bullet without referring to PistolBulletNormal() or future sub types of Bullets. So will this class work with the abstract method?
Advertisement
If you need a bullet or subBullet to be added to a list when you create it. Just pass the list reference to the constructor and have it add itself.

theTroll
Quote:Original post by Spa8nky

I will have a Bullet without referring to PistolBulletNormal() or future sub types of Bullets. So will this class work with the abstract method?


It should. Base classes rarely refer to derived classes.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

This topic is closed to new replies.

Advertisement