Example #1
0
        public int GetHexCellIndex(HexDirection direction, int sourceHex, bool isHexRow)
        {
            int returnHex = -1;

            switch (direction)
            {
                case HexDirection.TopLeft:
                    returnHex = isHexRow ? sourceHex - textSurface.Width : sourceHex - textSurface.Width - 1;
                    break;
                case HexDirection.TopRight:
                    returnHex = isHexRow ? sourceHex - textSurface.Width + 1 : sourceHex - textSurface.Width ;
                    break;
                case HexDirection.Left:
                    returnHex = sourceHex - 1;
                    break;
                case HexDirection.Right:
                    returnHex = sourceHex + 1;
                    break;
                case HexDirection.BottomLeft:
                    returnHex = isHexRow ? sourceHex + textSurface.Width : sourceHex + textSurface.Width - 1;
                    break;
                case HexDirection.BottomRight:
                    returnHex = isHexRow ? sourceHex + textSurface.Width + 1 : sourceHex + textSurface.Width;
                    break;
                default:
                    break;
            }

            if (returnHex < 0 || returnHex > textSurface.Cells.Length - 1)
                returnHex = -1;

            return returnHex;
        }
 public static GlobalPos GetNeighborMapCoords(GlobalPos pos, HexDirection direction)
 {
     switch (direction)
     {
         case HexDirection.TOP_LEFT:
             return new GlobalPos(pos.X - 1, pos.Y + (pos.X & 1));
         case HexDirection.TOP:
             return new GlobalPos(pos.X, pos.Y + 1);
         case HexDirection.TOP_RIGHT:
             return new GlobalPos(pos.X + 1, pos.Y + (pos.X & 1));
         case HexDirection.BOTTOM_RIGHT:
             return new GlobalPos(pos.X + 1, pos.Y - (pos.X & 1 ^ 1));
         case HexDirection.BOTTOM:
             return new GlobalPos(pos.X, pos.Y - 1);
         case HexDirection.BOTTOM_LEFT:
             return new GlobalPos(pos.X - 1, pos.Y - (pos.X & 1 ^ 1));
     }
     throw new System.ArgumentException("Invalid direction", "direction");
 }
Example #3
0
 public HexCell GetNeighbor(HexDirection direction)
 {
     return(neighbors[(int)direction]);
 }
Example #4
0
 public static Vector3 GetBridge(HexDirection direction)
 {
     return((corners[(int)direction] + corners[(int)direction + 1]) *
            blendFactor);
 }
Example #5
0
 /// <summary>
 /// Gets first inside corner of the hexagon, proportionate by how much water is to be scaled
 /// </summary>
 /// <param name="direction"> Direction of the hexagon vertex e.g. NE, SE etc</param>
 /// <returns> vector 3 position of the first point for a water triangle in that direction </returns>
 public static Vector3 GetFirstWaterCorner(HexDirection direction)
 {
     return(corners[(int)direction] * waterFactor);
 }
Example #6
0
	// returns the direction from a face to the specified vertex (world space only!)
	public Vector3 VertexToDirection(HexDirection vert) {
		return VertexToDirection (vert, true);
	}
 public static Vector2 GetNeighbor(Vector2 coords, HexDirection dir)
 {
     return GetCoordsAtDistance(coords, 1, dir);
 }
Example #8
0
 public static Vector3 GetFirstSolidCorner(HexDirection direction)
 {
     return corners[(int)direction] * solidFactor;
 }
Example #9
0
 public bool HasRoadThroughEdge(HexDirection direction)
 {
     return(roads[(int)direction]);
 }
Example #10
0
 public bool HasRiverThroughEdge(HexDirection direction)
 {
     return(hasIncomingRiver && incomingRiver == direction || hasOutgoingRiver && outgoingRiver == direction);
 }
Example #11
0
 public HexEdgeType GetEdgeType(HexDirection direction)
 {
     return(HexMetrics.GetEdgeType(elevation, neighbors[(int)direction].elevation));
 }
Example #12
0
 public void SetNeighbor(HexDirection direction, HexCell cell)
 {
     neighbors[(int)direction] = cell;
     cell.neighbors[(int)direction.Opposite()] = this;
 }
 public bool CanTransverse(HexCell from, HexCell to, HexDirection direction)
 {
     return(m_movableType.CanTransverse(from, to, direction));
 }
Example #14
0
 /// <summary>
 /// Gets second inside corner of the hexagon, proportionate by how much water is to be scaled
 /// </summary>
 /// <param name="direction"> Direction of the hexagon vertex e.g. NE, SE etc</param>
 /// <returns> vector 3 position of the second point for a water triangle in that direction </returns>
 public static Vector3 GetSecondWaterCorner(HexDirection direction)
 {
     return(corners[(int)direction + 1] * waterFactor);
 }
Example #15
0
    void TriangulateConnection(
		HexDirection direction, HexCell cell, Vector3 v1, Vector3 v2
	)
    {
        HexCell neighbor = cell.GetNeighbor(direction);
        if (neighbor == null) {
            return;
        }

        Vector3 bridge = HexMetrics.GetBridge(direction);
        Vector3 v3 = v1 + bridge;
        Vector3 v4 = v2 + bridge;
        v3.y = v4.y = neighbor.Elevation * HexMetrics.elevationStep;

        if (cell.GetEdgeType(direction) == HexEdgeType.Slope) {
            TriangulateEdgeTerraces(v1, v2, cell, v3, v4, neighbor);
        }
        else {
            AddQuad(v1, v2, v3, v4);
            AddQuadColor(cell.color, neighbor.color);
        }

        HexCell nextNeighbor = cell.GetNeighbor(direction.Next());
        if (direction <= HexDirection.E && nextNeighbor != null) {
            Vector3 v5 = v2 + HexMetrics.GetBridge(direction.Next());
            v5.y = nextNeighbor.Elevation * HexMetrics.elevationStep;

            if (cell.Elevation <= neighbor.Elevation) {
                if (cell.Elevation <= nextNeighbor.Elevation) {
                    TriangulateCorner(v2, cell, v4, neighbor, v5, nextNeighbor);
                }
                else {
                    TriangulateCorner(v5, nextNeighbor, v2, cell, v4, neighbor);
                }
            }
            else if (neighbor.Elevation <= nextNeighbor.Elevation) {
                TriangulateCorner(v4, neighbor, v5, nextNeighbor, v2, cell);
            }
            else {
                TriangulateCorner(v5, nextNeighbor, v2, cell, v4, neighbor);
            }
        }
    }
