I am writing collision detection for a 2D top down game. But I am stuck on updating environmental information for each player.
Suppose I have 3 or 4 thousand static entities and a few hundreds moving players on the map. At each move of my player, he can only see a small part of the map on his screen.
Using brutal-force to iterate all the entities and moving players is a bad idea. And this is also true for sending all the information on the map from the server to a client. So I want the player to only receive the update of other entities/players that are on his screen, maybe within an area slightly bigger than his screen.
A small example goes like this. At time step N, the player sees 200 entities and a dozen other players, all other entities and players are unknown to him. I suppose not all information is needed to be sent over the network. At time step N + 10, the player may see other stuff including 50 more other entities and some updated information. Now maybe I should send the information of that 50 entities from the server to the player.
However, some entities may be destroyed even before the player sees it and will never show up again. In this situation, does the player need to know/receive status update of the entity at all? In the mean time, some new static entities may appear on the map, and I also don’t think the player should know the status of the entities that are outside of his screen.
I am thinking of using array or map to store this information, just to make it simple. Maybe one array to store the entity information on the screen area, and one array to store the new entity information that is about to show up on the screen area. But even in this way I am still sending duplicated information to the player because some entities or players may not update their status between two updates.
I have also learned about publish-subscribe pattern that can be used in this situation, but I don’t know how to implement it. An example for this would be very helpful. I might need to put a linked list to each player and store information that is needed to send out, delete the information previously sent but is not required to update, so maybe less memory space is required and probably less data to be sent when using this pattern.
Another problem is that I send out all the information of everything to the client at the initial state when the player joins. If I don’t maintain the information of all the static entities things may go wrong on the client side. So I also need some suggestion on how to begin the game when a player joins the game.