예제 #1
0
        public static List <Hex> GetHexOnHorizontalLine(Vector2 right, Vector2 left, RectangleMap map)
        {
            List <Hex> hexList = new List <Hex>();

            Hex hexLeft  = FractionalHex.HexRound(Layout.PixelToHex(map.MapLayout, Vector2ToPoint(left)));
            Hex hexRight = FractionalHex.HexRound(Layout.PixelToHex(map.MapLayout, Vector2ToPoint(right)));

            hexList.Add(hexRight);
            hexList.Add(hexLeft);

            //starting from the top and getting all the hexes till the bottom
            Hex     currentHex   = hexRight;
            Vector2 currentPoint = right;
            double  nudge        = 0.0001;

            while (!currentHex.Equals(hexLeft))
            {
                //get bottom point
                Point leastXCorner = Layout.HexCornerOffset(map.MapLayout, (int)Layout.PointyCorners.LEFTUP);
                Point center       = Layout.HexToPixel(map.MapLayout, currentHex);
                currentPoint.x = (float)(leastXCorner.x + center.x - nudge);
                if (currentPoint.x < left.x)
                {
                    break;
                }
                currentHex = FractionalHex.HexRound(Layout.PixelToHex(map.MapLayout, Vector2ToPoint(currentPoint)));
                hexList.Add(currentHex);
            }

            return(hexList);
        }
예제 #2
0
        public static List <Hex> GetHexOnVerticalLine(Vector2 top, Vector2 bottom, RectangleMap map)
        {
            List <Hex> hexList = new List <Hex>();

            Hex hexTop    = FractionalHex.HexRound(Layout.PixelToHex(map.MapLayout, Vector2ToPoint(top)));
            Hex hexBottom = FractionalHex.HexRound(Layout.PixelToHex(map.MapLayout, Vector2ToPoint(bottom)));

            hexList.Add(hexTop);
            hexList.Add(hexBottom);

            //starting from the top and getting all the hexes till the bottom
            Hex     currentHex   = hexTop;
            Vector2 currentPoint = top;
            double  nudge        = 0.0001;

            while (!currentHex.Equals(hexBottom))
            {
                //get bottom point
                Point bottomCorner = Layout.HexCornerOffset(map.MapLayout, (int)Layout.PointyCorners.DOWN);
                Point center       = Layout.HexToPixel(map.MapLayout, currentHex);
                currentPoint.y = (float)(bottomCorner.y + center.y - nudge);
                if (currentPoint.y < bottom.y)
                {
                    break;
                }
                currentHex = FractionalHex.HexRound(Layout.PixelToHex(map.MapLayout, Vector2ToPoint(currentPoint)));
                hexList.Add(currentHex);
            }

            return(hexList);
        }
예제 #3
0
        /// <summary>
        /// Before each test.
        /// </summary>
        public RectangleMapTests()
        {
            Map = new RectangleMap();
            var config = new PlanetMapConfiguration
            {
                Origin = Coordinates.Zero,
                Height = 5,
                Width  = 5,
            };

            Map.Configure(config);
        }
예제 #4
0
    void InitHexGrid(float mapWidth, float mapHeight, float hexDiminishFactor, float worldToGridRatio, List <NavMeshObstacle> obstacles, List <NavMeshActor> actors)
    {
        Obstacles  = new List <Rect>();
        ActorData  = new Dictionary <NavMeshActor, Vector2>();
        NavMap     = new RectangleMap(mapWidth * hexDiminishFactor, mapHeight * hexDiminishFactor, hexDiminishFactor, worldToGridRatio);
        BlockedHex = new HashSet <Hex>();
        foreach (NavMeshObstacle obstacle in obstacles)
        {
            AddObstacle(obstacle);
        }

        foreach (NavMeshActor actor in actors)
        {
            AddActor(actor);
        }
    }
