コード例 #1
0
 /// <summary>
 /// The opposite of claiming a hex.
 /// </summary>
 /// <param name="a">The axialcoordinate of the hex you want to unclaim.</param>
 public void FreeHex (AxialCoordinate a) 
 {
   if (_hexes.Contains(a)) {
     _grid.GetHexagonAtPosition(a).Unclaim();
     _hexes.Remove(a);
   } 
 }
コード例 #2
0
        private static Snapshot CreateSnapshot(Arguments arguments)
        {
            var snapshot = new Snapshot();

            AddPlayerSpawner(snapshot);
            AddSpawnController(snapshot);

            var halfWidth = arguments.WorldDimensions.x / 2f;
            var halfHeight = arguments.WorldDimensions.y / 2f;

            for (var i = 0; i < arguments.NumResourceNodes; i += 1)
            {
                // Generate random grid position that is within the real world bounds.
                //
                // TODO: Use a better algorithm for picking random grid coordinates.
                // We're currently picking random spots in a skewed rectangle, and
                // it would better to more evenly distrubte the nodes around the
                // center of the map.
                AxialCoordinate gridPos;
                Vector2 worldPos;
                do
                {
                    gridPos = new AxialCoordinate(
                        (int)Random.Range(-halfWidth, halfWidth),
                        (int)Random.Range(-halfHeight, halfHeight));
                    worldPos = HexUtils.GridToWorld(gridPos);
                } while (Mathf.Abs(worldPos.x) >= halfWidth || Mathf.Abs(worldPos.y) >= halfHeight);

                var type = Random.value < 0.5f ? ResourceType.Coal : ResourceType.Copper;

                AddResourceNode(snapshot, gridPos, type);
            }

            return snapshot;
        }
コード例 #3
0
 public void AddToSector (AxialCoordinate a)
 {
   if (!sector.Contains(a)) {
     sector.Add(a);
     _grid.GetHexagonAtPosition(a).SetSectorId(SectorId);
   }
 }
コード例 #4
0
        private void Update()
        {
            // TODO: Use a better source of pointer input than Input.mousePosition.
            var viewportPosition = Input.mousePosition;

            viewportPosition.z = _camera.transform.position.y;
            var worldPosition = _camera.ScreenToWorldPoint(viewportPosition);

            // Snap the cursor display to the cell the mouse is over.
            _cursorGridPosition        = HexUtils.WorldToGrid(new Vector2(worldPosition.x, worldPosition.z));
            worldPosition              = HexUtils.GridToWorld(_cursorGridPosition);
            _cursor.transform.position = new Vector3(worldPosition.x, 0f, worldPosition.y);

            // TODO: Use a better source of input than Input. It would be nice to
            // use the EventSystem, but it's not clear how use it when clicking on
            // empty space.
            if (Input.GetMouseButtonDown(0) && _selectedDrone != null)
            {
                var command = new MoveToPosition
                {
                    Target = _cursorGridPosition,
                };

                AddCommandToQueue(CommandType.MoveToPosition, JsonUtility.ToJson(command), 5);
            }
        }
コード例 #5
0
    public void FullTriangleTest()
    {
        // Add a traingle to a hexagon, then test traingle
        PartData partData = new PartData();
        Part     player   = new Part {
            shape = new GameObject(), type = 1
        };
        Part triangle = new Part {
            shape = new GameObject(), type = 0
        };
        AxialCoordinate playerCoord = new AxialCoordinate {
            x = 0, y = 0
        };
        AxialCoordinate triangleCoord = new AxialCoordinate {
            x = -1, y = 0
        };

        partData.addPart(playerCoord, player);
        partData.addPart(triangleCoord, triangle);

        // Get all full neighbours (ie, places to attach objects - there should be
        // 1 for a triangle)
        List <AxialCoordinate> neighbours = partData.getFullNeighbors(triangleCoord);

        // If 1 object attached, pass
        Assert.True(neighbours.Count == 1);
    }
