7 Replies - 393 Views - Last Post: 11 December 2012 - 09:44 AM Rate Topic: -----

#1 xDardas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-December 12

Inheritance VS single class - whats better?

Posted 10 December 2012 - 07:52 AM

Hello, I'm currently working on a game library [DLL] with XNA.
And well, I was wondering what is better/more efficient:
having a single class, for this purpose lets call it GameObject,
or multiple classes arranged like so:
Obj_Data
Obj_Painter (Inherits Obj_Data)
Obj_Updater (Inherits Obj_Data)
Obj_PaintUpdater (Inherits Obj_Data) and so on.
Obj_Char_Anim (Holds a name, index and a source rectangle)
Obj_Animation (Inherits Obj_PaintUpdater + contains a list of Obj_Char_Anim)

This is the code I'm using right now, if it helps in any way:
Obj_Data
Obj_Updater
Obj_Painter
Obj_PaintUpdater
So far, Obj_Char_Anim and Obj_Animation are only in the planning process, so I have nothing to show regarding that.

P.S. If my question wasn't quite clear enough, this is it again:
What is more efficient or rather, more accepted in the c# community:
A single class that does everything (The so called GameObject)
or, multiple classes that inherit from one another (like what I did with my code)?

Thank you in advance, ~Dardas.

Is This A Good Question/Topic? 0
  • +

Replies To: Inheritance VS single class - whats better?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,651
  • Joined: 02-June 10

Re: Inheritance VS single class - whats better?

Posted 10 December 2012 - 07:59 AM

Forgive how this sounds, but the fact that you're asking or even considering putting everything in one class shows you don't really understand about classes or how they can be used to make your life easier. I suspect you haven't used them much, meaning you are no where near ready to try to make a game library.

Have a read through the tutorial linked in my signature block for "Classes and objects". It should help bring the concept of classes more in focus.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,651
  • Joined: 02-June 10

Re: Inheritance VS single class - whats better?

Posted 10 December 2012 - 08:07 AM

Looking at code like this from your examples:

Quote

        /// <summary>
        /// Gets or sets the texture of the object.
        /// </summary>
        public Texture2D Texture
        {
            get
            {
                return this.texture;
            }
            set
            {
                this.texture = value;
            }
        }


I'm not sure you are familiar with properties.

If all youa re going to do is get and set a backing field, then you can make it an automatic property and do away with the backing field completely..

        /// <summary>
        /// Gets or sets the texture of the object.
        /// </summary>
        public Texture2D Texture
        {
           get; set;
        }


Looking through those code files I fail to see their purpose. They are just collections of properties, many of which are duplicate like 'color'.

Are you sure you're not just in a hurry to design something before you are familiar with the material? This feels a lot like a student trying to write and publish a novel after being in their journalism class for 2 weeks.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,651
  • Joined: 02-June 10

Re: Inheritance VS single class - whats better?

Posted 10 December 2012 - 08:12 AM

Based on the names of the classes they have no reason to inherit from one another. They simply don't exist in a hierarchy.

Look at it this way.

A piston is related to an engine, but it shouldn't inherit from an engine class. They just don't relate in that type of descending order of specialization way.

But a DodgeRam would inherit from a more generic Truck class. And a truck could inherit from an even more generic Vehicle.
Was This Post Helpful? 1
  • +
  • -

#5 xDardas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-December 12

Re: Inheritance VS single class - whats better?

Posted 10 December 2012 - 08:24 AM

tlhIn'toq, thanks for your answer, feels like I understand a little bit better how inheritance works, and when I should use it. Anyhow, I'll start by looking into the classes and objects tutorial, thanks once again.
Was This Post Helpful? 0
  • +
  • -

#6 xDardas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-December 12

Re: Inheritance VS single class - whats better?

Posted 10 December 2012 - 05:27 PM

Alright, well I've read some of your tutorials, and decided to re-try the code.
Heres my new version:
{link to unknown server removed}

I've also decided to add an interactable entity, that can collide with other entities, as a friend sugguested.
{link to unknown server removed}


Is this the correct way to do this?
P.S. If this still isn't correct, I guess I'll trully give up on this code, and read more tutorials before I try again..

