Exemple #1
0
        public void Update(List <PlayerAction> playerActions)
        {
            //restart game if only 1 active player
            int activePlayers = 0;

            foreach (Player player in Players.Values)
            {
                if (player.Health > 0 && player.Ammo > 0)
                {
                    activePlayers++;
                }
            }

            if (activePlayers == 1)
            {
                StartNewGame(playerActions);
                return;
            }

            //AI players take their turn here
            Player[] players = new Player[Players.Count];
            Players.Values.CopyTo(players, 0);

            foreach (Player player in Players.Values)
            {
                if (player is AIPlayer)
                {
                    ((AIPlayer)player).DoTurnAction(players);

                    foreach (PlayerAction action in playerActions)
                    {
                        if (action.PlayerID == player.ID)
                        {
                            action.Action = ((AIPlayer)player).Action;
                        }
                    }
                }
            }

            foreach (PlayerAction playerAction in playerActions)
            {
                Player player = Players[playerAction.PlayerID];
                if (player.Health > 0)
                {
                    //set velocities on players, etc.
                    switch (playerAction.Action)
                    {
                    case MessageConstants.PlayerActionDown:
                        player.Velocity = new PointF(0, 4);
                        break;

                    case MessageConstants.PlayerActionLeft:
                        player.Velocity = new PointF(-4, 0);
                        break;

                    case MessageConstants.PlayerActionRight:
                        player.Velocity = new PointF(4, 0);
                        break;

                    case MessageConstants.PlayerActionUp:
                        player.Velocity = new PointF(0, -4);
                        break;

                    case MessageConstants.PlayerActionRotateLeft:
                        player.RotateLeft();
                        break;

                    case MessageConstants.PlayerActionRotateRight:
                        player.RotateRight();
                        break;

                    case MessageConstants.PlayerActionShieldBack:
                        player.MoveShieldToBack();
                        break;

                    case MessageConstants.PlayerActionShieldFront:
                        player.MoveShieldToFront();
                        break;

                    case MessageConstants.PlayerActionShieldLeft:
                        player.MoveShieldToLeft();
                        break;

                    case MessageConstants.PlayerActionShieldRight:
                        player.MoveShieldToRight();
                        break;

                    case MessageConstants.PlayerActionShoot:
                        if (player.Ammo > 0)
                        {
                            PointF velocity = new PointF(0, 0);
                            PointF location = new PointF(0, 0);

                            switch (player.FacingDirection)
                            {
                            case FacingDirectionType.North:
                                velocity = new PointF(0, -4);
                                location = new PointF(player.Location.X + (DimensionsUtil.GetAvatarWidth() / 2) - (DimensionsUtil.GetPaintballWidth() / 2), Players[playerAction.PlayerID].Location.Y - DimensionsUtil.GetGunHeight() - DimensionsUtil.GetPaintballOffset());
                                break;

                            case FacingDirectionType.South:
                                velocity = new PointF(0, 4);
                                location = new PointF(player.Location.X + (DimensionsUtil.GetAvatarWidth() / 2) - (DimensionsUtil.GetPaintballWidth() / 2), Players[playerAction.PlayerID].Location.Y + DimensionsUtil.GetPaintballOffset() + DimensionsUtil.GetAvatarHeight());
                                break;

                            case FacingDirectionType.East:
                                velocity = new PointF(4, 0);
                                location = new PointF(player.Location.X + DimensionsUtil.GetPaintballOffset() + DimensionsUtil.GetAvatarWidth(), Players[playerAction.PlayerID].Location.Y + (DimensionsUtil.GetAvatarHeight() / 2) - (DimensionsUtil.GetPaintballHeight() / 2));
                                break;

                            case FacingDirectionType.West:
                                velocity = new PointF(-4, 0);
                                location = new PointF(player.Location.X - DimensionsUtil.GetPaintballOffset(), Players[playerAction.PlayerID].Location.Y + (DimensionsUtil.GetAvatarHeight() / 2) - (DimensionsUtil.GetPaintballHeight() / 2));
                                break;
                            }

                            Paintballs.Add(new Paintball(location, velocity));
                            player.RecordPaintballFired();
                        }
                        break;

                    case MessageConstants.PlayerActionPowerShoot:

                        break;

                    case MessageConstants.PlayerActionNone:

                        break;
                    }
                }
            }


            //move all objects
            foreach (Player player in Players.Values)
            {
                player.Move();
                player.Velocity = new PointF(0, 0);
            }

            foreach (Paintball paintball in Paintballs)
            {
                paintball.Move();
            }

            List <PaintballHit> paintballHitsToRemove = new List <PaintballHit>();

            foreach (PaintballHit ph in PaintballHits)
            {
                if (ph.ShouldRemove)
                {
                    paintballHitsToRemove.Add(ph);
                }

                ph.RenderTimes++;
            }

            foreach (PaintballHit ph in paintballHitsToRemove)
            {
                PaintballHits.Remove(ph);
            }

            //detect and record collisions between paintballs and players
            //issue [B.2.6] of the design document
            Shield shield         = null;
            PointF shieldLocation = new PointF(0, 0);
            SizeF  shieldSize     = new SizeF(0, 0);
            bool   wasCollision   = false;

            foreach (Paintball paintball in Paintballs)
            {
                wasCollision = false;

                foreach (Player player in Players.Values)
                {
                    if (player.Health > 0)
                    {
                        switch (player.ShieldLocation)
                        {
                        case ShieldLocationType.Front:
                            shieldLocation = new PointF(player.Location.X, player.Location.Y - DimensionsUtil.GetAvatarShieldOffset());
                            shieldSize     = new SizeF(DimensionsUtil.GetAvatarWidth(), 1);
                            break;

                        case ShieldLocationType.Back:
                            shieldLocation = new PointF(player.Location.X, player.Location.Y + DimensionsUtil.GetAvatarHeight() + DimensionsUtil.GetAvatarShieldOffset());
                            shieldSize     = new SizeF(DimensionsUtil.GetAvatarWidth(), 1);
                            break;

                        case ShieldLocationType.Left:
                            shieldLocation = new PointF(player.Location.X - DimensionsUtil.GetAvatarShieldOffset(), player.Location.Y);
                            shieldSize     = new SizeF(1, DimensionsUtil.GetAvatarHeight());
                            break;

                        case ShieldLocationType.Right:
                            shieldLocation = new PointF(player.Location.X + DimensionsUtil.GetAvatarWidth() + DimensionsUtil.GetAvatarShieldOffset(), player.Location.Y);
                            shieldSize     = new SizeF(1, DimensionsUtil.GetAvatarHeight());
                            break;
                        }

                        shield = new Shield(shieldLocation, shieldSize);
                        if (CollisionDetector.Collision(paintball, shield))
                        {
                            wasCollision = true;
                            _paintballsToRemove.Add(paintball);
                        }
                        else if (CollisionDetector.Collision(paintball, player))
                        {
                            wasCollision = true;
                            player.RecordPaintballHit();
                            _paintballsToRemove.Add(paintball);
                            PaintballHits.Add(new PaintballHit(paintball.Location, 0));
                        }

                        if (wasCollision)
                        {
                            break;
                        }
                    }
                }

                if (!wasCollision)
                {
                    foreach (Obstacle obstacle in Obstacles)
                    {
                        if (CollisionDetector.Collision(paintball, obstacle))
                        {
                            wasCollision = true;
                            _paintballsToRemove.Add(paintball);
                        }
                    }
                }

                if (!wasCollision)
                {
                    if (OffscreenDetector.OffScreen(paintball))
                    {
                        _paintballsToRemove.Add(paintball);
                    }
                }
            }

            //remove paintballs that hit players, obstacles or went offscreen
            foreach (Paintball paintball in _paintballsToRemove)
            {
                Paintballs.Remove(paintball);
            }
            _paintballsToRemove.Clear();


            //don't allow players to move over other players or over obstacles, or off-screen
            //issue [B.2.5] of the design document
            bool wasUndoed = false;

            foreach (Player player in Players.Values)
            {
                wasUndoed = false;
                if (OffscreenDetector.OffScreen(player))
                {
                    player.UndoMove();
                }
                else
                {
                    foreach (Obstacle obstacle in Obstacles)
                    {
                        if (CollisionDetector.Collision(player, obstacle))
                        {
                            wasUndoed = true;
                            player.UndoMove();
                        }

                        if (wasUndoed)
                        {
                            break;
                        }
                    }

                    if (!wasUndoed)
                    {
                        foreach (Player playerB in Players.Values)
                        {
                            if (playerB.Health > 0) //issue [B.2.7] of the design document
                            {
                                if (CollisionDetector.Collision(player, playerB) && player.ID != playerB.ID)
                                {
                                    wasUndoed = true;
                                    player.UndoMove();
                                }

                                if (wasUndoed)
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// issue [A.1.5] of the design document
        /// </summary>
        /// <param name="graphics"></param>
        public void Render(Graphics graphics)
        {
            Pen pen = new Pen(TeamColor);

            int   healthMeterX         = (int)Location.X + (int)(DimensionsUtil.GetAvatarWidth() - 20);
            int   healthMeterY         = (int)Location.Y + 20;
            float healthMeterBarHeight = ((float)Health / (float)_maxHealth) * DimensionsUtil.GetHealthMeterHeight();

            int        ammoMeterX         = (int)Location.X + 10;
            int        ammoMeterY         = (int)Location.Y + 20;
            float      ammoMeterBarHeight = ((float)Ammo / (float)_maxAmmo) * DimensionsUtil.GetAmmoMeterHeight();
            Color      healthMeterColor   = MeterUtil.GetColorFromValue(Health);
            SolidBrush healthMeterBrush   = new SolidBrush(healthMeterColor);

            Color      ammoMeterColor = MeterUtil.GetColorFromValue(Ammo);
            SolidBrush ammoMeterBrush = new SolidBrush(ammoMeterColor);

            GraphicsPath graphicsPath       = new GraphicsPath();
            RectangleF   avatarMainBody     = new RectangleF(Location, Size);
            RectangleF   avatarGun          = new RectangleF(Location.X + ((Size.Width / 2) - (DimensionsUtil.GetGunWidth() / 2)), Location.Y - DimensionsUtil.GetGunHeight(), DimensionsUtil.GetGunWidth(), DimensionsUtil.GetGunHeight());
            RectangleF   healthMeterOutline = new RectangleF(healthMeterX, healthMeterY, DimensionsUtil.GetHealthMeterWidth(), DimensionsUtil.GetHealthMeterHeight());
            RectangleF   ammoMeterOutline   = new RectangleF(ammoMeterX, ammoMeterY, DimensionsUtil.GetAmmoMeterWidth(), DimensionsUtil.GetAmmoMeterHeight());

            graphicsPath.AddRectangle(avatarMainBody);
            graphicsPath.AddRectangle(avatarGun);
            graphicsPath.AddRectangle(healthMeterOutline);
            graphicsPath.AddRectangle(ammoMeterOutline);

            RectangleF healthMeterBar = new RectangleF(healthMeterX, healthMeterY + (DimensionsUtil.GetHealthMeterHeight() - healthMeterBarHeight), DimensionsUtil.GetHealthMeterWidth(), healthMeterBarHeight);
            RectangleF ammoMeterBar   = new RectangleF(ammoMeterX, ammoMeterY + DimensionsUtil.GetAmmoMeterHeight() - ammoMeterBarHeight, DimensionsUtil.GetAmmoMeterWidth(), ammoMeterBarHeight);

            GraphicsPath healthMeterBarPath = new GraphicsPath();

            healthMeterBarPath.AddRectangle(healthMeterBar);

            GraphicsPath ammoMeterBarPath = new GraphicsPath();

            ammoMeterBarPath.AddRectangle(ammoMeterBar);

            Matrix rotationMatrix = new Matrix(1, 0, 0, 1, 0, 0);
            PointF rotationPoint  = new PointF(avatarMainBody.Location.X + (DimensionsUtil.GetAvatarWidth() / 2), avatarMainBody.Location.Y + (DimensionsUtil.GetAvatarHeight() / 2));

            switch (FacingDirection)
            {
            case FacingDirectionType.North:
                break;

            case FacingDirectionType.East:
                rotationMatrix.RotateAt(90F, rotationPoint);
                break;

            case FacingDirectionType.South:
                rotationMatrix.RotateAt(180F, rotationPoint);
                break;

            case FacingDirectionType.West:
                rotationMatrix.RotateAt(270F, rotationPoint);
                break;
            }

            graphicsPath.Transform(rotationMatrix);
            ammoMeterBarPath.Transform(rotationMatrix);
            healthMeterBarPath.Transform(rotationMatrix);

            graphics.FillPath(healthMeterBrush, healthMeterBarPath);
            graphics.FillPath(ammoMeterBrush, ammoMeterBarPath);

            graphics.DrawPath(pen, graphicsPath);

            //render the shield, ignoring any rotations (rotations are ignored because it is easier for collision detection with the shield)
            switch (ShieldLocation)
            {
            case ShieldLocationType.Front:
                graphics.DrawLine(pen, Location.X, Location.Y - DimensionsUtil.GetAvatarShieldOffset(), Location.X + Size.Width, Location.Y - DimensionsUtil.GetAvatarShieldOffset());
                break;

            case ShieldLocationType.Back:
                graphics.DrawLine(pen, Location.X, Location.Y + DimensionsUtil.GetAvatarShieldOffset() + Size.Height, Location.X + Size.Width, Location.Y + DimensionsUtil.GetAvatarShieldOffset() + Size.Height);
                break;

            case ShieldLocationType.Left:
                graphics.DrawLine(pen, Location.X - DimensionsUtil.GetAvatarShieldOffset(), Location.Y, Location.X - DimensionsUtil.GetAvatarShieldOffset(), Location.Y + Size.Height);
                break;

            case ShieldLocationType.Right:
                graphics.DrawLine(pen, Location.X + Size.Width + DimensionsUtil.GetAvatarShieldOffset(), Location.Y, Location.X + Size.Width + DimensionsUtil.GetAvatarShieldOffset(), Location.Y + Size.Height);
                break;
            }
        }