问题描述
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.
推荐答案
这篇关于我如何在线制作游戏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!