示例#1
0
        private void LoadPool()
        {
            //Create the empty pool
            _pool = new Pool<Ball>();

            //Preload balls
            for (int i = 0; i < _maxBalls; i++)
            {
                Ball ball = new Ball();
                ball.Load(ScreenManager.GraphicsDevice, PhysicsSimulator);
                ball.Geom.Tag = ball;

                _pool.Insert(ball);
            }
        }
示例#2
0
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            if (IsActive)
            {
                while (_ballsToDraw.Count < _maxBalls)
                {
                    _stopWatch.Reset();
                    _stopWatch.Start();

                    if (_usePool)
                    {
                        Ball ball = _pool.Fetch();

                        //We need to reset the dynamics of the body.
                        ball.Body.ResetDynamics();

                        ball.Body.Position = new Vector2(Calculator.RandomNumber(50, ScreenManager.ScreenWidth - 50),
                                                         Calculator.RandomNumber(50, ScreenManager.ScreenHeight - 50));
                        ball.Geom.OnCollision += OnCollision;

                        //Reactivate the body
                        ball.Geom.Body.Enabled = true;

                        _ballsToDraw.Add(ball);
                    }
                    else
                    {
                        Ball ball = new Ball();
                        ball.Load(ScreenManager.GraphicsDevice, PhysicsSimulator);
                        ball.Body.Position = new Vector2(Calculator.RandomNumber(50, ScreenManager.ScreenWidth - 50),
                                                         Calculator.RandomNumber(50, ScreenManager.ScreenHeight - 50));
                        ball.Geom.OnCollision += OnCollision;
                        ball.Geom.Tag = ball;
                        _ballsToDraw.Add(ball);
                    }

                    _stopWatch.Stop();

                }
            }
        }