Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (789)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (864)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  2D shooter game weapon class structure  (Read 7160 times)
0 Members and 1 Guest are viewing this topic.
Offline GreenLantern101
« Posted 2015-12-21 02:21:37 »

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.
Offline CommanderKeith
« Reply #1 - Posted 2015-12-21 03:36:16 »

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:
Adam Martin used to frequent this forum many years ago as Blahblahblah and wrote this article which inspired a lot of interest in ECS:


Pages: [1]
  ignore  |  Print  

hadezbladez (2614 views)
2018-11-16 13:46:03

hadezbladez (940 views)
2018-11-16 13:41:33

hadezbladez (2567 views)
2018-11-16 13:35:35

hadezbladez (506 views)
2018-11-16 13:32:03

EgonOlsen (3694 views)
2018-06-10 19:43:48

EgonOlsen (4091 views)
2018-06-10 19:43:44

EgonOlsen (2452 views)
2018-06-10 19:43:20

DesertCoockie (3257 views)
2018-05-13 18:23:11

nelsongames (3348 views)
2018-04-24 18:15:36

nelsongames (4327 views)
2018-04-24 18:14:32
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!