Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (773)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
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 6660 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':
https://en.wikipedia.org/wiki/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:
https://en.wikipedia.org/wiki/Entity_component_system#cite_note-4
Adam Martin used to frequent this forum many years ago as Blahblahblah and wrote this article which inspired a lot of interest in ECS:
http://t-machine.org/index.php/2007/09/03/entity-systems-are-the-future-of-mmog-development-part-1/

Cheers,
Keith

Pages: [1]
  ignore  |  Print  
 
 

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

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

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

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

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

nelsongames (1963 views)
2018-04-24 18:14:32

ivj94 (2736 views)
2018-03-24 14:47:39

ivj94 (1938 views)
2018-03-24 14:46:31

ivj94 (3029 views)
2018-03-24 14:43:53

Solater (1078 views)
2018-03-17 05:04:08
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

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org 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‑gaming.org
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!