예제 #1
0
파일: POE_RTS.cs 프로젝트: AJMuller/POE
    }             // creates new buttons for every unit and building saved in the arrays. each button is made to match the type of unit or building it is by changing colours and letters displayed on it.

    private void UpdateMap()
    {
        int distancebuild;

        foreach (Building b in map.Buildings)
        {
            if (b.GetType() == typeof(ResourceBuilding))
            {
                ResourceBuilding rb = (ResourceBuilding)b;
                foreach (Unit e in map.Units)
                {
                    if (e.GetType() == typeof(Melee_Unit))
                    {
                        Melee_Unit m = (Melee_Unit)e;


                        distancebuild = Math.Abs(rb.XPos - m.XPos) + Math.Abs(rb.YPos - m.YPos);

                        if (distancebuild <= m.AttackRange)
                        {
                            rb.Fight(m.Attack);
                        }
                    }
                    else if (e.GetType() == typeof(Ranged_Unit))
                    {
                        Ranged_Unit m = (Ranged_Unit)e;


                        distancebuild = Math.Abs(rb.XPos - m.XPos) + Math.Abs(rb.YPos - m.YPos);

                        if (distancebuild <= m.AttackRange)
                        {
                            rb.Fight(m.Attack);
                        }
                    }
                    else if (e.GetType() == typeof(Fighter_Jets))
                    {
                        Fighter_Jets m = (Fighter_Jets)e;


                        distancebuild = Math.Abs(rb.XPos - m.XPos) + Math.Abs(rb.YPos - m.YPos);

                        if (distancebuild <= m.AttackRange)
                        {
                            rb.Fight(m.Attack);
                        }
                    }
                    else if (e.GetType() == typeof(Helicopter))
                    {
                        Helicopter m = (Helicopter)e;


                        distancebuild = Math.Abs(rb.XPos - m.XPos) + Math.Abs(rb.YPos - m.YPos);

                        if (distancebuild <= m.AttackRange)
                        {
                            rb.Fight(m.Attack);
                        }
                    }
                    else if (e.GetType() == typeof(Tank))
                    {
                        Tank m = (Tank)e;


                        distancebuild = Math.Abs(rb.XPos - m.XPos) + Math.Abs(rb.YPos - m.YPos);

                        if (distancebuild <= m.AttackRange)
                        {
                            rb.Fight(m.Attack);
                        }
                    }
                }
            }
            else if (b.GetType() == typeof(FactoryBuilding))
            {
                FactoryBuilding rb = (FactoryBuilding)b;
                foreach (Unit e in map.Units)
                {
                    if (e.GetType() == typeof(Melee_Unit))
                    {
                        Melee_Unit m = (Melee_Unit)e;


                        distancebuild = Math.Abs(rb.XPos - m.XPos) + Math.Abs(rb.YPos - m.YPos);

                        if (distancebuild <= m.AttackRange)
                        {
                            rb.Fight(m.Attack);
                        }
                    }
                    else if (e.GetType() == typeof(Ranged_Unit))
                    {
                        Ranged_Unit m = (Ranged_Unit)e;


                        distancebuild = Math.Abs(rb.XPos - m.XPos) + Math.Abs(rb.YPos - m.YPos);

                        if (distancebuild <= m.AttackRange)
                        {
                            rb.Fight(m.Attack);
                        }
                    }
                    else if (e.GetType() == typeof(Fighter_Jets))
                    {
                        Fighter_Jets m = (Fighter_Jets)e;


                        distancebuild = Math.Abs(rb.XPos - m.XPos) + Math.Abs(rb.YPos - m.YPos);

                        if (distancebuild <= m.AttackRange)
                        {
                            rb.Fight(m.Attack);
                        }
                    }
                    else if (e.GetType() == typeof(Helicopter))
                    {
                        Helicopter m = (Helicopter)e;


                        distancebuild = Math.Abs(rb.XPos - m.XPos) + Math.Abs(rb.YPos - m.YPos);

                        if (distancebuild <= m.AttackRange)
                        {
                            rb.Fight(m.Attack);
                        }
                    }
                    else if (e.GetType() == typeof(Tank))
                    {
                        Tank m = (Tank)e;


                        distancebuild = Math.Abs(rb.XPos - m.XPos) + Math.Abs(rb.YPos - m.YPos);

                        if (distancebuild <= m.AttackRange)
                        {
                            rb.Fight(m.Attack);
                        }
                    }
                }
            }
            if (b.GetType() == typeof(Field_Hospital))
            {
                Field_Hospital rb = (Field_Hospital)b;
                foreach (Unit e in map.Units)
                {
                    if (e.GetType() == typeof(Melee_Unit))
                    {
                        Melee_Unit m = (Melee_Unit)e;


                        distancebuild = Math.Abs(rb.XPos - m.XPos) + Math.Abs(rb.YPos - m.YPos);

                        if (distancebuild <= m.AttackRange)
                        {
                            rb.Fight(m.Attack);
                        }
                    }
                    else if (e.GetType() == typeof(Ranged_Unit))
                    {
                        Ranged_Unit m = (Ranged_Unit)e;


                        distancebuild = Math.Abs(rb.XPos - m.XPos) + Math.Abs(rb.YPos - m.YPos);

                        if (distancebuild <= m.AttackRange)
                        {
                            rb.Fight(m.Attack);
                        }
                    }
                    else if (e.GetType() == typeof(Fighter_Jets))
                    {
                        Fighter_Jets m = (Fighter_Jets)e;


                        distancebuild = Math.Abs(rb.XPos - m.XPos) + Math.Abs(rb.YPos - m.YPos);

                        if (distancebuild <= m.AttackRange)
                        {
                            rb.Fight(m.Attack);
                        }
                    }
                    else if (e.GetType() == typeof(Helicopter))
                    {
                        Helicopter m = (Helicopter)e;


                        distancebuild = Math.Abs(rb.XPos - m.XPos) + Math.Abs(rb.YPos - m.YPos);

                        if (distancebuild <= m.AttackRange)
                        {
                            rb.Fight(m.Attack);
                        }
                    }
                    else if (e.GetType() == typeof(Tank))
                    {
                        Tank m = (Tank)e;


                        distancebuild = Math.Abs(rb.XPos - m.XPos) + Math.Abs(rb.YPos - m.YPos);

                        if (distancebuild <= m.AttackRange)
                        {
                            rb.Fight(m.Attack);
                        }
                    }
                }
            }
            if (b.GetType() == typeof(Weapon_Upgrade))
            {
                Weapon_Upgrade rb = (Weapon_Upgrade)b;
                foreach (Unit e in map.Units)
                {
                    if (e.GetType() == typeof(Melee_Unit))
                    {
                        Melee_Unit m = (Melee_Unit)e;


                        distancebuild = Math.Abs(rb.XPos - m.XPos) + Math.Abs(rb.YPos - m.YPos);

                        if (distancebuild <= m.AttackRange)
                        {
                            rb.Fight(m.Attack);
                        }
                    }
                    else if (e.GetType() == typeof(Ranged_Unit))
                    {
                        Ranged_Unit m = (Ranged_Unit)e;


                        distancebuild = Math.Abs(rb.XPos - m.XPos) + Math.Abs(rb.YPos - m.YPos);

                        if (distancebuild <= m.AttackRange)
                        {
                            rb.Fight(m.Attack);
                        }
                    }
                    else if (e.GetType() == typeof(Fighter_Jets))
                    {
                        Fighter_Jets m = (Fighter_Jets)e;


                        distancebuild = Math.Abs(rb.XPos - m.XPos) + Math.Abs(rb.YPos - m.YPos);

                        if (distancebuild <= m.AttackRange)
                        {
                            rb.Fight(m.Attack);
                        }
                    }
                    else if (e.GetType() == typeof(Helicopter))
                    {
                        Helicopter m = (Helicopter)e;


                        distancebuild = Math.Abs(rb.XPos - m.XPos) + Math.Abs(rb.YPos - m.YPos);

                        if (distancebuild <= m.AttackRange)
                        {
                            rb.Fight(m.Attack);
                        }
                    }
                    else if (e.GetType() == typeof(Tank))
                    {
                        Tank m = (Tank)e;


                        distancebuild = Math.Abs(rb.XPos - m.XPos) + Math.Abs(rb.YPos - m.YPos);

                        if (distancebuild <= m.AttackRange)
                        {
                            rb.Fight(m.Attack);
                        }
                    }
                }
            }
        }

        foreach (Unit u in map.Units)
        {
            if (u.GetType() == typeof(Melee_Unit))
            {
                Melee_Unit m = (Melee_Unit)u;

                if (m.Health < 25)
                {
                    switch (r.Next(0, 4))
                    {
                    case 0: m.MovePos(Direction.North); break;

                    case 1: m.MovePos(Direction.East); break;

                    case 2: m.MovePos(Direction.South); break;

                    case 3: m.MovePos(Direction.West); break;
                    } //end switch
                }     //end if
                else
                {
                    bool inCombat = false;
                    foreach (Unit e in map.Units)
                    {
                        if (m.WithinRange(e))
                        {
                            m.Fight(e);
                            inCombat = true;
                        } //end if
                    }     //end foreach
                    if (!inCombat)
                    {
                        Unit c = m.NearestUnit(map.Units);
                        m.MovePos(m.DirectionTo(c));
                    } //end if
                }     //end else
            }         //end if
            else if (u.GetType() == typeof(Ranged_Unit))
            {
                Ranged_Unit m = (Ranged_Unit)u;
                if (m.IsDeath())
                {
                    m.Symbol = "D";
                }//end if
                if (m.Health < 25)
                {
                    switch (r.Next(0, 4))
                    {
                    case 0: m.MovePos(Direction.North); break;

                    case 1: m.MovePos(Direction.East); break;

                    case 2: m.MovePos(Direction.South); break;

                    case 3: m.MovePos(Direction.West); break;
                    } //end switch
                }     //end if
                else
                {
                    bool inCombat = false;
                    foreach (Unit e in map.Units)
                    {
                        if (m.WithinRange(e))
                        {
                            m.Fight(e);
                            inCombat = true;
                        } //end if
                    }     //end foreach
                    if (!inCombat)
                    {
                        Unit c = m.NearestUnit(map.Units);
                        m.MovePos(m.DirectionTo(c));
                    } //end if
                }     //end else
            }         //end else if
            else if (u.GetType() == typeof(Tank))
            {
                Tank m = (Tank)u;
                if (m.IsDeath())
                {
                    m.Symbol = "D";
                }//end if
                if (m.Health < 25)
                {
                    switch (r.Next(0, 4))
                    {
                    case 0: m.MovePos(Direction.North); break;

                    case 1: m.MovePos(Direction.East); break;

                    case 2: m.MovePos(Direction.South); break;

                    case 3: m.MovePos(Direction.West); break;
                    } //end switch
                }     //end if
                else
                {
                    bool inCombat = false;
                    foreach (Unit e in map.Units)
                    {
                        if (m.WithinRange(e))
                        {
                            m.Fight(e);
                            inCombat = true;
                        } //end if
                    }     //end foreach
                    if (!inCombat)
                    {
                        Unit c = m.NearestUnit(map.Units);
                        m.MovePos(m.DirectionTo(c));
                    } //end if
                }     //end else
            }         //end else if
            else if (u.GetType() == typeof(Fighter_Jets))
            {
                Fighter_Jets m = (Fighter_Jets)u;
                if (m.IsDeath())
                {
                    m.Symbol = "D";
                }//end if
                if (m.Health < 25)
                {
                    switch (r.Next(0, 4))
                    {
                    case 0: m.MovePos(Direction.North); break;

                    case 1: m.MovePos(Direction.East); break;

                    case 2: m.MovePos(Direction.South); break;

                    case 3: m.MovePos(Direction.West); break;
                    } //end switch
                }     //end if
                else
                {
                    bool inCombat = false;
                    foreach (Unit e in map.Units)
                    {
                        if (m.WithinRange(e))
                        {
                            m.Fight(e);
                            inCombat = true;
                        } //end if
                    }     //end foreach
                    if (!inCombat)
                    {
                        Unit c = m.NearestUnit(map.Units);
                        m.MovePos(m.DirectionTo(c));
                    } //end if
                }     //end else
            }         //end else if
            else if (u.GetType() == typeof(Helicopter))
            {
                Helicopter m = (Helicopter)u;
                if (m.IsDeath())
                {
                    m.Symbol = "D";
                }//end if
                if (m.Health < 25)
                {
                    switch (r.Next(0, 4))
                    {
                    case 0: m.MovePos(Direction.North); break;

                    case 1: m.MovePos(Direction.East); break;

                    case 2: m.MovePos(Direction.South); break;

                    case 3: m.MovePos(Direction.West); break;
                    } //end switch
                }     //end if
                else
                {
                    bool inCombat = false;
                    foreach (Unit e in map.Units)
                    {
                        if (m.WithinRange(e))
                        {
                            m.Fight(e);
                            inCombat = true;
                        } //end if
                    }     //end foreach
                    if (!inCombat)
                    {
                        Unit c = m.NearestUnit(map.Units);
                        m.MovePos(m.DirectionTo(c));
                    } //end if
                }     //end else
            }         //end else if
        }             //end foreach
    }                 // updates the map by chaging values of units. calls movement methods and fight methods to change values in order for the map to change.