Thanks in advance, once again ~Dardas.

This post has been edited by tlhIn`toq: 11 December 2012 - 08:50 AM

Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,651
  • Joined: 02-June 10

Re: Inheritance VS single class - whats better?

Posted 11 December 2012 - 08:52 AM

We do NOT go downloading entire projects from strange servers and do debugging for people.

Paste the relevant code in this thread. We want to keep the entire question together if only for posterity sake. Some one else may have a similar issue in 3 years but not be able to access the code you have on another site.
Was This Post Helpful? 0
  • +
  • -

#8 xDardas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-December 12

Re: Inheritance VS single class - whats better?

Posted 11 December 2012 - 09:44 AM

Right then, heres the code:
Entity:
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Goblin_GameLib
{
    /// <summary>
    /// Represents an object that can be drawn and updated.
    /// </summary>
    public class Entity
    {
        /// <summary>
        /// Gets or sets the texture of the entity.
        /// </summary>
        public Texture2D Texture { get; set; }

        /// <summary>
        /// Gets or sets the color at which the entity gets drawn.
        /// </summary>
        public Color Color { get; set; }

        /// <summary>
        /// Gets or sets the X axis coordinate of the entitys location.
        /// </summary>
        public Int32 X { get; set; }

        /// <summary>
        /// Gets or sets the Y axis coordinate of the entitys location.
        /// </summary>
        public Int32 Y { get; set; }

        /// <summary>
        /// Gets or sets the width of the entity when drawn.
        /// </summary>
        public Int32 Width { get; set; }

        /// <summary>
        /// Gets or sets the height of the entity when drawn.
        /// </summary>
        public Int32 Height { get; set; }

        /// <summary>
        /// Gets or sets the offsets x axis coordinate of the entity.
        /// </summary>
        public Int32 OffsetX { get; set; }

        /// <summary>
        /// Gets or sets the offsets y axis coordinate of the entity.
        /// </summary>
        public Int32 OffsetY { get; set; }

        /// <summary>
        /// Gets or sets the source rectangle used to draw the entity.
        /// </summary>
        public Rectangle? SourceRectangle { get; set; }

        /// <summary>
        /// Initializes a new entity instance.
        /// </summary>
        /// <param name="Texture">The texture to use to draw the entity.</param>
        /// <param name="Color">The color to use to draw the entity.</param>
        public Entity(Texture2D Texture, Color Color)
        {
            this.Texture = Texture;
            this.Color = Color;

            this.Width = this.Texture.Width;
            this.Height = this.Texture.Height;
        }

        /// <summary>
        /// Initializes a new entity instance.
        /// </summary>
        /// <param name="Texture">The texture to use to draw the entity.</param>
        /// <param name="Color">The color to use to draw the entity.</param>
        /// <param name="X">The x coordinate to move the entitys location to.</param>
        /// <param name="Y">The y coordinate to move the entitys location to.</param>
        public Entity(Texture2D Texture, Color Color, Int32 X, Int32 Y)
        {
            this.Texture = Texture;
            this.Color = Color;

            this.Width = this.Texture.Width;
            this.Height = this.Texture.Height;

            this.X = X;
            this.Y = Y;
        }

        /// <summary>
        /// A method that updates the entity.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of the game timing state.</param>
        public virtual void Update(GameTime gameTime)
        {
            //TODO: Insert update logic here.
            //Not much to update though..
        }

        /// <summary>
        /// A method that draws the entity.
        /// </summary>
        /// <param name="spriteBatch">The SpriteBatch instance to draw the entity with.</param>
        /// <param name="gameTime">Provides a snapshot of the game timing state.</param>
        public virtual void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            spriteBatch.Draw(this.Texture, this.GetDrawRectangle(), this.SourceRectangle, this.Color);
        }

        public virtual Rectangle GetDrawRectangle()
        {
            return new Rectangle(this.X - this.OffsetX, this.Y - this.OffsetY, this.Width, this.Height);
        }
    }
}



Interactable Entity:
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Goblin_GameLib
{
    /// <summary>
    /// Represents the collision type for an interactable entity.
    /// </summary>
    public enum CollisionType
    {
        RectangleCollision,
        PerPixelCollision,
    }

    /// <summary>
    /// Contains event data for interaction between 2 entities.
    /// </summary>
    public class InteractionEventArgs : EventArgs
    {
        /// <summary>
        /// Gets the entity that was interacted with.
        /// </summary>
        public Entity Interacted { get; private set; }

        /// <summary>
        /// Gets the collision type used to check the interaction.
        /// </summary>
        public CollisionType CollisionType { get; private set; }

        /// <summary>
        /// Initializes a new interaction event data instance.
        /// </summary>
        /// <param name="Interacted">The entity that was interacted with.</param>
        /// <param name="CollisionType">The collision type used to check the interaction.</param>
        public InteractionEventArgs(Entity Interacted, CollisionType CollisionType)
        {
            this.Interacted = Interacted;
            this.CollisionType = CollisionType;
        }
    }

    /// <summary>
    /// Represents an entity that can interact with other entities.
    /// </summary>
    public class InteractableEntity : Entity
    {
        //I know it shouldn't be public, still WIP.
        public event EventHandler<InteractionEventArgs> InteractionNotifier;

        /// <summary>
        /// Gets or sets the collision type of the entity.
        /// </summary>
        public CollisionType CollisionType { get; set; }

        /// <summary>
        /// Initializes a new entity.
        /// </summary>
        /// <param name="Texture">The texture to use to draw the entity.</param>
        /// <param name="Color">The color to use to draw the entity.</param>
        /// <param name="CollisionType">The collision type to check the entitys interaction with.</param>
        public InteractableEntity(Texture2D Texture, Color Color, CollisionType CollisionType)
            : base(Texture, Color)
        {
            this.CollisionType = CollisionType;
        }

        /// <summary>
        /// Initializes a new entity instance.
        /// </summary>
        /// <param name="Texture">The texture to use to draw the entity.</param>
        /// <param name="Color">The color to use to draw the entity.</param>
        /// <param name="X">The x coordinate to move the entitys location to.</param>
        /// <param name="Y">The y coordinate to move the entitys location to.</param>
        /// <param name="CollisionType">The collision type to check the entitys interaction with.</param>
        public InteractableEntity(Texture2D Texture, Color Color, Int32 X, Int32 Y, CollisionType CollisionType)
            : base(Texture, Color, X, Y)
        {
            this.CollisionType = CollisionType;
        }

        /// <summary>
        /// A method that checks if this entity collides with the given entity.
        /// </summary>
        /// <param name="Entity">The entity to check collision with.</param>
        /// <returns>True if the entities collide, false otherwise.</returns>
        public virtual bool Interacts(Entity Entity)
        {
            EventHandler<InteractionEventArgs> handler = this.InteractionNotifier;
            bool ReturnValue = false;

            Rectangle a = this.GetDrawRectangle();
            Rectangle b = Entity.GetDrawRectangle();

            if (this.CollisionType == CollisionType.RectangleCollision)
            {
                ReturnValue = a.Intersects(B)/>/>;
            }
            else if (this.CollisionType == CollisionType.PerPixelCollision)
            {
                Int32 Left = Math.Max(a.Left, b.Left);
                Int32 Right = Math.Max(a.Right, b.Right);
                Int32 Top = Math.Max(a.Top, b.Top);
                Int32 Bottom = Math.Max(a.Bottom, b.Bottom);
                Color[] c = new Color[1];
                Color[] d = new Color[1];

                for (int y = Top; y < Bottom && !ReturnValue; y++)
                {
                    for (int x = Left; x < Right && !ReturnValue; x++)
                    {
                        this.Texture.GetData<Color>(c, (x - a.Left) - (y - a.Top) * a.Width, 1);
                        this.Texture.GetData<Color>(d, (x - b.Left) - (y - b.Top) * b.Width, 1);

                        if (c[0].A != 0 && d[0].A != 0)
                            ReturnValue = true;
                    }
                }
            }

            if (ReturnValue && handler != null)
                handler.Invoke((object)this, new InteractionEventArgs(Entity, this.CollisionType));
            return ReturnValue;
        }
    }
}




Sorry about posting it in a website, seemed better at the time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1