Ejemplo n.º 1
0
 // we check whether this is a static, a moving object or our ship and we add it to the respective list of objects (containers).
 public virtual void AddObject(WorldObject obj)
 {
     if (obj is MovableObject)
     {
         this.MovingObjects.Add((MovableObject)obj);
         this.AllObjects.Add(obj);
     }
     else
     {
         this.StaticObjects.Add((StaticObject)obj);
         this.AllObjects.Add(obj);
     }
 }
        public static bool IsCollided(WorldObject first, WorldObject second, DirectionType direction)
        {
            int firstTop = first.TopLeft.Row;
            int firstLeft = first.TopLeft.Col;
            int firstHeight = first.GetImage().GetLength(0);
            int firstWidth = first.GetImage().GetLength(1);
            int firstBottom = first.TopLeft.Row + firstHeight;
            int firstRight = first.TopLeft.Col + firstWidth;

            int secondTop = second.TopLeft.Row;
            int secondLeft = second.TopLeft.Col;
            int secondHeight = second.GetImage().GetLength(0);
            int secondWidth = second.GetImage().GetLength(1);
            int secondBottom = second.TopLeft.Row + secondHeight;
            int secondRight = second.TopLeft.Col + secondWidth;

            bool horizontalMatch = false;
            if (firstHeight >= secondHeight)
            {
                if (secondTop >= firstTop && secondTop <= firstBottom ||
                    secondBottom >= firstTop && secondBottom <= firstBottom)
                {
                    horizontalMatch = true;
                }
            }
            else
            {
                if (firstTop >= secondTop && firstTop <= secondBottom ||
                    firstBottom >= secondTop && firstBottom <= secondBottom)
                {
                    horizontalMatch = true;
                }
            }
            bool verticalMatch = false;
            if (firstWidth >= secondWidth)
            {
                if (secondLeft >= firstLeft && secondLeft <= firstRight ||
                    secondRight >= firstLeft && secondRight <= firstRight)
                {
                    verticalMatch = true;
                }
            }
            else
            {
                if (firstLeft >= secondLeft && firstLeft <= secondRight ||
                    firstRight >= secondLeft && firstRight <= secondRight)
                {
                    verticalMatch = true;
                }
            }

            bool right = (firstRight >= secondLeft && firstRight < secondRight) && horizontalMatch;
            bool left = (firstLeft > secondLeft && firstLeft <= secondRight) && horizontalMatch;
            bool top = (firstTop > secondTop && firstTop <= secondBottom) && verticalMatch;
            bool bottom = (firstBottom >= secondTop && firstBottom <= secondBottom) && verticalMatch;

            switch (direction)
            {
                case DirectionType.Bottom: return bottom;
                case DirectionType.Left: return left;
                case DirectionType.Right: return right;
                case DirectionType.Top: return top;
                case DirectionType.All: return bottom || left || right || top;
                default: return false;
            }
        }
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();
            }
        }