/// <summary>
 /// Returns all the coordinates of the neighbouring hexes.
 /// </summary>
 /// <returns>The neighbours.</returns>
 /// <param name="a">The cubecoordinate you want to know the neighbours of.</param>
 public static CubeCoordinate[] GetNeighbours (CubeCoordinate a)
 {
   CubeCoordinate[] neighbours = new CubeCoordinate[6];
   for (byte i = 0; i < 6; i++) {
     neighbours [i] = GetNeighbour (a, i);
   }
   return neighbours;
 }
Exemplo n.º 2
0
    public void SetCubeData(Vector3 position, byte data)
    {
        CubeCoordinate chunkCoordinate = new CubeCoordinate(position, CubeCoordinate.CoordinateType.chunk);

        if (chunks.TryGetValue(chunkCoordinate.ToString(), out CubeChunk chunk))
        {
            chunk.SetCubeData(position, data, true);
        }
    }
Exemplo n.º 3
0
    public void SetCubeData(Vector3 position, byte data)
    {
        Vector3        cubePosition    = CubeMetrics.WorldPosition2CubePosition(position);
        CubeCoordinate chunkCoordinate = new CubeCoordinate(position, CubeCoordinate.CoordinateType.chunk);
        CubeCoordinate cubeCoordinate  = new CubeCoordinate(position, CubeCoordinate.CoordinateType.cubeWorld);
        CubeChunk      chunk           = chunks[chunkCoordinate.ToString()];

        chunk.SetCubeData(cubePosition, data, true);
    }
Exemplo n.º 4
0
        public void Destroy(CubeCoordinate coord)
        {
            if (cells.TryGetValue(coord, out var cell) == false)
            {
                return;
            }

            cell.block = null;
        }
    public Vector3 Hex2World(CubeCoordinate hex)
    {
        var     spacing = this.spacing;
        Vector3 position;

        position.y = spacing.y * (hex.z + 0.5f * hex.x);
        position.x = spacing.x * hex.x;
        position.z = 0;
        return(position);
    }
Exemplo n.º 6
0
    public static OffsetCoordinate CubeCoordToOffsetCoord(CubeCoordinate cubeCoordinate, bool isEven)
    {
        OffsetCoordinate offset = new OffsetCoordinate();

        offset.y = cubeCoordinate.x;
        offset.x = isEven ?
                   cubeCoordinate.z + (cubeCoordinate.x - (cubeCoordinate.x & 1)) / 2 :
                   cubeCoordinate.z + (cubeCoordinate.x + (cubeCoordinate.x & 1)) / 2;
        return(offset);
    }
Exemplo n.º 7
0
 public static IEnumerable <CubeCoordinate> Fan(CubeCoordinate center, int radius)
 {
     for ( ; radius >= 0; --radius)
     {
         foreach (var position in Arc(center, radius))
         {
             yield return(position);
         }
     }
 }
Exemplo n.º 8
0
    public byte GetCubeData(Vector3 position)
    {
        CubeCoordinate chunkCoordinate = new CubeCoordinate(position, CubeCoordinate.CoordinateType.chunk);

        if (chunks.TryGetValue(chunkCoordinate.ToString(), out CubeChunk chunk))
        {
            return(chunk.GetCubeData(position));
        }
        return(byte.MinValue);
    }
Exemplo n.º 9
0
    public CubeCoordinate GetAdjacentCoordinate(params AdjacentDirection[] dirs)
    {
        CubeCoordinate temp = new CubeCoordinate(x, y, z);

        for (int i = 0; i < dirs.Length; i++)
        {
            temp = temp.GetAdjacentCoordinate(dirs[i]);
        }
        return(temp);
    }
Exemplo n.º 10
0
 private Transform Spawn( CubeCoordinate hexPosition )
 {
     var mesh = Instantiate<GameObject>( hexMesh );
     mesh.name = hexPosition.ToString();
     var trans = mesh.transform;
     trans.parent = transform;
     trans.localPosition = hexPosition.ToWorldPosition();
     trans.localRotation = Quaternion.identity;
     trans.localScale = Vector3.one;
     return trans;
 }
