Reputation: 144
I am doing a little programming exercise in Java to get me back into the swing of things before school. My program will be video game (Team Fortress 2) related and as such I need to store data about a large number of weapons.
My current model is that I have a class for each type of weapon, (e.g. Scout_Primary or Soldier_Secondary) and I will modify the variables in each class depending on what specific weapon is selected. For example if Scout_Primary is "Scattergun" I will modify its base damage and reload speed accordingly.
If you are familiar with TF2 you will know that there is a large amount of weapons. How do I store all the weapons' stats in my program? I do not wish to write giant switch statements containing stats for each weapon in a category. I am very eager to work with SQL more (I have some experience with it but not too much, I have used JDBC before).
Is it possible to create a database and just package it with the program itself so that I don't have to worry about hosting? The only experience I have is with remote databases.
What I would like to do would be to have that local database and perform Selects and pass the data to a constructor. Thanks in advance.
EDIT: I have been looking at Derby, is this the right direction?
Upvotes: 3
Views: 959
Reputation: 49572
I think a simple class weapon could do this:
class Weapon {
private WeaponType weaponType;
private ClassType classType;
private int damage;
...
}
enum WeaponType {
PRIMARY, SECONDARY, ..
}
enum ClassType {
SCOUT, SOLDIER, ...
}
You can also map this to/from json with libraries like Jackson. Json is a lot simpler compared to a database if you have to update values from time to time by hand.
Upvotes: 0
Reputation: 6073
I would recommend using a database, especially because you mentioned having experience with JDBC.
If you do not want to have to worry about installing a database engine such as MySQL or Postgres on your server, you could use H2 Database Engine, which can be distributed with your Java Application.
H2 is written in Java, and has a JDBC API.
Upvotes: 0
Reputation: 18424
Yes, SQL could be appropriate for this. You should look into Apache Derby, its main benefit is that it is lightweight and very easy to embed into a Java application. You can do exactly what you're talking about - use it locally.
The only issue will be bundling the actual data with your application. I would suggest you start with it in a simple file, maybe JSON or XML encoded, or even just CSV - basically anything you choose. Then when the program starts, you check to see if the database exists. If not, you create it, read the file, and store the data from the file into the database. Then you're ready to do querying with SELECT statements or whatever else you want.
Upvotes: 1
Reputation:
Whatever you choose, you will most likely have to use SOME sort of database with which to hold your weapons. SQLite is one of the smallest and easiest to implement, although it requires some background knowledge of SQL.
You could also store all the weapons in a plain-text file formatted with JSON or XML... I would opt for JSON because it would involve less typing on your part, but XML is also easy enough to do (and am pretty sure there's an XML parser available already). JSON is, however, easier to implement even though you still have to download the source from json.org.
{
"weapons": [
{ "name":"Scattergun" , "damage":"5" },
{ "name":"Flamethrower" , "damage":"8" , "pyro":"yes" }
]
}
As you can see I have no clue about TF2.
Upvotes: 0
Reputation: 113
Why do you need different class for every weapon? I guess creating a class weapon would be better choice, or rather an abstract class which can be extended by various other categories of weapons. How about storing the weapons in a collection
(list, map, set depending upon the kind of operation you want to perform). If you aim at storing game stats and launching the game on as a web based app, using database is recommended
Upvotes: 0