Beispiel #1
0
        private void OnReceived(GenericMessage msg)
        {
            var id = msg.ReadUnsignedInteger();

            if (id == 0)
            {
                ClientID = msg.ReadGuid();
            }

            if (id == 1)
            {
                if (SharpCraft.Instance.World is WorldClientServer w)
                {
                    var x = msg.ReadSignedInteger();
                    var z = msg.ReadSignedInteger();

                    var chp = new ChunkPos((int)x, (int)z);

                    var count = msg.ReadUnsignedInteger();

                    var data = msg.ReadByteArray((int)count);

                    var raw = new short[Chunk.ChunkSize, Chunk.ChunkHeight, Chunk.ChunkSize];

                    Buffer.BlockCopy(data, 0, raw, 0, (int)count);

                    w.PutChunk(chp, raw);
                }
            }
            if (id == 2)
            {
                int count = (int)msg.ReadUnsignedInteger();

                for (int i = 0; i < count; i++)
                {
                    var guid = msg.ReadGuid();

                    var x = msg.ReadFloat();
                    var y = msg.ReadFloat();
                    var z = msg.ReadFloat();

                    var pos = new Vector3(x, y, z);

                    x = msg.ReadFloat();
                    y = msg.ReadFloat();
                    z = msg.ReadFloat();

                    var dir = new Vector3(x, y, z);

                    if (guid == ClientID || SharpCraft.Instance.World == null)
                    {
                        continue;
                    }

                    //TODO - update entities in WorldMP

                    var entity = (EntityMp)SharpCraft.Instance.World.Entities.SingleOrDefault(e => e is EntityMp emp && emp.ID == guid);

                    if (entity == null)
                    {
                        entity = new EntityMp(SharpCraft.Instance.World, pos, guid);
                        SharpCraft.Instance.World.AddEntity(entity);
                    }

                    entity.PosServer = pos;
                    entity.Dir       = dir;
                }
            }
        }