Exemplo n.º 11
0
    public static CubeCoordinate OffsetCoordToCubeCoord(OffsetCoordinate offsetCoordinate)
    {
        CubeCoordinate cube = new CubeCoordinate();

        cube.x = offsetCoordinate.y;
        cube.z = offsetCoordinate.IsEven() ?
                 offsetCoordinate.x - (offsetCoordinate.y - (offsetCoordinate.y & 1)) / 2 :
                 offsetCoordinate.x - (offsetCoordinate.y + (offsetCoordinate.y & 1)) / 2;
        cube.y = -cube.x - cube.z;
        return(cube);
    }
        private int CalculateFewestNumberOfSteps(IEnumerable <string> stepsTaken)
        {
            var location = new CubeCoordinate(0, 0, 0);

            foreach (var step in stepsTaken)
            {
                location = ApplyStep(step, location);
            }

            return(CalculateDistanceFromZero(location));
        }
Exemplo n.º 13
0
 ICell IBoard.this[CubeCoordinate coord] {
     get {
         if (cells.TryGetValue(coord, out var cell))
         {
             return(cell);
         }
         else
         {
             return(null);
         }
     }
 }
Exemplo n.º 14
0
    private Transform Spawn(CubeCoordinate hexPosition)
    {
        var mesh = Instantiate <GameObject>(template);

        mesh.name = hexPosition.ToString();
        var trans = mesh.transform;

        trans.parent        = transform;
        trans.localPosition = Hex2World(hexPosition);
        trans.localRotation = Quaternion.identity;
        trans.localScale    = Vector3.one;
        return(trans);
    }
Exemplo n.º 15
0
    public CubeChunk GetAdjacentChunk(CubeChunk chunk, AdjacentDirection direction)
    {
        CubeCoordinate adjChunkCoordinate = chunk.ChunkCoordinate.GetAdjacentCoordinate(direction);

        if (chunks.ContainsKey(adjChunkCoordinate.ToString()))
        {
            return(chunks[adjChunkCoordinate.ToString()]);
        }
        else
        {
            return(null);
        }
    }
 /// <summary>
 /// Method that contains the algorithm for placement of a hex.
 /// </summary>
 /// <param name="c">The cubecoordiante of the hex you want to get through the algorithm.</param>
 /// <param name="mapsize">Size of the map.</param>
 /// <param name="layer">What kind of layer this layer should be.</param>
 private void PlacementAlgorithm(CubeCoordinate c, sbyte mapSize, GroundType layer)
 {
   // Determine the chance this tile has to be placed. This calculation makes sure that tiles closer to the middle have a higher chance to spawn, then we multiply it by a facter determined by the type of layer we are talking about so that we for example have mountains be placed more in the middle of the map.
   float chance = ( (c.x * c.x) / (3.0f * mapSize * mapSize) + 
                    (c.y * c.y) / (3.0f * mapSize * mapSize) + 
                    (c.z * c.z) / (3.0f * mapSize * mapSize) ) * 
                    (Ground.CenterFactor(layer));            
             
   // Perlin noise returns a value between 0.0-1.0
   if (Mathf.Clamp(Mathf.PerlinNoise ( _randomMultiplier * (c.x + mapSize), 
                   _randomMultiplier * (c.y + mapSize)), 0f, 1f) >= chance) {
     PlaceHex(c, layer);
   }
 }
Exemplo n.º 17
0
    /// <summary>
    /// Creates a hexagon at an axial position
    /// </summary>
    /// <returns>The created hexagon.</returns>
    /// <param name="a">The axial coordinate to place the hexagon on to.</param>
    /// <param name="type">The type of hexagon that should be placed</param>
    public Hexagon CreateHexagon(CubeCoordinate c, GroundType type)
    {
      GameObject hObj = GameObject.Instantiate(
        HexagonPrefab,
        HexCalculation.HexToWorld(c),
        HexagonPrefab.transform.rotation) as GameObject;

      Color tileColor = Ground.GroundColor(type);

      HexController hCon= hObj.GetComponent<HexController>();
      hCon.SetColor (tileColor);

      hObj.transform.SetParent (this.gameObject.transform);
      return new Hexagon (c, type, hCon);
    }
 /// <summary>
 /// Creates one layer of hexagons using a few other methods (PlacementAlgorithm and PlaceHex).
 /// </summary>
 /// <param name="mapsize">Size of the map.</param>
 /// <param name="layer">What kind of layer this layer should be.</param>
 private void CreateLayer(sbyte mapSize, GroundType layer)
 {
   // create a world of size 'mapsize' in each direction (x, y, z)
   for (sbyte x = (sbyte) -mapSize; x <= mapSize; x++) {
     for (sbyte y = (sbyte) -mapSize; y <= mapSize; y++) {
       // the z value has to be determined by the x and y
       sbyte z = (sbyte)(-x - y);
       // check if the z value also listens to the grid size
       if (z >= -mapSize && z <= mapSize) {
         CubeCoordinate coordinate = new CubeCoordinate (x, y, z);
         PlacementAlgorithm(coordinate, mapSize, layer);
       }
     }
   }
 }
