예제 #1
0
 public bool attack(character a)
 {
     if (a.isAlive)
     {
         if (a.damageby(this.hit_point(this.weapon), this.hit_prob))
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
     return(false);
 }
예제 #2
0
        public void random_move(character a, LinkedList<enemy> m)
        {
            approach_player = false;
               if (move_distance > 0)
               {
               move_distance--;
               switch (last_direction)
               {
                   case 1://left move
                       {
                           if (cell.X < 0)
                           {
                               cell.X = 0;
                               move_distance = 0;
                           }
                           else
                           {
                               for(int i=0;i<tree.Count;i++)//check trees
                               {
                                   //if(cell.X<tree.ElementAt(i).X+tree.ElementAt(i).Width && cell.Intersects(tree.ElementAt(i)))
                                   if(cell.Intersects(tree.ElementAt(i)) && object_direction(cell,tree.ElementAt(i))==1)
                                   {
                                       //cell.X=tree.ElementAt(i).X+tree.ElementAt(i).Width;
                                       move_distance=0;
                                       return;
                                   }
                               }
                               for (int i = 0; i < m.Count; i++)//check other monsters
                               {
                                   if (m.ElementAt(i).name != name || m.ElementAt(i).id != id)//exclude itself
                                   {
                                       if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell,m.ElementAt(i).cell) == 1)
                                       {
                                           move_distance = 0;
                                           return;
                                       }

                                   }
                               }
                               if (cell.Intersects(a.cell) && object_direction(cell,a.cell) == 1)//check players
                               {
                                   approach_player = true;
                                   cell.X -= 0;
                                   return;
                               }
                               cell.X -= speed;
                           }
                           break;
                       }
                   case 2://right move
                       if (cell.X > 1024 - 30)
                       {
                           cell.X = 1024 - 30;
                           move_distance = 0;
                       }
                       else
                       {
                           for (int i = 0; i < tree.Count; i++)
                           {
                               //if (cell.X+cell.Width > tree.ElementAt(i).X && cell.Intersects(tree.ElementAt(i)))
                               if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 2)
                               {
                                   //cell.X = tree.ElementAt(i).X-cell.Width;
                                   move_distance = 0;
                                   return;
                               }
                           }
                           for (int i = 0; i < m.Count; i++)//check other monsters
                           {
                               if (m.ElementAt(i).name != name || m.ElementAt(i).id != id)//exclude itself
                               {
                                   if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell,m.ElementAt(i).cell) == 2)
                                   {
                                       move_distance = 0;
                                       return;
                                   }

                               }
                           }
                           if (cell.Intersects(a.cell) && object_direction(cell, a.cell) == 2)
                           {
                               approach_player = true;
                               cell.X += 0;
                               return;
                           }
                           cell.X += speed;
                       }

                       break;
                   case 3://up move
                       if (cell.Y < 0)
                       {
                           cell.Y = 0;
                           move_distance = 0;
                       }
                       else
                       {
                           for (int i = 0; i < tree.Count; i++)
                           {
                               //if (cell.Y < tree.ElementAt(i).Y + tree.ElementAt(i).Height && cell.Intersects(tree.ElementAt(i)))
                               if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 3)
                               {
                                   //cell.Y = tree.ElementAt(i).Y + tree.ElementAt(i).Height;
                                   move_distance = 0;
                                   return;
                               }
                           }
                           for (int i = 0; i < m.Count; i++)//check other monsters
                           {
                               if (m.ElementAt(i).name != name || m.ElementAt(i).id != id)//exclude itself
                               {
                                   if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell, m.ElementAt(i).cell) == 3)
                                   {
                                       move_distance = 0;
                                       return;
                                   }

                               }
                           }
                           if (cell.Intersects(a.cell) && object_direction(cell, a.cell) == 3)
                           {
                               approach_player = true;
                               cell.Y -= 0;
                               return;
                           }
                           cell.Y -= speed;
                       }
                       break;
                   case 4://down move
                       if (cell.Y >= 768 / 4 * 3 - 40)
                       {
                           cell.Y = 768 / 4 * 3 - 40;
                           move_distance = 0;
                       }
                       else
                       {
                           for (int i = 0; i < tree.Count; i++)
                           {
                               //if (cell.Y +cell.Height> tree.ElementAt(i).Y && cell.Intersects(tree.ElementAt(i)))
                               if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 4)
                               {
                                   //cell.Y = tree.ElementAt(i).Y-cell.Height;
                                   move_distance = 0;
                                   return;
                               }
                           }
                           for (int i = 0; i < m.Count; i++)//check other monsters
                           {
                               if (m.ElementAt(i).name != name || m.ElementAt(i).id != id)//exclude itself
                               {
                                   if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell, m.ElementAt(i).cell) == 4)
                                   {
                                       move_distance = 0;
                                       return;
                                   }

                               }
                           }
                           if (cell.Intersects(a.cell) && object_direction(cell, a.cell) == 4)
                           {
                               approach_player = true;
                               cell.Y += 0;
                               return;
                           }
                           cell.Y += speed;
                       }
                       break;
               }
               }
               else
               {
               //finish_walking = true;
               move_distance = 10;
               last_direction = rand.Next(1, 5);
               if (last_direction == 1)
                   direction = -1;
               else if (last_direction == 2)
                   direction = 1;
               }
        }
