Exemple #1
0
        public void Update(GameTime gameTime)
        {
            var time = gameTime.ElapsedGameTime.TotalSeconds;

            _timeCurrent      += time;
            _lastComputeScore += time;

            if (_timeCurrent >= _timeGame)
            {
                StateManager.Instance.SetGameState(Enums.EnumGameState.Score, _lastScore);
            }

            _lastBallRed    += time;
            _lastBallBlue   += time;
            _lastBallGreen  += time;
            _lastBallYellow += time;

            var touches = TouchPanel.GetState().ToList();

            //touches.RemoveAll(x => x.Pressure == 0);
            foreach (var touch in touches)
            {
                //Move
                var team = GetTeam(touch.Position);

                if (team != EnumTeam.NONE)
                {
                    Color   color          = Color.Red;
                    Vector2 positionBall   = touch.Position;
                    Vector2 positionCenter = _positionRed;
                    double  lastBall       = _lastBallRed;

                    switch (team)
                    {
                    case EnumTeam.BLUE:
                        color          = Color.Blue;
                        positionCenter = _positionBlue;
                        lastBall       = _lastBallBlue;
                        break;

                    case EnumTeam.GREEN:
                        color          = Color.Green;
                        positionCenter = _positionGreen;
                        lastBall       = _lastBallGreen;
                        break;

                    case EnumTeam.YELLOW:
                        color          = Color.Yellow;
                        positionCenter = _positionYellow;
                        lastBall       = _lastBallYellow;
                        break;
                    }

                    if (lastBall > 0.1)
                    {
                        Vector2 direction = positionBall - positionCenter;
                        direction.Normalize();
                        float distanceThrow = Vector2.DistanceSquared(positionBall, positionCenter);

                        switch (team)
                        {
                        case EnumTeam.RED:
                            _lastBallRed   = 0;
                            distanceThrow /= 55;
                            break;

                        case EnumTeam.BLUE:
                            _lastBallBlue  = 0;
                            distanceThrow /= 30f;
                            break;

                        case EnumTeam.GREEN:
                            _lastBallGreen = 0;
                            distanceThrow /= 55;
                            break;

                        case EnumTeam.YELLOW:
                            _lastBallYellow = 0;
                            distanceThrow  /= 30f;
                            break;
                        }

                        _paintBalls.Add(new PaintBall()
                        {
                            Position = positionCenter,
                            Color    = color,
                            IsDrawed = false,
                            Height   = 32,
                            Width    = 32,
                            Time     = 1,
                            Velocity = direction * distanceThrow
                        });
                    }
                }
            }
            _touches = touches;

            //Update balls
            foreach (var ball in _paintBalls)
            {
                if (!ball.IsDrawed)
                {
                    ball.Time -= time;

                    if (ball.Time <= 0)
                    {
                        var textureSplashs = TextureManager.Instance.GetTexture("Textures/Splashs");

                        var rand = _random.Next(0, 10);

                        _mapManager.AddColor(
                            textureSplashs,
                            new Rectangle(rand * 64, 0, 64, 64),
                            new Rectangle((int)(ball.Position.X - 32), (int)(ball.Position.Y - 32), 64, 64),
                            ball.Color
                            );

                        ball.IsDrawed = true;
                    }

                    ball.Position += ball.Velocity * (float)time;
                }
            }

            _paintBalls.RemoveAll(x => x.IsDrawed);

            _mapManager.Update(time);

            if (_lastComputeScore > 0.2)
            {
                _lastScore        = _mapManager.ComputeScore(Color.Red, Color.Blue, Color.Green, Color.Yellow);
                _lastComputeScore = 0;
            }
        }