helsont
helsont

Reputation: 4962

Game Interface Design

So I'm making a game similar to Age of Empires 2. So far, what I've been doing is using an Applet and making an array of tiles (grid) to contain units and buildings. I feel that in the long run this is going to be inefficient. I'm sure there is a better way to do this, perhaps using GUI, but I'm not a very experienced programmer yet so I've resorted to this more primitive method. Is there a better, more efficient way to have tiles that contain characters and buildings on the map?

Thanks.

This is pretty much how I want it to look like: enter image description here

Upvotes: 1

Views: 486

Answers (2)

Erica
Erica

Reputation: 2261

It depends on what you consider to be the 'inefficient' part. If most of the array elements are going to be containing data, and you need to be able to look up that data quickly and easily, than arrays are certainly the best way to go. They're easy to understand, and the logic mikera described for storing data is a very good way to get started.

There are data structures that very big tile based games use for storing data. Most of those are optimized for 'sparse' graphs at the like. A sparse graph is one where most of the locations are empty, but a few of them have content. Very big world maps often have this qualit. If you're curious about these scenarios, then you should google "quad tree tiling".

But in general, if you're still learning development, stick to the array. Premature optimization is the root of all evil. :)

Upvotes: 0

mikera
mikera

Reputation: 106401

An array of tiles should be fine - this is what is usually used to represent the map in a 2D game.

Note that you will probably want to distinguish between:

  • Map terrain tiles, where each square contains just one tile
  • Units / buildings / other game objects, where each square might contain multiple objects

For the terrain a single array is fine, with one tile in each position.

For the other objects, you need a way to store multiple objects in each square. An array with an ArrayList in each square can work - though you will probably want to leave nulls in all the squares that don't have any contents to avoid creating a lot of empty ArrayLists.

It's also useful to create a global collection of game objects so that you can iterate over all game objects and/or find a speciifc object quickly without searching the whole map. I typically use something like a HashMap for this, where the integer key is a unique ID for each game object.

Upvotes: 1

Related Questions