예제 #3
0
        public void evading_move(character a,LinkedList<enemy> m)
        {
            approach_player = false;
            if(a.cell.X<cell.X)
            {
            if (cell.X >= 1024 - 30)
            {
                cell.X = 1024 - 30;

                if (a.cell.X < 1024 - 30)
                {
                    if (rand.Next(0, 2) == 0)
                        cell.Y -= speed;
                    else
                        cell.Y += speed;
                    return;
                }
                else
                {
                    cell.X -= speed; direction = -1;
                    return;
                }

            }
            else
            {

                for (int i = 0; i < tree.Count; i++)
                {
                    if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 2)
                    {
                        if (cell.Y - cell.Height / 2 < tree.ElementAt(i).Y - tree.ElementAt(i).Height / 2)
                            cell.Y -= speed;
                        else
                            cell.Y += speed;
                        return;
                    }
                }
                for (int i = 0; i < m.Count; i++)//check other monsters
                {
                    if (m.ElementAt(i).name != name || m.ElementAt(i).id != id)//exclude itself
                    {
                        if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell,m.ElementAt(i).cell) == 2)
                        {
                            if (cell.Y - cell.Height / 2 < m.ElementAt(i).cell.Y - m.ElementAt(i).cell.Height / 2)
                                cell.Y -= speed;
                            else
                                cell.Y += speed;
                            return;
                        }

                    }
                }
                cell.X += speed; direction = 1;
            }
            }
            else if (a.cell.X > cell.X)
            {
            if (cell.X <= 0)
            {
                cell.X = 0;
                if (a.cell.X > 0)
                {
                    if (rand.Next(0, 2) == 0)
                        cell.Y -= speed;
                    else
                        cell.Y += speed;
                    return;
                }
                else
                {
                    cell.X += speed; direction = 1;
                    return;
                }
            }
            else
            {
                for (int i = 0; i < tree.Count; i++)
                {
                    //if (cell.X + cell.Width > tree.ElementAt(i).X && cell.Intersects(tree.ElementAt(i)))
                    if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 1)
                    {
                        if (cell.Y - cell.Height / 2 < tree.ElementAt(i).Y - tree.ElementAt(i).Height / 2)
                            cell.Y -= speed;
                        else
                            cell.Y += speed;
                        return;
                    }
                }
                for (int i = 0; i < m.Count; i++)//check other monsters
                {
                    if (m.ElementAt(i).name != name || m.ElementAt(i).id != id)//exclude itself
                    {
                        if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell, m.ElementAt(i).cell) == 1)
                        {
                            if (cell.Y - cell.Height / 2 < m.ElementAt(i).cell.Y - m.ElementAt(i).cell.Height / 2)
                                cell.Y -= speed;
                            else
                                cell.Y += speed;
                            return;
                        }

                    }
                }
                cell.X -= speed; direction = -1;
            }
            }
            if (a.cell.Y < cell.Y)
            {
            if (cell.Y + cell.Height >= 768 / 4 * 3)
            {
                cell.Y = 768 / 4 * 3 - cell.Height;
                if (a.cell.Y + a.cell.Height < 768 / 4 * 3)
                {
                    if (rand.Next(0, 2) == 0)
                    { cell.X -= speed; direction = -1; }
                    else
                    { cell.X += speed; direction = 1; }
                    return;
                }
                else
                {
                    cell.Y -= speed;
                    return;
                }

            }
            else
            {
                for (int i = 0; i < tree.Count; i++)
                {
                    if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 4)
                    {
                        if (cell.X + cell.Width / 2 < tree.ElementAt(i).X + (tree.ElementAt(i).Width / 2))
                        { cell.X -= speed; direction = -1; }
                        else
                        { cell.X += speed; direction = 1; }
                        return;
                    }
                }
                for (int i = 0; i < m.Count; i++)//check other monsters
                {
                    if (m.ElementAt(i).name != name || m.ElementAt(i).id != id)//exclude itself
                    {
                        if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell, m.ElementAt(i).cell) == 4)
                        {
                            if (cell.Y - cell.Height / 2 < m.ElementAt(i).cell.Y - m.ElementAt(i).cell.Height / 2)
                            { cell.X -= speed; direction = -1; }
                            else
                            { cell.X += speed; direction = 1; }
                            return;
                        }

                    }
                }
                cell.Y += speed;
            }
            }
            else if (a.cell.Y > cell.Y)
            {
            if (cell.Y <= 0)
            {
                cell.Y = 0;
                if (a.cell.Y > 0)
                {
                    if (rand.Next(0, 2) == 0)
                    { cell.X -= speed; direction = -1; }
                    else
                    { cell.X += speed; direction = 1; }
                    return;
                }
                else
                {
                    cell.Y += speed;
                    return;
                }
            }
            else
            {
                for (int i = 0; i < tree.Count; i++)
                {
                    if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 3)
                    {
                        if (cell.X + cell.Width / 2 < tree.ElementAt(i).X + (tree.ElementAt(i).Width / 2))
                        { cell.X -= speed; direction = -1; }
                        else
                        { cell.X += speed; direction = 1; }
                        return;
                    }
                }
                for (int i = 0; i < m.Count; i++)//check other monsters
                {
                    if (m.ElementAt(i).name != name || m.ElementAt(i).id != id)//exclude itself
                    {
                        if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell, m.ElementAt(i).cell) == 3)
                        {
                            if (cell.Y - cell.Height / 2 < m.ElementAt(i).cell.Y - m.ElementAt(i).cell.Height / 2)
                            { cell.X -= speed; direction = -1; }
                            else
                            { cell.X += speed; direction = 1; }
                            return;
                        }

                    }
                }
                cell.Y -= speed;
            }
            }
        }
