Beispiel #1
0
        public void GenerateBattleField()
        {
            Battlefield = new IUnit[gridSizeX, gridSizeY];

            for (int i = 0; i < numberOfUnits; i++)
            {
                Unit unit = GenerateRandomUnit(i);
                units.Add(unit);
            }

            for (int i = 0; i < numberOfBuildings / 2; i++)
            {
                Building building = GenerateBuilding();
                building.unitNumber = i;
                buildings.Add(building);
            }

            for (int i = 0; i < numberOfUnits / 2; i++)
            {
                Point      point  = GetRandomOpenPosition();
                int        xPos   = point.xPos;
                int        yPos   = point.yPos;
                WizardUnit wizard = new WizardUnit("Wizard", xPos, yPos, 10, rand.Next(1, 4), 4, "Neutral", 'W');
                units.Add(wizard);
            }
        }
Beispiel #2
0
        private void PerformAction(Unit aUnit)
        {
            if (aUnit is RangedUnit)
            {
                RangedUnit lUnit        = aUnit as RangedUnit;
                IUnit      closestEnemy = aUnit.FindClosestEnemy(map.Battlefield);

                if (closestEnemy != null && lUnit.Health >= 0.25 * lUnit.MaxHealth)
                {
                    if (lUnit.RangeCheck(closestEnemy))
                    {
                        lUnit.EngageUnit(closestEnemy);
                        lUnit.IsAttacking = true;
                    }
                    else if (roundsCompleted % lUnit.Speed == 0)
                    {
                        if (closestEnemy is IPosition)
                        { //Move toward the enemy
                            var lTarget = closestEnemy as IPosition;
                            int differenceInXPosition = Math.Abs(lUnit.xPos - lTarget.xPos);
                            int differenceInYPosition = Math.Abs(lUnit.yPos - lTarget.yPos);
                            if (differenceInXPosition > differenceInYPosition)
                            { //Move vertical
                                if (lUnit.yPos <= lTarget.yPos)
                                {
                                    lUnit.Move(Unit.Direction.Up);
                                }
                                else if (lUnit.yPos > lTarget.yPos)
                                {
                                    lUnit.Move(Unit.Direction.Down);
                                }
                            }
                            else if (differenceInXPosition > differenceInYPosition)
                            { //Move horizontal
                                if (lUnit.xPos <= lTarget.xPos)
                                {
                                    lUnit.Move(Unit.Direction.Right);
                                }
                                else if (lUnit.xPos > lTarget.xPos)
                                {
                                    lUnit.Move(Unit.Direction.Left);
                                }
                            }
                            else
                            {
                                lUnit.Move(Unit.Direction.Up);
                            }
                        }
                    }
                    else if (lUnit.Health < 0.25 * lUnit.MaxHealth)
                    {
                        lUnit.Move(RandomDirection());
                    }
                }
            }
            if (aUnit is MeleeUnit)
            {
                MeleeUnit lUnit        = aUnit as MeleeUnit;
                IUnit     closestEnemy = lUnit.FindClosestEnemy(map.Battlefield);

                if (closestEnemy != null && lUnit.Health >= 0.25 * lUnit.MaxHealth)
                {
                    if (lUnit.RangeCheck(closestEnemy))
                    {
                        lUnit.EngageUnit(closestEnemy);
                        lUnit.IsAttacking = true;
                    }
                    else if (roundsCompleted % lUnit.Speed == 0)
                    {
                        if (closestEnemy is IPosition)
                        { //Move toward the enemy
                            var lTarget = closestEnemy as IPosition;
                            int differenceInXPosition = Math.Abs(lUnit.xPos - lTarget.xPos);
                            int differenceInYPosition = Math.Abs(lUnit.yPos - lTarget.yPos);
                            if (differenceInXPosition > differenceInYPosition)
                            { //Move vertical
                                if (lUnit.yPos <= lTarget.yPos)
                                {
                                    lUnit.Move(Unit.Direction.Up);
                                }
                                else if (lUnit.yPos > lTarget.yPos)
                                {
                                    lUnit.Move(Unit.Direction.Down);
                                }
                            }
                            else if (differenceInXPosition > differenceInYPosition)
                            { //Move horizontal
                                if (lUnit.xPos <= lTarget.xPos)
                                {
                                    lUnit.Move(Unit.Direction.Right);
                                }
                                else if (lUnit.xPos > lTarget.xPos)
                                {
                                    lUnit.Move(Unit.Direction.Left);
                                }
                            }
                            else
                            {
                                lUnit.Move(Unit.Direction.Up);
                            }
                        }
                    }
                }
                else if (lUnit.Health < 0.25 * lUnit.MaxHealth)
                {
                    lUnit.Move(RandomDirection());
                }
            }
            if (aUnit is WizardUnit)
            {
                WizardUnit lUnit        = aUnit as WizardUnit;
                IUnit      closestEnemy = lUnit.FindClosestEnemy(map.Battlefield);

                if (closestEnemy != null && lUnit.Health >= 0.5 * lUnit.MaxHealth)
                {
                    if (lUnit.RangeCheck(closestEnemy))
                    {
                        lUnit.AOEDamage(map.units);
                        lUnit.IsAttacking = true;
                    }
                    else if (roundsCompleted % lUnit.Speed == 0)
                    {
                        if (closestEnemy is IPosition)
                        { //Move toward the enemy
                            var lTarget = closestEnemy as IPosition;
                            int differenceInXPosition = Math.Abs(lUnit.xPos - lTarget.xPos);
                            int differenceInYPosition = Math.Abs(lUnit.yPos - lTarget.yPos);
                            if (differenceInXPosition > differenceInYPosition)
                            { //Move vertical
                                if (lUnit.yPos <= lTarget.yPos)
                                {
                                    lUnit.Move(Unit.Direction.Up);
                                }
                                else if (lUnit.yPos > lTarget.yPos)
                                {
                                    lUnit.Move(Unit.Direction.Down);
                                }
                            }
                            else if (differenceInXPosition > differenceInYPosition)
                            { //Move horizontal
                                if (lUnit.xPos <= lTarget.xPos)
                                {
                                    lUnit.Move(Unit.Direction.Right);
                                }
                                else if (lUnit.xPos > lTarget.xPos)
                                {
                                    lUnit.Move(Unit.Direction.Left);
                                }
                            }
                            else
                            {
                                lUnit.Move(Unit.Direction.Up);
                            }
                        }
                    }
                }
                else if (lUnit.Health < 0.25 * lUnit.MaxHealth)
                {
                    lUnit.Move(RandomDirection());
                }
            }
        }