So I’m trying to make a 2D shooter game from scratch using the best possible code architecture, and I can’t decide on structure for the weapons class (and subclasses)
So I have a base Weapon class, and it has properties like damage, bullet speed, reloading time, clip size, etc.
But then the problem is how do I create weapons with tracking projectiles or shotguns or lasers (how should the inheritance structure look like?)
I guess I don’t really want to have: “boolean tracking; boolean laser; int numBulletsPerShot;” because that would be kind of clunky.
Hi Greenlantern101,
I think your work is great, and I’m looking forward to see the 2D game you make!
I’ve noticed 3 main ways to structure the data:
-Inheritance by sub-classing which is simple but a little restrictive and often to keep flexibility you end up with a hollow ‘Weapon’ class that barely has any member data.
-Interfaces which allow a ‘Laser’ class to implement different interfaces such as ‘Equipable’, ‘Weapon’, ‘Renderable’ and so on with the corresponding methods which are all implemented in the individual Laser class. This is the pattern that most programmers use here I believe. The problem with this is code repetition since every Laser, MachineGun, and so on class will have the same code to do basic things like fire a bullet. But in Java 8 I think that interfaces can now hold some non-abstract methods which might mitigate this downside, though I haven’t tried it.
There is wider support for this Object Oriented composition pattern over the inheritance structure, it’s called ‘Composition over Inheritance’:
-Entity component systems which are quite controversial on this forum with clever people arguing for and against them. I haven’t tried it, but there’s some info about it here: