Ejemplo n.º 1
0
        private bool Intersect(Cell cell)
        {
            var circleDistance = new Vector2f(
                Math.Abs(cell.Position.X - (Position.X + Size.X / 2)),
                Math.Abs(cell.Position.Y - (Position.Y + Size.Y / 2)));
            var sizeAbs = new Vector2f(Math.Abs(Size.X), Math.Abs(Size.Y));

            if (circleDistance.X > (sizeAbs.X / 2 + cell.Radius))
            {
                return false;
            }

            if (circleDistance.Y > (sizeAbs.Y / 2 + cell.Radius))
            {
                return false;
            }

            if (circleDistance.X <= sizeAbs.X / 2)
            {
                return true;
            }

            if (circleDistance.Y <= sizeAbs.Y / 2)
            {
                return true;
            }

            var cornerDistance = (circleDistance.X - sizeAbs.X / 2) * (circleDistance.X - sizeAbs.X / 2)
                                 + (circleDistance.Y - sizeAbs.Y / 2) * (circleDistance.Y - sizeAbs.Y / 2);

            return cornerDistance <= cell.RadiusPow;
        }
Ejemplo n.º 2
0
Archivo: AI.cs Proyecto: Yozer/NanoWar
        protected void Attack(Cell fromCell, Cell toCell, int units)
        {
            if (fromCell.Units - units < 1 || units <= 0)
            {
                return;
            }

            var unitCell = new UnitCell(toCell, fromCell, units, _aiPlayerInstance);
            fromCell.Player.AddUnitCell(unitCell);
        }
Ejemplo n.º 3
0
        public UnitCell(Cell targetCell, Cell sourceCell, int units, PlayerInstance player)
        {
            TargetCell = targetCell;
            SourceCell = sourceCell;
            Units = UnitsLeft = units;
            _currentVelocity = Velocity;

            string colorName = null;
            if (SourceCell.Player.Color.R == 255 && SourceCell.Player.Color.G == 0 && SourceCell.Player.Color.B == 0)
            {
                colorName = "red";
            }
            else if (SourceCell.Player.Color.R == 0 && SourceCell.Player.Color.G == 255 && SourceCell.Player.Color.B == 0)
            {
                colorName = "green";
            }
            else if (SourceCell.Player.Color.R == 0 && SourceCell.Player.Color.G == 0
                     && SourceCell.Player.Color.B == 255)
            {
                colorName = "blue";
            }

            _sprite = new Sprite(ResourceManager.Instance["game/unit_cell_" + colorName] as Texture);

            _text = new Text(units.ToString(), ResourceManager.Instance["fonts/verdana"] as Font, FontSize);
            _text.Origin = new Vector2f(
                _text.GetLocalBounds().Left + _text.GetLocalBounds().Width / 2,
                _text.GetLocalBounds().Top);

            _particleSystem = new ParticleSystem(ResourceManager.Instance["game/particle"] as Texture);

            var scale = 0.5f + Units * 0.0125f;
            if (scale >= 1f)
            {
                scale = 1f;
            }

            Scale = _initialScale = new Vector2f(scale, scale);

            Radius = _sprite.GetGlobalBounds().Height / 2;
            _sprite.Origin = new Vector2f(_sprite.GetLocalBounds().Width / 2, _sprite.GetLocalBounds().Height / 2);
            SourcePlayer = player;
        }
Ejemplo n.º 4
0
        protected void GenerateCells()
        {
            var id = 0;
            var random = new Random();

            // rand cells
            var cellsNumPerPlayer = random.Next(2, 4);
            var neutralCells = random.Next(4, 7);

            var cellTypes = new List<Cell.CellSizeEnum>();
            var cellUnits = new List<int>();

            for (var i = 0; i < cellsNumPerPlayer; i++)
            {
                cellTypes.Add((Cell.CellSizeEnum)random.Next(1, 5));
            }

            for (var i = 0; i < cellsNumPerPlayer; i++)
            {
                cellUnits.Add(random.Next(10, Cell.CellSettingsDic[cellTypes[i]].MaxUnits));
            }

            for (var i = 0; i < neutralCells; i++)
            {
                cellTypes.Add((Cell.CellSizeEnum)random.Next(1, 5));
            }

            for (var i = 0; i < neutralCells; i++)
            {
                cellUnits.Add(random.Next(5, Cell.CellSettingsDic[cellTypes[i]].MaxUnits / 3));
            }

            var playerMaxUnits = cellUnits.Take(cellsNumPerPlayer).Max() - 1;

            // all neutral units ale bigger than max player unit fix it
            if (cellUnits.TrueForAll(t => t > playerMaxUnits))
            {
                cellUnits[cellTypes.IndexOf((Cell.CellSizeEnum)cellTypes.Min(t => (int)t))] = playerMaxUnits - 1;
            }

            List<float> distances = new List<float>();
            Cell centralCellMain = null;

            foreach (var playerInstance in Game.Instance.AllPlayers.Values)
            {
                var centralCell = new Cell(Vector2f.Zero, cellUnits[0], cellTypes[0], id++);

                RandPositionForCell(centralCell, random, centralCellMain == null ? new Vector2f(Game.Instance.Width / 2, Game.Instance.Height / 2) : centralCellMain.Position, centralCellMain == null ? 300 : 100);
                playerInstance.AddCell(centralCell);
                AllCells.Add(centralCell);

                if (centralCellMain == null)
                {
                    centralCellMain = centralCell;
                }

                for (var i = 1; i < cellsNumPerPlayer; i++)
                {
                    var cell = new Cell(Vector2f.Zero, cellUnits[i], cellTypes[i], id++);
                    if (distances.Count < i)
                    {
                        RandPositionForCell(cell, random, centralCellMain.Position, random.Next(200, 800));
                        distances.Add(cell.GetDistanceBetweenCells(centralCell));
                    }
                    else
                    {
                        RandPositionForCell(cell, random, centralCell.Position, distances[i - 1]);
                    }
                    playerInstance.AddCell(cell);
                    AllCells.Add(cell);
                }
            }

            for (var i = 0; i < neutralCells; i++)
            {
                var cell = new Cell(Vector2f.Zero, cellUnits[i], cellTypes[i], id++);
                RandPositionForCell(cell, random, Vector2f.Zero);
                AllCells.Add(cell);
            }
        }
