Ejemplo n.º 1
0
 protected WorldObject(MatrixCoords topLeft, char[,] body)
 {
     this.TopLeft = topLeft;
     this.body = body;
     this.IsDestroyed = false;
     this.Life = InitialLife;
 }
        // this method calls a second method for a collision of moving andstatic onjects
        public static bool HandleCollisions(List<MovableObject> movingObjects, List<StaticObject> staticObjects)
        {
            //HandleMovingWithStaticCollisions(MovingObjects, StaticObjects);
            bool exitCollision = false;

            var movingObjectWithoutHero = new List<MovableObject>();

            int superHeroIndex = 0;
            int index = 0;
            foreach (var hero in movingObjects)
            {
                if (hero is SuperHero)
                {
                    superHeroIndex = index;
                }
                else
                {
                    movingObjectWithoutHero.Add(hero);
                }
                index++;
            }

            SuperHero superHero = movingObjects[superHeroIndex] as SuperHero;

            foreach (var movingObject in movingObjectWithoutHero)
            {
                if (IsCollided(superHero, movingObject, DirectionType.All))
                {
                    //Menu.MainMenu(superHero, movingObject);
                    Menu menu = new Menu(superHero, movingObject);
                    menu.ExitGame += new EventHandler(ShowExitMessage);
                    menu.ShowMenu();
                }
            }

            foreach (var staticObject in staticObjects)
            {
                if (IsCollided(superHero, staticObject, DirectionType.All))
                {
                    if ((staticObject is House) && superHero.Experience >= Program.LevelExitExperience)
                    {
                        Console.SetCursorPosition(50, 13);
                        Console.Write("YOU WON");
                        Console.SetCursorPosition(35, 14);
                        Console.Write("Press \"Enter\" to start next level");
                        while (true)
                        {
                            var keyInfo = Console.ReadKey();
                            if (keyInfo.Key.Equals(ConsoleKey.Enter))
                            {
                                exitCollision = true;
                                break;
                            }
                            while (Console.KeyAvailable)
                            {
                                Console.ReadKey();
                            }
                        }
                    }
                }
            }

            foreach (var moving in movingObjectWithoutHero)
            {
                foreach (var staticObject in staticObjects)
                {
                    MatrixCoords newSpeed = moving.Speed;
                    if (IsCollided(moving, staticObject, DirectionType.Top) ||
                        IsCollided(moving, staticObject, DirectionType.Bottom))
                    {
                        newSpeed.Row *= -1;
                    }
                    if (IsCollided(moving, staticObject, DirectionType.Left) ||
                        IsCollided(moving, staticObject, DirectionType.Right))
                    {
                        newSpeed.Col *= -1;
                    }
                    moving.Speed = newSpeed;
                }
            }

            for (int i = 0; i < movingObjectWithoutHero.Count; i++)
            {
                for (int j = i + 1; j < movingObjectWithoutHero.Count; j++)
                {
                    MovableObject obj1 = movingObjectWithoutHero[i];
                    MovableObject obj2 = movingObjectWithoutHero[j];

                    if (IsCollided(obj1, obj2, DirectionType.All))
                    {
                        MatrixCoords newSpeed = new MatrixCoords(obj1.Speed.Row, obj1.Speed.Col);
                        newSpeed.Row *= -1;
                        newSpeed.Col *= -1;
                        obj1.Speed = newSpeed;
                        newSpeed = new MatrixCoords(obj2.Speed.Row, obj2.Speed.Col);
                        newSpeed.Row *= -1;
                        newSpeed.Col *= -1;
                        obj2.Speed = newSpeed;
                    }
                }
            }

            return exitCollision;
        }
Ejemplo n.º 3
0
        public void ProcessInput(WorldObject hero, List<WorldObject> otherObjects)
        {
            if (!Console.KeyAvailable) return;

            bool collisionLeft = false;
            bool collisionRight = false;
            bool collisionTop = false;
            bool collisionBottom = false;
            foreach (var item in otherObjects)
            {
                if ((item is SuperHero) == false)
                {
                    if (CollisionDispatcher.IsCollided(hero, item, DirectionType.Left))
                    {
                        collisionLeft = true;
                    }
                    if (CollisionDispatcher.IsCollided(hero, item, DirectionType.Right))
                    {
                        collisionRight = true;
                    }
                    if (CollisionDispatcher.IsCollided(hero, item, DirectionType.Top))
                    {
                        collisionTop = true;
                    }
                    if (CollisionDispatcher.IsCollided(hero, item, DirectionType.Bottom))
                    {
                        collisionBottom = true;
                    }
                }
            }

            var keyInfo = Console.ReadKey();
            var oneLeft = new MatrixCoords(0, -2);
            var oneRight = new MatrixCoords(0, 2);
            var oneUp = new MatrixCoords(-1, 0);
            var oneDown = new MatrixCoords(1, 0);
            if (keyInfo.Key.Equals(ConsoleKey.LeftArrow))
            {
                if (hero.TopLeft.Col > 0 && !collisionLeft)
                {
                    hero.TopLeft = hero.TopLeft + oneLeft;
                }

            }
            else if (keyInfo.Key.Equals(ConsoleKey.RightArrow))
            {
                if ((hero.TopLeft.Col < (Program.ConsoleCols - hero.GetImage().GetLength(1) - 1)) && !collisionRight)
                {
                    hero.TopLeft = hero.TopLeft + oneRight;
                }

            }
            else if (keyInfo.Key.Equals(ConsoleKey.UpArrow))
            {
                if (hero.TopLeft.Row > 0 && !collisionTop)
                {
                    hero.TopLeft = hero.TopLeft + oneUp;
                }

            }
            else if (keyInfo.Key.Equals(ConsoleKey.DownArrow))
            {
                if ((hero.TopLeft.Row < (Program.ConsoleRows - hero.GetImage().GetLength(0))) && !collisionBottom)
                {
                    hero.TopLeft = hero.TopLeft + oneDown;
                }

            }
            else if (keyInfo.Key.Equals(ConsoleKey.Spacebar)) // shooting
            {

            }
            while (Console.KeyAvailable)
            {
                Console.ReadKey();
            }
        }