Esempio n. 1
0
        private void OnEvent(Event e)
        {
            var master = BootMaster.instance.worldMaster;

            if (e is EventGroundClickerAddClick eventAddClick)
            {
                var gc = eventAddClick.GroundClicker;

                var packetAddVoxel = new PacketAddVoxel(client)
                {
                    ValidAdd          = master.hoverData.validAdd,
                    DestinationHeight = master.hoverData.dstHeight,
                    VertAngle         = master.hoverData.dstVert.angle,
                    HexPos            = master.hoverData.dstVert.hexPos,
                    VoxelType         = gc.currentVoxelType,
                    PlanePos          = master.hoverData.dstVert.planePos
                };
                client.SendMessage(packetAddVoxel.Serialize(), NetDeliveryMethod.ReliableOrdered);
                client.FlushSendQueue();
            }
            else if (e is EventGroundClickerRemoveClick eventRemoveClick)
            {
                var gc = eventRemoveClick.GroundClicker;

                var packetRemoveVoxel = new PacketRemoveVoxel(client)
                {
                    ValidRemove       = master.hoverData.validRemove,
                    DestinationHeight = master.hoverData.dstHeight,
                    HexPos            = master.hoverData.dstVert.hexPos,
                    PlanePos          = master.hoverData.dstVert.planePos,
                    VoxelPosition     = master.hoverData.voxel.transform.position
                };
                client.SendMessage(packetRemoveVoxel.Serialize(), NetDeliveryMethod.ReliableOrdered);
                client.FlushSendQueue();
            }
        }
Esempio n. 2
0
        public void Update()
        {
            if (server != null)
            {
                NetIncomingMessage incoming;
                while ((incoming = server.ReadMessage()) != null)
                {
                    switch (incoming.MessageType)
                    {
                    case NetIncomingMessageType.VerboseDebugMessage:
                    case NetIncomingMessageType.DebugMessage:
                    case NetIncomingMessageType.WarningMessage:
                    case NetIncomingMessageType.ErrorMessage:
                    {
                        var message = incoming.ReadString();
                        Print(message);
                    }
                    break;

                    case NetIncomingMessageType.StatusChanged:
                    {
                        var status = (NetConnectionStatus)incoming.ReadByte();
                        var reason = incoming.ReadString();

                        switch (status)
                        {
                        case NetConnectionStatus.Connected:
                        {
                            var hailMessage = incoming.SenderConnection.RemoteHailMessage;
                            var name        = hailMessage.ReadString();
                            Print($"{name} joined the game");

                            if (BootMaster.instance.worldMaster && BootMaster.instance.worldMaster.state == WorldMaster.State.Done)
                            {
                                var packetWorld = new PacketWorld(server)
                                {
                                    WorldData = BootMaster.instance.worldMaster.GetSaveString()
                                };
                                var outgoing = packetWorld.Serialize();
                                incoming.SenderConnection.SendMessage(outgoing, NetDeliveryMethod.ReliableOrdered, 0);
                            }
                        }
                        break;

                        case NetConnectionStatus.Disconnected:
                        {
                            Print($"{NetUtility.ToHexString(incoming.SenderConnection.RemoteUniqueIdentifier)} {status}: {reason}");
                        }
                        break;
                        }
                    }
                    break;

                    case NetIncomingMessageType.Data:
                    {
                        var packetType = (PacketType)incoming.ReadByte();
                        switch (packetType)
                        {
                        case PacketType.WORLD_DATA:
                        {
                            var packet = new PacketWorld(server);
                            packet.Deserialize(incoming);
                        }
                        break;

                        case PacketType.ADD_VOXEL:
                        {
                            var packet = new PacketAddVoxel(server);
                            packet.Deserialize(incoming);

                            var others = server.Connections;
                            others.Remove(incoming.SenderConnection);

                            if (others.Count > 0)
                            {
                                server.SendMessage(packet.Serialize(), others, NetDeliveryMethod.ReliableOrdered, 0);
                            }
                        }
                        break;

                        case PacketType.REMOVE_VOXEL:
                        {
                            var packet = new PacketRemoveVoxel(server);
                            packet.Deserialize(incoming);

                            var others = server.Connections;
                            others.Remove(incoming.SenderConnection);

                            if (others.Count > 0)
                            {
                                server.SendMessage(packet.Serialize(), others, NetDeliveryMethod.ReliableOrdered, 0);
                            }
                        }
                        break;
                        }
                    }
                    break;
                    }
                    server.Recycle(incoming);
                }
            }
        }