示例#1
0
    }     // displays positions of units and buildings

    public void CombatEngine(Unit u)
    {
        if (u.attackRange == 2)
        {
            MeleeUnit u2 = (MeleeUnit)u;
            if ((u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList)) != null)
            {
                int distance = u2.FindUnit(u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList));
                // info.Text = info.Text + "\n" + distance.ToString();
                if (distance <= u2.attackRange)
                {
                    u2.Combat(u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList));
                } //combat
                else
                {
                    if (u2.Health > 2)
                    {
                        if (u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList).attackRange < 3)
                        {
                            MeleeUnit Position = (MeleeUnit)u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList);
                            if (Position.X > u2.X)
                            {
                                u.MoveUnit(1, 0);
                            }
                            else if (Position.X < u2.X)
                            {
                                u.MoveUnit(-1, 0);
                            }
                            if (Position.Y > u2.Y)
                            {
                                u.MoveUnit(0, 1);
                            }
                            else if (Position.Y < u2.Y)
                            {
                                u.MoveUnit(0, -1);
                            }
                        }
                        else
                        {
                            RangedUnit Position = (RangedUnit)u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList);
                            if (Position.X > u2.X)
                            {
                                u.MoveUnit(1, 0);
                            }
                            else if (Position.X < u2.X)
                            {
                                u.MoveUnit(-1, 0);
                            }
                            if (Position.Y > u2.Y)
                            {
                                u.MoveUnit(0, 1);
                            }
                            else if (Position.Y < u2.Y)
                            {
                                u.MoveUnit(0, -1);
                            }
                        }
                    }// movement
                    else
                    {
                        if (u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList).attackRange < 3)
                        {
                            MeleeUnit Position = (MeleeUnit)u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList);

                            if (Position.X < u2.X)
                            {
                                u.MoveUnit(1, 0);
                            }
                            else if (Position.X > u2.X)
                            {
                                u.MoveUnit(-1, 0);
                            }
                            if (Position.Y < u2.Y)
                            {
                                u.MoveUnit(0, 1);
                            }
                            else if (Position.Y > u2.Y)
                            {
                                u.MoveUnit(0, -1);
                            }
                        }
                        else
                        {
                            RangedUnit Position = (RangedUnit)u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList);

                            if (Position.X < u2.X)
                            {
                                u.MoveUnit(1, 0);
                            }
                            else if (Position.X > u2.X)
                            {
                                u.MoveUnit(-1, 0);
                            }
                            if (Position.Y < u2.Y)
                            {
                                u.MoveUnit(0, 1);
                            }
                            else if (Position.Y > u2.Y)
                            {
                                u.MoveUnit(0, -1);
                            }
                        }
                    }// running away / needs work
                }

                if (u.Death())
                {
                    for (int i = 0; i < 15; i++)
                    {
                        if (GameMap.MeleeList[i] != null)
                        {
                            if (GameMap.MeleeList[i] == u)
                            {
                                for (int k = i; k < 14; k++)
                                {
                                    GameMap.MeleeList[k] = GameMap.MeleeList[k + 1];
                                }

                                GameMap.MeleeList[14] = null;
                            }
                        }
                    }
                } // handels death in the game
            }     // melee
        }

        else if (u.attackRange == 4)
        {
            RangedUnit u2     = (RangedUnit)u;
            int        counti = 0;
            for (int i = 0; i < 5; i++)
            {
                if (GameMap.MeleeList[i] != null && u.ReturnPosition(GameMap.MeleeList, GameMap.RangedList) != null)
                {
                    if (u.ReturnPosition(GameMap.MeleeList, GameMap.RangedList).team != u.team)
                    {
                        counti++;
                    }
                }
            }
            if (counti > 0) // see number of units in array
            {
                int distance = u2.FindUnit(u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList));

                if (distance <= u2.attackRange)
                {
                    u2.Combat(u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList));
                } //combat
                else
                {
                    if (u2.Health > 2)
                    {
                        if (u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList).attackRange > 3)
                        {
                            RangedUnit Position = (RangedUnit)u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList);
                            if (Position.X > u2.X)
                            {
                                u.MoveUnit(1, 0);
                            }
                            else if (Position.X < u2.X)
                            {
                                u.MoveUnit(-1, 0);
                            }
                            if (Position.Y > u2.Y)
                            {
                                u.MoveUnit(0, 1);
                            }
                            else if (Position.Y < u2.Y)
                            {
                                u.MoveUnit(0, -1);
                            }
                        }
                        else
                        {
                            MeleeUnit Position = (MeleeUnit)u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList);
                            if (Position.X > u2.X)
                            {
                                u.MoveUnit(1, 0);
                            }
                            else if (Position.X < u2.X)
                            {
                                u.MoveUnit(-1, 0);
                            }
                            if (Position.Y > u2.Y)
                            {
                                u.MoveUnit(0, 1);
                            }
                            else if (Position.Y < u2.Y)
                            {
                                u.MoveUnit(0, -1);
                            }
                        }
                    }// movement
                    else
                    {
                        if (u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList).attackRange > 3)
                        {
                            RangedUnit Position = (RangedUnit)u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList);
                            if (Position.X < u2.X)
                            {
                                u.MoveUnit(1, 0);
                            }
                            else if (Position.X > u2.X)
                            {
                                u.MoveUnit(-1, 0);
                            }


                            if (Position.Y < u2.Y)
                            {
                                u.MoveUnit(0, 1);
                            }
                            else if (Position.Y > u2.Y)
                            {
                                u.MoveUnit(0, -1);
                            }
                        }
                        else
                        {
                            MeleeUnit Position = (MeleeUnit)u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList);
                            if (u2.X + u2.Speed >= 0 && u2.X + u2.Speed <= 19)
                            {
                                if (Position.X < u2.X)
                                {
                                    u.MoveUnit(1, 0);
                                }
                                else if (Position.X > u2.X)
                                {
                                    u.MoveUnit(-1, 0);
                                }
                            }

                            if (u2.Y + u2.Speed >= 0 && u2.Y + u2.Speed <= 19)
                            {
                                if (Position.Y < u2.Y)
                                {
                                    u.MoveUnit(0, 1);
                                }
                                else if (Position.Y > u2.Y)
                                {
                                    u.MoveUnit(0, -1);
                                }
                            }
                        }
                    }// running away / needs work
                }
            }

            if (u.Death())
            {
                for (int i = 0; i < 15; i++)
                {
                    if (GameMap.RangedList[i] != null)
                    {
                        if (GameMap.RangedList[i] == u)
                        {
                            for (int k = i; k < 14; k++)
                            {
                                GameMap.RangedList[k] = GameMap.RangedList[k + 1];
                            }

                            GameMap.RangedList[14] = null;
                        }
                    }
                }
            } // handels death in the game
        }

        else
        {
            Wizard u2     = (Wizard)u;
            int    counti = 0;
            for (int i = 0; i < 5; i++)
            {
                if (GameMap.MeleeList[i] != null && u.ReturnPosition(GameMap.MeleeList, GameMap.RangedList) != null)
                {
                    if (u.ReturnPosition(GameMap.MeleeList, GameMap.RangedList).team != u.team)
                    {
                        counti++;
                    }
                }
            }
            if (counti > 0) // see number of units in array
            {
                int distance = u2.FindUnit(u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList));

                if (distance <= u2.attackRange)
                {
                    u2.Combat(u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList));
                } //combat
                else
                {
                    if (u2.Health > 2)
                    {
                        if (u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList).attackRange > 3)
                        {
                            RangedUnit Position = (RangedUnit)u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList);
                            if (Position.X > u2.X)
                            {
                                u.MoveUnit(1, 0);
                            }
                            else if (Position.X < u2.X)
                            {
                                u.MoveUnit(-1, 0);
                            }
                            if (Position.Y > u2.Y)
                            {
                                u.MoveUnit(0, 1);
                            }
                            else if (Position.Y < u2.Y)
                            {
                                u.MoveUnit(0, -1);
                            }
                        }
                        else
                        {
                            MeleeUnit Position = (MeleeUnit)u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList);
                            if (Position.X > u2.X)
                            {
                                u.MoveUnit(1, 0);
                            }
                            else if (Position.X < u2.X)
                            {
                                u.MoveUnit(-1, 0);
                            }
                            if (Position.Y > u2.Y)
                            {
                                u.MoveUnit(0, 1);
                            }
                            else if (Position.Y < u2.Y)
                            {
                                u.MoveUnit(0, -1);
                            }
                        }
                    }// movement
                    else
                    {
                        if (u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList).attackRange > 3)
                        {
                            RangedUnit Position = (RangedUnit)u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList);
                            if (Position.X < u2.X)
                            {
                                u.MoveUnit(1, 0);
                            }
                            else if (Position.X > u2.X)
                            {
                                u.MoveUnit(-1, 0);
                            }


                            if (Position.Y < u2.Y)
                            {
                                u.MoveUnit(0, 1);
                            }
                            else if (Position.Y > u2.Y)
                            {
                                u.MoveUnit(0, -1);
                            }
                        }
                        else
                        {
                            MeleeUnit Position = (MeleeUnit)u2.ReturnPosition(GameMap.MeleeList, GameMap.RangedList);
                            if (u2.X + u2.Speed >= 0 && u2.X + u2.Speed <= 19)
                            {
                                if (Position.X < u2.X)
                                {
                                    u.MoveUnit(1, 0);
                                }
                                else if (Position.X > u2.X)
                                {
                                    u.MoveUnit(-1, 0);
                                }
                            }

                            if (u2.Y + u2.Speed >= 0 && u2.Y + u2.Speed <= 19)
                            {
                                if (Position.Y < u2.Y)
                                {
                                    u.MoveUnit(0, 1);
                                }
                                else if (Position.Y > u2.Y)
                                {
                                    u.MoveUnit(0, -1);
                                }
                            }
                        }
                    }// running away / needs work
                }
            }

            if (u.Death())
            {
                for (int i = 0; i < 15; i++)
                {
                    if (GameMap.RangedList[i] != null)
                    {
                        if (GameMap.RangedList[i] == u)
                        {
                            for (int k = i; k < 14; k++)
                            {
                                GameMap.RangedList[k] = GameMap.RangedList[k + 1];
                            }

                            GameMap.RangedList[14] = null;
                        }
                    }
                }
            } // handels death in the game
        }
    }         // the working of the game
