3
\$\begingroup\$

(I hope the title is clear enough, if not feel free to suggest a better one)

I'm trying to make a simple tile-based map for a college project in Java. But I'm stuck on how to literally place things on my map. On the programming side of things, I still haven't got into graphics on this. As of now I have:

A "Robot" class, which implements an "Attackable" and "Spawnable" interface, the robot can attack and moved

A "Obstacle" class, the obstacle too can be attacked, so it implements Attackable, Spawnable, but it also can be pushed by a robot (methods by the class itself).

A "Station", which is nor attacable nor can be pushed, so it only implements Spawnable and other specific methods.

A "GameWorld" class which hold the map (a 2D array of Tiles) and various methods. A "Tile" class (which is arrayed to create the map). It has a boolean "Occupied" (which doubles as a collision too). Previously it stored just a single Attackable variable, because both Obstacles and Robot can be attacked, so I just tought it would be easy to just store them as such on the map for when I need to attack something, and then use some other reference to Robot instances when I need to move the robots and have a reference to the station in GameWorld in order to use that. But then I remembered that the Obstacles need to be moved too, so it wouldn't really work.

Now I'm thinking about having two maps, basically, an AttackableMap[][] for the attackable object (which would store both robots and the obstacles) and a ObstaclesMap[][] for the obstacles alone (with tiles occupied by robots set as occupied). Which would obviously mean having two tile classes, one to hold each kind of object.

But it feels cheap, I think. It uses a lot of memory and I think it might be hard to render at a later time. But having only one map would mean having to check what kind of object I'm getting and I'd have to do loads of casts and I want to avoid that.

So, I think, there must be a better way to do this. How do other people do it then? So basically my question is... How can I store different "things" on a tilemap in a better way? Is there some technique widely used?

What I've come up with feels rigid, if I wanted to add another kind of object that can be placed on the map (like a mine) I would need to create another map entirely, another tile, write new methods... There must be some flexible (and not too complex) way to have some freedom in this aspect.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Narrow this down man. We're supposed to be giving clear, concise answers to clear, concise questions. That way people in the future might be able to get help if they have the same question. \$\endgroup\$
    – Almo
    Nov 21, 2015 at 17:52

1 Answer 1

0
\$\begingroup\$

Right now you have an array of Tiles that has an Occupied boolean value, and previously had an Attackable value. You are suggesting that you have multiple arrays of tiles, one to store the Attackable things and another to store the Obstacles. I do not think that this is the best approach for you to take.

You need to make the decision about whether a Tile can ever have more than one object in it. For example, what if a Robot had the ability to move through walls? Or if there was a power-up that the player could pick up that gave them that ability? So let's assume that it is theoretically possible for more than one object to be on a Tile.

In this case, you could give the Tile an array of objects, and when a Robot or Obstacle moves to that tile, it could be added to that array, and removed from the array of the previous tile. In order for this to work, all of the movable things would need to inherit from the same class, such as a Moveable or Character class, and the array of the Tile would be an array of this type. That way, any subclasses of that type will be able to enter the Tile array.

When you want to check whether a tile is attackable or occupied, you could have a method of the Tile class that searches through the array, looking for a Robot or an Obstacle or whatever you need.

There are lots of different ways to solve this problem, but since you are concerned about the inflexibility of your current approach, I think this is a valid way to program it.

\$\endgroup\$
6
  • \$\begingroup\$ I like what you're suggesting, but how would the method you suggest work? If I have an array of Moveable for example, then how can I know if that object is a Robot or an Obstacle or something else? \$\endgroup\$
    – Paul
    Nov 21, 2015 at 18:02
  • \$\begingroup\$ Iterate over the array and test whether that object is a specific type. What language are you using? \$\endgroup\$
    – bazola
    Nov 21, 2015 at 18:03
  • \$\begingroup\$ Java (as I said in the open post). You mean something like "for each object in the array > if (object instanceof class), elseif (object instanceof otherclass), elseif..." and so on? But then what would I return? The index to the object? \$\endgroup\$
    – Paul
    Nov 21, 2015 at 18:09
  • \$\begingroup\$ What to return, if anything, depends on the specific gameplay of your game. However, you could replace the isOccupied() and isAttackable() methods with a search through the array of the Tile, returning true if a Robot is found, for example. \$\endgroup\$
    – bazola
    Nov 21, 2015 at 18:14
  • \$\begingroup\$ And once I know if there is a robot to attack, how would I attack the robot itself though? I need a reference to don't I? \$\endgroup\$
    – Paul
    Nov 21, 2015 at 18:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .