Ejemplo n.º 1
0
        private void RunUniverse()
        {
            try
            {
                var frameTime = GameTime.Now();

                while (!_isDisposed)
                {
                    var startTime = DateTime.Now;

                    lock (_mutex)
                    {
                        foreach (var channel in _channels)
                        {
                            while (true)
                            {
                                var playerEvent = channel.TakePlayerEvent();
                                if (playerEvent == null)
                                {
                                    break;
                                }
                                HandlePlayerEvent(channel, playerEvent);
                            }
                        }

                        var elapsedDuration = GameTime.Update(ref frameTime);
                        foreach (var entity in _entities)
                        {
                            if (entity.PositionData == null)
                            {
                                continue;
                            }

                            if (entity.IsAiControlled)
                            {
                                Ai.Control(_mod.PhysicsValues, elapsedDuration, entity, _entities.FindAll(e => !e.Equals(entity)));
                            }

                            Movement.MoveEntity(
                                _mod.PhysicsValues,
                                _startWorld,
                                entity.PositionData,
                                elapsedDuration,
                                entity.PositionData.Velocity);
                        }

                        foreach (var channel in _channels)
                        {
                            if (channel.NeedsGameEvent())
                            {
                                var gameEvent = GetCurrentGameEvent(channel);
                                if (!channel.HasSentInitialValues)
                                {
                                    channel.HasSentInitialValues = true;
                                    gameEvent.PhysicsValues      = _mod.PhysicsValues;
                                }

                                channel.QueueGameEvent(gameEvent);
                            }
                        }
                    }

                    var endTime = DateTime.Now;

                    double sleepMillis = 10 - (endTime - startTime).TotalMilliseconds;
                    if (sleepMillis > 0)
                    {
                        Thread.Sleep((int)Math.Ceiling(sleepMillis));
                    }
                }
            }
            catch
            {
            }
        }
Ejemplo n.º 2
0
        private void RunUniverse()
        {
            try
            {
                var frameTime = GameTime.Now();

                while (!_isDisposed)
                {
                    var startTime = DateTime.Now;

                    lock (_mutex)
                    {
                        foreach (var channel in _channels)
                        {
                            while (true)
                            {
                                var playerEvent = channel.TakePlayerEvent();
                                if (playerEvent == null)
                                {
                                    break;
                                }
                                HandlePlayerEvent(channel, playerEvent);
                            }
                        }

                        var elapsedDuration = GameTime.Update(ref frameTime);
                        foreach (var entity in Universe.GetEntitiesWithComponent <PositionComponent>())
                        {
                            if (entity.Has <AiComponent>())
                            {
                                Ai.Control(Mod.PhysicsValues, elapsedDuration, entity);
                            }

                            var position = entity.Get <PositionComponent>();

                            Movement.MoveEntity(
                                Mod.PhysicsValues,
                                Universe.StartWorld,
                                position,
                                elapsedDuration,
                                position.Velocity);

                            entity.Set(position);
                        }

                        foreach (var channel in _channels)
                        {
                            if (channel.NeedsGameEvent())
                            {
                                var gameEvent = GetCurrentGameEvent(channel);
                                if (!channel.HasSentInitialValues)
                                {
                                    channel.HasSentInitialValues = true;
                                    gameEvent.PhysicsValues      = Mod.PhysicsValues;
                                }

                                channel.QueueGameEvent(gameEvent);
                            }
                        }
                    }

                    var endTime = DateTime.Now;

                    double sleepMillis = 10 - (endTime - startTime).TotalMilliseconds;
                    if (sleepMillis > 0)
                    {
                        Thread.Sleep((int)Math.Ceiling(sleepMillis));
                    }
                }
            }
            catch
            {
            }
        }