I'm trying to make an online game where I want to simulate a world that is connected in all directions, just like "PAC-MAN" does. When the player crosses the boundaries of the map he would be in the other side of the grid.
So far so good, I managed to build a server that holds a 10x10 matrix, (I attach some images as a reference), and using some modular-arithmetics it calculates and sends to the client the corresponding tiles of the map given the player's position.
So assuming "view_distance = 2" for example, the server sends the corresponding tiles based on the player's position:
I think I made my point, now lets face my problem.
When the client gets the list of tiles to render from the server, it needs to calculate the distance to the player (unit-vector) for each tile so it can instance it in the right place. Every instanced tile has a script that recalculates the distance to the player every time he moves so it destroys itself when it is farther than the "view_distance".
So for example, if I the client is in the position (9,5) the unit-vector of the tile (7,7) would be (-2,2)
The client has the following information:
- The tile where he's standing:
Globals.PlayerInfo.PlayerPosition
- The lenght of the map:
Globals.MAP_LENGHT
(in this case it's 10) - The view distance:
Globals.DATA_DISTANCE
(in this case it's 2)
How do i calculate the unit-vector? I made the following function but it doesn't seem to work. Am I missing something?
public static Position GetPlayerDistance(Position position)
{
var unitVector = new Position() { X = position.X, Y = position.Y };
if (position.Y > Math.Truncate(Globals.PlayerInfo.PlayerPosition.Y) + Globals.DATA_DISTANCE)
{
unitVector.Y = position.Y - Globals.MAP_LENGHT;
}
if (position.X > Math.Truncate(Globals.PlayerInfo.PlayerPosition.X) + Globals.DATA_DISTANCE)
{
unitVector.X = position.X - Globals.MAP_LENGHT;
}
unitVector.X -= (float)Math.Truncate(Globals.PlayerInfo.PlayerPosition.X);
unitVector.Y -= (float)Math.Truncate(Globals.PlayerInfo.PlayerPosition.Y);
return unitVector;
}