예제 #4
0
        public void chasing_move(character a,LinkedList<enemy> m)
        {
            approach_player = false;
            if(a.cell.X<cell.X)
            {
            for (int i = 0; i < tree.Count; i++)
            {
                //if (cell.X < tree.ElementAt(i).X + tree.ElementAt(i).Width && cell.Intersects(tree.ElementAt(i)))
                if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 1)
                {
                    if (cell.Y - cell.Height/2 < tree.ElementAt(i).Y - tree.ElementAt(i).Height / 2)
                        cell.Y -= speed;
                    else
                        cell.Y += speed;
                    return;
                }
            }
            for (int i = 0; i < m.Count; i++)//check other monsters
            {
                if (m.ElementAt(i).name != name || m.ElementAt(i).id != id)//exclude itself
                {
                    if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell, m.ElementAt(i).cell) == 1)
                    {
                        if (cell.Y - cell.Height / 2 < m.ElementAt(i).cell.Y - m.ElementAt(i).cell.Height / 2)
                            cell.Y -= speed;
                        else
                            cell.Y += speed;
                        return;
                    }

                }
            }
            if (cell.Intersects(a.cell) && object_direction(cell, a.cell) == 1)
            {
                approach_player = true;
                cell.X -= 0;
                return;
            }
            cell.X-=speed;
            direction = -1;
            }
            else if (a.cell.X > cell.X)
            {
            for (int i = 0; i < tree.Count; i++)
            {
                //if (cell.X + cell.Width > tree.ElementAt(i).X && cell.Intersects(tree.ElementAt(i)))
                if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 2)
                {
                    if (cell.Y - cell.Height/2 < tree.ElementAt(i).Y - tree.ElementAt(i).Height / 2)
                        cell.Y -= speed;
                    else
                        cell.Y += speed;
                    return;
                }
            }
            for (int i = 0; i < m.Count; i++)//check other monsters
            {
                if (m.ElementAt(i).name != name || m.ElementAt(i).id != id)//exclude itself
                {
                    if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell, m.ElementAt(i).cell) == 2)
                    {
                        if (cell.Y - cell.Height / 2 < m.ElementAt(i).cell.Y - m.ElementAt(i).cell.Height / 2)
                            cell.Y -= speed;
                        else
                            cell.Y += speed;
                        return;
                    }

                }
            }
            if (cell.Intersects(a.cell) && object_direction(cell, a.cell) == 2)
            {
                approach_player = true;
                cell.X -= 0;
                return;
            }
            cell.X += speed;
            direction = 1;
            }
            if (a.cell.Y < cell.Y)
            {
            for (int i = 0; i < tree.Count; i++)
            {
                //if (cell.Y< tree.ElementAt(i).Y + tree.ElementAt(i).Height && cell.Intersects(tree.ElementAt(i)))
                if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 3)
                {
                    if (cell.X + cell.Width / 2 < tree.ElementAt(i).X + (tree.ElementAt(i).Width / 2))
                    { cell.X -= speed; direction = -1; }
                    else
                    { cell.X += speed; direction = 1; }
                    return;
                }
            }
            for (int i = 0; i < m.Count; i++)//check other monsters
            {
                if (m.ElementAt(i).name != name || m.ElementAt(i).id != id)//exclude itself
                {
                    if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell, m.ElementAt(i).cell) == 3)
                    {
                        if (cell.Y - cell.Height / 2 < m.ElementAt(i).cell.Y - m.ElementAt(i).cell.Height / 2)
                        { cell.X -= speed; direction = -1; }
                        else
                        { cell.X += speed; direction = 1; }
                        return;
                    }

                }
            }
            if (cell.Intersects(a.cell) && object_direction(cell, a.cell) == 3)
            {
                approach_player = true;
                cell.X -= 0;
                return;
            }
            cell.Y -= speed;
            }
            else if (a.cell.Y > cell.Y)
            {
            for (int i = 0; i < tree.Count; i++)
            {
                //if (cell.Y + cell.Height > tree.ElementAt(i).Y && cell.Intersects(tree.ElementAt(i)))
                if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell,tree.ElementAt(i)) == 4)
                {
                    if (cell.X + cell.Width / 2 < tree.ElementAt(i).X + (tree.ElementAt(i).Width / 2))
                    { cell.X -= speed; direction = -1; }
                    else
                    { cell.X += speed; direction = 1; }
                    return;
                }
            }
            for (int i = 0; i < m.Count; i++)//check other monsters
            {
                if (m.ElementAt(i).name != name || m.ElementAt(i).id != id)//exclude itself
                {
                    if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell, m.ElementAt(i).cell) == 4)
                    {
                        if (cell.Y - cell.Height / 2 < m.ElementAt(i).cell.Y - m.ElementAt(i).cell.Height / 2)
                        { cell.X -= speed; direction = -1; }
                        else
                        { cell.X += speed; direction = 1; }
                        return;
                    }

                }
            }
            if (cell.Intersects(a.cell) && object_direction(cell, a.cell) == 4)
            {
                approach_player = true;
                cell.X -= 0;
                return;
            }
            cell.Y += speed;
            }
        }