Example #16
0
 public void Change(HexDirection d)
 {
     HexPoint p = Shift(d);
     x = p.x;
     y = p.y;
 }
Example #17
0
    public int GetElevationDifference(HexDirection direction)
    {
        int difference = elevation - GetNeighbor(direction).elevation;

        return(difference >= 0 ? difference : -difference);
    }
Example #18
0
 public static Vector3 GetBridge(HexDirection direction)
 {
     return (corners[(int)direction] + corners[(int)direction + 1]) *
         blendFactor;
 }
Example #19
0
    private void TriangulateWaterShore(HexDirection direction, HexCell cell, HexCell neighbor, Vector3 center)
    {
        EdgeVertices e1 = new EdgeVertices(
            center + HexMetrics.GetFirstWaterCorner(direction),
            center + HexMetrics.GetSecondWaterCorner(direction)
            );

        water.AddTriangle(center, e1.v1, e1.v2);
        water.AddTriangle(center, e1.v2, e1.v3);
        water.AddTriangle(center, e1.v3, e1.v4);
        water.AddTriangle(center, e1.v4, e1.v5);
        Vector3 indices;

        indices.x = indices.z = cell.Index;
        indices.y = neighbor.Index;

        water.AddTriangleCellData(indices, weights1);
        water.AddTriangleCellData(indices, weights1);
        water.AddTriangleCellData(indices, weights1);
        water.AddTriangleCellData(indices, weights1);

        Vector3 center2 = neighbor.Position;

        if (neighbor.ColumnIndex < cell.ColumnIndex - 1)
        {
            center2.x += HexMetrics.wrapSize * HexMetrics.innerDiameter;
        }
        else if (neighbor.ColumnIndex > cell.ColumnIndex + 1)
        {
            center2.x -= HexMetrics.wrapSize * HexMetrics.innerDiameter;
        }
        center2.y = center.y;

        EdgeVertices e2 = new EdgeVertices(
            center2 + HexMetrics.GetSecondSolidCorner(direction.Opposite()),
            center2 + HexMetrics.GetFirstSolidCorner(direction.Opposite())
            );

        if (cell.HasRiverThroughEdge(direction))
        {
            TriangulateEstuary(e1, e2, cell.IncomingRiver == direction, indices);
        }
        else
        {
            waterShore.AddQuad(e1.v1, e1.v2, e2.v1, e2.v2);
            waterShore.AddQuad(e1.v2, e1.v3, e2.v2, e2.v3);
            waterShore.AddQuad(e1.v3, e1.v4, e2.v3, e2.v4);
            waterShore.AddQuad(e1.v4, e1.v5, e2.v4, e2.v5);
            waterShore.AddQuadUV(0f, 0f, 0f, 1f);
            waterShore.AddQuadUV(0f, 0f, 0f, 1f);
            waterShore.AddQuadUV(0f, 0f, 0f, 1f);
            waterShore.AddQuadUV(0f, 0f, 0f, 1f);
            waterShore.AddQuadCellData(indices, weights1, weights2);
            waterShore.AddQuadCellData(indices, weights1, weights2);
            waterShore.AddQuadCellData(indices, weights1, weights2);
            waterShore.AddQuadCellData(indices, weights1, weights2);
        }

        HexCell nextNeighbor = cell.GetNeighbor(direction.Next());

        if (nextNeighbor != null)
        {
            Vector3 center3 = nextNeighbor.Position;
            if (nextNeighbor.ColumnIndex < cell.ColumnIndex - 1)
            {
                center3.x += HexMetrics.wrapSize * HexMetrics.innerDiameter;
            }
            else if (nextNeighbor.ColumnIndex > cell.ColumnIndex + 1)
            {
                center3.x -= HexMetrics.wrapSize * HexMetrics.innerDiameter;
            }

            Vector3 v3 = center3 + (nextNeighbor.IsUnderwater ?
                                    HexMetrics.GetFirstWaterCorner(direction.Previous()) :
                                    HexMetrics.GetFirstSolidCorner(direction.Previous())
                                    );
            v3.y = center.y;
            waterShore.AddTriangle(e1.v5, e2.v5, v3);
            waterShore.AddTriangleUV(new Vector2(0f, 0f), new Vector2(0f, 1f), new Vector2(0f, nextNeighbor.IsUnderwater ? 0f : 1f));

            indices.z = nextNeighbor.Index;
            waterShore.AddTriangleCellData(indices, weights1, weights2, weights3);
        }
    }
Example #20
0
 public static Vector3 GetSecondSolidCorner(HexDirection direction)
 {
     return corners[(int)direction + 1] * solidFactor;
 }