Exemplo n.º 19
0
    /// <summary>
    /// A* algorithm
    /// </summary>
    public List<CubeCoordinate> GetPath(CubeCoordinate start, CubeCoordinate end)
    {
      
      Hexagon startHex = _grid.GetHexagonAtPosition(start.ToAxial());
      Hexagon endHex = _grid.GetHexagonAtPosition(end.ToAxial());
      
      if (startHex == null || endHex == null) return null;
      if (startHex.occupied || endHex.occupied) return null;
      
      PriorityQueue frontier = new PriorityQueue();
      frontier.Enqueue(start, 0);

      Dictionary<CubeCoordinate, CubeCoordinate> came_from = new Dictionary<CubeCoordinate, CubeCoordinate>();
      Dictionary<CubeCoordinate, int> cost_so_far = new Dictionary<CubeCoordinate, int>();

      cost_so_far.Add(start, 0);

      while (frontier.Count != 0) {
        CubeCoordinate current = frontier.Dequeue();

        if (current.Equals(end)) break;

        foreach (CubeCoordinate neighbour in HexCalculation.GetNeighbours(current)) {
          
          Hexagon hex = _grid.GetHexagonAtPosition(neighbour.ToAxial());
          
          if (hex != null && !hex.occupied) {
            int new_cost = cost_so_far[current] + hex.cost;
            
            if (!cost_so_far.ContainsKey(neighbour) || new_cost < cost_so_far[neighbour]) {
              if (cost_so_far.ContainsKey(neighbour))
                cost_so_far[neighbour] = new_cost;
              else 
                cost_so_far.Add(neighbour, new_cost);
                
              int priority = new_cost + HexCalculation.GetDistance(neighbour, end);
              frontier.Enqueue(neighbour, priority);
              if (came_from.ContainsKey(neighbour))
                came_from[neighbour] = current;
              else 
                came_from.Add(neighbour, current);
            }
          }
        }
      }
      
      return RetracePath (start, end, came_from);
    }
        private int CalculateFurthestDistance(IEnumerable <string> stepsTaken)
        {
            var location    = new CubeCoordinate(0, 0, 0);
            var maxDistance = 0;

            foreach (var step in stepsTaken)
            {
                location = ApplyStep(step, location);
                var distance = CalculateDistanceFromZero(location);
                if (distance > maxDistance)
                {
                    maxDistance = distance;
                }
            }

            return(maxDistance);
        }
Exemplo n.º 21
0
    private IEnumerable <Cell> FindSplashArea(CubeCoordinate target)
    {
        Cell cell = null;

        foreach (var direction in FlatTopDirection.around)
        {
            if (map.TryGetValue(target + direction, out cell) == false)
            {
                continue;
            }

            if (cell.block == null)
            {
                continue;
            }

            yield return(cell);
        }
    }
Exemplo n.º 22
0
    private Cell FindCellHasBlock(CubeCoordinate position, IEnumerable <CubeCoordinate> directions)
    {
        foreach (var dir in directions)
        {
            var next = position + dir;
            if (map.ContainsKey(next) == false)
            {
                continue;
            }

            if (map[next].block == null)
            {
                continue;
            }

            return(map[next]);
        }

        return(null);
    }
Exemplo n.º 23
0
    Stack <Vector3> GetPath(CubeCoordinate start, CubeCoordinate end)
    {
        path.Clear();
        map.Clear();
        tempNodes.Clear();
        PathNode final = SearchRoad(start, end);

        while (final.previous != null)
        {
            final = final.previous;
            path.Push(final.coordinate.ToVector3(CubeCoordinate.CoordinateType.cubeWorld) + (Vector3.up * CubeMetrics.CUBE_SIDE_LENGTH * 0.5f));
        }
        if (path.Count > 0)
        {
            path.Pop();//第一个坐标就是自己的位置 ,出栈不要.
        }


        return(path);
    }
