I'm building a game where enemies come in waves. I want to create a file where I can define data about the waves (# of enemies, spawn times, speeds, etc.). I come from a background in iOS and would normally use something like a plist. What would be the best way to do something like this in Java and Android?
Answer
There's no single "best" way to do this, it depends on the needs of your game. Here are some options:
Hard Coded
Both the easiest and probably least flexible approach. No reason you can't just make a static class with static fields for each of the levels. Actually, this isn't a bad way to prototype, so you can move it into some other approach later. If you're going to prototype this way, do yourself a favor and make the fields private (accessible through a get() method) so you can just refactor those methods into pulling from the proper source later.
XML
This one's my vote. If you're going the XML route, there are a number of available libraries you can use to help parse the XML. (Here's one called XMLPullParser.) When you're storing flat files in android you'll have to decide whether to use internal or external storage.
Internal: "You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed." (Source and more detail on how to implement here)
External: "Every Android-compatible device supports a shared 'external storage' that you can use to save files. This can be a removable storage media (such as an SD card) or an internal (non-removable) storage. Files saved to the external storage are world-readable and can be modified by the user when they enable USB mass storage to transfer files on a computer." (Source and more info here.)
In your case, you probably want to use internal storage, but if you want to allow players to "mod" the game or create their own levels, you could store it externally.
A note about reading from hard files: You're going to need to do this asynchronously from your main thread. Read up on AsyncTask for implementation details.
JSON
As an alternative to XML, you could store the objects as JSON objects. Storing as a JSON is very similar to what you're used to (i.e., storing in plist). Both describe storage of serialized objects.
Most of what I just said about XML above applies here as it's another flat file approach. For JSON the best tip I can give is check out GSON, it's super-handy.
Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.
DATABASE
SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. It's built into every android device. The libraries you'll need are available in Android's SQLite Package. Google's developer docs can help you with this as well. A fantastic tutorial on this from vogella.com is available here.
PULL FROM A SERVER
This may be overkill, but if you already have some online component to your game, you might consider pulling the levels down from the server so you can update them without any update on the client side. I recommend and use KryoNet for rolling one's own server in java. Course, this just backs up the question of how the levels are stored on the server side, but at that point we're not talking android specific anymore.
No comments:
Post a Comment