Example #21
0
    void TriangulateRoadAdjacentToRiver(
        HexDirection direction, HexCell cell, Vector3 center, EdgeVertices e
        )
    {
        bool    hasRoadThroughEdge = cell.HasRoadThroughEdge(direction);
        bool    previousHasRiver   = cell.HasRiverThroughEdge(direction.Previous());
        bool    nextHasRiver       = cell.HasRiverThroughEdge(direction.Next());
        Vector2 interpolators      = GetRoadInterpolators(direction, cell);
        Vector3 roadCenter         = center;

        if (cell.HasRiverBeginOrEnd)
        {
            roadCenter += HexMetrics.GetSolidEdgeMiddle(
                cell.RiverBeginOrEndDirection.Opposite()
                ) * (1f / 3f);
        }
        else if (cell.IncomingRiver == cell.OutgoingRiver.Opposite())
        {
            Vector3 corner;
            if (previousHasRiver)
            {
                if (
                    !hasRoadThroughEdge &&
                    !cell.HasRoadThroughEdge(direction.Next())
                    )
                {
                    return;
                }
                corner = HexMetrics.GetSecondSolidCorner(direction);
            }
            else
            {
                if (
                    !hasRoadThroughEdge &&
                    !cell.HasRoadThroughEdge(direction.Previous())
                    )
                {
                    return;
                }
                corner = HexMetrics.GetFirstSolidCorner(direction);
            }
            roadCenter += corner * 0.5f;

            if (cell.IncomingRiver == direction.Next() && (
                    cell.HasRoadThroughEdge(direction.Next2()) ||
                    cell.HasRoadThroughEdge(direction.Opposite())
                    ))
            {
                features.AddBridge(roadCenter, center - corner * 0.5f);
            }
            center += corner * 0.25f;
        }
        else if (cell.IncomingRiver == cell.OutgoingRiver.Previous())
        {
            roadCenter -= HexMetrics.GetSecondCorner(cell.IncomingRiver) * 0.2f;
        }
        else if (cell.IncomingRiver == cell.OutgoingRiver.Next())
        {
            roadCenter -= HexMetrics.GetFirstCorner(cell.IncomingRiver) * 0.2f;
        }
        else if (previousHasRiver && nextHasRiver)
        {
            if (!hasRoadThroughEdge)
            {
                return;
            }
            Vector3 offset = HexMetrics.GetSolidEdgeMiddle(direction) *
                             HexMetrics.innerToOuter;
            roadCenter += offset * 0.7f;
            center     += offset * 0.5f;
        }
        else
        {
            HexDirection middle;
            if (previousHasRiver)
            {
                middle = direction.Next();
            }
            else if (nextHasRiver)
            {
                middle = direction.Previous();
            }
            else
            {
                middle = direction;
            }
            if (
                !cell.HasRoadThroughEdge(middle) &&
                !cell.HasRoadThroughEdge(middle.Previous()) &&
                !cell.HasRoadThroughEdge(middle.Next())
                )
            {
                return;
            }
            Vector3 offset = HexMetrics.GetSolidEdgeMiddle(middle);
            roadCenter += offset * 0.25f;
            if (
                direction == middle &&
                cell.HasRoadThroughEdge(direction.Opposite())
                )
            {
                features.AddBridge(
                    roadCenter,
                    center - offset * (HexMetrics.innerToOuter * 0.7f)
                    );
            }
        }

        Vector3 mL = Vector3.Lerp(roadCenter, e.v1, interpolators.x);
        Vector3 mR = Vector3.Lerp(roadCenter, e.v5, interpolators.y);

        TriangulateRoad(roadCenter, mL, mR, e, hasRoadThroughEdge, cell.Index);
        if (previousHasRiver)
        {
            TriangulateRoadEdge(roadCenter, center, mL, cell.Index);
        }
        if (nextHasRiver)
        {
            TriangulateRoadEdge(roadCenter, mR, center, cell.Index);
        }
    }
Example #22
0
	// returns the direction from a face to the specified face (world space only!)
	public Vector3 GetDirection (HexDirection dir) {
		return GetDirection (dir, hexSideMode);
	}
Example #23
0
    void TriangulateWithRiver(
        HexDirection direction, HexCell cell, Vector3 center, EdgeVertices e
        )
    {
        Vector3 centerL, centerR;

        if (cell.HasRiverThroughEdge(direction.Opposite()))
        {
            centerL = center +
                      HexMetrics.GetFirstSolidCorner(direction.Previous()) * 0.25f;
            centerR = center +
                      HexMetrics.GetSecondSolidCorner(direction.Next()) * 0.25f;
        }
        else if (cell.HasRiverThroughEdge(direction.Next()))
        {
            centerL = center;
            centerR = Vector3.Lerp(center, e.v5, 2f / 3f);
        }
        else if (cell.HasRiverThroughEdge(direction.Previous()))
        {
            centerL = Vector3.Lerp(center, e.v1, 2f / 3f);
            centerR = center;
        }
        else if (cell.HasRiverThroughEdge(direction.Next2()))
        {
            centerL = center;
            centerR = center +
                      HexMetrics.GetSolidEdgeMiddle(direction.Next()) *
                      (0.5f * HexMetrics.innerToOuter);
        }
        else
        {
            centerL = center +
                      HexMetrics.GetSolidEdgeMiddle(direction.Previous()) *
                      (0.5f * HexMetrics.innerToOuter);
            centerR = center;
        }
        center = Vector3.Lerp(centerL, centerR, 0.5f);

        EdgeVertices m = new EdgeVertices(
            Vector3.Lerp(centerL, e.v1, 0.5f),
            Vector3.Lerp(centerR, e.v5, 0.5f),
            1f / 6f
            );

        m.v3.y = center.y = e.v3.y;


        TriangulateEdgeStrip(
            m, weights1, cell.Index,
            e, weights1, cell.Index
            );

        terrain.AddTriangle(centerL, m.v1, m.v2);
        terrain.AddQuad(centerL, center, m.v2, m.v3);
        terrain.AddQuad(center, centerR, m.v3, m.v4);
        terrain.AddTriangle(centerR, m.v4, m.v5);

        Vector3 indices;

        indices.x = indices.y = indices.z = cell.Index;
        terrain.AddTriangleCellData(indices, weights1);
        terrain.AddQuadCellData(indices, weights1);
        terrain.AddQuadCellData(indices, weights1);
        terrain.AddTriangleCellData(indices, weights1);

        //terrain.AddTriangleColor( weights1 );
        //terrain.AddQuadColor( weights1 );
        //terrain.AddQuadColor( weights1 );
        //terrain.AddTriangleColor( weights1 );

        //Vector3 types;
        //types.x = types.y = types.z = cell.TerrainTypeIndex;
        //terrain.AddTriangleTerrainTypes(types);
        //terrain.AddQuadTerrainTypes(types);
        //terrain.AddQuadTerrainTypes(types);
        //terrain.AddTriangleTerrainTypes(types);

        if (!cell.IsUnderwater)
        {
            bool reversed = cell.IncomingRiver == direction;
            TriangulateRiverQuad(
                centerL, centerR, m.v2, m.v4, cell.RiverSurfaceY, 0.4f, reversed, indices
                );
            TriangulateRiverQuad(
                m.v2, m.v4, e.v2, e.v4, cell.RiverSurfaceY, 0.6f, reversed, indices
                );
        }
    }
