Beispiel #1
0
        public void SelectUnit(int pixelX, int pixelY)
        {
            var cube     = HexGridMath.HexToCube(HexGridMath.PixelToHex(pixelX, pixelY));
            var selected = HexGridMath.CubeToOffset(cube.X, cube.Y, cube.Z);

            var unit = _units.FindUnit(selected.Col, selected.Row);

            if (unit != null && _alliedEnemyMatrix.AreAlliedOrEqual(unit.Nationality, NATIONALITY.USA))
            {
                if (unit.TurnComplete && unit.Sleep == false)
                {
                    unit.Selected = false;
                    unit.Flash    = null;

                    return;
                }

                // unselect previously selected unit
                foreach (var otherUnit in _units)
                {
                    if (otherUnit != unit)
                    {
                        otherUnit.Selected = false;
                        otherUnit.Flash    = null;
                    }
                }

                unit.Selected     = true;
                unit.Sleep        = false;
                _units.IdleUnit   = unit;
                unit.Flash        = new UnitFlash(-1, 0.6f, new Color(100, 200, 100));
                ShowUnitRangeMask = true;
            }
        }
Beispiel #2
0
        public void PlayerDrawRoute(int pixelX, int pixelY)
        {
            var startUnit = _units.FindSelectedUnit();

            if (startUnit != null)
            {
                var cube        = HexGridMath.HexToCube(HexGridMath.PixelToHex(pixelX, pixelY));
                var destination = HexGridMath.CubeToOffset(cube.X, cube.Y, cube.Z);

                if (startUnit.Col != destination.Col || startUnit.Row != destination.Row)
                {
                    _shortestPath.ComputeShortestPath((TerrainMap)_terrainMap, startUnit.Col, startUnit.Row, destination.Col, destination.Row, startUnit.UnitType);

                    if (_shortestPath.WayPoint.Count > 0)
                    {
                        _shortestPath.WayPoint.Insert(0, new Offset(startUnit.Col, startUnit.Row));
                    }

                    for (int i = 1; i < _shortestPath.WayPoint.Count; i++)
                    {
                        GameContent.Sb.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
                        GraphicHelpers.DrawLine(_shortestPath.WayPoint[i - 1].ToPixel, _shortestPath.WayPoint[i].ToPixel, Color.Red, 6);
                        GameContent.Sb.End();
                    }
                }
            }
        }
        /// <summary>
        /// Find all adjacent cells that are passible by the unitType provided
        /// </summary>
        /// <param name="col"></param>
        /// <param name="row"></param>
        /// <param name="unitType"></param>
        /// <param name="range"></param>
        /// <returns></returns>
        public List <MapCoordinates> FindAdjacentCells(int col, int row, int unitType, int range)
        {
            var list = new List <MapCoordinates>();

            // first convert to cube coords
            var cube = HexGridMath.OffsetToCube(col, row);

            for (int dx = -range; dx <= range; dx++)
            {
                var maxDx = Math.Max(-range, -dx - range);
                var minDx = Math.Min(range, -dx + range);
                for (int dy = maxDx; dy <= minDx; dy++)
                {
                    var dz = -dx - dy;
                    if (dx != 0 || dy != 0 || dz != 0)                     // don't include the square occupied by the unit
                    {
                        var terrain = (TerrainCell)Map[FindMapHash(cube.X + dx, cube.Y + dy, cube.Z + dz)];
                        if (terrain != null)
                        {
                            if (unitType == -1 || (!terrain.Blocked(unitType)))                             // don't include any blocked terrain (mountains, oceans)
                            {
                                var offset = HexGridMath.CubeToOffset(cube.X + dx, cube.Y + dy, cube.Z + dz);
                                list.Add(new MapCoordinates(offset.Col, offset.Row));
                            }
                        }
                    }
                }
            }

            return(list);
        }
Beispiel #4
0
        public void PlayerUnitMouseUp(int pixelX, int pixelY)
        {
            var cube   = HexGridMath.HexToCube(HexGridMath.PixelToHex(pixelX, pixelY));
            var offset = HexGridMath.CubeToOffset(cube.X, cube.Y, cube.Z);

            var enemyUnit = _units.FindUnit(offset.Col, offset.Row);

            if (enemyUnit != null && _alliedEnemyMatrix.AreEnemies(NATIONALITY.USA, enemyUnit.Nationality))
            {
                AttackUnit(enemyUnit);
                return;
            }

            //TODO: need to tweak algorithm so an allied unit cannot slide past an enemy unit.  Can only back away to a square that has no enemy _units in any surrounding hex.

            // user just dropped the unit
            var unit = _units.FindSelectedUnit();

            if (unit != null)
            {
                if (unit.Movement < 0.1)
                {
                    return;
                }

                _units.IdleUnit = unit;

                //check to see if this is a new hex location
                if (unit.Col != offset.Col || unit.Row != offset.Row)
                {
                    //TODO: need to account for coordinates that are off the map (negatives, etc.)
                    int endCol = offset.Col;
                    int endRow = offset.Row;

                    // find the path
                    unit.FindShortestPath(_terrainMap, unit.Col, unit.Row, endCol, endRow, unit.UnitType);

                    _nextAlliedUnitToMove = unit;
                }
                else
                {
                    // user clicked on unit, wake unit and make this unit the active unit
                    if (!unit.TurnComplete)
                    {
                        unit.Sleep = false;
                    }
                }

                return;
            }

            _units.UnselectUnits();
        }