コード例 #6
0
    public void FullPlayerTest()
    {
        // Add a heaxgon to player, then test player
        PartData partData = new PartData();
        Part     player   = new Part {
            shape = new GameObject(), type = 1
        };
        Part hexagon = new Part {
            shape = new GameObject(), type = 0
        };
        AxialCoordinate playerCoord = new AxialCoordinate {
            x = 0, y = 0
        };
        AxialCoordinate hexCoord = new AxialCoordinate {
            x = -1, y = 0
        };

        partData.addPart(playerCoord, player);
        partData.addPart(hexCoord, hexagon);

        // Get all attached neighbours
        List <AxialCoordinate> neighbours = partData.getFullNeighbors(playerCoord);

        // If 1 attached neighbour, pass
        Assert.True(neighbours.Count == 1);
    }
コード例 #7
0
    public void PathExistsTest()
    {
        // Add a row of three hexagons including player
        PartData partData = new PartData();
        Part     player   = new Part {
            shape = new GameObject(), type = 1
        };
        Part hexagon = new Part {
            shape = new GameObject(), type = 0
        };
        AxialCoordinate playerCoord = new AxialCoordinate {
            x = 0, y = 0
        };
        AxialCoordinate hexCoord = new AxialCoordinate {
            x = -1, y = 0
        };
        AxialCoordinate hex2Coord = new AxialCoordinate {
            x = -2, y = 1
        };

        partData.addPart(playerCoord, player);
        partData.addPart(hexCoord, hexagon);
        partData.addPart(hex2Coord, hexagon);

        // Find all destroyed parts if middle hexagon hit
        List <AxialCoordinate> neighbours = partData.findDestroyedPartLocations(hexCoord);

        // If 2 object destroyed, pass
        Assert.True(neighbours.Count == 2);
    }
コード例 #8
0
        public IEnumerable <T> GetMembersAtLocation <T>(AxialCoordinate position) where T : ITilemapMember
        {
            var positionList = GetListFromCoord(position);

            return(positionList?
                   .OfType <T>());
        }
コード例 #9
0
ファイル: Hexagon.cs プロジェクト: Helliaca/CSCW-Project
    public void toPosition(AxialCoordinate pos)
    {
        this.pos = pos;
        Vector2 worldPos = pos.toWorld(size);

        transform.position = new Vector3(worldPos.x, pos.y, worldPos.y);
    }
コード例 #10
0
        public static EntityTemplate Drone(AxialCoordinate position, string playerId)
        {
            // Calculate the world position based off the grid position.
            var worldPosition = HexUtils.GridToWorld(position);

            var entityTemplate = new EntityTemplate();

            entityTemplate.AddComponent(
                new Drone.Snapshot {
                Owner = playerId, CommandQueue = new List <Command>()
            },
                WorkerUtils.UnityGameLogic);

            entityTemplate.AddComponent(
                new Position.Snapshot(new Coordinates
            {
                X = worldPosition.x,
                Z = worldPosition.y
            }),
                WorkerUtils.UnityGameLogic);

            entityTemplate.AddComponent(
                new PlayerInventory.Snapshot(0, 0),
                WorkerUtils.UnityGameLogic);

            entityTemplate.AddComponent(new Metadata.Snapshot("Drone"), WorkerUtils.UnityGameLogic);
            entityTemplate.AddComponent(new Persistence.Snapshot(), WorkerUtils.UnityGameLogic);
            entityTemplate.SetReadAccess(WorkerUtils.AllWorkers);
            entityTemplate.SetComponentWriteAccess(EntityAcl.ComponentId, WorkerUtils.UnityGameLogic);

            return(entityTemplate);
        }
コード例 #11
0
    // used to get the neighbours of an axial coordinate

    /// <summary>
    /// Returns all the coordinates of the neighbouring hexes.
    /// </summary>
    /// <returns>The neighbours.</returns>
    /// <param name="a">The axialcoordinate you want to know the neighbours of.</param>
    public static AxialCoordinate[] GetNeighbours (AxialCoordinate a)
    {
      AxialCoordinate[] neighbours = new AxialCoordinate[6];
      for (byte i = 0; i < 6; i++) {
        neighbours [i] = GetNeighbour (a, i);
      }
      return neighbours;
    }
