Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object Oriented Programming correct use interface

Tags:

c#

oop

I'm trying to design a few classes for a game, but I'm not sure I'm doing it right.

I have two classes: Actor and Building. These have a few subclasses: Policeman, Fireman and PoliceStation, FireStation.

I want to be able to put all these items together in a list to iterate through later, so I've added a base class: GameEntity.
So what I have is this:

public abstract class GameEntity: 
{
    public GameEntity()
    {

    }       
}

public class Actor: GameEntity
{
    public int _speed;
    public TileSprite _UI;

    public Actor()
    {

    }

    public bool CollidesWith(Vector2 pos)
    {
        //Do stuff here
    }

    public virtual void OnClick()
    {
        //Do stuff here
    }

    public void DoActing()
    {

    }
}

public class Policeman: Actor
{
    public Policeman()
    {
        _speed = 10;
    }

    public override void OnClick()
    {
        //Do stuff
    }
}

public class Building: GameEntity
{
    public TileSprite _UI;

    public Building()
    {

    }

    public bool CollidesWith(Vector2 pos)
    {
        //Do stuff here
    }

    public virtual void OnClick()
    {
        //Do stuff here
    }

    public void DoBuilding()
    {

    }
}

public class PoliceStation: Building
{
    public PoliceStation()
    {

    }

    public override void OnClick()
    {
        //Do stuff
    }
}

Now, I want to be able to do this:

List<GameEntity> Entities = new List<GameEntity>();

Actor a1 = new PoliceMan();
Building b1 = new PoliceStation

Entities.Add(a1);
Entities.Add(b1);

foreach(GameEntity ent in Entities)
{
    if (ent.CollidesWith(something))
    {
        ent.OnClick();

        //If Actor then do 
        ent.DoActing();

        //If Building then do
        ent.DoBuilding();
    }
}

Now, in order to do the last bit, would it be best if I implement an interface that contains the OnClick and CollidesWith, or can I do it with inheritance? If so, how would I do this?

Cheers.

like image 907
CJ Scholten Avatar asked Jun 25 '26 20:06

CJ Scholten


2 Answers

This is only to give you the idea, I guess that's what you need.

 public interface IGameEntity
    {
        bool CollidesWith();
        void OnClick();
        void DoActing();
    }
  public class Actor : IGameEntity { //Interface implemented }
  public class Building: IGameEntity { //Interface implemented }
  public class Policeman: IGameEntity { //Interface implemented }
  public class Fireman: IGameEntity { //Interface implemented }
  public class FireStation: IGameEntity { //Interface implemented }

In your client object just do something like this:

List<IGameEntity> entities = new List<IGameEntity>()
{
    new Actor(), 
    new Building(), 
    new Policeman(), 
    new Fireman(), 
    new Fireman()
};
foreach (IGameEntity entity in entities)
{
    entity.CollidesWith();
    entity.OnClick();
    entity.DoActing();
}
like image 127
Wilson Avatar answered Jun 27 '26 10:06

Wilson


Number of ways to go here. If all GameEntities can be clicked on then you could add an OnClick event property to the base class.

If Not then I'd have an Interface IClick and then implement that on clickable entities.

Collision, depends. Building don't move about so they can't collide with anything, but they can be collided with and so for instance you might want do a damage rountine, which then suggests a destroyed, routine a repair routine maybe.

There's no right answer, but Three code smells would be a A Base class with nothing in it.

A very deep inheritance hierarchy (more than two levels is cause for suspicion in my book) So if you start seeing GameEntity -> BuildingEntity -> ActiveBuildingEntity -> RepairableBuildingEntity, you are in a mess that's about to get messier.

And above all a base class with do nothing methods in it that only exist because you need to add behaviour (override) in some descendants.

Don't be scared about having a few interfaces. IClick, ICollision, IDamage, IRepair etc. It's way better than implying a building can crash into a car.

like image 28
Tony Hopkinson Avatar answered Jun 27 '26 09:06

Tony Hopkinson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!