Ejemplo n.º 1
0
    protected virtual void Awake()
    {
        _anim               = GetComponent <Animator>();
        _isAlive            = true;
        _movementController = GetComponent <MovementController>();
        _render             = GetComponent <SpriteRenderer>();
        _collider           = GetComponent <Collider2D>();
        OffscreenDetector detector = GetComponent <OffscreenDetector>();

        detector.onOut       = onOffscreen;
        detector.onBottomOut = onBottomOut;
        detector.onTopOut    = onTopOut;
        detector.onLeftOut   = onLeftOut;
        detector.onRightOut  = onRight;
    }
Ejemplo n.º 2
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;
                                }
                            }
                        }
                    }
                }
            }
        }