コード例 #12
0
 public IEnumerable <T> GetItemsWithinJumpDistance <T>(AxialCoordinate origin, int jumpDistance)
 {
     return(GetPositionsWithinJumpDistance(origin, jumpDistance)
            .Select(position => GetItemsAtLocation <T>(position))
            .Where(items => items != null)
            .SelectMany(items => items)
            .Where(x => x != null));
 }
コード例 #13
0
        public IEnumerable <T> GetMembersAtLocation <T>(AxialCoordinate position, Func <T, bool> filter) where T : ITilemapMember
        {
            var positionList = GetListFromCoord(position);

            return(positionList?
                   .OfType <T>()
                   .Where(member => filter(member)));
        }
コード例 #14
0
        public IEnumerable <T> GetItemsAtLocation <T>(AxialCoordinate position)
        {
            var positionList = GetListFromCoord(position);

            return(positionList?
                   .Select(member => member.TryGetType <T>())
                   .Where(x => x != null));
        }
コード例 #15
0
 public IEnumerable <ITilemapMember> GetMembersWithinJumpDistanceSlow(AxialCoordinate origin, int jumpDistance)
 {
     return(GetPositionsWithinJumpDistance(origin, jumpDistance)
            .Select(position => GetListFromCoord(position))
            .Where(members => members != null)
            .SelectMany(members => members)
            .Where(member => member != null));
 }
コード例 #16
0
        public void AddLastWaypoint(AxialCoordinate waypoint)
        {
            var distance = PeekNext().ToCube().DistanceTo(waypoint.ToCube());

            if (distance > 1)
            {
                throw new System.Exception($"Attempted to add {waypoint} to route ending in {PeekNext()}. distance between points is {distance}");
            }
            waypoints.Add(waypoint);
        }
コード例 #17
0
 bool isOccupied(AxialCoordinate place)
 {
     foreach (AxialCoordinate c in occupied)
     {
         if (c.q == place.q && c.r == place.r)
         {
             return(true);
         }
     }
     return(false);
 }
コード例 #18
0
        public void ShouldGetAllPositionsWithinDistanceWhenCrossDomain()
        {
            var offsetToCrossDomain = new AxialCoordinate(-4, -2);
            var coord = new AxialCoordinate(3, 2) + offsetToCrossDomain;
            //var realOffsetCoord = new AxialCoordinate(coord.x, coord.y);
            var expectedCoordsInDistance = new[]
            {
                new AxialCoordinate(3, 2),

                new AxialCoordinate(3, 3),
                new AxialCoordinate(2, 3),
                new AxialCoordinate(4, 2),
                new AxialCoordinate(2, 2),
                new AxialCoordinate(4, 1),
                new AxialCoordinate(3, 1),

                new AxialCoordinate(3, 4),
                new AxialCoordinate(2, 4),
                new AxialCoordinate(4, 3),
                new AxialCoordinate(1, 4),
                new AxialCoordinate(5, 2),
                new AxialCoordinate(1, 3),
                new AxialCoordinate(5, 1),
                new AxialCoordinate(1, 2),
                new AxialCoordinate(5, 0),
                new AxialCoordinate(2, 1),
                new AxialCoordinate(4, 0),
                new AxialCoordinate(3, 0),
            }
            .Select(x => x + offsetToCrossDomain)
            .OrderBy(x => x.q)
            .ThenBy(x => x.r)
            .ToList();

            var coordSystem = new HexCoordinateSystem(2);

            var actualJumps = HexCoordinateSystem.GetPositionsWithinJumpDistance(coord, 2)
                              .OrderBy(x => x.q)
                              .ThenBy(x => x.r)
                              .ToList();

            Assert.AreEqual(expectedCoordsInDistance.Count, actualJumps.Count);

            foreach (var pair in expectedCoordsInDistance
                     .Zip(actualJumps, (a, b) => new { expected = a, actual = b }))
            {
                Assert.AreEqual(
                    pair.expected,
                    pair.actual);
            }
        }