Example #24
0
 public bool CanHop(HexLocation zl, HexDirection dir)
 {
     //CH3
     HexLocation zla = zl;
     ZertzPiece zp = this[zla];
     HexLocation hldir = HexLocation.NeighbourDirections[(byte) dir];
     if(zp.IsAlive && zp.ContainsBall) {
         zla += hldir;
         zp = this[zla];
         if(zp.IsAlive && zp.ContainsBall) {
             zla += hldir;
             zp = this[zla];
             if(zp.IsAlive && !zp.ContainsBall) {
                 return true;
             }
         }
     }
     return false;
 }
Example #25
0
    void TriangulateConnection(
        HexDirection direction, HexCell cell, EdgeVertices e1
        )
    {
        HexCell neighbor = cell.GetNeighbor(direction);

        if (neighbor == null)
        {
            return;
        }

        Vector3 bridge = HexMetrics.GetBridge(direction);

        bridge.y = neighbor.Position.y - cell.Position.y;
        EdgeVertices e2 = new EdgeVertices(
            e1.v1 + bridge,
            e1.v5 + bridge
            );

        bool hasRiver = cell.HasRiverThroughEdge(direction);
        bool hasRoad  = cell.HasRoadThroughEdge(direction);

        if (hasRiver)
        {
            e2.v3.y = neighbor.StreamBedY;

            Vector3 indices;
            indices.x = indices.z = cell.Index;
            indices.y = neighbor.Index;

            if (!cell.IsUnderwater)
            {
                if (!neighbor.IsUnderwater)
                {
                    TriangulateRiverQuad(
                        e1.v2, e1.v4, e2.v2, e2.v4,
                        cell.RiverSurfaceY, neighbor.RiverSurfaceY, 0.8f,
                        cell.HasIncomingRiver && cell.IncomingRiver == direction,
                        indices
                        );
                }
                else if (cell.Elevation > neighbor.WaterLevel)//瀑布
                {
                    TriangulateWaterfallInWater(
                        e1.v2, e1.v4, e2.v2, e2.v4,
                        cell.RiverSurfaceY, neighbor.RiverSurfaceY,
                        neighbor.WaterSurfaceY, indices
                        );
                }
            }
            else if (!neighbor.IsUnderwater && neighbor.Elevation > cell.WaterLevel) //瀑布
            {
                TriangulateWaterfallInWater(
                    e2.v4, e2.v2, e1.v4, e1.v2,
                    neighbor.RiverSurfaceY, cell.RiverSurfaceY,
                    cell.WaterSurfaceY, indices
                    );
            }
        }


        if (cell.GetEdgeType(direction) == HexEdgeType.Slope)
        {
            TriangulateEdgeTerraces(
                e1, cell, e2, neighbor, hasRoad
                );
        }
        else
        {
            TriangulateEdgeStrip(
                e1, weights1, cell.Index,
                e2, weights2, neighbor.Index, hasRoad
                );
        }

        features.AddWall(e1, cell, e2, neighbor, hasRiver, hasRoad);

        HexCell nextNeighbor = cell.GetNeighbor(direction.Next());

        if (direction <= HexDirection.E && nextNeighbor != null)
        {
            Vector3 v5 = e1.v5 + HexMetrics.GetBridge(direction.Next());
            v5.y = nextNeighbor.Position.y;

            if (cell.Elevation <= neighbor.Elevation)
            {
                if (cell.Elevation <= nextNeighbor.Elevation)
                {
                    TriangulateCorner(
                        e1.v5, cell, e2.v5, neighbor, v5, nextNeighbor
                        );
                }
                else
                {
                    TriangulateCorner(
                        v5, nextNeighbor, e1.v5, cell, e2.v5, neighbor
                        );
                }
            }
            else if (neighbor.Elevation <= nextNeighbor.Elevation)
            {
                TriangulateCorner(
                    e2.v5, neighbor, v5, nextNeighbor, e1.v5, cell
                    );
            }
            else
            {
                TriangulateCorner(
                    v5, nextNeighbor, e1.v5, cell, e2.v5, neighbor
                    );
            }
        }
    }
Example #26
0
 public static Vector3 GetSecondSolidCorner(HexDirection direction)
 {
     return(corners[(int)direction + 1] * solidFactor);
 }
Example #27
0
 public static HexDirection Previous2(this HexDirection direction)
 {
     direction -= 2;
     return(direction >= HexDirection.NE ? direction : (direction + 6));
 }