Exemplo n.º 24
0
    bool GetAdjacentPath(CubeCoordinate coordinate, CubeOrientate orientate, out CubeCoordinate result)
    {
        AdjacentDirection temp = AdjacentDirection.front;

        switch (orientate)
        {
        case CubeOrientate.back:
            temp = AdjacentDirection.back;
            break;

        case CubeOrientate.left:
            temp = AdjacentDirection.left;
            break;

        case CubeOrientate.right:
            temp = AdjacentDirection.right;
            break;
        }
        if (HasCube(coordinate.GetAdjacentCoordinate(temp, AdjacentDirection.up, AdjacentDirection.up)))
        {
            result = coordinate.GetAdjacentCoordinate(temp, AdjacentDirection.up, AdjacentDirection.up);
            return(false);
        }
        if (HasCube(coordinate.GetAdjacentCoordinate(temp, AdjacentDirection.up)))
        {
            result = coordinate.GetAdjacentCoordinate(temp, AdjacentDirection.up);
            return(true);
        }
        if (HasCube(coordinate.GetAdjacentCoordinate(temp)))
        {
            result = coordinate.GetAdjacentCoordinate(temp);
            return(true);
        }
        if (HasCube(coordinate.GetAdjacentCoordinate(temp, AdjacentDirection.down)))
        {
            result = coordinate.GetAdjacentCoordinate(temp, AdjacentDirection.down);
            return(true);
        }
        result = new CubeCoordinate(0, 0, 0);
        return(false);
    }
Exemplo n.º 25
0
 void TriangulateByData()
 {
     Clear();
     for (int y = 0; y < CubeMetrics.CHUNK_WIDTH; y++)
     {
         for (int z = 0; z < CubeMetrics.CHUNK_WIDTH; z++)
         {
             for (int x = 0; x < CubeMetrics.CHUNK_WIDTH; x++)
             {
                 byte     d    = cubes[x + y * CubeMetrics.CHUNK_WIDTH * CubeMetrics.CHUNK_WIDTH + z * CubeMetrics.CHUNK_WIDTH];
                 CubeData data = CubeData.ToCubeData(d);
                 if (data.active)
                 {
                     CubeCoordinate coordinate = new CubeCoordinate(x, y, z);
                     TriangulateCube(x, y, z, data);
                 }
             }
         }
     }
     Apply();
 }
Exemplo n.º 26
0
    public static IEnumerable <CubeCoordinate> Ring(CubeCoordinate center, int radius)
    {
        if (radius <= 0f)
        {
            yield return(center);

            yield break;
        }

        var position = center + (around[4] * radius);

        foreach (var dir in around)
        {
            for (var i = 0; i < radius; ++i)
            {
                yield return(position);

                position += dir;
            }
        }
    }
Exemplo n.º 27
0
    Dictionary <string, CubeCoordinate> GetCoordinateByBounds(Bounds bounds)
    {
        tempCoordinate.Clear();
        float delta = CubeMetrics.CHUNK_WIDTH * CubeMetrics.CUBE_SIDE_LENGTH;

        //根据包围盒计算出需要显示的所有chunk的坐标

        for (float y = bounds.min.y; y <= bounds.max.y; y += delta)
        {
            for (float z = bounds.min.z; z <= bounds.max.z; z += delta)
            {
                for (float x = bounds.min.x; x <= bounds.max.x; x += delta)
                {
                    CubeCoordinate chunkCoordinate = new CubeCoordinate(new Vector3(x, y, z), CubeCoordinate.CoordinateType.chunk);
                    tempCoordinate.Add(chunkCoordinate.ToString(), chunkCoordinate);
                }
            }
        }

        return(tempCoordinate);
    }
Exemplo n.º 28
0
 private List<CubeCoordinate> RetracePath (CubeCoordinate start, CubeCoordinate end, Dictionary<CubeCoordinate, CubeCoordinate> steps) 
 {
   List<CubeCoordinate> path = new List<CubeCoordinate>();
   
   bool done = false;
   CubeCoordinate current = end;
   
   UnityEngine.Debug.Log(steps.Count + ", " + end);
   // foreach (KeyValuePair<CubeCoordinate, CubeCoordinate> pair in steps) {
   //   UnityEngine.Debug.Log(pair.Key + ", " + pair.Value);
   // }
   
   while (!done) {
     path.Add(current);
     if (steps.ContainsKey(current))
       current = steps[current];
     else done = true;
   }
   
   path.Reverse();
   return path;
 }
        private CubeCoordinate ApplyStep(string direction, CubeCoordinate location)
        {
            switch (direction)
            {
            case North:
                location.Y += 1;
                location.Z -= 1;
                break;

            case NorthEast:
                location.X += 1;
                location.Z -= 1;
                break;

            case SouthEast:
                location.X += 1;
                location.Y -= 1;
                break;

            case South:
                location.Y -= 1;
                location.Z += 1;
                break;

            case SouthWest:
                location.X -= 1;
                location.Z += 1;
                break;

            case NorthWest:
                location.X -= 1;
                location.Y += 1;
                break;
            }

            return(location);
        }