예제 #5
0
 public bool attack(character a)
 {
     if (a.isAlive)
     {
         if (a.damageby(this.hit_point(this.weapon), this.hit_prob))
             return true;
         else
             return false;
     }
     return false;
 }
예제 #6
0
        public void evading_move(character a, LinkedList <enemy> m)
        {
            approach_player = false;
            if (a.cell.X < cell.X)
            {
                if (cell.X >= 1024 - 30)
                {
                    cell.X = 1024 - 30;

                    if (a.cell.X < 1024 - 30)
                    {
                        if (rand.Next(0, 2) == 0)
                        {
                            cell.Y -= speed;
                        }
                        else
                        {
                            cell.Y += speed;
                        }
                        return;
                    }
                    else
                    {
                        cell.X -= speed; direction = -1;
                        return;
                    }
                }
                else
                {
                    for (int i = 0; i < tree.Count; i++)
                    {
                        if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 2)
                        {
                            if (cell.Y - cell.Height / 2 < tree.ElementAt(i).Y - tree.ElementAt(i).Height / 2)
                            {
                                cell.Y -= speed;
                            }
                            else
                            {
                                cell.Y += speed;
                            }
                            return;
                        }
                    }
                    for (int i = 0; i < m.Count; i++)                               //check other monsters
                    {
                        if (m.ElementAt(i).name != name || m.ElementAt(i).id != id) //exclude itself
                        {
                            if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell, m.ElementAt(i).cell) == 2)
                            {
                                if (cell.Y - cell.Height / 2 < m.ElementAt(i).cell.Y - m.ElementAt(i).cell.Height / 2)
                                {
                                    cell.Y -= speed;
                                }
                                else
                                {
                                    cell.Y += speed;
                                }
                                return;
                            }
                        }
                    }
                    cell.X += speed; direction = 1;
                }
            }
            else if (a.cell.X > cell.X)
            {
                if (cell.X <= 0)
                {
                    cell.X = 0;
                    if (a.cell.X > 0)
                    {
                        if (rand.Next(0, 2) == 0)
                        {
                            cell.Y -= speed;
                        }
                        else
                        {
                            cell.Y += speed;
                        }
                        return;
                    }
                    else
                    {
                        cell.X += speed; direction = 1;
                        return;
                    }
                }
                else
                {
                    for (int i = 0; i < tree.Count; i++)
                    {
                        //if (cell.X + cell.Width > tree.ElementAt(i).X && cell.Intersects(tree.ElementAt(i)))
                        if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 1)
                        {
                            if (cell.Y - cell.Height / 2 < tree.ElementAt(i).Y - tree.ElementAt(i).Height / 2)
                            {
                                cell.Y -= speed;
                            }
                            else
                            {
                                cell.Y += speed;
                            }
                            return;
                        }
                    }
                    for (int i = 0; i < m.Count; i++)                               //check other monsters
                    {
                        if (m.ElementAt(i).name != name || m.ElementAt(i).id != id) //exclude itself
                        {
                            if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell, m.ElementAt(i).cell) == 1)
                            {
                                if (cell.Y - cell.Height / 2 < m.ElementAt(i).cell.Y - m.ElementAt(i).cell.Height / 2)
                                {
                                    cell.Y -= speed;
                                }
                                else
                                {
                                    cell.Y += speed;
                                }
                                return;
                            }
                        }
                    }
                    cell.X -= speed; direction = -1;
                }
            }
            if (a.cell.Y < cell.Y)
            {
                if (cell.Y + cell.Height >= 768 / 4 * 3)
                {
                    cell.Y = 768 / 4 * 3 - cell.Height;
                    if (a.cell.Y + a.cell.Height < 768 / 4 * 3)
                    {
                        if (rand.Next(0, 2) == 0)
                        {
                            cell.X -= speed; direction = -1;
                        }
                        else
                        {
                            cell.X += speed; direction = 1;
                        }
                        return;
                    }
                    else
                    {
                        cell.Y -= speed;
                        return;
                    }
                }
                else
                {
                    for (int i = 0; i < tree.Count; i++)
                    {
                        if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 4)
                        {
                            if (cell.X + cell.Width / 2 < tree.ElementAt(i).X + (tree.ElementAt(i).Width / 2))
                            {
                                cell.X -= speed; direction = -1;
                            }
                            else
                            {
                                cell.X += speed; direction = 1;
                            }
                            return;
                        }
                    }
                    for (int i = 0; i < m.Count; i++)                               //check other monsters
                    {
                        if (m.ElementAt(i).name != name || m.ElementAt(i).id != id) //exclude itself
                        {
                            if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell, m.ElementAt(i).cell) == 4)
                            {
                                if (cell.Y - cell.Height / 2 < m.ElementAt(i).cell.Y - m.ElementAt(i).cell.Height / 2)
                                {
                                    cell.X -= speed; direction = -1;
                                }
                                else
                                {
                                    cell.X += speed; direction = 1;
                                }
                                return;
                            }
                        }
                    }
                    cell.Y += speed;
                }
            }
            else if (a.cell.Y > cell.Y)
            {
                if (cell.Y <= 0)
                {
                    cell.Y = 0;
                    if (a.cell.Y > 0)
                    {
                        if (rand.Next(0, 2) == 0)
                        {
                            cell.X -= speed; direction = -1;
                        }
                        else
                        {
                            cell.X += speed; direction = 1;
                        }
                        return;
                    }
                    else
                    {
                        cell.Y += speed;
                        return;
                    }
                }
                else
                {
                    for (int i = 0; i < tree.Count; i++)
                    {
                        if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 3)
                        {
                            if (cell.X + cell.Width / 2 < tree.ElementAt(i).X + (tree.ElementAt(i).Width / 2))
                            {
                                cell.X -= speed; direction = -1;
                            }
                            else
                            {
                                cell.X += speed; direction = 1;
                            }
                            return;
                        }
                    }
                    for (int i = 0; i < m.Count; i++)                               //check other monsters
                    {
                        if (m.ElementAt(i).name != name || m.ElementAt(i).id != id) //exclude itself
                        {
                            if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell, m.ElementAt(i).cell) == 3)
                            {
                                if (cell.Y - cell.Height / 2 < m.ElementAt(i).cell.Y - m.ElementAt(i).cell.Height / 2)
                                {
                                    cell.X -= speed; direction = -1;
                                }
                                else
                                {
                                    cell.X += speed; direction = 1;
                                }
                                return;
                            }
                        }
                    }
                    cell.Y -= speed;
                }
            }
        }
