Ejemplo n.º 1
0
        /// <summary>
        /// Running in separate thread and generate ball locations
        /// </summary>
        private void BeginInvokeGenerateLocation()
        {
            Thread t = new Thread(() =>
            {
                while (true)
                {
                    //generate vector if previous are 0
                    if (Convert.ToInt32(_velocityX) == 0 && Convert.ToInt32(_velocityY) == 0)
                    {
                        Log.Print("Demo generating ball kick!", eCategory.Info, LogTag.IMAGE);
                        Thread.Sleep(100);
                        _velocityX  = _random.Next(-10, 10);
                        _velocityX *= 4;
                        _velocityY  = _random.Next(-10, 10);
                        _velocityY *= 4;
                    }

                    playerTempX = _x + _velocityX;
                    playerTempY = _y + _velocityY;
                    foreach (eRod rodType in Enum.GetValues(typeof(eRod)))
                    {
                        Point point = Marks.PlayerPosition(rodType);
                        point       = TransformAgent.Data.InvertTransform(point);
                        if (playerTempX < point.X + deltaX && playerTempX > point.X - deltaX &&
                            playerTempY < point.Y + deltaY && playerTempY > point.Y - deltaY &&
                            _velocityX < 0)
                        {
                            if (random.Next(0, 100) > 30)
                            {
                                _x = Ricochet(_x, point.X, ref _velocityX, ref _velocityY);
                                Log.Print(String.Format("Rod [{0}] responding to the ball!", rodType.ToString()), eCategory.Info, LogTag.COMMON);
                            }
                        }
                    }

                    //check if we passed the border and set new X coordinate
                    double tempX = _x + _velocityX;
                    if (tempX <= _leftBorder)
                    {
                        _x = Ricochet(_x, _leftBorder, ref _velocityX, ref _velocityY);
                    }
                    else if (tempX >= _rightBorder)
                    {
                        _x = Ricochet(_x, _rightBorder, ref _velocityX, ref _velocityY);
                    }
                    else
                    {
                        _x = Convert.ToInt32(tempX);
                    }

                    //check if we passed the border and set new Y coordinate
                    double tempY = _y + _velocityY;
                    if (tempY <= _upeerBorder)
                    {
                        _y = Ricochet(_y, _upeerBorder, ref _velocityY, ref _velocityX);
                    }
                    else if (tempY >= _buttomBorder)
                    {
                        _y = Ricochet(_y, _buttomBorder, ref _velocityY, ref _velocityX);
                    }
                    else
                    {
                        _y = Convert.ToInt32(tempY);
                    }

                    //Sleep before next generation
                    Thread.Sleep(10);
                }
            });

            t.IsBackground = true;
            t.Start();
        }