Example #28
0
    void ErodeLand()
    {
        List <HexCell> erodibleCells = ListPool <HexCell> .Get();

        for (int i = 0; i < cellCount; i++)
        {
            HexCell cell = grid.GetCell(i);
            if (IsErodible(cell))
            {
                erodibleCells.Add(cell);
            }
        }

        int targetErodibleCount =
            (int)(erodibleCells.Count * (100 - erosionPercentage) * 0.01f);

        while (erodibleCells.Count > targetErodibleCount)
        {
            int     index      = Random.Range(0, erodibleCells.Count);
            HexCell cell       = erodibleCells[index];
            HexCell targetCell = GetErosionTarget(cell);

            cell.Elevation       -= 1;
            targetCell.Elevation += 1;

            if (!IsErodible(cell))
            {
                erodibleCells[index] = erodibleCells[erodibleCells.Count - 1];
                erodibleCells.RemoveAt(erodibleCells.Count - 1);
            }

            for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++)
            {
                HexCell neighbor = cell.GetNeighbor(d);
                if (
                    neighbor && neighbor.Elevation == cell.Elevation + 2 &&
                    !erodibleCells.Contains(neighbor)
                    )
                {
                    erodibleCells.Add(neighbor);
                }
            }

            if (IsErodible(targetCell) && !erodibleCells.Contains(targetCell))
            {
                erodibleCells.Add(targetCell);
            }

            for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++)
            {
                HexCell neighbor = targetCell.GetNeighbor(d);
                if (
                    neighbor && neighbor != cell &&
                    neighbor.Elevation == targetCell.Elevation + 1 &&
                    !IsErodible(neighbor)
                    )
                {
                    erodibleCells.Remove(neighbor);
                }
            }
        }

        ListPool <HexCell> .Add(erodibleCells);
    }
Example #29
0
 public static HexDirection Next2(this HexDirection direction)
 {
     direction += 2;
     return(direction <= HexDirection.NW ? direction : (direction - 6));
 }
Example #30
0
    void Triangulate(HexDirection direction, HexCell cell)
    {
        Vector3 center = cell.transform.localPosition;
        Vector3 v1 = center + HexMetrics.GetFirstSolidCorner(direction);
        Vector3 v2 = center + HexMetrics.GetSecondSolidCorner(direction);

        AddTriangle(center, v1, v2);
        AddTriangleColor(cell.color);

        if (direction <= HexDirection.SE) {
            TriangulateConnection(direction, cell, v1, v2);
        }
    }
Example #31
0
 public static HexDirection Opposite(this HexDirection direction)
 {
     return((int)direction < 3 ? (direction + 3) : (direction - 3));
 }
Example #32
0
 public static HexDirection Opposite(HexDirection d)
 {
     if (d == HexDirection.N) return HexDirection.S;
     else if (d == HexDirection.S) return HexDirection.N;
     else if (d == HexDirection.SE) return HexDirection.NW;
     else if (d == HexDirection.NE) return HexDirection.SW;
     else if (d == HexDirection.SW) return HexDirection.NE;
     else return HexDirection.SE;
 }
Example #33
0
    void TriangulateConnection(
        HexDirection direction, HexCell cell, EdgeVertices e1
        )
    {
        HexCell neighbor = cell.GetNeighbor(direction);

        if (neighbor == null)
        {
            return;
        }

        Vector3 bridge = HexMetrics.GetBridge(direction);

        bridge.y = neighbor.Position.y - cell.Position.y;
        EdgeVertices e2 = new EdgeVertices(
            e1.v1 + bridge,
            e1.v4 + bridge
            );

        if (cell.GetEdgeType(direction) == HexEdgeType.Slope)
        {
            TriangulateEdgeTerraces(e1, cell, e2, neighbor);
        }
        else
        {
            TriangulateEdgeStrip(e1, cell.Color, e2, neighbor.Color);
        }

        HexCell nextNeighbor = cell.GetNeighbor(direction.Next());

        if (direction <= HexDirection.E && nextNeighbor != null)
        {
            Vector3 v5 = e1.v4 + HexMetrics.GetBridge(direction.Next());
            v5.y = nextNeighbor.Position.y;

            if (cell.Elevation <= neighbor.Elevation)
            {
                if (cell.Elevation <= nextNeighbor.Elevation)
                {
                    TriangulateCorner(
                        e1.v4, cell, e2.v4, neighbor, v5, nextNeighbor
                        );
                }
                else
                {
                    TriangulateCorner(
                        v5, nextNeighbor, e1.v4, cell, e2.v4, neighbor
                        );
                }
            }
            else if (neighbor.Elevation <= nextNeighbor.Elevation)
            {
                TriangulateCorner(
                    e2.v4, neighbor, v5, nextNeighbor, e1.v4, cell
                    );
            }
            else
            {
                TriangulateCorner(
                    v5, nextNeighbor, e1.v4, cell, e2.v4, neighbor
                    );
            }
        }
    }
