Inheritance Headache

I was going through my game setup, and I started changing more things to be better down the road. For example, when an “item” was picked up/ran into/collided, I wanted to run a onCollision() method. The problem that occured throught this is I required certain interfaces to have methods that didn’t really make sense.

So the question is pretty simple I guess. I know inheritance can be set up different ways, but I’m running into a problem with “creatures” and when to use an interface. I guess, just can someone give me some advice on how to make this system better.

Current Setup

Tile Interface represents creatures/blocks
Entity represents any items/objects being fired as projectiles
Creature is an object (should be abstract but eh)

AttackableTile implements MoveableTile

MoveableTile implements Tile

MoveableEntity implements Entity

Creature implements AttackableTile

Bullet extends Projectile implements MoveableEntity

Now the problem…

Rifle (First weapon im making)

Rifle extends AbstractShootableWeapon implements ShootableWeapon

AbstractShootableWeapon extends AbstractWeapon

So if I wanted to always take an input of a projectile (some interface that has damage), have a weapon that may not be able to shoot, but also weapons that must be able to shoot, and be able to access all projectiles (like through parameters), and all weapons (through parameters), what would be a better setup?

Point is, if I wanted to have a weapon that was melee only, and another that was shooting only, what’s the best way of using an interface for this?

Also, if I wanted to have something (may not be a creature) that can shoot, but may not have health, but always can take damage. I don’t know, I just ended up adding a takedamage() method to MoveableEntity cause that’s how I’m passing the shooter and hit objects through my bullet/gun.

If that made any sense, does anyone have advice, or should i just yolo it and make moveableEntity have a bunch of stuff? I just want to do everything in a good way.

Tiles are tiles. They aren’t entities nor creatures.
Creatures fall under the category of Entity because they aren’t static.
If you want Entity to represent objects that are projectiles, make a Projectile class and make it a sub-class of Entity.

Items can be a bit different because I suppose they’re not truly represented in the world as physical, collideable objects. I would separate them from Entity entirely and make an Item abstract class for them. Then they can create Entities when you call their onFire method to make a bullet, etc.

As for your shootable/non-shootable question…
Have a sub-class of Item that’s called ItemWeapon (or just Weapon). Then you can sub-class it further and have a WeaponShootable and a WeaponMelee. Instead of making new methods for each (onStrike, onFire), just have a common method inside Item that’s called onLeftClick (or something). Then WeaponShootable and WeaponMelee can override that method as they wish

Something that can take damage yet have no health makes no sense. If you want an entity to have health, make it under Creature or EntityLiving. If you want it to shoot, that’s more of an AI thing. You could implement some AI system for an Entity. That way the AI will carry through with Entity’s subclasses like Creature.

[quote]should i just yolo it and make moveableEntity have a bunch of stuff?
[/quote]
No. Object-oriented programming is supposed to make sense and be able to elaborate on in the future, not be a pile of spaghetti laying around that manages to become a game. It has to be easy for you to use so that in case you leave a project and come back in the future it won’t be totally incomprehensible.

EDIT: why are lists so hard to do god damn just take this

So if i made a picture, i would load it in as a tile. if i wanted all things that could shoot something to be able to use .takedamage(), i would run into a problem. I was using MoveableTile but didn’t wanted all of them to be able to take damage (cause it may just be a moveable picture/button). That’s where I ran into this issue.

When you mean you wanted something to be able to use the takeDamage method, do you mean they should be able to call it or actually have it inside their class?

Also not to be rude, but I have no idea what you mean by a picture.

Looks like you’re just about at the point where inheritance is more of a problem then a solution and something like a component entity system starts being beneficial:


http://gameprogrammingpatterns.com/component.html

Favor composition over inheritance.