0

I'm making a grid/tile based game. Each tile on the grid has a few attributes a x position, y position, and an ID. The ID changes based on what occupies the tile. Currently, there are two different types of buildings that can occupy a tile, a command center, or a turret. If I wanted to delete a building, I would need to remove it from it's ArrayList:

ArrayList<CommandCenter> commandCenters = new ArrayList<>();
ArrayList<Turret> turrets = new ArrayList<>();

Structure IDs:

Command Center : 1
Turret : 2
...

How can I pick which ArrayList to remove from, given the ID? For example(some pseudocode):

if (square(x, y).id == 1)commandCenters.remove(index);
if (square(x, y).id == 2)turrets.remove(index);
...

Is there a way to make this less tedious, or more efficient?

3
  • Are CommandCenter's and Turrets's descendant from a parent building class? If so you could just give each tile a property of building and choose that way. Commented Jun 24, 2015 at 20:24
  • @scrappedcola CommandCenter and Turret are their own classes. They aren't really linked in anyway. Commented Jun 24, 2015 at 20:25
  • It would be easier and more oop if they were. Since they conceivably share some properties and methods. Commented Jun 24, 2015 at 20:26

1 Answer 1

4

Have both classes extend from a Building base class.

class CommandCenter extends Building { }
class Turret extends Building { }

Then you can maintain a single list of buildings of all types.

List<Building> buildings = new ArrayList<>();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.