Madmenyo
Madmenyo

Reputation: 8584

Data driven design, referencing the data

I am trying to come up with a good design for my game. I am currently testing a data driven item system and I want this system to be very flexible.

If the player wants to fire a weapon I need to lookup if it has the right ammunition type in it's inventory. Same goes for the actual ammo that needs to hold a reference to the type of ammo. When the player picks up ammo it already has it should be added to each other.

What I'm currently thinking off is a class for the type of ammo that has a "short" string for reference since a string is more readable in a Json file. This class also holds information of the ammo like a proper name and some extra info. Finally I create a list to hold the different types of ammo.

Then when I create actual ammo (items) I will pick a type from the list of ammo types that I generated from a file. This ammo item holds the amount of it. I might use a Interface and a factory here for the different types of ammo when the ammo is shot and something is hit (Explosive, piercing, light ammo all behaves differently).

Then when a weapon wants to fire I check if the corresponding ammo is in the players inventory by looking up the "short" string.

I am wondering if I'm on the right track here. I have doubts about my reference system using the "short" string for the ammo type. If I create factories of my ammo types I could check in my weapons fire method if there is ammo of that class type and I would only use the short hand string in the factory for creating the various objects.

If I want to use a factories and go OOP I need a lot of classes.

[Ammo]
    [Bullet]
        [762mm] //I know this is not a valid class name, have to come up with something.
        [5.56mm]
        [Etc.] //Plenty more to come thanks to data driven
    [Rocket]
        [Tornado]
        [Etc.] //Plenty more
    [Grenade] //Launcher
        [Etc.]
    [Etc.] //..

But for each type I can have a interface and create functionality. I can easily compare the type of ammo by comparing the class.

If I use the reference by the short hand string I don't have to create all the classes, interface and factory but have to setup the ammo rules in a long statement. Things might get messy in the long run.

Any thoughts about this or another "better" approach to this?

Upvotes: 0

Views: 352

Answers (1)

luksch
luksch

Reputation: 11712

I would advise against the differentiated OOP approach, because of the following reasons:

  • Data driven

You say that the ammo and weapons will be created through a data driven design. That is all well, but if you need a new subclass for each type of ammo, you still need to actually implement that new class. That is for each ammo. There goes the ease of getting new ammo into the game...

The logic of which ammo goes into what gun needs to be somewhere. You can do it in OOP, but you may as well hold all the relevant info in generic data tables (Data driven! Here you go!). Your code would implement only the general rules but not the actual relation of bullet type to gun etc. This I would store in a database. Your code stays the same, but the behavior is data driven... The draw back is of course that your tests now need to deal with the data from the database.

  • OOP vs object composition

OOP is all well and good if your objects are really following this rather rigid model. i am really not an expert on ammo at all, so I might be wrong, but isn't it possible to think of ammo entities that share characteristics of several parent types? Such relations are hard to model in OOP. This is a shortcoming in JAVA OOP, since you cant inherit from several parents. Instead, you should look into object composition.

Your idea of a short string from human readable identifiers is okay I think. These should also come from your data and should not be hard coded.

Upvotes: 1

Related Questions