# Moving cars on a tiled map

I'm writing a game which is situated on a tiled map. I have a PlayState on which there's a Board which cosists of Fields.

My Board is an isometric map of a city, where each tile is a kind of road. Depending on the type of the road, there's a different texture of a car (if we go up/ down or left/ right) + on some tiles a car will take more than one texture (because it turns).

I don't know what's the correct idea of putting a car into the game. Should a class Car be part of Field? Probably not because there are moments in which the car is on more than one field. So maybe a part of Board? But the texture of a car depends on the type of the road that it is on. What is more, I must deal with the fact that once a car is on a field, it moves in the boundaries of it - so should I insert some coordinates of a field itself?

Could someone write a pseudocode of classes Board and Field with the necessary variables? And explain how I should deal with car's movements? I'm working in Java's Libgdx if it matters.

Firstly, I believe that there are some conditions needed for the car to appear on the field, and also i assume that you will want to have more than one car in the future.

I believe it's a bad idea to put all logic into the field objects, the only reason to do so would be performance reasons. But since you have a clear separation of concerns in your case, I would make a Car class containing the cars logic: reference to the Field object that the car is visible at, coordinates within this field, direction the car is pointing towards (based on which you would change textures) etc. Store the cars in an array-like structure and on render just iterate over it, rendering cars whenever needed.

I would continue with this simple architecture while adding houses, dogs, cows and whatever else you would need to place on the board. If the performance matters, consider storing all of the references to the particular field in one data structure (dictionary of lists?), and then iterate only over Fields, checking this structure for each field to find all elements that the selected one relates to.

Note that I have no experience with making games in Java, and this is just a simple tip following the Keep-It-Single-Stupid rule.

I am not going to write the pseudo code for you, but I can tell you what I would have done creating the game.

Keep the Car and the Field as different classes. The Field can have zero or more instances of a Car. Either use an Array of cars, or a map of cars, where the carId is the key. It is totally up to you.

If the texture of the car is field dependent, you can simply have a method in your Car-object, e.g setTexture(), and the Field can call that method on all the cars in its array (or map). The car itself can change the texture based on a direction-property. If the car is moving up, left, right or down, set the direction, and render the correct texture.

I would still use x, and y coordinates to defined your map, in a traditional 2D-way, no isometric thinking here, you can just transform all the points to fit the isometric grid in rendering. If you think about it, an isometric grid is just a 2D-grid. Here is an article I stumbled upon while looking around on the internet, that might be useful: https://gamedevelopment.tutsplus.com/tutorials/creating-isometric-worlds-a-primer-for-game-developers--gamedev-6511. Here is a picture from the same article