Click here to Skip to main content
15,905,782 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello Codeproject,

I've been working on a game of mine for a few days now. And I want to get the basic things Online, I currently have a few classes;

Entity,
Entity Player : Entity,
World.

All the things going on in my game are being controlled by my World class:

C#
public class World
{
    /* List of Entities */
    public List<Entity> Entities { get; set;}

    /* Entity Player. */
    public Entity Player;

    /* Initialization void */
    public World()
    {
        /* Initialize list of entities. */
        Entities = new List<Entity>();

        /* Initialize Player. */
        Player = new EntityPlayer();

        /* Speed */
        Player.SetMovingSpeed(4);
    }
}


Which then is drawn via:

C#
// -- Draw the player.
/// <summary>
/// Draws the player on the given Batch.
/// </summary>
/// <param name="Batch">The SpriteBatch to use.</param>
public static void DrawWorld(SpriteBatch Batch)
{
    /* Draw the player. */
    Batch.Draw(Main.World.Player.Sprite, PlayerRectangle, null, Color.White, Main.World.Player.GetAngle(), new Vector2(PlayerRectangle.Width / 2, PlayerRectangle.Height / 2), SpriteEffects.None, 0);

    /* Draw every Entity if needed.*/
    long X = 0;
    long Y = 0;
    Rectangle Rectangle;
    foreach (Entity Entity in Main.World.Entities)
    {
        /* Should we draw it? */
        if (Entity.GetDistanceFromEntity(Main.World.Player) < Main.BiggestSize)
        {
            /* Set the variables to draw. */
            X = (Entity.PositionX - Main.World.Player.PositionX) + Main.WindowWidth / 2;
            Y = (Entity.PositionY - Main.World.Player.PositionY) + Main.WindowHeight / 2;

            /* Create the rectangle. */
            Rectangle = new Rectangle((int)X,(int)Y,Entity.Sprite.Width, Entity.Sprite.Height);

            /* Draw the Entity. */
            Batch.Draw(Entity.Sprite, Rectangle, null , Color.White, Entity.GetAngle(), new Vector2(Rectangle.Width / 2, Rectangle.Height / 2), SpriteEffects.None, 0);
        }
    }
}


So, clearing everything up, this is everything I currently need for my game. So, summing up, I only need to be able to Control the List<entity> Entities of my world in order to get multiplayer working for right now. But how would I do that?

Server:
1. Listen for TcpClient.
2. If TcpClient connects, we create a new Entity in the server's World class.
3. Then, if anything goes on, moving, shooting(Not yet created), we update it in the current entity which then is send to all clients.
4. Player logs out, entity is removed.


That is basically how it would work, but I have no clue on how to send these sort of packets without using an XML Serializer to send the whole class via string converted to a byte array. This is the correct solution for me, but I am really sure that there is a better way on doing it, and I would love to know where to get started.
Posted

1 solution

I´m not sure if sending whole class serialized is a good idea.
I think you need to make sure the server only tells the clients what they need to know.
In order to do this you might only want to send important variables (or instructions?) to clients.
Avoid sending images and big objects as much as possible.
You should also consider using UDP instead of TCP if you need high speed.

However, if you insist on sending classes you might want to check up on WCF.
Not sure if it is available on express version of VS.

This is a big topic and it´s hard to give a definite answer without being involved with the coding, so good luck!
 
Share this answer
 
v2
Comments
Yvar Birx 3-Mar-13 8:45am    
Thanks, I really appreciate the help, and I have finally gotten an idea on how to. ;)
Per Söderlund 3-Mar-13 8:47am    
Np, good luck. I think you have alot of work ahead of you.
Yvar Birx 3-Mar-13 9:38am    
Yeah, I actually don't mind. Sounds like a great thing to learn.
nika2008 3-Mar-13 17:35pm    
nice +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900