예제 #7
0
 public void chasing_move(character a, LinkedList <enemy> m)
 {
     approach_player = false;
     if (a.cell.X < cell.X)
     {
         for (int i = 0; i < tree.Count; i++)
         {
             //if (cell.X < tree.ElementAt(i).X + tree.ElementAt(i).Width && cell.Intersects(tree.ElementAt(i)))
             if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 1)
             {
                 if (cell.Y - cell.Height / 2 < tree.ElementAt(i).Y - tree.ElementAt(i).Height / 2)
                 {
                     cell.Y -= speed;
                 }
                 else
                 {
                     cell.Y += speed;
                 }
                 return;
             }
         }
         for (int i = 0; i < m.Count; i++)                               //check other monsters
         {
             if (m.ElementAt(i).name != name || m.ElementAt(i).id != id) //exclude itself
             {
                 if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell, m.ElementAt(i).cell) == 1)
                 {
                     if (cell.Y - cell.Height / 2 < m.ElementAt(i).cell.Y - m.ElementAt(i).cell.Height / 2)
                     {
                         cell.Y -= speed;
                     }
                     else
                     {
                         cell.Y += speed;
                     }
                     return;
                 }
             }
         }
         if (cell.Intersects(a.cell) && object_direction(cell, a.cell) == 1)
         {
             approach_player = true;
             cell.X         -= 0;
             return;
         }
         cell.X   -= speed;
         direction = -1;
     }
     else if (a.cell.X > cell.X)
     {
         for (int i = 0; i < tree.Count; i++)
         {
             //if (cell.X + cell.Width > tree.ElementAt(i).X && cell.Intersects(tree.ElementAt(i)))
             if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 2)
             {
                 if (cell.Y - cell.Height / 2 < tree.ElementAt(i).Y - tree.ElementAt(i).Height / 2)
                 {
                     cell.Y -= speed;
                 }
                 else
                 {
                     cell.Y += speed;
                 }
                 return;
             }
         }
         for (int i = 0; i < m.Count; i++)                               //check other monsters
         {
             if (m.ElementAt(i).name != name || m.ElementAt(i).id != id) //exclude itself
             {
                 if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell, m.ElementAt(i).cell) == 2)
                 {
                     if (cell.Y - cell.Height / 2 < m.ElementAt(i).cell.Y - m.ElementAt(i).cell.Height / 2)
                     {
                         cell.Y -= speed;
                     }
                     else
                     {
                         cell.Y += speed;
                     }
                     return;
                 }
             }
         }
         if (cell.Intersects(a.cell) && object_direction(cell, a.cell) == 2)
         {
             approach_player = true;
             cell.X         -= 0;
             return;
         }
         cell.X   += speed;
         direction = 1;
     }
     if (a.cell.Y < cell.Y)
     {
         for (int i = 0; i < tree.Count; i++)
         {
             //if (cell.Y< tree.ElementAt(i).Y + tree.ElementAt(i).Height && cell.Intersects(tree.ElementAt(i)))
             if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 3)
             {
                 if (cell.X + cell.Width / 2 < tree.ElementAt(i).X + (tree.ElementAt(i).Width / 2))
                 {
                     cell.X -= speed; direction = -1;
                 }
                 else
                 {
                     cell.X += speed; direction = 1;
                 }
                 return;
             }
         }
         for (int i = 0; i < m.Count; i++)                               //check other monsters
         {
             if (m.ElementAt(i).name != name || m.ElementAt(i).id != id) //exclude itself
             {
                 if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell, m.ElementAt(i).cell) == 3)
                 {
                     if (cell.Y - cell.Height / 2 < m.ElementAt(i).cell.Y - m.ElementAt(i).cell.Height / 2)
                     {
                         cell.X -= speed; direction = -1;
                     }
                     else
                     {
                         cell.X += speed; direction = 1;
                     }
                     return;
                 }
             }
         }
         if (cell.Intersects(a.cell) && object_direction(cell, a.cell) == 3)
         {
             approach_player = true;
             cell.X         -= 0;
             return;
         }
         cell.Y -= speed;
     }
     else if (a.cell.Y > cell.Y)
     {
         for (int i = 0; i < tree.Count; i++)
         {
             //if (cell.Y + cell.Height > tree.ElementAt(i).Y && cell.Intersects(tree.ElementAt(i)))
             if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 4)
             {
                 if (cell.X + cell.Width / 2 < tree.ElementAt(i).X + (tree.ElementAt(i).Width / 2))
                 {
                     cell.X -= speed; direction = -1;
                 }
                 else
                 {
                     cell.X += speed; direction = 1;
                 }
                 return;
             }
         }
         for (int i = 0; i < m.Count; i++)                               //check other monsters
         {
             if (m.ElementAt(i).name != name || m.ElementAt(i).id != id) //exclude itself
             {
                 if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell, m.ElementAt(i).cell) == 4)
                 {
                     if (cell.Y - cell.Height / 2 < m.ElementAt(i).cell.Y - m.ElementAt(i).cell.Height / 2)
                     {
                         cell.X -= speed; direction = -1;
                     }
                     else
                     {
                         cell.X += speed; direction = 1;
                     }
                     return;
                 }
             }
         }
         if (cell.Intersects(a.cell) && object_direction(cell, a.cell) == 4)
         {
             approach_player = true;
             cell.X         -= 0;
             return;
         }
         cell.Y += speed;
     }
 }