Exemplo n.º 30
0
    public HexTile[] GetNeighborsInRange(int radius)
    {
        HashSet <HexTile> tiles = new HashSet <HexTile>();

        for (int dx = -radius; dx <= radius; ++dx)
        {
            for (int dy = Mathf.Max(-radius, -dx - radius); dy <= Mathf.Min(radius, -dx + radius); ++dy)
            {
                int dz = -dx - dy;

                var     center     = new OffsetCoordinate(this.x, this.y);
                var     offsetCube = new CubeCoordinate(dx, dy, dz);
                HexTile neighbor   = HexTile.FindByOffset(center, HexUtils.CubeCoordToOffsetCoord(offsetCube, center.IsEven()));
                if (neighbor != null)
                {
                    tiles.Add(neighbor);
                }
            }
        }

        HexTile[] results = new HexTile[tiles.Count];
        tiles.CopyTo(results);
        return(results);
    }
Exemplo n.º 31
0
 public static int Distance( CubeCoordinate left, CubeCoordinate right )
 {
     var x = Mathf.Abs( left.x - right.x );
     var y = Mathf.Abs( left.y - right.y );
     var z = Mathf.Abs( left.z - right.z );
     return Mathf.Max( x, y, z );
 }
Exemplo n.º 32
0
    /// <summary>
    /// Rounds a value to the neirest cubecoordinate
    /// </summary>
    /// <returns>The rounded cubecoordinate.</returns>
    /// <param name="h">The cubecoordinate to round.</param>
    public static CubeCoordinate HexRound (CubeCoordinate h)
    {

      // This code does not seem to be neccesary since sbytes cannot contain decimal values, this might cause an issue though.
      sbyte rx = h.x; // Math.Round (h.x);
      sbyte ry = h.y; // Math.Round (h.y);
      sbyte rz = h.z; // Math.Round (h.z);
      // end

      byte x_diff = (byte)Math.Abs (rx - h.z);
      byte y_diff = (byte)Math.Abs (ry - h.y);
      byte z_diff = (byte)Math.Abs (rz - h.z);

      if (x_diff > y_diff && x_diff > z_diff) {
        rx = (sbyte)(-ry - rz);
      } else if (y_diff > z_diff) {
        ry = (sbyte)(-rx - rz);
      } else {
        rz = (sbyte)(-rx - ry);
      }

      return new CubeCoordinate (rx, ry, rz);
    }
Exemplo n.º 33
0
 /// <summary>
 /// Converts a cubecoordinate array to an axialcoordinate array.
 /// </summary>
 /// <returns>Array of axialcoordinates.</returns>
 /// <param name="cubes">The array of cubecoordinates to convert.</param>
 public static AxialCoordinate[] ToAxials (CubeCoordinate[] cubes)
 {
   List<AxialCoordinate> axial_results = new List<AxialCoordinate> ();
   foreach (CubeCoordinate c in cubes) {
     axial_results.Add (c.ToAxial ());
   }
   return axial_results.ToArray ();
 }
