示例#1
0
        public void NotifyCollision(MarvinsArena.Robot.Core.Collision collision)
        {
            if (!IsActive)
            {
                return;
            }

            messageExchangeFromHost.Collisions.Add(collision);
        }
示例#2
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            double        time = gameTime.ElapsedGameTime.TotalMilliseconds;
            KeyboardState currentKeyboardState = Keyboard.GetState();
            MouseState    currentMouseState    = Mouse.GetState();

            if (currentKeyboardState.IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            if (hud.StatusMessageActive && (currentKeyboardState.IsKeyDown(Keys.Space) || currentKeyboardState.IsKeyDown(Keys.Enter)))
            {
                hud.StatusMessageActive = false;

                var teamsExit = from robot in robots
                                group robot by((GameObjectRobot)robot.GameObject).Team into robotGroup
                                select robotGroup.Key;

                int teamsCountExit = teamsExit.Count();
                if (round == Program.Tournament.Rules.Rounds && (robots.Count < 2 || teamsCountExit < 2))
                {
                    Exit();
                }
            }

            if (!IsActive || hud.StatusMessageActive)
            {
                return;
            }

            // -- Game is active --------------------------------------------------------
            if (currentMouseState.LeftButton == ButtonState.Released)
            {
                leftMouseButtonDown = false;
            }

            if (currentMouseState.LeftButton == ButtonState.Pressed && leftMouseButtonDown == false)
            {
                cameraTargetIndex++;
                if (cameraTargetIndex >= robots.Count)
                {
                    camera = new Vector2(cameraScreen.X / 2 - (map.MapWidth + 1) / 2 * map.MapScale,
                                         cameraScreen.Y / 2 - (map.MapHeight + 1) / 2 * map.MapScale);

                    cameraTargetIndex = -1;
                }

                leftMouseButtonDown = true;
            }

            if (currentKeyboardState.IsKeyDown(Keys.Left))
            {
                camera.X++;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Right))
            {
                camera.X--;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Up))
            {
                camera.Y++;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Down))
            {
                camera.Y--;
            }

            #region Move objects
            // Move robots and execute plugin
            foreach (WorldObject obj in robots)
            {
                obj.Update(time);
            }

            // Move missile and bullets
            foreach (WorldObject obj in projectiles)
            {
                if (obj is WorldObjectMissile)
                {
                    obj.Update(time);
                }
                else if (obj is WorldObjectBullet)
                {
                    obj.Update(time);
                }
            }
            #endregion

            #region Assign camera to target
            if (cameraTargetIndex != -1 && cameraTargetIndex < robots.Count)
            {
                camera.X = cameraScreen.X / 2 - robots[cameraTargetIndex].PositionX;
                camera.Y = cameraScreen.Y / 2 - robots[cameraTargetIndex].PositionY;
            }
            #endregion

            #region Collision detection
            // Collision: robot vs Walls
            foreach (WorldObject robot1 in robots)
            {
                if (map.CheckCollision(robot1))
                {
                    MarvinsArena.Robot.Core.Collision collision1 =
                        new MarvinsArena.Robot.Core.Collision("Wall", 1,
                                                              MarvinsArena.Robot.Core.MessageExchangeType.Wall
                                                              );
                    ((GameObjectRobot)robot1.GameObject).NotifyCollision(collision1);
                    // Add status message to HUD
                    hud.AddMessage(String.Format("{0} hit a wall.", robot1.GameObject.FullName));
                }
            }

            // Collision: Projectile vs Walls
            foreach (WorldObject projectile1 in projectiles)
            {
                if (map.CheckCollision(projectile1))
                {
                    projectile1.Dead = true;
                }
            }

            // Collision: robot vs robot and SCAN
            foreach (WorldObject robot1 in robots)
            {
                foreach (WorldObject robot2 in robots)
                {
                    if (robot1 == robot2)
                    {
                        continue;
                    }

                    if (robot1.CheckForCollisions(robot2))
                    {
                        MarvinsArena.Robot.Core.Collision collision1 =
                            new MarvinsArena.Robot.Core.Collision(
                                robot2.GameObject.FullName, 1,
                                MarvinsArena.Robot.Core.MessageExchangeType.Robot
                                );
                        ((GameObjectRobot)robot1.GameObject).NotifyCollision(collision1);

                        MarvinsArena.Robot.Core.Collision collision2 =
                            new MarvinsArena.Robot.Core.Collision(
                                robot1.GameObject.FullName, 1,
                                MarvinsArena.Robot.Core.MessageExchangeType.Robot
                                );
                        ((GameObjectRobot)robot2.GameObject).NotifyCollision(collision2);

                        // Add status message to HUD
                        hud.AddMessage(String.Format("{0} bumped {1}", robot1.GameObject.FullName, robot2.GameObject.FullName));
                    }

                    if (GameObject.ObjectInScannerRange(((GameObjectRobot)robot1.GameObject), robot2.GameObject))
                    {
                        MarvinsArena.Robot.Core.ScannerTarget target =
                            new MarvinsArena.Robot.Core.ScannerTarget(
                                robot2.PositionX, robot2.PositionY, robot2.GameObject.FullName,
                                MarvinsArena.Robot.Core.MessageExchangeType.Robot
                                );
                        ((GameObjectRobot)robot1.GameObject).NotifyTarget(target);
                    }
                }
            }

            // Collision: robot vs Projectile
            foreach (WorldObject robot1 in robots)
            {
                foreach (WorldObject projectile1 in projectiles)
                {
                    if (!projectile1.Dead && robot1.CheckForCollisions(projectile1))
                    {
                        MarvinsArena.Robot.Core.Collision collision;
                        if (projectile1 is WorldObjectBullet)
                        {
                            collision = new MarvinsArena.Robot.Core.Collision(
                                projectile1.GameObject.Name,
                                ((GameObjectBullet)projectile1.GameObject).Power,
                                MarvinsArena.Robot.Core.MessageExchangeType.Bullet
                                );
                        }
                        else                         //if (projectile1 is WorldObjectMissile)
                        {
                            collision = new MarvinsArena.Robot.Core.Collision(
                                projectile1.GameObject.Name,
                                ((GameObjectMissile)projectile1.GameObject).Power,
                                MarvinsArena.Robot.Core.MessageExchangeType.Missile
                                );
                        }
                        ((GameObjectRobot)robot1.GameObject).NotifyCollision(collision);
                        projectile1.Dead = true;

                        // Add status message to HUD
                        hud.AddMessage(robot1.GameObject.FullName + " was hit by a " +
                                       collision.Source + " and got " + collision.Damage + " damage");
                    }
                }
            }

            // Collision: Projectile vs Projectile
            foreach (WorldObject projectile1 in projectiles)
            {
                foreach (WorldObject projectile2 in projectiles)
                {
                    if (projectile1 == projectile2 || projectile1.Dead || projectile2.Dead)
                    {
                        continue;
                    }

                    if (projectile1.CheckForCollisions(projectile2))
                    {
                        projectile1.Dead = true;
                        projectile2.Dead = true;
                    }
                }
            }

            projectiles.RemoveAll(obj => obj.Dead == true);

            foreach (WorldObject robot in robots)
            {
                ((GameObjectRobot)robot.GameObject).Notify();
            }

            var deadrobotlist = from robot in robots
                                where ((GameObjectRobot)robot.GameObject).Hitpoints < 1
                                select robot;
            foreach (WorldObject t in deadrobotlist)
            {
                hud.AddMessage(String.Format("{0} died.", t.GameObject.FullName), true);
            }
            robots.RemoveAll(obj => ((GameObjectRobot)obj.GameObject).Hitpoints < 1);

            var disabledrobotlist = from robot in robots
                                    where ((GameObjectRobot)robot.GameObject).IsActive == false
                                    select robot;
            foreach (WorldObject t in disabledrobotlist)
            {
                hud.AddMessage(String.Format("{0} disabled: {1}", t.GameObject.FullName, ((GameObjectRobot)t.GameObject).DisabledMessage), true);
            }
            robots.RemoveAll(obj => ((GameObjectRobot)obj.GameObject).IsActive == false);
            #endregion

            #region Determine winner, save tournament and inform hud
            var teams = from robot in robots
                        group robot by((GameObjectRobot)robot.GameObject).Team into robotGroup
                        select robotGroup.Key;

            int teamsCount = teams.Count();

            if (robots.Count == 0 || teamsCount == 0)
            {
                hud.StatusMessage = "Draw!";
            }
            else if (robots.Count == 1 || teamsCount == 1)
            {
                hud.StatusMessage = robots[0].GameObject.FullName + " won the round!";
                scoreKeeper.AddScore(((GameObjectRobot)robots[0].GameObject).Team.ToString());
            }

            // Check for next round or game ist over
            if (robots.Count < 2 || teamsCount < 2)
            {
                if (round == Program.Tournament.Rules.Rounds)
                {
                    List <string> names = scoreKeeper.GetHighscoreName().ToList();
                    if (names.Count() > 1)
                    {
                        hud.StatusMessage = "Game ended with Draw!";
                    }
                    else
                    {
                        int winnerTeam = Convert.ToInt32(names[0]);
                        hud.StatusMessage = "Team " + winnerTeam + " won the game!\n";
                        foreach (KeyValuePair <string, int> score in scoreKeeper.GetAllScores())
                        {
                            hud.StatusMessage += (String.Format("\nTeam {0} has {1,3} Point{2}", score.Key, score.Value, score.Value != 1 ? "s" : ""));
                        }
                        Program.Tournament.Bracket.SetWinner(winnerTeam);
                        Program.Tournament.SaveToXml();
                    }
                }
                else
                {
                    InitializeRound();                     // Start new round
                }
            }
            #endregion

            // Update hud
            hud.Update();

            // Base update
            base.Update(gameTime);
        }