コード例 #19
0
        public IEnumerable <ITilemapMember> GetMembersWithinJumpDistanceByChunk(AxialCoordinate origin, int jumpDistance)
        {
            var chunk = this.GetIndexInChunkMap(origin);
            var originOffsetByDistance = origin + new AxialCoordinate(jumpDistance, 0);
            var maxChunkOffset         = GetIndexInChunkMap(originOffsetByDistance);

            // the distance between these two points will always be the maximum distance we need to traverse
            //  in chunk-space to encompass every point in regular-space that is JumpDistance or more distance away
            var chunkDistance = chunk.DistanceTo(maxChunkOffset);

            return(HexCoordinateSystem.GetPositionsWithinJumpDistance(chunk.ToAxial(), chunkDistance)
                   .Where(pos => tileMemberChunkMap.ContainsKey(pos))
                   .SelectMany(pos => tileMemberChunkMap[pos]));
        }
コード例 #20
0
    public void AddPartTest()
    {
        // Test randomly attached part
        PartData        partData = new PartData();
        AxialCoordinate coord    = new AxialCoordinate {
            x = -1, y = 0
        };
        Part addedPart = new Part {
            shape = new GameObject(), type = -1
        };

        partData.addPart(coord, addedPart);

        // If false, means coord is occupied
        Assert.False(partData.checkPart(coord));
    }
コード例 #21
0
    public void RemovePartTest()
    {
        // Test randomly attached part
        PartData        partData = new PartData();
        AxialCoordinate coord    = new AxialCoordinate {
            x = -1, y = 0
        };
        Part addedPart = new Part {
            shape = new GameObject(), type = -1
        };

        partData.addPart(coord, addedPart);

        partData.removePart(coord);
        // If null, part is not present
        Assert.Null(partData.getPart(coord));
    }
コード例 #22
0
 /// <summary>
 /// Method that checks if a hex is claimable and if so claims it.
 /// </summary>
 /// <param name="a">The axialcoordinate of the hex to claim.</param>
 public bool TakeHex (AxialCoordinate a)
 {
   Hexagon h = _grid.GetHexagonAtPosition(a);
   if (h != null) {
     if (!h.occupied) {
       if (_hexes.Count == 0 || _claimable.Contains(a)) {
         if (h.GetPartyId() != 0) {
           _partyManager.parties[h.GetPartyId()-1].FreeHex(a);
         }
         h.Claim(PartyId, _partyColor);
         _hexes.Add(a); 
         return true;
       }
     }
   }
   return false;
 }
コード例 #23
0
        private static void AddResourceNode(Snapshot snapshot, AxialCoordinate position, ResourceType type)
        {
            var template = new EntityTemplate();

            var worldPos = HexUtils.GridToWorld(position);
            template.AddComponent(new Position.Snapshot(new Coordinates(worldPos.x, 0, worldPos.y)), WorkerUtils.UnityGameLogic);
            template.AddComponent(
                new Metadata.Snapshot { EntityType = "ResourceNode" },
                WorkerUtils.UnityGameLogic);
            template.AddComponent(new Persistence.Snapshot(), WorkerUtils.UnityGameLogic);
            template.AddComponent(new ResourceNode.Snapshot(type, 100), WorkerUtils.UnityGameLogic);
            template.AddComponent(new GridPosition.Snapshot(position.ToGridCoordinate()), WorkerUtils.UnityGameLogic);

            template.SetReadAccess(WorkerUtils.AllWorkers);
            template.SetComponentWriteAccess(EntityAcl.ComponentId, WorkerUtils.UnityGameLogic);

            snapshot.AddEntity(template);
        }
        private void OnSpawnDroneRequest(SpawnController.SpawnDrone.ReceivedRequest request)
        {
            Debug.Log($"Handling spawn drone request {request.EntityId} from {request.CallerWorkerId}");

            // Generate a random starting point for the drone.
            var position = new AxialCoordinate(
                Random.Range(-10, 10),
                Random.Range(-10, 10));

            // Create an entity for the drone, and once it's been made, respond to the player.
            //
            // TODO: Can we do this with async/await, rather than callbacks?
            _worldSender.SendCreateEntityCommand(
                new WorldCommands.CreateEntity.Request(EntityTemplates.Drone(position, request.CallerWorkerId)),
                response =>
            {
                _commandReceiver.SendSpawnDroneResponse(request.RequestId, new SpawnDroneResponse());
            });
        }