Exemplo n.º 34
0
 /// <summary>
 /// Returns an array of cubecoordinates which form a line from one point to another.
 /// </summary>
 /// <returns>Array of cubecoordinates which form a line from the start position to the end position.</returns>
 /// <param name="a">Cubecoordinate (start position).</param>
 /// <param name="b">Cubecoordinate (end position).</param>
 public static CubeCoordinate[] GetLine (CubeCoordinate a, CubeCoordinate b)
 {
   byte n = GetDistance (a, b);
   List<CubeCoordinate> results = new List<CubeCoordinate> ();
   for (byte i = 0; i <= n; i++) {
     results.Add (HexRound (Lerp (a, b, (byte)(1.0 / n * i))));
   }
   return results.ToArray ();
 }
 /// <summary>
 /// Creates a hexagon of type land, these remove hexes that have already been placed.
 /// </summary>
 /// <param name="c">The coordinate of the hex we are going to place.</param>
 private void CreateLandHex(CubeCoordinate c) 
 {
   Hexagon h = _grid.CreateHexagon (c, GroundType.Land);
   _grid.AddHexagonToGrid(h, true);
 }
 /// <summary>
 /// Creates a hexagon of type mountain, these remove hexes that have already been placed.
 /// </summary>
 /// <param name="c">The coordinate of the hex we are going to place.</param>
 private void CreateMountainHex(CubeCoordinate c)
 {
   if (Random.Range(0, 10) < 4)
   {
     Hexagon h = _grid.CreateHexagon (c, GroundType.Mountain);
     _grid.AddHexagonToGrid(h, true);
   }
 }
Exemplo n.º 37
0
 public PriorityHex (CubeCoordinate cube, int priority)
 {
   this.c = cube;
   this.priority = priority;
 }
Exemplo n.º 38
0
    public static CubeCoordinate[] HexRing (CubeCoordinate c, byte radius)
    {
      List<CubeCoordinate> results = new List<CubeCoordinate> ();
      CubeCoordinate cube = HexAdd (c, HexScale (_offsets [4].ToCube (), radius));

      for (byte i = 0; i < 6; i++) {
        for (byte j = 0; j < radius; j++) {
          results.Add (cube);
          cube = GetNeighbour (cube, i);
        }
      }
      return results.ToArray ();
    }
Exemplo n.º 39
0
 public Spawner(CubeCoordinate down)
 {
     this.down = down;
 }
Exemplo n.º 40
0
 public void Enqueue(CubeCoordinate c, int priority)
 {
   elements.Add(new PriorityHex(c, priority));
 }
Exemplo n.º 41
0
 public int Distance(CubeCoordinate subject)
 {
     return(Distance(this, subject));
 }
Exemplo n.º 42
0
 public bool Equal(CubeCoordinate c)
 {
     return(x == c.x && y == c.y && z == c.z);
 }
Exemplo n.º 43
0
    public Hexagon (CubeCoordinate c, GroundType type, HexController controller) : base (c)
    {
      WorldPos = HexCalculation.HexToWorld (CubePos);
      occupied = (type != GroundType.Land);
      _hexType = type;
      _controller = controller;

      if (occupied) _controller.SetIndicatorColor(new Color(1f,1f,1f,0.1f));
    }
Exemplo n.º 44
0
 /// <summary>
 /// Scales a coordinate.
 /// </summary>
 /// <returns>The scaled coordinate.</returns>
 /// <param name="c">The original cubecoordinate.</param>
 /// <param name="scale">The scale to 'scale' by.</param>
 public static CubeCoordinate HexScale (CubeCoordinate c, byte scale)
 {
   return new CubeCoordinate ((sbyte)(c.x * scale), (sbyte)(c.y * scale), (sbyte)(c.z * scale));
 }
Exemplo n.º 45
0
 bool IBoard.HasCell(CubeCoordinate coord)
 {
     return(cells.ContainsKey(coord));
 }
Exemplo n.º 46
0
 /// <summary>
 /// Adds an offset to a given cubecoordinate
 /// </summary>
 /// <returns>The result of the addition.</returns>
 /// <param name="c">The original cubecoordinate.</param>
 /// <param name="a">The offset to give the origin cubecoordinate.</param>
 public static CubeCoordinate HexAdd (CubeCoordinate c, CubeCoordinate a)
 {
   return new CubeCoordinate ((sbyte)(c.x + a.x), (sbyte)(c.y + a.y), (sbyte)(c.z + a.z));
 }
Exemplo n.º 47
0
 void IBoard.Drop(CubeCoordinate from, CubeCoordinate to)
 {
     ((IBoard)this).Swap(from, to);
 }
Exemplo n.º 48
0
 /// <summary>
 /// Returns all cubecoordinates in range of the given cubecoordinate.
 /// </summary>
 /// <returns>All cubecoordinates 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 CubeCoordinate[] Range (CubeCoordinate c, byte range)
 {
   List<CubeCoordinate> cube_results = new List<CubeCoordinate> ();
   for (sbyte dx = (sbyte)-range; dx <= range; dx++) {
     for (sbyte dy = (sbyte)Math.Max (-range, -dx - range); 
     dy <= Math.Min (range, -dx + range); dy++) {
       sbyte dz = (sbyte)(-dx - dy);
       cube_results.Add (HexAdd (c, new CubeCoordinate (dx, dy, dz)));
     }
   }
   return cube_results.ToArray ();
 }
