S M T W T F S
1
2345678
9101112131415
16171819202122
23242526272829
30

# Move, <player>!

xna beginner tile

Yes, this entry is about movement. I think it'll be a short one, though I must admit, movement in a tile-based game is harder than I thought. Ok, now on to the technical bits.

You see, the difficulty (at least for me) of making a tile-based game mostly lies on the fact that everything, virtually everything must adhere to a certain set of grid; The positions of the the objects, the map, the tiles themselves, and last but not least, the movement. Now, of course there are are aspects of it that makes the development process easier. The collision for example, is easier to implement compared to, say, a platformer, since all we need to do is mark a certain tile "occupied" and tell the objects they can't enter an occupied tile, other than their current tile. While that certainly sounds easier to do than the actual practice, it's still simpler to implement than the full-fledged collision checking often required in other types of games.

For the game, I needed a way to select a certain game object when the user clicks on it, which (since the end destination would also be determined by a mouse click) means I also needed a way to know which tile my mouse is currently in when I clicked the left/right button. I had a solution in my mind, but I wasn't certain it'd be good enough, since it involves scanning through the entire tileArray. So, I searched around and found something called "picking". Unfortunately though, further inquiries only revealed 3D solutions. The few answers I found regarding 2D tile-based picking were pretty much similar to my own solution. One StackOverflow question answer however, said that while it would certainly be heavy, it's still good enough, since we're not doing it every single frame, only on a mouse click. So, I decided to go with it.
```        public static Point MouseCoordinate(MapParser mapParser)
{
Vector2 mousePos = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
foreach (Tile tile in mapParser.tileArray)
{
if (mousePos.X >= tile.position.X && mousePos.X < tile.position.X + 20)
{
if (mousePos.Y >= tile.position.Y && mousePos.Y < tile.position.Y + 20)
{
Console.WriteLine(tile.coordinate.X.ToString() + " " + tile.coordinate.Y.ToString()); // For debugging only.
return tile.coordinate;
}
}
}
return new Point(0, 0);
}
```
First of all, please disregard the magic numbers. My tiles are 20 by 20 pixels in size. I plan to refactor that, so that it can accept an arbitrary size.

As you can see, I've put a console output there, just so I can be sure it works. I also tweaked the MapParser and GameObject class a bit since using a 2D array means your coordinate axes are flipped. And now that I have a way to tell which tile my mouse is in, I'll also need a way to tell which object I selected, or whether I have any object selected at all.
```        public void SelectedObject(List<GameObject> objectCollection)
{
foreach (GameObject item in objectCollection)
{
if (item.Coordinate == MouseCoordinate(mapParser))
{
currentObject = item;
}
else
{
currentObject = null;
}
}
}
```
There! Now I not only know which tile is selected, and which object is selected. That leaves the movement function.
```        public void Movement(GameObject gameObject = null)
{
if (gameObject != null)
{
gameObject.Coordinate = destinationTileCoord;

if ((int)gameObject.Position.X < (int)destinationTile.position.X)
{ gameObject.Position = new Vector2(gameObject.Position.X + 1, gameObject.Position.Y); }
else gameObject.Position = new Vector2(gameObject.Position.X - 1, gameObject.Position.Y);
if ((int)gameObject.Position.X == (int)destinationTile.position.X)
{
if ((int)gameObject.Position.Y < (int)destinationTile.position.Y)
{ gameObject.Position = new Vector2(gameObject.Position.X, gameObject.Position.Y + 1); }
else gameObject.Position = new Vector2(gameObject.Position.X, gameObject.Position.Y - 1);
}
if (gameObject.Position == destinationTile.position) gameObject.isMoving = false;
}
}

```
Yes, I know, that code looks superheavy, and superbad. I also think so, but turns out the performance doesn't get any perceivable hit. The superbad is true though. I don't have any profiling tools, so I simply fired up the trusty Task Manager and played around while checking to see if it's too heavy. For now, it gets 7-10% CPU usage and around 9MB memory. However, I realize that this is still very early, and the game contains too few objects for it to be accurately measured. So things will certainly get heavier from here on.

Oh, and if you're wondering why I had make sure the X movement is finished before starting the Y movement, that's because I wanted the movement to be like Super Robot Wars, which is the inspiration for this experimental game. In SRW (at least the GBA version, I haven't played SRW in any other platforms) objects would move in a straight line along an axis, no diagonal movements. The end result is this:

Apr 19 2014 07:30 AM

Couldnt you use a simple mathematical expression to find the tile coordinates out of the mouse position (translate,scale,offset and round the mouse position to get the tile integer position)

Apr 19 2014 09:30 AM

translate,scale,offset and round the mouse position to get the tile integer position

Can you elaborate more on that?

Edit: Nevermind. I think I understand. And now I feel stupid.

Yes, you're right. The width and height of each tiles are fixed, so I shouln't have any problems getting the tile coordinate from the mouse position. Thanks for the suggestion.