Good afternoon,
currenty I’ll making a online rpg(2d). I’ve already finished my server code.
My client has some managers which are all using the singleton pattern.
- SocketManager
- PacketManager
- MapManager
- SpawnManager
- UiManager
Introduction
SocketManager
This is the main part of the client “network system”. He is connecting to the UDP server and starts a listener thread for incoming packets. If a new packet is received the SocketManager will check its PacketID and sends the packet with the correct PacketType to the PacketManager. With sending I mean that the packet gets stored in a static “message queue” of PacketManager.
The SocketManager also checks every Update() if a new packet is laying in the static send queue at the PacketManager. If so the SocketManager will send it.
PacketManager
He will read the packet (splits it into message pieces) e.g Packet(id|subid|name|lastmap) and execute the effect of the packet. So as an example: If a login packet gets received, the PacketManager will call the MapManager to load a map with the id of MapID and call SpawnManager for spawning a player with the name of name.
Inside PacketManager → Receive Login Type Class
MapManager.mapManager.load(int mapID)
SpawnManager.spawnManager.player(string name)
(In the future I might put here also the params charID, name, listInv, listAttributes (lvl, hp, mp etc.)
The PacketManager not only reads packets, he also pack packets for sending.
So if e.g the login packet gets sends (through the login button) he gets the name and password from the UiManager function getLoginData().
Inside PacketManager → Send Login Type Class
List<string> loginList = LoginUiManager.loginUiManager.getLoginData();
//where loginList[0] = name
//and loginList[1] = password
MapManager
Just loads the map with the id of MapID ( maybe useless manager )
SpawnManager
He is spawning currently only players. But later he also should spawn mobs. And he will find spawnpoints on the map.
UiManager
Contains the functions to interact with the Ui (getLoginData, setHp, setExp etc.) and of course the Ui.
So basically this was my introduction.
Problem
But after I successfully wrote all this code I’ve run into some problems.
I should send at a constant rate: update packets(position of the player… etc), but which update loop In which Manager should I use ?
For instance I could make a class “Player” which will at a constant rate sends, lets say every 60 frames an update packet.
Inside Player
public void Update()
{
if (counter == 60)
{
PacketManager.packetManager.send(PacketSendType.S_UMove)
counter = 0
}
counter += 1
}
So the PacketManager has to get the Entity’s posX and posY … maybe through a function get pos ? But I also have to then pass the Instantiated player var somehow to the PacketManager (Or somehow with Find or so).
Would this be a good Idea ?
Maybe I forgot a problem :S Then I will add it later
Sorry for the long post >.<
Have a nice day !
LifeArtist