Exemplo n.º 49
0
    private void Update()
    {
        if (_previousMode != m_mode)
        {
            ChangeMode(m_mode);
        }

        switch (m_mode)
        {
        case Mode.DrawArea:
            DrawHexagon(HexGrid.DrawArea(
                            m_grid.WorldToCoordinate(new Vector2(_center.position.x, _center.position.z)),
                            m_rangeCenter),
                        Color.cyan
                        );
            break;



        case Mode.DrawIntersectArea:
            DrawHexagon(
                HexGrid.DrawArea(
                    m_grid.WorldToCoordinate(new Vector2(_pointA.position.x, _pointA.position.z)),
                    m_rangeA),
                Color.blue
                );
            DrawHexagon(
                HexGrid.DrawArea(
                    m_grid.WorldToCoordinate(new Vector2(_pointB.position.x, _pointB.position.z)),
                    m_rangeB),
                Color.yellow
                );
            DrawHexagon(
                HexGrid.DrawIntersectArea(
                    m_grid.WorldToCoordinate(new Vector2(_pointA.position.x, _pointA.position.z)),
                    m_rangeA,
                    m_grid.WorldToCoordinate(new Vector2(_pointB.position.x, _pointB.position.z)),
                    m_rangeB),
                Color.green
                );
            break;



        case Mode.RotateCoordinate:
            CubeCoordinate a = m_grid.WorldToCoordinate(new Vector2(_pointA.position.x, _pointA.position.z));
            CubeCoordinate centerRotation = m_grid.WorldToCoordinate(new Vector2(_center.position.x, _center.position.z));
            DrawHexagonColorRamp(
                new CubeCoordinate[] {
                centerRotation,
                a,
                HexGrid.RotateCoordinate(a, centerRotation, HexGrid.CubeRotation.CCW_60),
                HexGrid.RotateCoordinate(a, centerRotation, HexGrid.CubeRotation.CCW_120),
                HexGrid.RotateCoordinate(a, centerRotation, HexGrid.CubeRotation.CCW_180),
                HexGrid.RotateCoordinate(a, centerRotation, HexGrid.CubeRotation.CCW_240),
                HexGrid.RotateCoordinate(a, centerRotation, HexGrid.CubeRotation.CCW_300)
            },
                Color.white,
                Color.blue
                );
            break;



        case Mode.DrawRing:
            CubeCoordinate centerRing = m_grid.WorldToCoordinate(new Vector2(_center.position.x, _center.position.z));

            DrawHexagon(
                HexGrid.DrawRing(centerRing, m_rangeCenter),
                Color.blue
                );

            m_grid.DebugDrawHexagon(centerRing, Color.white);
            break;



        case Mode.DrawLargeRing:
            CubeCoordinate centerLargeRing = m_grid.WorldToCoordinate(new Vector2(_center.position.x, _center.position.z));

            DrawHexagon(
                HexGrid.DrawLargeRing(centerLargeRing, m_rangeA, m_rangeB),
                Color.blue
                );

            m_grid.DebugDrawHexagon(centerLargeRing, Color.white);
            break;



        default:
            break;
        }
    }
 /// <summary>
 /// Creates a hexagon of type water, these do not remove hexes that have already been placed.
 /// </summary>
 /// <param name="c">The coordinate of the hex we are going to place.</param>
 private void CreateWaterHex(CubeCoordinate c)
 {
   Hexagon h = _grid.CreateHexagon (c, GroundType.Water);
   _grid.AddHexagonToGrid(h);
   
 }
 private int CalculateDistanceFromZero(CubeCoordinate location)
 {
     return((Math.Abs(location.X) + Math.Abs(location.Y) + Math.Abs(location.Z)) / 2);
 }
Exemplo n.º 52
0
 /// <summary>
 /// Converts a hexagon coordinate to a world coordinate
 /// </summary>
 /// <returns>The world coordinate.</returns>
 /// <param name="a">The cubecoordinate to convert.</param>
 public static Vector2 HexToWorld (CubeCoordinate c)
 {
   return HexToWorld (c.ToAxial ());
 }
