Exemplo n.º 1
0
        protected override void Update(GameTime gameTime)
        {
            //NetTime.Realtime = false;
            //NetTime.Advance(1000/60);

            float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            if (server != null)
            {
                server.Update();
            }


            KeyboardState keys  = Keyboard.GetState();
            MouseState    mouse = Mouse.GetState();

            controlVector.Thrust = (keys.IsKeyDown(Keys.W) ? 1.0f : 0f);
            controlVector.Torque = (keys.IsKeyDown(Keys.A) ? 1.0f : 0f) + (keys.IsKeyDown(Keys.D) ? -1.0f : 0f);
            controlVector.Firing = keys.IsKeyDown(Keys.Space); // Should be done as events.

            if (keys.IsKeyDown(Keys.Q) && !lastKeys.IsKeyDown(Keys.Q))
            {
                outgoingPool.AddEvent(new PlayerRequest(PlayerRequest.RequestType.FireMissile));
            }
            if (keys.IsKeyDown(Keys.E) && !lastKeys.IsKeyDown(Keys.E))
            {
                outgoingPool.AddEvent(new PlayerRequest(PlayerRequest.RequestType.FireMultiMissile));
            }

            tickCounter += delta;
            if (tickCounter >= 1f / 20)
            {
                tickCounter -= 1f / 20;
                outgoingPool.Synchronise();
            }

            client.Update();
            incomingPool.Synchronise();

            networkPlot.AddValue(client.Connection.Stats.BytesRecieved.PerSecond / 1024);


            lastKeys  = keys;
            lastMouse = mouse;
            base.Update(gameTime);
        }
Exemplo n.º 2
0
        public void Update(float elapsed)
        {
            foreach (Entity entity in Entities)
            {
                entity.Update(elapsed);

                if (entity is Unit unit)
                {
                    Grid.StaticCollide(unit);
                }
            }

            UDPFeed newFeed = Server.RecieveConnection();

            if (newFeed != null)
            {
                AddClient(new AirlockHostedClient(NetDefs, new NetworkClient(newFeed), WorldContent));
            }

            foreach (AirlockHostedClient client in Clients)
            {
                client.Update(elapsed);
            }
            Clients.OnReverse((client) =>
            {
                if (client.Network.State == NetworkClient.ConnectionState.Closed)
                {
                    DropClient(client);
                }
            });

            if (SyncTimer.IsElapsed(elapsed))
            {
                WorldContent.Synchronise();

                foreach (AirlockHostedClient client in Clients)
                {
                    // A second opportunity to update to get any pending packets out.
                    client.Network.Update();
                }
            }
        }
Exemplo n.º 3
0
        public void Update(float elapsed)
        {
            if (SyncTimer.IsElapsed(elapsed))
            {
                ClientContent.Synchronise();
            }
            Network.Update();
            ReturnContent.Synchronise();

            foreach (SyncHandle handle in ReturnContent.Handles)
            {
                if (handle.Updated)
                {
                    if (handle.Obj is LocalPlayer request)
                    {
                        Player.UpdateWithMotionRequest(request);
                    }
                }
            }
        }
Exemplo n.º 4
0
        public void Update(Camera camera, float elapsed)
        {
            if (SyncTimer.IsElapsed(elapsed))
            {
                ReturnContent.Synchronise();
            }

            HandleInputs(camera, elapsed);

            Network.Update();
            WorldContent.Synchronise();
            ClientContent.Synchronise();

            foreach (SyncHandle handle in WorldContent.NewHandles)
            {
                if (handle.Obj is MapRoom room)
                {
                    Grid.AddRoom(room);
                }
            }
            foreach (SyncHandle handle in WorldContent.RemovedHandles)
            {
                if (handle.Obj is MapRoom room)
                {
                    Grid.RemoveRoom(room);
                }
            }

            foreach (SyncHandle handle in ClientContent.NewHandles)
            {
                if (handle.Obj is ClientInfo info)
                {
                    ClientInfo = info;
                }
            }
        }
Exemplo n.º 5
0
        public void Update()
        {
            if (LastTimestamp == 0)
            {
                LastTimestamp = NetTime.Seconds() - 0.016;
                foreach (Physical phys in Physicals)
                {
                    phys.RequestMotionUpdate();
                }
            }

            double seconds = NetTime.Seconds();
            float  delta   = (float)(seconds - LastTimestamp);

            LastTimestamp = seconds;

            NetworkConnection connection = Server.RecieveConnection();

            if (connection != null)
            {
                NetworkClient client = new NetworkClient(connection);
                client.SetState(NetworkClient.ConnectionState.Open);
                client.Attach(OutgoingPool);
                IncomingSyncPool incoming = new IncomingSyncPool(NetDefs, 0);
                client.Attach(incoming);

                RemoteClient newClient = new RemoteClient()
                {
                    Client   = client,
                    Incoming = incoming,
                    Player   = null
                };

                Clients.Add(newClient);
            }
            UpdateEntitites(delta);

            syncCounter += delta;
            if (syncCounter >= TransmitRate)
            {
                syncCounter -= TransmitRate;

                OutgoingPool.Synchronise();
            }

            foreach (RemoteClient client in Clients)
            {
                client.Client.Update();
                client.Incoming.Synchronise();

                foreach (SyncEvent evt in client.Incoming.Events)
                {
                    if (evt.Obj is PlayerRequest request)
                    {
                        switch (request.Request)
                        {
                        case PlayerRequest.RequestType.FireMissile:
                            client.Player?.FireMissile();
                            break;

                        case PlayerRequest.RequestType.FireMultiMissile:
                            client.Player?.FireMultiMissile();
                            break;
                        }
                    }
                    evt.Clear();
                }

                foreach (SyncHandle handle in client.Incoming.Handles)
                {
                    if (handle.Obj is PlayerControl control)
                    {
                        if (control.Ready)
                        {
                            if (client.Player == null)
                            {
                                client.Player = NewPlayer(RandomNiceColor());
                                AddEntity(client.Player);
                                client.PlayerName = control.PlayerName;
                                serverReport.Clients.Add(control.PlayerName);
                                serverReport.Ships.Add(client.Player);
                            }
                            client.Player.Control(control.Thrust, control.Torque, control.Firing);
                        }

                        break;
                    }
                }
            }

            for (int i = Clients.Count - 1; i >= 0; i--)
            {
                RemoteClient client = Clients[i];
                if (client.Client.State == NetworkClient.ConnectionState.Closed)
                {
                    Clients.RemoveAt(i);
                    client.Client.Destroy();
                    if (client.Player != null)
                    {
                        RemoveEntity(client.Player);
                    }

                    int k = serverReport.Ships.IndexOf(client.Player);
                    serverReport.Ships.RemoveAt(k);
                    serverReport.Clients.RemoveAt(k);
                }
            }
        }