예제 #5
0
    public void SetupMap()
    {
        map = new RectangleMap();

        ITilemap tilemap = (ITilemap)FormatterServices.GetUninitializedObject(typeof(ITilemap));

        typeof(ITilemap).GetField("m_Tilemap", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(tilemap, tilemap2D);

        foreach (Vector3Int position in tilemap2D.cellBounds.allPositionsWithin)
        {
            TileData tileData = new TileData();

            TileBase tilebase = tilemap2D.GetTile(position);

            if (tilebase != null)
            {
                tilebase.GetTileData(position, tilemap, ref tileData);

                if (onlyColliders && tileData.colliderType == Tile.ColliderType.None)
                {
                    continue;
                }

                RectangleTile tile = new RectangleTile();
                tile.position.x = position.x;
                tile.position.y = position.y;

                map.mapTiles.Add(tile);
            }
        }

        map.width  = tilemap2D.cellBounds.size.x * 2;
        map.height = tilemap2D.cellBounds.size.y * 2;

        map.Init();
    }
예제 #6
0
        public void ShouldComputeInstructions()
        {
            // Arrange
            var map = new RectangleMap();

            map.Configure(new PlanetMapConfiguration {
                Height = 5 + 1, Width = 3 + 1, Origin = Coordinates.Zero
            });

            var pairs = new Dictionary <Robot, IRobotActionHandler[]>
            {
                {
                    new Robot(new Coordinates(1, 1), Orientation.East),
                    new IRobotActionHandler[] {
                        TurnRightAction.Instance,
                        MoveForwardAction.Instance,
                        TurnRightAction.Instance,
                        MoveForwardAction.Instance,
                        TurnRightAction.Instance,
                        MoveForwardAction.Instance,
                        TurnRightAction.Instance,
                        MoveForwardAction.Instance,
                    }
                },
                {
                    new Robot(new Coordinates(3, 2), Orientation.North),
                    new IRobotActionHandler[] {
                        MoveForwardAction.Instance,
                        TurnRightAction.Instance,
                        TurnRightAction.Instance,
                        MoveForwardAction.Instance,
                        TurnLeftAction.Instance,
                        TurnLeftAction.Instance,
                        MoveForwardAction.Instance,
                        MoveForwardAction.Instance,
                        TurnRightAction.Instance,
                        TurnRightAction.Instance,
                        MoveForwardAction.Instance,
                        TurnLeftAction.Instance,
                        TurnLeftAction.Instance,
                    }
                },
                {
                    new Robot(new Coordinates(0, 3), Orientation.West),
                    new IRobotActionHandler[] {
                        TurnLeftAction.Instance,
                        TurnLeftAction.Instance,
                        MoveForwardAction.Instance,
                        MoveForwardAction.Instance,
                        MoveForwardAction.Instance,
                        TurnLeftAction.Instance,
                        MoveForwardAction.Instance,
                        TurnLeftAction.Instance,
                        MoveForwardAction.Instance,
                        TurnLeftAction.Instance
                    }
                },
            };

            // Act
            var control = new MissionControl();

            control.MapPlanet(map);

            foreach (var pair in pairs)
            {
                control.DeployRobot(pair.Key);
                foreach (var instruction in pair.Value)
                {
                    control.CommandRobot(instruction);
                }
            }

            // Assert
            control.Robots
            .Should()
            .HaveCount(3);

            control.Robots
            .Should()
            .ContainSingle(r => r.Coordinates == new Coordinates(1, 1, 0) && r.Orientation == Orientation.East);

            control.Robots
            .Should()
            .ContainSingle(r => r.Coordinates == new Coordinates(3, 3, 0) && r.Orientation == Orientation.North &&
                           r.Troubles.OfType <LostRobotTrouble>().Any());

            control.Robots
            .Should()
            .ContainSingle(r => r.Coordinates == new Coordinates(2, 3, 0) && r.Orientation == Orientation.South);
        }
예제 #7
0
        public static HashSet <Hex> BoxToHexList(Vector2 boxCenter, Vector2 boxDimensions, RectangleMap map)
        {
            HashSet <Hex> hexList = new HashSet <Hex>();

            Vector2 boxTopLeft     = new Vector2((boxCenter.x - boxDimensions.x / 2), (boxCenter.y + boxDimensions.y / 2));
            Vector2 boxBottomLeft  = CopyVector2(boxCenter - (boxDimensions / 2));
            Vector2 boxBottomRight = new Vector2((boxCenter.x + boxDimensions.x / 2), (boxCenter.y - boxDimensions.y / 2));
            Vector2 boxTopRight    = CopyVector2(boxCenter + (boxDimensions / 2));

            hexList.UnionWith(GetHexOnVerticalLine(boxTopLeft, boxBottomLeft, map));
            hexList.UnionWith(GetHexOnVerticalLine(boxTopRight, boxBottomRight, map));

            hexList.UnionWith(GetHexOnHorizontalLine(boxTopRight, boxTopLeft, map));
            hexList.UnionWith(GetHexOnHorizontalLine(boxBottomRight, boxBottomLeft, map));

            return(hexList);
        }