コード例 #25
0
    public void EmptyHexagonTest()
    {
        // Add player
        PartData partData = new PartData();
        Part     hexagon  = new Part {
            shape = new GameObject(), type = 0
        };
        AxialCoordinate coord = new AxialCoordinate {
            x = 0, y = 0
        };

        partData.addPart(coord, hexagon);

        // Get all empty neighbours (ie, places to attach objects)
        List <AxialCoordinate> neighbours = partData.getEmptyNeighbors(coord);

        // If 6 free edges, pass
        Assert.True(neighbours.Count == 6);
    }
コード例 #26
0
    public void EmptyTriangleTest()
    {
        // Add player
        PartData partData = new PartData();
        Part     triangle = new Part {
            shape = new GameObject(), type = -1
        };
        AxialCoordinate coord = new AxialCoordinate {
            x = 0, y = 0
        };

        partData.addPart(coord, triangle);

        // Get all empty neighbours (ie, places to attach objects - there should be
        // 0 for a triangle)
        List <AxialCoordinate> neighbours = partData.getEmptyNeighbors(coord);

        // If 0 free edges, pass
        Assert.True(neighbours.Count == 0);
    }
コード例 #27
0
        public void ShouldGeneratePathBetweenPoints2()
        {
            var origin             = new AxialCoordinate(-2, 2);
            var destination        = new AxialCoordinate(0, 3);
            var expectedPathLength = 3;

            var coordSystem = new HexCoordinateSystem(2);

            var actualPath = coordSystem.GetRouteGenerator(origin, destination)
                             .ToList();

            Assert.AreEqual(expectedPathLength, actualPath.Count);

            foreach (var pair in actualPath.RollingWindow(2))
            {
                var distanceBetween = pair[0].DistanceTo(pair[1]);
                Assert.AreEqual(
                    1,
                    distanceBetween,
                    $"Jump between {pair[0]} and {pair[1]} should have been distance of 1 but was {distanceBetween}");
            }
        }
コード例 #28
0
        public void ShouldGenerateSpiralAroundPoint()
        {
            var origin = new AxialCoordinate(-2, 2);

            var coordSystem = new HexCoordinateSystem(2);

            var generator = coordSystem.GetPositionsSpiralingAround(origin);

            var             lastDistance = -1;
            AxialCoordinate?lastPoint    = null;

            foreach (var coordinate in generator.Take(100))
            {
                if (lastPoint.HasValue)
                {
                    var distanceToLast = coordinate.DistanceTo(lastPoint.Value);
                    Assert.IsTrue(0 < distanceToLast && distanceToLast <= 2);
                }
                var distanceToCenter = coordinate.DistanceTo(origin);
                Assert.IsTrue(distanceToCenter == lastDistance || distanceToCenter == lastDistance + 1, "Distance must increase by only one at a time, and must never decrease");
                lastDistance = distanceToCenter;
            }
        }
コード例 #29
0
        public void ShouldGenerateRingAtDistance()
        {
            var origin = new AxialCoordinate(-2, 2);

            var coordSystem = new HexCoordinateSystem(2);

            var ring = HexCoordinateSystem.GetRing(origin, 3).ToList();

            Assert.AreEqual(6 * 3, ring.Count);

            AxialCoordinate?lastPoint = null;

            foreach (var coordinate in ring)
            {
                if (lastPoint.HasValue)
                {
                    var distanceToLast = coordinate.DistanceTo(lastPoint.Value);
                    Assert.IsTrue(distanceToLast == 1);
                }
                var distanceToCenter = coordinate.DistanceTo(origin);
                Assert.AreEqual(3, distanceToCenter);
            }
        }