Ejemplo n.º 5
0
        protected void RandPositionForCell(Cell cell, Random random, Vector2f centralCell, float distance = -1)
        {
            const float padding = 35f;
            int counter = 0;
            do
            {
                var position = new Vector2f(
                        random.Next((int)(cell.Radius + padding), (int)(Game.Instance.Width - cell.Radius - padding)),
                        random.Next((int)(cell.Radius + padding + 50f), (int)(Game.Instance.Height - cell.Radius - padding)));
                cell.Position = position;

                if (!AllCells.Any(t => t.IntersectCircle(cell, padding)))
                {
                    if (distance == -1)
                    {
                        return;
                    }

                    float currentDistance = MathHelper.DistanceBetweenTwoPints(cell.Position, centralCell);
                    if (Math.Abs(currentDistance - distance) <= 150f && counter <= 1000)
                    {
                        return;
                    }
                }

                counter++;
            } while (true);
        }
Ejemplo n.º 6
0
Archivo: AI.cs Proyecto: Yozer/NanoWar
 protected IEnumerable<Cell> GetCellsNearestTo(Cell sourceCell, IEnumerable<Cell> cells)
 {
     return cells.OrderBy(t => t.GetDistanceBetweenCells(sourceCell));
 }
Ejemplo n.º 7
0
Archivo: AI.cs Proyecto: Yozer/NanoWar
 protected IEnumerable<Cell> GetCellsNearestTo(Cell sourceCell)
 {
     return GetCellsNearestTo(sourceCell, _allCells.Except(_aiPlayerInstance.Cells));
 }
Ejemplo n.º 8
0
Archivo: AI.cs Proyecto: Yozer/NanoWar
 protected bool CanTakeCell(Cell attackCell, Cell targetCell)
 {
     return attackCell.Units - 1 > targetCell.Units;
 }
Ejemplo n.º 9
0
Archivo: AI.cs Proyecto: Yozer/NanoWar
 protected void Attack(Cell fromCell, Cell toCell)
 {
     var unitCell = new UnitCell(toCell, fromCell, fromCell.Units - 1, _aiPlayerInstance);
     fromCell.Player.AddUnitCell(unitCell);
 }
Ejemplo n.º 10
0
Archivo: AI.cs Proyecto: Yozer/NanoWar
 protected int MaxHelpValue(Cell helper)
 {
     return helper.Units
            - Game.Instance.Player.UnitCells.Where(t => Equals(t.TargetCell, helper)).Sum(t => t.Units);
 }
Ejemplo n.º 11
0
Archivo: AI.cs Proyecto: Yozer/NanoWar
 protected int GetUnitsDiffrence(Cell cell)
 {
     return _aiPlayerInstance.UnitCells.Where(t => Equals(t.TargetCell, cell)).Sum(t => t.UnitsLeft)
            - Game.Instance.Player.UnitCells.Where(t => Equals(t.TargetCell, cell)).Sum(t => t.UnitsLeft)
            + (cell.Units * (Equals(cell.Player, _aiPlayerInstance) ? 1 : -1));
 }
Ejemplo n.º 12
0
Archivo: AI.cs Proyecto: Yozer/NanoWar
 protected IEnumerable<Cell> GetCellsNearestTo(Cell sourceCell, IEnumerable<Cell> cells, float distance)
 {
     return
         cells.Select(t => new { cell = t, distance = t.GetDistanceBetweenCells(sourceCell) })
             .Where(t => t.distance <= distance)
             .OrderBy(t => t.distance)
             .Select(t => t.cell);
 }
Ejemplo n.º 13
0
 public void AddCell(Cell cell)
 {
     cell.Player = this;
     Cells.Add(cell);
 }