示例#1
0
        protected override void Init()
        {
            _clientData = ClientDataRegistry.GetInstance();


            UIManager.StartLayer(nameof(LoadingGameUI), this);

            /**
             * Scene events
             */
            #region Update Input State

            _playerInput.Init(this);

            #endregion

            /**
             * Reserved Socket Manager routes
             */
            #region Client Connect

            Client.OnConnect = (m) =>
            {
                GlobalEventManager.Emit("onConnectionStatus", new GlobalEvent("Server Connected..."));
                Thread.Sleep(100);
                AudioManager.PlaySong("HumanSong");
            };

            #endregion

            #region Client Authentication

            Client.OnAuthentication = (m) =>
            {
                GlobalEventManager.Emit("onConnectionStatus", new GlobalEvent("Authenticating User..."));
                Thread.Sleep(100);

                return(null);
            };

            #endregion

            #region Client Prepare

            Client.OnPrepare = message =>
            {
                GlobalEventManager.Emit("onConnectionStatus", new GlobalEvent("Preparing World..."));
                Thread.Sleep(100);

                _clientData.World.ApplyWorldInitData((WorldDataMessage)message);
                _worldRenderer.Init(_clientData.World, ContentManager, GraphicsDeviceManager.GraphicsDevice);
            };

            #endregion

            #region Client Ready

            Client.OnReady = () =>
            {
                GlobalEventManager.Emit("onConnectionStatus", new GlobalEvent("Ready..."));
            };

            Client.On("PlayerUIRequest", (m) =>
            {
                UIManager.StartLayer(m.Message, this);
            });

            #endregion

            #region Client Disconnect

            Client.OnDisconnect = (m) =>
            {
                UIManager.StartLayer(nameof(LoadingGameUI), this);
                GlobalEventManager.Emit("onConnectionStatus", new GlobalEvent("Disconnected By Server"));
                AudioManager.StopSong();
            };

            #endregion

            /**
             * Custom Socket Routes
             */
            #region Handle Entity Disconnect

            Client.On("/player/disconnect", message =>
            {
                //Remove entity who disconnected
                lock (_lock)
                {
                    _clientData.Entities.Remove(message.Message);
                }
            });

            #endregion

            #region Handle Entity Updates

            Client.On("/players/updates", message =>
            {
                EntityUpdateMessage m = (EntityUpdateMessage)message;

                lock (_lock)
                {
                    foreach (EntityMessage em in m.Updates)
                    {
                        if (_clientData.Entities.ContainsKey(em.EntityId))
                        {
                            _clientData.Entities[em.EntityId].Position       = em.Position;
                            _clientData.Entities[em.EntityId].Velocity       = em.Velocity;
                            _clientData.Entities[em.EntityId].Rotation       = em.Rotation;
                            _clientData.Entities[em.EntityId].AnimationState = em.AnimationState;
                            _clientData.Entities[em.EntityId].Facing         = em.Facing;
                            _clientData.Entities[em.EntityId].Meta           = em.Meta;

                            if (em.EntityId == Client.ClientId)
                            {
                                _clientData.Client          = _clientData.Entities[em.EntityId];
                                _worldRenderer.Camera.Focus = _clientData.Entities[em.EntityId].OriginPosition.ToVector2();
                            }
                        }
                        else
                        {
                            _clientData.Entities.Add(em.EntityId, ClientEntity.ClientEntityFactory(em, ContentManager));
                        }
                    }
                }
            });

            #endregion

            #region Handle Chunk Updates

            Client.On("/players/chunkUpdates", message =>
            {
                _clientData.World.ApplyChunkUpdateMessage((ChunkUpdatesMessage)message);
            });

            #endregion

            #region HandleInventoryUpdates

            Client.On("/players/inventoryUpdates", message =>
            {
                _clientData.Inventory.ApplySlotUpdates((InventorySlotMessage)message);
            });

            #endregion

            #region KeyEventListener

            AddInputListener("keypress", e =>
            {
                if (e.Keyboard.KeyEnum == Keys.M)
                {
                    UIManager.StartLayer(nameof(MonsterMenuUI), this);
                }
                else if (e.Keyboard.KeyEnum == Keys.F3)
                {
                    _worldRenderer.Debug = !_worldRenderer.Debug;
                }
            });

            #endregion

            #region ScrollEventListener

            AddInputListener("mousescroll", e =>
            {
                if (e.Keyboard.KeyboardState.IsKeyDown(Keys.LeftControl))
                {
                    _worldRenderer.Camera.ApplyZoom(e.Mouse.VerticalScrollAmount / 1000f);
                }
            });

            #endregion

            Client.Ip   = ConnectionSettings.Ip;
            Client.Port = ConnectionSettings.Port;

            if (!Client.Connect())
            {
                GlobalEventManager.Emit("onConnectionStatus", new GlobalEvent("Connection Failed"));
            }
        }