コード例 #30
0
 public Vector2 TileMapToReal(AxialCoordinate tileMapPosition)
 {
     return(coordinateSystem.TileMapToRelative(tileMapPosition));
 }
コード例 #31
0
 private CubeCoordinate GetIndexInChunkMap(AxialCoordinate member)
 {
     return(member.ToCube().GetCoordInLargerHexGrid(ChunkRadius));
 }
コード例 #32
0
 /// <summary>
 /// Converts a hexagon coordinate to a world coordinate
 /// </summary>
 /// <returns>The world coordinate.</returns>
 /// <param name="a">The axialcoordinate to convert.</param>
 public static Vector2 HexToWorld (AxialCoordinate a)
 {
   float x = (float)(hexSize * Math.Sqrt (3) * (a.q + a.r / 2.0f));
   float y = hexSize * 3.0f / 2.0f * a.r;
   return new Vector2 (x, y);
 }
コード例 #33
0
 private IList <ITilemapMember> GetListFromCoord(AxialCoordinate coordinates)
 {
     return(GetListFromCoord(coordinates.ToOffset()));
 }
コード例 #34
0
 /// <summary>
 /// Converts an axialcoordinate array to a cubecoordinate array.
 /// </summary>
 /// <returns>Array of cubecoordinates.</returns>
 /// <param name="axials">The array of axialcoordinates to convert.</param>
 public static CubeCoordinate[] ToCubes (AxialCoordinate[] axials)
 {
   List<CubeCoordinate> cube_results = new List<CubeCoordinate> ();
   foreach (AxialCoordinate a in axials) {
     cube_results.Add (a.ToCube ());
   }
   return cube_results.ToArray ();
 }
コード例 #35
0
 public Vector2 TileMapPositionToPositionInPlane(AxialCoordinate tileMapPosition)
 {
     return(coordinateSystem.TileMapToReal(tileMapPosition));
 }
コード例 #36
0
 public HexSector (HexGrid grid, byte sectorId, AxialCoordinate spawn)
 {
   SectorId = sectorId;
   _grid = grid;
   AddToSector(spawn);
 }
コード例 #37
0
 /// <summary>
 /// Returns all axialcoordinates in range of the given axialcoordinate.
 /// </summary>
 /// <returns>All axialcoordinates in range of the given coordinate.</returns>
 /// <param name="c">The center of the range to look in.</param>
 /// <param name="range">The range to look in.</param>
 public static AxialCoordinate[] Range (AxialCoordinate c, byte range)
 {
   return ToAxials (Range (c.ToCube (), range));
 }
コード例 #38
0
 /// <summary>
 /// Returns one single neighbour in a single direction
 /// </summary>
 /// <returns>The neighbour.</returns>
 /// <param name="a">The axialcoordinate you want to know the neighbour of.</param>
 /// <param name="dir">The direction you want to look at.</param>
 private static AxialCoordinate GetNeighbour (AxialCoordinate a, byte dir)
 {
   dir %= 6; // make sure value is between 0-6
   return new AxialCoordinate ((sbyte)(a.q + _offsets [dir].q), (sbyte)(a.r + _offsets [dir].r));
 }
コード例 #39
0
 /// <summary>
 /// Rounds a value to the neirest cubecoordinate
 /// </summary>
 /// <returns>The rounded axialcoordinate.</returns>
 /// <param name="h">The axialcoordinate to round.</param>
 public static AxialCoordinate HexRound (AxialCoordinate a)
 {
   return HexRound (a.ToCube ()).ToAxial ();
 }
コード例 #40
0
 /// <summary>
 /// Returns the axialcoordinate from the mouseposition.
 /// </summary>
 /// <returns>The axialcoordinate of the mouseposition.</returns>
 public AxialCoordinate GetMousePosition()
 {
   Vector2 mousePos = Camera.main.ScreenPointToRay (Input.mousePosition).origin;
   _mousePos = HexCalculation.WorldToHex (mousePos);
   return _mousePos;
 }