예제 #8
0
        public void random_move(character a, LinkedList <enemy> m)
        {
            approach_player = false;
            if (move_distance > 0)
            {
                move_distance--;
                switch (last_direction)
                {
                case 1:   //left move
                {
                    if (cell.X < 0)
                    {
                        cell.X        = 0;
                        move_distance = 0;
                    }
                    else
                    {
                        for (int i = 0; i < tree.Count; i++)//check trees
                        {
                            //if(cell.X<tree.ElementAt(i).X+tree.ElementAt(i).Width && cell.Intersects(tree.ElementAt(i)))
                            if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 1)
                            {
                                //cell.X=tree.ElementAt(i).X+tree.ElementAt(i).Width;
                                move_distance = 0;
                                return;
                            }
                        }
                        for (int i = 0; i < m.Count; i++)                               //check other monsters
                        {
                            if (m.ElementAt(i).name != name || m.ElementAt(i).id != id) //exclude itself
                            {
                                if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell, m.ElementAt(i).cell) == 1)
                                {
                                    move_distance = 0;
                                    return;
                                }
                            }
                        }
                        if (cell.Intersects(a.cell) && object_direction(cell, a.cell) == 1)      //check players
                        {
                            approach_player = true;
                            cell.X         -= 0;
                            return;
                        }
                        cell.X -= speed;
                    }
                    break;
                }

                case 2:   //right move
                    if (cell.X > 1024 - 30)
                    {
                        cell.X        = 1024 - 30;
                        move_distance = 0;
                    }
                    else
                    {
                        for (int i = 0; i < tree.Count; i++)
                        {
                            //if (cell.X+cell.Width > tree.ElementAt(i).X && cell.Intersects(tree.ElementAt(i)))
                            if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 2)
                            {
                                //cell.X = tree.ElementAt(i).X-cell.Width;
                                move_distance = 0;
                                return;
                            }
                        }
                        for (int i = 0; i < m.Count; i++)                               //check other monsters
                        {
                            if (m.ElementAt(i).name != name || m.ElementAt(i).id != id) //exclude itself
                            {
                                if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell, m.ElementAt(i).cell) == 2)
                                {
                                    move_distance = 0;
                                    return;
                                }
                            }
                        }
                        if (cell.Intersects(a.cell) && object_direction(cell, a.cell) == 2)
                        {
                            approach_player = true;
                            cell.X         += 0;
                            return;
                        }
                        cell.X += speed;
                    }

                    break;

                case 3:   //up move
                    if (cell.Y < 0)
                    {
                        cell.Y        = 0;
                        move_distance = 0;
                    }
                    else
                    {
                        for (int i = 0; i < tree.Count; i++)
                        {
                            //if (cell.Y < tree.ElementAt(i).Y + tree.ElementAt(i).Height && cell.Intersects(tree.ElementAt(i)))
                            if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 3)
                            {
                                //cell.Y = tree.ElementAt(i).Y + tree.ElementAt(i).Height;
                                move_distance = 0;
                                return;
                            }
                        }
                        for (int i = 0; i < m.Count; i++)                               //check other monsters
                        {
                            if (m.ElementAt(i).name != name || m.ElementAt(i).id != id) //exclude itself
                            {
                                if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell, m.ElementAt(i).cell) == 3)
                                {
                                    move_distance = 0;
                                    return;
                                }
                            }
                        }
                        if (cell.Intersects(a.cell) && object_direction(cell, a.cell) == 3)
                        {
                            approach_player = true;
                            cell.Y         -= 0;
                            return;
                        }
                        cell.Y -= speed;
                    }
                    break;

                case 4:   //down move
                    if (cell.Y >= 768 / 4 * 3 - 40)
                    {
                        cell.Y        = 768 / 4 * 3 - 40;
                        move_distance = 0;
                    }
                    else
                    {
                        for (int i = 0; i < tree.Count; i++)
                        {
                            //if (cell.Y +cell.Height> tree.ElementAt(i).Y && cell.Intersects(tree.ElementAt(i)))
                            if (cell.Intersects(tree.ElementAt(i)) && object_direction(cell, tree.ElementAt(i)) == 4)
                            {
                                //cell.Y = tree.ElementAt(i).Y-cell.Height;
                                move_distance = 0;
                                return;
                            }
                        }
                        for (int i = 0; i < m.Count; i++)                               //check other monsters
                        {
                            if (m.ElementAt(i).name != name || m.ElementAt(i).id != id) //exclude itself
                            {
                                if (cell.Intersects(m.ElementAt(i).cell) && object_direction(cell, m.ElementAt(i).cell) == 4)
                                {
                                    move_distance = 0;
                                    return;
                                }
                            }
                        }
                        if (cell.Intersects(a.cell) && object_direction(cell, a.cell) == 4)
                        {
                            approach_player = true;
                            cell.Y         += 0;
                            return;
                        }
                        cell.Y += speed;
                    }
                    break;
                }
            }
            else
            {
                //finish_walking = true;
                move_distance  = 10;
                last_direction = rand.Next(1, 5);
                if (last_direction == 1)
                {
                    direction = -1;
                }
                else if (last_direction == 2)
                {
                    direction = 1;
                }
            }
        }