Example #1
0
        //Moves the enemy buy 1 unit down and 1 unit left (1 line)
        public void Move()
        {
            if (inFiringRange)
            {
                this.castle.takeDamage(1);

                return;
            }

            Rectangle tempLocation = this.Location;


            if (this.castle == null)
            {
                this.location = new Rectangle(this.location.X + 1, this.location.Y + 1, this.location.Width, this.location.Height);

                return;
            }

            Rectangle up    = new Rectangle(this.location.X, this.location.Y - 1, this.location.Width, this.location.Height);
            Rectangle down  = new Rectangle(this.location.X, this.location.Y + 1, this.location.Width, this.location.Height);
            Rectangle left  = new Rectangle(this.location.X - 1, this.location.Y, this.location.Width, this.location.Height);
            Rectangle right = new Rectangle(this.location.X + 1, this.location.Y, this.location.Width, this.location.Height);

            bool conflictUp    = map.isConflicting(up);
            bool conflictDown  = map.isConflicting(down);
            bool conflictLeft  = map.isConflicting(left);
            bool conflictRight = map.isConflicting(right);

            if (!conflictDown && this.location.Y < this.castle.Location.Y)
            {
                this.location = down;
            }

            else if (!conflictUp && this.location.Y > this.castle.Location.Y)
            {
                this.location = up;
            }


            else if (!conflictRight && this.location.X < this.castle.Location.X)
            {
                this.location = right;
            }
            else if (!conflictLeft && this.location.X > this.castle.Location.X)
            {
                this.location = left;
            }
            else if (this.location.X == this.castle.Location.X)
            {
                if (!conflictRight)
                {
                    this.location = right;
                }
                else if (!conflictLeft)
                {
                    this.location = left;
                }

                else if (!conflictDown)
                {
                    this.location = down;
                }
                else if (!conflictUp)
                {
                    this.location = up;
                }
            }
            else if (this.location.Y == this.castle.Location.Y)
            {
                if (!conflictDown)
                {
                    this.location = down;
                }
                else if (!conflictUp)
                {
                    this.location = up;
                }
                else if (!conflictRight)
                {
                    this.location = right;
                }
                else if (!conflictLeft)
                {
                    this.location = left;
                }
            }
            else if (conflictDown && conflictUp && conflictLeft && !conflictRight)
            {
                this.location = right;
            }
            else if (conflictDown && conflictUp && !conflictLeft && conflictRight)
            {
                this.location = left;
            }
            else if (conflictDown && !conflictUp && conflictLeft && conflictRight)
            {
                this.location = up;
            }
            else if (!conflictDown && conflictUp && conflictLeft && conflictRight)
            {
                this.location = down;
            }



            if (this.Location.Intersects(this.castle.Location))
            {
                this.Location      = tempLocation;
                this.inFiringRange = true;
            }
        }