コード例 #41
0
 public static IEnumerable <AxialCoordinate> GetPositionsWithinJumpDistance(AxialCoordinate origin, int jumpDistance)
 {
     return(HexCoordinateSystem.GetPositionsWithinJumpDistance(origin, jumpDistance));
 }
コード例 #42
0
 /// <summary>
 /// Returns the distance between two axialcoordinates.
 /// </summary>
 /// <returns>The distance between two cubecoordinates.</returns>
 /// <param name="a">Axialcoordinate (start position).</param>
 /// <param name="b">Axialcoordinate (end position).</param>
 public static byte GetDistance (AxialCoordinate a, AxialCoordinate b)
 {
   return GetDistance (a.ToCube (), b.ToCube ());
 }
コード例 #43
0
 /// <summary>
 /// Adds an offset to a given axialcoordinate
 /// </summary>
 /// <returns>The result of the addition.</returns>
 /// <param name="c">The original axialcoordinate.</param>
 /// <param name="a">The offset to give the origin axialcoordinate.</param>
 public static AxialCoordinate HexAdd (AxialCoordinate c, AxialCoordinate a)
 {
   return HexAdd (c.ToCube (), a.ToCube ()).ToAxial ();
 }
コード例 #44
0
 /// <summary>
 /// Returns the hexagon from a coordinate.
 /// </summary>
 /// <returns>The Hexagon at a specific coordinate.</returns>
 /// <param name="a">The coordinate you want the hexagon from.</param>
 public Hexagon GetHexagonAtPosition(AxialCoordinate a)
 {
   if (hexgrid.ContainsKey(a)) return hexgrid[a];
   else return null;
 }
コード例 #45
0
 private void CreateSectors(byte amountOfSectors) 
 {
   AxialCoordinate[] spawnSector = new AxialCoordinate[amountOfSectors];
   
   for (byte i = 0; i < amountOfSectors; i++) {
     byte tries = 0;
     bool succes = false; 
     while (!succes && tries <= 50) {
       tries++;
       AxialCoordinate coordinate = _grid.GetRandomMapPos();
       Hexagon hex = _grid.GetHexagonAtPosition(coordinate);
       if (hex != null && !hex.occupied) {
         succes = true;
         for (byte s = 0; s < i; s++) {
           if (spawnSector[s].Equals(coordinate)) succes = false;
         }
         if (succes) spawnSector[i] = coordinate;
       }
     }
     
     if (!succes && tries >= 50) throw new System.Exception("Could not find possible starting positions for sectors");
   }
   
   foreach (AxialCoordinate a in spawnSector) Debug.Log(a);
   
   HexSector[] sectors = new HexSector[amountOfSectors];
   for (byte i = 0; i < amountOfSectors; i++) {
     sectors[i] = new HexSector(_grid, i, spawnSector[i]);
   }
   
   bool done = false;
   while (!done) {
     done = true;
     foreach (HexSector hs in sectors) {
       if (hs.AddRandomNeighbourToSector()) {
         done = false;
       }
     }
   }
   
   foreach (HexSector hs in sectors) {
     Color c = new Color(Random.Range(0.1f, 0.6f), Random.Range(0.1f, 0.6f), Random.Range(0.1f, 0.6f));
     foreach (AxialCoordinate a in hs.sector) {
       _grid.GetHexagonAtPosition(a).SetIndicatorColor(c);
     }
   }
 }
コード例 #46
0
 /// <summary>
 /// Returns an array of axialcoordinates which form a line from one point to another.
 /// </summary>
 /// <returns>Array of axialcoordinates which form a line from the start position to the end position.</returns>
 /// <param name="a">Axialcoordinate (start position).</param>
 /// <param name="b">Axialcoordinate (end position).</param>
 public static AxialCoordinate[] GetLine (AxialCoordinate a, AxialCoordinate b)
 {
   CubeCoordinate[] cube_results = GetLine (a.ToCube (), b.ToCube ());
   return ToAxials (cube_results);
 }