Quantcast
Channel: Question and Answer » programming
Viewing all articles
Browse latest Browse all 103

Store and retrieve different objects on a tile

$
0
0

(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.


Viewing all articles
Browse latest Browse all 103

Trending Articles