我如何在线制作游戏? [英] How would I make my game Online?

查看:68
本文介绍了我如何在线制作游戏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Codeproject,



我已经在我的游戏中工作了几天了。我想在线获取基本内容,我目前有几个课程;



实体,

实体播放器:实体,

世界。



我游戏中发生的所有事情都由我的世界级控制:



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:

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:

// -- 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);
        }
    }
}





所以,清理所有内容,这是我目前在游戏中需要的一切。所以,总结一下,我只需要能够控制列表< entity>我的世界的实体,以便立即让多人游戏工作。但是我该怎么做?



服务器:

1.收听TcpClient。

2.如果TcpClient连接,我们创建一个新的服务器的World类中的实体。

3.然后,如果有任何事情继续,移动,拍摄(尚未创建),我们会在当前实体中更新它,然后发送给所有客户端。

4.播放器注销,实体被移除。





这基本上是如何工作的,但我不知道如何发送这些类型的数据包,而不使用XML Serializer通过字符串转换为字节数组发送整个类。这对我来说是正确的解决方案,但我确信有更好的方法可以做到这一点,我很想知道从哪里开始。



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.

推荐答案

我不确定发送全班序列化是不是一个好主意。

我认为你需要确保服务器只告诉客户他们需要知道什么。

为了做到这一点,你可能只想向客户发送重要的变量(或指令?)。

避免尽可能多地发送图像和大对象。

你如果你需要高速,也应该考虑使用UDP而不是TCP。



但是,如果你坚持发送课程,你可能想要检查WCF。

不确定它是否可以在VS的快递版本上使用。



这是一个很大的话题,很难给出一个明确的答案而不是参与编码,祝你好运!
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!


这篇关于我如何在线制作游戏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