Exemplo n.º 53
0
 /// <summary>
 /// Returns the distance between two cubecoordinates.
 /// </summary>
 /// <returns>The distance between two cubecoordinates.</returns>
 /// <param name="a">Cubecoordinate (start position).</param>
 /// <param name="b">Cubecoordinate (end position).</param>
 public static byte GetDistance (CubeCoordinate a, CubeCoordinate b)
 {
   return (byte)((Math.Abs (a.x - b.x) + Math.Abs (a.y - b.y) + Math.Abs (a.z - b.z)) / 2);
 }
 /// <summary>
 /// Places a hex after it has been through the algorithm (PlacementAlgorithm). It determines what kind of hex it should be and calls the right method for placement of that type of hex.
 /// </summary>
 /// <param name="c">The cubecoordiante of the hex you want to get through the algorithm.</param>
 /// <param name="layer">What kind of layer this layer should be.</param>
 private void PlaceHex(CubeCoordinate c, GroundType layer)
 {
   switch (layer) {
       case GroundType.Water:
         CreateWaterHex(c);
         break;
       case GroundType.Land:
         CreateLandHex(c);
         break;
       case GroundType.Mountain:
         CreateMountainHex(c);
         break;
     }
 }
Exemplo n.º 55
0
 /// <summary>
 /// Returns one single neighbour in a single direction
 /// </summary>
 /// <returns>The neighbour.</returns>
 /// <param name="a">The cubecoordinate you want to know the neighbour of.</param>
 /// <param name="dir">The direction you want to look at.</param>
 private static CubeCoordinate GetNeighbour (CubeCoordinate a, byte dir)
 {
   return GetNeighbour (a.ToAxial (), dir).ToCube ();
 }
Exemplo n.º 56
0
 public static Vector3 Hex2World( CubeCoordinate hex )
 {
     Vector3 position;
     position.y = 0;
     position.x = horizontalSpacing * (hex.x + 0.5f * hex.z);
     position.z = verticalSpacing * hex.z;
     return position;
 }
Exemplo n.º 57
0
 /// <summary>
 /// Lerps between two coordinates.
 /// </summary>
 /// <returns>The cubecoordinate that you are on.</returns>
 /// <param name="a">Cubecoordinate (start position).</param>
 /// <param name="b">Cubecoordinate (end position).</param>
 /// <param name="t">Percentage from 0-1 where 0 is the start position and 1 the end position.</param>
 public static CubeCoordinate Lerp (CubeCoordinate a, CubeCoordinate b, byte t)
 {
   return new CubeCoordinate ((sbyte)(a.x + (b.x - a.x) * t),
     (sbyte)(a.y + (b.y - a.y) * t),
     (sbyte)(a.z + (b.z - a.z) * t));
 }
Exemplo n.º 58
0
    public List<CubeCoordinate> MovesInRange(CubeCoordinate start, byte range)
    {
      if (_grid.GetHexagonAtPosition(start.ToAxial()).occupied) {
        // If we start on an occupied hex return an empty list.
        return new List<CubeCoordinate>();
      }

      List<CubeCoordinate> visited = new List<CubeCoordinate>();
      try {
        visited.Add(start);
        Dictionary<byte, List<CubeCoordinate>> fringes = new Dictionary<byte, List<CubeCoordinate>>();
        fringes.Add(0, new List<CubeCoordinate>());
        fringes[0].Add(start);

        for (byte k = 1 /*k<1*/; k <= range; k++) {
          fringes.Add(k, new List<CubeCoordinate>());
          foreach (CubeCoordinate c in fringes[(byte) (k-1)]) {
            foreach (CubeCoordinate neighbour in HexCalculation.GetNeighbours(c)) {
              if (!visited.Contains(neighbour) && !_grid.GetHexagonAtPosition(neighbour.ToAxial()).occupied) {
                visited.Add(neighbour);
                fringes[k].Add(neighbour);
              }
            }
          }
        }
      } catch (System.NullReferenceException nrf) {
        UnityEngine.Debug.Log("Something went wrong with the pathfinding: " + nrf.ToString());
        // If something went wrong return an empty list.
        return new List<CubeCoordinate>();
      }

      return visited;
    }
Exemplo n.º 59
0
 public int Distance( CubeCoordinate subject )
 {
     return Distance( this, subject );
 }