Example #34
0
 public HexPoint Shift(HexDirection d)
 {
     if (d == HexDirection.S) return new HexPoint(x, y + 1);
     else if (d == HexDirection.SE) return new HexPoint(x + 1, y + MyMath.IsOdd(x));
     else if (d == HexDirection.NE) return new HexPoint(x + 1, y - MyMath.IsEven(x));
     else if (d == HexDirection.N) return new HexPoint(x, y - 1);
     else if (d == HexDirection.NW) return new HexPoint(x - 1, y - MyMath.IsEven(x));
     else return new HexPoint(x - 1, y + MyMath.IsOdd(x));
 }
    /// <summary>
    ///
    /// </summary>
    /// <param name="roomId">房间id</param>
    /// <param name="ownerId">所属玩家id</param>
    /// <param name="actorId">自己的id</param>
    /// <param name="posX"></param>
    /// <param name="posZ"></param>
    /// <param name="orientation"></param>
    /// <param name="unitName"></param>
    /// <param name="cellIndex">该单位在地形块HexCell中的Index,因为根据PosX,PosZ可能得不到正确的cell,只能用这个数据确保正确</param>
    /// <param name="actorInfoId">兵种ID,在actor_info表中的id</param>
    /// <param name="name"></param>
    /// <param name="hp"></param>
    /// <param name="attackPower"></param>
    /// <param name="defencePower"></param>
    /// <param name="speed"></param>
    /// <param name="filedOfVision"></param>
    /// <param name="shootingRange"></param>
    /// <param name="attackDuration"></param>
    /// <param name="attackInterval"></param>
    /// <param name="ammoBase"></param>
    /// <param name="ammoBaseMax"></param>
    /// <returns></returns>
    public ActorVisualizer CreateUnit(long roomId, long ownerId, long actorId, int posX, int posZ, float orientation,
                                      string unitName, int cellIndex, int actorInfoId,
                                      string name, int hp, int hpMax, float attackPower, float defencePower, float speed, float filedOfVision, float shootingRange,
                                      float attackDuration, float attackInterval, int ammoBase, int ammoBaseMax)
    {
        HexCell cell = GetCell(cellIndex);

        if (!cell)
        {
            GameRoomManager.Instance.Log($"HexmapHelper CreateUnit :创建Actor失败!格子越界 - <{posX},{posZ}> - {unitName}");
            return(null);
        }

        if (cellIndex == 0)
        {
            Debug.LogError("HexmapHelper CreateUnit Error - CellIndex is lost!!!");
            return(null);
        }

        if (cell.Unit)
        {
            HexCell newCell = null;
            for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++)
            {
                HexCell neighbor = cell.GetNeighbor(d);
                if (neighbor && !neighbor.Unit)
                {
                    newCell = neighbor;
                    break;
                }
            }

            if (newCell)
            {
                GameRoomManager.Instance.Log($"HexmapHelper :创建Actor失败!物体位置重合了,重新放置! - 原坐标:<{posX},{posZ}> - 新坐标:<{newCell.coordinates.X},{newCell.coordinates.Z}> - {unitName}");
                cell = newCell;
            }
            else
            {
                GameRoomManager.Instance.Log($"HexmapHelper :创建Actor失败!原来这个格子没有物体,现在有了物体, 附近也没有空地! - <{posX},{posZ}> - {unitName}");
                return(null);
            }
        }

        // 区分颜色
        string newUnitName = unitName;

        if (ownerId != GameRoomManager.Instance.CurrentPlayer.TokenId)
        { // 如果不是自己,蓝色就换成红色(绿色换成黄色)
            if (unitName.IndexOf("BLUE") > 0)
            {
                newUnitName = unitName.Replace("BLUE", "RED");
            }
            else if (unitName.IndexOf("GREEN") > 0)
            {
                newUnitName = unitName.Replace("GREEN", "YELLOW");
            }
        }

        string unitPathName = $"Arts/BeffioPrefabs/Soldiers/{newUnitName}";
        var    go           = Resources.Load <HexUnit>(unitPathName);

        if (go != null)
        {
            HexUnit hu = Instantiate(go);
            if (hu != null)
            {
                hexGrid.AddUnit(hu, cell, orientation);
                var av = hu.GetComponent <ActorVisualizer>();
                if (av != null)
                {
                    av.RoomId      = roomId;
                    av.OwnerId     = ownerId;
                    av.ActorId     = actorId;
                    av.PosX        = posX;
                    av.PosZ        = posZ;
                    av.Orientation = orientation;
                    av.Species     = unitName;
                    av.CellIndex   = cellIndex;
                    av.ActorInfoId = actorInfoId;

                    av.Name          = name;
                    av.Hp            = hp;
                    av.HpMax         = hpMax;
                    av.AttackPower   = attackPower;
                    av.DefencePower  = defencePower;
                    av.Speed         = speed;
                    av.FieldOfVision = filedOfVision;
                    av.ShootingRange = shootingRange;

                    av.AttackDuration = attackDuration;
                    av.AttackInterval = attackInterval;
                    av.AmmoBase       = ammoBase;
                    av.AmmoBaseMax    = ammoBaseMax;
                }

                // 关闭预制件上没用的东西,看以后这东西能否用得上,如果没用,就完全干掉
                if (hu.GetComponentInChildren <ThirdPersonUserControl>())
                {
                    hu.GetComponentInChildren <ThirdPersonUserControl>().enabled = false;
                }
                if (hu.GetComponentInChildren <ThirdPersonCharacter>())
                {
                    hu.GetComponentInChildren <ThirdPersonCharacter>().enabled = false;
                }
                if (hu.GetComponentInChildren <CapsuleCollider>())
                {
                    hu.GetComponentInChildren <CapsuleCollider>().enabled = false;
                }
                EnableFollowCamera(av, false);

                if (!GameRoomManager.Instance.RoomLogic.ActorManager.AllActors.ContainsKey(actorId))
                {
                    ActorBehaviour ab = new ActorBehaviour()
                    {
                        RoomId      = roomId,
                        OwnerId     = ownerId,
                        ActorId     = actorId,
                        PosX        = posX,
                        PosZ        = posZ,
                        Orientation = orientation,
                        Species     = unitName,
                        CellIndex   = cellIndex,
                        ActorInfoId = actorInfoId,

                        Name          = name,
                        Hp            = hp,
                        HpMax         = hpMax,
                        AttackPower   = attackPower,
                        DefencePower  = defencePower,
                        Speed         = speed,
                        FieldOfVision = filedOfVision,
                        ShootingRange = shootingRange,

                        AttackDuration = attackDuration,
                        AttackInterval = attackInterval,
                        AmmoBase       = ammoBase,
                        AmmoBaseMax    = ammoBaseMax,
                    };
                    GameRoomManager.Instance.RoomLogic.ActorManager.AddActor(ab, hu);
                }
                GameRoomManager.Instance.Log($"MSG: CreateATroopReply - 创建了一个Actor - {unitName}");
                return(av);
            }
        }

        return(null);
    }