示例#2
0
    public void action()
    {
        foreach (Building b in map.Buildings)
        {
            if (b is FactoryBuilding)
            {
                FactoryBuilding f = (FactoryBuilding)b;
                if (f.Production_Speed % round == 0 && resources >= UnitCost)
                {
                    map.Units.Add(f.SpawnUnit());
                }
            }
            if (b is ResourceBuilding)
            {
                ResourceBuilding rs = (ResourceBuilding)b;
                resources = rs.GenerateResources();
            }
        }
        for (int i = 0; i < map.Units.Count; i++)
        {
            if (map.Units[i] is MeleeUnit)
            {
                MeleeUnit mu = (MeleeUnit)map.Units[i];
                if (mu.health <= mu.maxHealth * 0.25) // Running Away
                {
                    mu.Move(r.Next(0, 4));
                }
                else
                {
                    (Unit closest, int distanceTo)           = mu.EnemyDistance(map.Units);
                    (Building closestbuilding, int distance) = mu.ClosestBuilding(map.Buildings);
                    if (distanceTo < distance)  //Checks if Unit is closer than building
                    {
                        //Check In Range
                        if (distanceTo <= mu.attackRange)
                        {
                            mu.isAttack = true;
                            mu.Combat(closest);
                            if (mu.IsDead == true)   //Returns resources if unit is dead
                            {
                                foreach (Building bg in map.Buildings)
                                {
                                    if (bg is ResourceBuilding)
                                    {
                                        ResourceBuilding rb = (ResourceBuilding)bg;
                                        if (mu.team == rb.team)
                                        {
                                            rb.ResourcesGenerated++;
                                        }
                                    }
                                }
                            }
                        }
                        else //Move Towards
                        {
                            if (closest is MeleeUnit)
                            {
                                MeleeUnit closestMu = (MeleeUnit)closest;
                                if (mu.xPos > closestMu.xPos) //North
                                {
                                    mu.Move(0);
                                }
                                else if (mu.xPos < closestMu.xPos) //South
                                {
                                    mu.Move(2);
                                }
                                else if (mu.yPos > closestMu.yPos) //West
                                {
                                    mu.Move(3);
                                }
                                else if (mu.yPos < closestMu.yPos) //East
                                {
                                    mu.Move(1);
                                }
                            }
                            else if (closest is RangedUnit)
                            {
                                RangedUnit closestRu = (RangedUnit)closest;
                                if (mu.xPos > closestRu.xPos) //North
                                {
                                    mu.Move(0);
                                }
                                else if (mu.xPos < closestRu.xPos) //South
                                {
                                    mu.Move(2);
                                }
                                else if (mu.yPos > closestRu.yPos) //West
                                {
                                    mu.Move(3);
                                }
                                else if (mu.yPos < closestRu.yPos) //East
                                {
                                    mu.Move(1);
                                }
                            }
                            if (closest is WizzardUnit)
                            {
                                WizzardUnit closestwu = (WizzardUnit)closest;
                                if (mu.xPos > closestwu.xPos) //North
                                {
                                    mu.Move(0);
                                }
                                else if (mu.xPos < closestwu.xPos) //South
                                {
                                    mu.Move(2);
                                }
                                else if (mu.yPos > closestwu.yPos) //West
                                {
                                    mu.Move(3);
                                }
                                else if (mu.yPos < closestwu.yPos) //East
                                {
                                    mu.Move(1);
                                }
                            }
                        }
                    }
                    else
                    {
                        //Check In Range
                        if (distanceTo <= mu.attackRange)
                        {
                            mu.isAttack = true;
                            mu.Combat(closest);
                        }
                        else //Move Towards
                        {
                            if (closestbuilding is FactoryBuilding)
                            {
                                FactoryBuilding closestfb = (FactoryBuilding)closestbuilding;
                                if (mu.xPos > closestfb.xPos) //North
                                {
                                    mu.Move(1);
                                }
                                else if (mu.xPos < closestfb.xPos) //South
                                {
                                    mu.Move(2);
                                }
                                else if (mu.yPos > closestfb.yPos) //West
                                {
                                    mu.Move(3);
                                }
                                else if (mu.yPos < closestfb.yPos) //East
                                {
                                    mu.Move(4);
                                }
                            }
                            else if (closestbuilding is ResourceBuilding)
                            {
                                ResourceBuilding closestrb = (ResourceBuilding)closestbuilding;
                                if (mu.xPos > closestrb.xPos) //North
                                {
                                    mu.Move(1);
                                }
                                else if (mu.xPos < closestrb.xPos) //South
                                {
                                    mu.Move(2);
                                }
                                else if (mu.yPos > closestrb.yPos) //West
                                {
                                    mu.Move(3);
                                }
                                else if (mu.yPos < closestrb.yPos) //East
                                {
                                    mu.Move(4);
                                }
                            }
                        }
                    }
                }
            }
            else if (map.Units[i] is RangedUnit)
            {
                RangedUnit ru = (RangedUnit)map.Units[i];
                if (ru.health <= ru.maxHealth * 0.25) // Running Away is commented out to make for a more interesting battle - and some insta-deaths
                {
                    ru.Move(r.Next(0, 4));
                }
                else
                {
                    (Unit closest, int distanceTo)           = ru.EnemyDistance(map.Units);
                    (Building closestbuilding, int distance) = ru.ClosestBuilding(map.Buildings);
                    if (distanceTo < distance)  //Checks if Unit is closer than building
                    {
                        //Check In Range
                        if (distanceTo <= ru.attackRange)
                        {
                            ru.isAttack = true;
                            ru.Combat(closest);
                            if (ru.IsDead == true)   //Returns resources if unit is dead
                            {
                                foreach (Building bg in map.Buildings)
                                {
                                    if (bg is ResourceBuilding)
                                    {
                                        ResourceBuilding rb = (ResourceBuilding)bg;
                                        if (ru.team == rb.team)
                                        {
                                            rb.ResourcesGenerated++;
                                        }
                                    }
                                }
                            }
                        }
                        else //Move Towards
                        {
                            if (closest is MeleeUnit)
                            {
                                MeleeUnit closestMu = (MeleeUnit)closest;
                                if (ru.xPos > closestMu.xPos) //North
                                {
                                    ru.Move(0);
                                }
                                else if (ru.xPos < closestMu.xPos) //South
                                {
                                    ru.Move(2);
                                }
                                else if (ru.yPos > closestMu.yPos) //West
                                {
                                    ru.Move(3);
                                }
                                else if (ru.yPos < closestMu.yPos) //East
                                {
                                    ru.Move(1);
                                }
                            }
                            else if (closest is RangedUnit)
                            {
                                RangedUnit closestRu = (RangedUnit)closest;
                                if (ru.xPos > closestRu.xPos) //North
                                {
                                    ru.Move(0);
                                }
                                else if (ru.xPos < closestRu.xPos) //South
                                {
                                    ru.Move(2);
                                }
                                else if (ru.yPos > closestRu.yPos) //West
                                {
                                    ru.Move(3);
                                }
                                else if (ru.yPos < closestRu.yPos) //East
                                {
                                    ru.Move(1);
                                }
                            }
                            if (closest is WizzardUnit)
                            {
                                WizzardUnit closestMu = (WizzardUnit)closest;
                                if (ru.xPos > closestMu.xPos) //North
                                {
                                    ru.Move(0);
                                }
                                else if (ru.xPos < closestMu.xPos) //South
                                {
                                    ru.Move(2);
                                }
                                else if (ru.yPos > closestMu.yPos) //West
                                {
                                    ru.Move(3);
                                }
                                else if (ru.yPos < closestMu.yPos) //East
                                {
                                    ru.Move(1);
                                }
                            }
                        }
                    }
                    else
                    {
                        //Check In Range
                        if (distanceTo <= ru.attackRange)
                        {
                            ru.isAttack = true;
                            ru.Combat(closest);
                        }
                        else //Move Towards
                        {
                            if (closestbuilding is FactoryBuilding)
                            {
                                FactoryBuilding closestfb = (FactoryBuilding)closestbuilding;
                                if (ru.xPos > closestfb.xPos) //North
                                {
                                    ru.Move(1);
                                }
                                else if (ru.xPos < closestfb.xPos) //South
                                {
                                    ru.Move(2);
                                }
                                else if (ru.yPos > closestfb.yPos) //West
                                {
                                    ru.Move(3);
                                }
                                else if (ru.yPos < closestfb.yPos) //East
                                {
                                    ru.Move(4);
                                }
                            }
                            else if (closestbuilding is ResourceBuilding)
                            {
                                ResourceBuilding closestrb = (ResourceBuilding)closestbuilding;
                                if (ru.xPos > closestrb.xPos) //North
                                {
                                    ru.Move(1);
                                }
                                else if (ru.xPos < closestrb.xPos) //South
                                {
                                    ru.Move(2);
                                }
                                else if (ru.yPos > closestrb.yPos) //West
                                {
                                    ru.Move(3);
                                }
                                else if (ru.yPos < closestrb.yPos) //East
                                {
                                    ru.Move(4);
                                }
                            }
                        }
                    }
                }
            }
            else if (map.Units[i] is WizzardUnit)
            {
                WizzardUnit wu = (WizzardUnit)map.Units[i];
                if (wu.health <= wu.maxHealth * 0.50) // Wizzards will run away when on half health or less
                {
                    wu.Move(r.Next(0, 4));
                }
                else
                {
                    (Unit closest, int distanceTo) = wu.EnemyDistance(map.Units);

                    //Check In Range
                    if (distanceTo <= wu.attackRange)
                    {
                        wu.isAttack = true;
                        wu.Combat(closest);
                    }
                    else //Move Towards
                    {
                        if (closest is MeleeUnit)
                        {
                            MeleeUnit closestMu = (MeleeUnit)closest;
                            if (wu.xPos > closestMu.xPos) //North
                            {
                                wu.Move(0);
                            }
                            else if (wu.xPos < closestMu.xPos) //South
                            {
                                wu.Move(2);
                            }
                            else if (wu.yPos > closestMu.yPos) //West
                            {
                                wu.Move(3);
                            }
                            else if (wu.yPos < closestMu.yPos) //East
                            {
                                wu.Move(1);
                            }
                        }
                        else if (closest is RangedUnit)
                        {
                            RangedUnit closestRu = (RangedUnit)closest;
                            if (wu.xPos > closestRu.xPos) //North
                            {
                                wu.Move(0);
                            }
                            else if (wu.xPos < closestRu.xPos) //South
                            {
                                wu.Move(2);
                            }
                            else if (wu.yPos > closestRu.yPos) //West
                            {
                                wu.Move(3);
                            }
                            else if (wu.yPos < closestRu.yPos) //East
                            {
                                wu.Move(1);
                            }
                        }
                    }
                }
            }
        }
    }