Example #36
0
    bool Search(HexCell fromCell, HexCell toCell, HexUnit unit)
    {
        int speed = unit.Speed;

        searchFrontierPhase += 2;
        if (searchFrontier == null)
        {
            searchFrontier = new HexCellPriorityQueue();
        }
        else
        {
            searchFrontier.Clear();
        }

        fromCell.SearchPhase = searchFrontierPhase;
        fromCell.Distance    = 0;
        searchFrontier.Enqueue(fromCell);
        while (searchFrontier.Count > 0)
        {
            HexCell current = searchFrontier.Dequeue();
            current.SearchPhase += 1;

            if (current == toCell)
            {
                return(true);
            }

            int currentTurn = (current.Distance - 1) / speed;

            for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++)
            {
                HexCell neighbor = current.GetNeighbor(d);
                if (
                    neighbor == null ||
                    neighbor.SearchPhase > searchFrontierPhase
                    )
                {
                    continue;
                }
                if (!unit.IsValidDestination(neighbor))
                {
                    continue;
                }
                int moveCost = unit.GetMoveCost(current, neighbor, d);
                if (moveCost < 0)
                {
                    continue;
                }

                int distance = current.Distance + moveCost;
                int turn     = (distance - 1) / speed;
                if (turn > currentTurn)
                {
                    distance = turn * speed + moveCost;
                }

                if (neighbor.SearchPhase < searchFrontierPhase)
                {
                    neighbor.SearchPhase     = searchFrontierPhase;
                    neighbor.Distance        = distance;
                    neighbor.PathFrom        = current;
                    neighbor.SearchHeuristic =
                        neighbor.coordinates.DistanceTo(toCell.coordinates);
                    searchFrontier.Enqueue(neighbor);
                }
                else if (distance < neighbor.Distance)
                {
                    int oldPriority = neighbor.SearchPriority;
                    neighbor.Distance = distance;
                    neighbor.PathFrom = current;
                    searchFrontier.Change(neighbor, oldPriority);
                }
            }
        }
        return(false);
    }
Example #37
0
    List <HexCell> GetVisibleCells(HexCell fromCell, int range)
    {
        List <HexCell> visibleCells = ListPool <HexCell> .Get();

        searchFrontierPhase += 2;
        if (searchFrontier == null)
        {
            searchFrontier = new HexCellPriorityQueue();
        }
        else
        {
            searchFrontier.Clear();
        }

        range += fromCell.ViewElevation;
        fromCell.SearchPhase = searchFrontierPhase;
        fromCell.Distance    = 0;
        searchFrontier.Enqueue(fromCell);
        HexCoordinates fromCoordinates = fromCell.coordinates;

        while (searchFrontier.Count > 0)
        {
            HexCell current = searchFrontier.Dequeue();
            current.SearchPhase += 1;
            visibleCells.Add(current);

            for (HexDirection d = HexDirection.NE; d <= HexDirection.NW; d++)
            {
                HexCell neighbor = current.GetNeighbor(d);
                if (
                    neighbor == null ||
                    neighbor.SearchPhase > searchFrontierPhase ||
                    !neighbor.Explorable
                    )
                {
                    continue;
                }

                int distance = current.Distance + 1;
                if (distance + neighbor.ViewElevation > range ||
                    distance > fromCoordinates.DistanceTo(neighbor.coordinates)
                    )
                {
                    continue;
                }

                if (neighbor.SearchPhase < searchFrontierPhase)
                {
                    neighbor.SearchPhase     = searchFrontierPhase;
                    neighbor.Distance        = distance;
                    neighbor.SearchHeuristic = 0;
                    searchFrontier.Enqueue(neighbor);
                }
                else if (distance < neighbor.Distance)
                {
                    int oldPriority = neighbor.SearchPriority;
                    neighbor.Distance = distance;
                    searchFrontier.Change(neighbor, oldPriority);
                }
            }
        }
        return(visibleCells);
    }
Example #38
0
 public static Vector3 GetFirstCorner(HexDirection direction)
 {
     return corners[(int)direction];
 }
Example #39
0
 /// <summary>
 //returns the opposite direction of the current one
 /// </summary>
 /// <param name="direction"></param>
 /// <returns> returns the opposite direction </returns>
 public static HexDirection Opposite(this HexDirection direction)
 {
     //if it is less than three add three to get the opposite if it is greater than three subtract three to get the opposite
     return((int)direction < 3 ? (direction + 3) : (direction - 3));
 }
Example #40
0
 public static Vector3 GetSecondCorner(HexDirection direction)
 {
     return corners[(int)direction + 1];
 }
Example #41
0
 /// <summary>
 /// returns the previous direction without going out of bounds
 /// </summary>
 /// <param name="direction"></param>
 /// <returns>the previous direction</returns>
 public static HexDirection Previous(this HexDirection direction)
 {
     return(direction == HexDirection.UR ? HexDirection.UL : (direction - 1));
 }
 public static Vector2 GetCoordsAtDistance(Vector2 coords, int distance, HexDirection dir)
 {
     switch(dir) {
         case HexDirection.North:
             return getNorth(coords, distance);
         case HexDirection.South:
             return getSouth(coords, distance);
         case HexDirection.Northeast:
             return getNortheast(coords, distance);
         case HexDirection.Northwest:
             return getNorthwest(coords, distance);
         case HexDirection.Southwest:
             return getSouthwest(coords, distance);
         case HexDirection.Southeast:
             return getSoutheast(coords, distance);
         default:
             return new Vector2();
     }
 }
Example #43
0
 /// <summary>
 /// returns the next direction without going out of bounds
 /// </summary>
 /// <param name="direction"></param>
 /// <returns>the next direction</returns>
 public static HexDirection Next(this HexDirection direction)
 {
     return(direction == HexDirection.UL ? HexDirection.UR : (direction + 1));
 }
Example #44
0
	/// <summary>Converts a cardinal direction to a corresponding world direction.</summary>
	/// <param name="direction">Cardinal direction.</param>
	/// <param name="origin">World position of the grid's origin (actual position and origin offset).</param>
	/// This method is intended for internal use when calculating the herring position of a vertex to draw.
	private Vector3 cardinalToDirection(HexDirection direction, Vector3 origin) {
		Vector3 result = Vector3.zero;
		switch (direction) {
		case HexDirection.E:
			result[idxS[0]] =  2.0f / 3.0f;	result[idxS[1]] = -1.0f / 3.0f;	break;
		case HexDirection.NE:
			result[idxS[0]] =  1.0f / 3.0f; result[idxS[1]] =  1.0f / 3.0f;	break;
		case HexDirection.NW:
			result[idxS[0]] = -1.0f / 3.0f;	result[idxS[1]] =  1.0f / 3.0f;	break;
		case HexDirection.W:
			result[idxS[0]] = -2.0f / 3.0f; result[idxS[1]] = -1.0f / 3.0f;	break;
		case HexDirection.SW:
			result[idxS[0]] = -1.0f / 3.0f;	result[idxS[1]] = -2.0f / 3.0f;	break;
		case HexDirection.SE:
			result[idxS[0]] =  1.0f / 3.0f;	result[idxS[1]] = -2.0f / 3.0f;	break;
		}
		return HerringOddToWorld(result) - origin;
	}
Example #45
0
 public static Vector3 GetFirstCorner(HexDirection direction)
 {
     return(corners[(int)direction]);
 }
Example #46
0
	public Vector3 GetDirection (HexDirection dir, HexOrientation mode) {
		Vector3 vec = Vector3.zero;
		if (mode == HexOrientation.PointySides) {
			if (dir == HexDirection.N || dir == HexDirection.S) {
				vec = height * locUnits[idxS[1]];
			} else if (dir == HexDirection.NE || dir == HexDirection.SW){
				vec = 1.5f * radius * locUnits[idxS[0]] + 0.5f * height * locUnits[idxS[1]];
			} else if (dir == HexDirection.E || dir == HexDirection.W) {
				vec = 1.5f * radius * locUnits[idxS[0]];
			} else if (dir == HexDirection.SE || dir == HexDirection.NW) {
				vec = 1.5f * radius * locUnits [idxS[0]] - 0.5f * height * locUnits [idxS[1]];
			}
		} else{
			if (dir == HexDirection.N || dir == HexDirection.S) {
				vec = 1.5f * radius * locUnits[idxS[1]];
			} else if (dir == HexDirection.NE || dir == HexDirection.SW){
				vec = 0.5f * height * locUnits[idxS[0]] + 1.5f * radius * locUnits[idxS[1]];
			} else if (dir == HexDirection.E || dir == HexDirection.W) {
				vec = height * locUnits[idxS[0]];
			} else if (dir == HexDirection.SE || dir == HexDirection.NW) {
				vec = 0.5f * height * locUnits [idxS[0]] - 1.5f * radius * locUnits [idxS[1]];
			}
		}
		if (dir == HexDirection.S || dir == HexDirection.SW || dir == HexDirection.W || dir == HexDirection.NW)
			vec *= -1.0f;
		return vec;
	}
Example #47
0
 public static Vector3 GetSecondCorner(HexDirection direction)
 {
     return(corners[(int)direction + 1]);
 }
Example #48
0
	public Vector3 VertexToDirection(HexDirection vert, bool worldSpace){
		Vector3 dir = new Vector3();
				
		if(vert == HexDirection.E || vert == HexDirection.W){
			dir = locUnits[idxS[0]] * radius;
			if(vert == HexDirection.W)
				dir = -dir;
		} else if(vert == HexDirection.N || vert == HexDirection.S){
			dir = locUnits[idxS[1]] * 0.5f * height;
			if(vert == HexDirection.S)
				dir = -dir;
		} else if(vert == HexDirection.NE || vert == HexDirection.SW){
			dir = 0.5f * radius * locUnits[idxS[0]] + 0.5f * height * locUnits[idxS[1]];
			if(vert == HexDirection.SW)
				dir = -dir;
		}  else if(vert == HexDirection.NW || vert == HexDirection.SE){
			dir = -0.5f * radius * locUnits[idxS[0]] + 0.5f * height * locUnits[idxS[1]];
			if(vert == HexDirection.SE)
				dir = -dir;
		}
		
		return dir;	
	}
Example #49
0
 public static Vector3 GetFirstSolidCorner(HexDirection direction)
 {
     return(corners[(int)direction] * solidFactor);
 }
Example #50
0
 public ZertzBallType DoHopMove(HexLocation zl, HexDirection dir)
 {
     HexLocation hldir = HexLocation.NeighbourDirections[(byte) dir];
     HexLocation zla = zl, zlb = zla+hldir, zlc = zlb+hldir;
     //if(this[zla].CanDropBall() && this[zlb].CanDropBall() && this[zlc].CanPutBall()) {
     ZertzPiece zpa = this[zla];
     ZertzPiece zpb = this[zlb];
     ZertzPiece zpc = this[zlc];
     zpc.PutBall(zpa.DropBall());
     ZertzBallType t = zpb.DropBall();
     this[zla] = zpa;
     this[zlb] = zpb;
     this[zlc] = zpc;
     return t;
     //}
     //throw new InvalidZertzActionException(String.Format("Can't perform this hop operation: [{0}:{1}]",zl,dir));
 }
Example #51
0
 /// <summary>
 /// Compares Two corner values of a hex going in its current direction and the next direction after it. This  combined it taking into account the solid factor, locates a vector3 middle edge where for the direction to meet up
 /// </summary>
 /// <param name="direction"> Direction of the hexagon vertex e.g. NE, SE etc </param>
 /// <returns> A middle edge between two hexes depending on its direction </returns>
 public static Vector3 GetSolidEdgeMiddle(HexDirection direction)
 {
     return((corners[(int)direction] + corners[(int)direction + 1]) * (0.5f * solidFactor));
 }