Ejemplo n.º 1
0
    public void SetOutgoingRiver(GridDirection direction)
    {
        if (hasOutgoingRivers[(int)direction] || HasRoads)
        {
            Debug.Log("Could not add river");
            return;
        }
        SquareCell neighbor = GetNeighbor(direction);

        if (!neighbor || CentreElevation < neighbor.CentreElevation)
        {
            Debug.Log("Could not add river uphill");
            return;
        }
        if (hasIncomingRivers[(int)direction])
        {
            RemoveIncomingRiver(direction);
        }
        hasOutgoingRivers[(int)direction] = true;
        RefreshChunkOnly();

        neighbor.RemoveOutgoingRiver(direction.Opposite());
        neighbor.hasIncomingRivers[(int)direction.Opposite()] = true;
        neighbor.RefreshChunkOnly();
    }
Ejemplo n.º 2
0
    public int GetElevationDifference(GridDirection direction)
    {
        int differencePrev = (int)this[direction.Previous()] - (int)this[direction.Opposite().Next()];
        int differenceNext = (int)this[direction.Next()] - (int)this[direction.Opposite().Previous()];
        int result         = Mathf.Max(differencePrev, differenceNext);

        return(result);
    }
Ejemplo n.º 3
0
    void TriangulateRiverTri(Vector3 v1, Vector3 v2, Vector3 v3, float y, bool reversed, GridDirection direction, GridDirection flowDirection)
    {
        v1.y = v2.y = v3.y = y;
        rivers.AddTriangle(v1, v2, v3);
        Vector2 centre = new Vector2(0.5f, 0.5f);
        Vector2 edgePrevious, edgeNext;

        if (direction == flowDirection || direction == flowDirection.Opposite())
        {
            edgePrevious = new Vector2(0.9f, 0.9f);
            edgeNext     = new Vector2(0.1f, 0.9f);
        }
        else
        {
            if (flowDirection == direction.Next2())
            {
                edgePrevious = new Vector2(0.9f, 0.1f);
                edgeNext     = new Vector2(0.9f, 0.9f);
            }
            else
            {
                edgePrevious = new Vector2(0.1f, 0.9f);
                edgeNext     = new Vector2(0.1f, 0.1f);
            }
        }
        if (reversed)
        {
            rivers.AddTriangleUV(centre, new Vector2(1f, 1f) - edgePrevious, new Vector2(1f, 1f) - edgeNext);
        }
        else
        {
            rivers.AddTriangleUV(centre, edgePrevious, edgeNext);
        }
    }
Ejemplo n.º 4
0
 public void ConnectWithNeigbour(SquareCell neighbour, GridDirection direction)
 {
     //if (parentCell.GetVertexElevations.AverageElevation == neighbour.GetVertexElevations.AverageElevation)
     //{
     ConnectNodes(GetNode(NodeDirection.In, direction), neighbour.roadNetwork.GetNode(NodeDirection.Out, direction.Opposite()));
     ConnectNodes(GetNode(NodeDirection.Out, direction), neighbour.roadNetwork.GetNode(NodeDirection.In, direction.Opposite()));
     network.DeleteLink(GetNode(NodeDirection.Out, direction), GetNode(NodeDirection.In, direction));
     network.DeleteLink(neighbour.roadNetwork.GetNode(NodeDirection.Out, direction.Opposite()), neighbour.roadNetwork.GetNode(NodeDirection.In, direction.Opposite()));
     //}
 }
Ejemplo n.º 5
0
    public void RemoveIncomingRiver(GridDirection direction)
    {
        if (!hasIncomingRivers[(int)direction])
        {
            return;
        }
        hasIncomingRivers[(int)direction] = false;
        RefreshChunkOnly();
        SquareCell neighbor = GetNeighbor(direction);

        hasOutgoingRivers[(int)direction.Opposite()] = false;
        neighbor.RefreshChunkOnly();
    }
Ejemplo n.º 6
0
    public void AddRoad()
    {
        SquareCell    randomRoad      = townRoads[Random.Range(0, townRoads.Count)];
        GridDirection randomDirection = (GridDirection)(Random.Range(0, 3) * 2);
        SquareCell    randomNeighbour = randomRoad.GetNeighbor(randomDirection);

        if (randomNeighbour && randomNeighbour.UrbanLevel == 0 && !townRoads.Contains(randomNeighbour))
        {
            randomRoad.AddRoad(randomDirection);
            randomNeighbour.AddRoad(randomDirection.Opposite());
            townRoads.Add(randomNeighbour);
        }
    }
Ejemplo n.º 7
0
    void TriangulateWithRiver(
        GridDirection direction, SquareCell cell, Vector3 centre, Vector3 v0, Vector3 v1, Vector3 v2, Vector3 v3, float v
        )
    {
        GridDirection flowDirection;
        bool          reversed = cell.HasIncomingRiver[(int)direction];

        if (cell.HasOutgoingRiver[(int)direction])
        {
            flowDirection = direction;
        }
        else if (reversed)
        {
            flowDirection = direction.Opposite();
        }
        else
        {
            if (cell.HasIncomingRiver[(int)direction.Next2()] && cell.HasOutgoingRiver[(int)direction.Previous2()])
            {
                flowDirection = direction.Previous2();
            }
            else if (cell.HasOutgoingRiver[(int)direction.Next2()] && cell.HasIncomingRiver[(int)direction.Previous2()])
            {
                flowDirection = direction.Next2();
            }
            else
            {
                flowDirection = direction.Opposite();
            }
        }
        TriangulateRiverTri(centre, v0, v3, cell.RiverSurfaceY, reversed, direction, flowDirection);
        SquareCell neighbor = cell.GetNeighbor(direction);

        if (neighbor && cell.HasRiverThroughEdge(direction))
        {
            TriangulateRiverQuad(v3, v0, v1, v2, cell.RiverSurfaceY, neighbor.RiverSurfaceY, 0.8f, reversed);
        }
    }
Ejemplo n.º 8
0
 public void SetNeighbor(GridDirection direction, SquareCell cell)
 {
     neighbors[(int)direction] = cell;
     cell.neighbors[(int)direction.Opposite()] = this;
 }
Ejemplo n.º 9
0
    public bool HasCliff(GridDirection direction)
    {
        SquareCell neighbor = GetNeighbor(direction);

        if (neighbor)
        {
            bool noCliff = (int)vertexElevations[direction.Previous()] == (int)neighbor.vertexElevations[direction.Opposite().Next()] && (int)vertexElevations[direction.Next()] == (int)neighbor.vertexElevations[direction.Opposite().Previous()];
            return(!noCliff);
        }
        else
        {
            return(false);
        }
    }
 public static bool IsColinear(this GridDirection dir, GridDirection other)
 {
     if (dir == other || dir == other.Opposite())
         return true;
     else
         return false;
 }
Ejemplo n.º 11
0
 void EditCell(SquareCell cell, Vector3 hitpoint)
 {
     if (activeMode == EditMode.color)
     {
         cell.Tile = activeTileMaterial.GetClone;
     }
     else if (activeMode == EditMode.elevation)
     {
         GridDirection vertex = squareGrid.GetVertex(hitpoint);
         if (Input.GetMouseButton(0) && (stopWatch.ElapsedMilliseconds > 500f || freshClick))
         {
             cell.ChangeVertexElevation(vertex, 1);
             if (!allowCliffs)
             {
                 if (cell.GetNeighbor(vertex))
                 {
                     cell.GetNeighbor(vertex).ChangeVertexElevation(vertex.Opposite(), 1);
                 }
                 if (cell.GetNeighbor(vertex.Next()))
                 {
                     cell.GetNeighbor(vertex.Next()).ChangeVertexElevation(vertex.Previous2(), 1);
                 }
                 if (cell.GetNeighbor(vertex.Previous()))
                 {
                     cell.GetNeighbor(vertex.Previous()).ChangeVertexElevation(vertex.Next2(), 1);
                 }
             }
             stopWatch.Reset();
             stopWatch.Start();
         }
         if (Input.GetMouseButton(1) && (stopWatch.ElapsedMilliseconds > 500f || freshClick))
         {
             cell.ChangeVertexElevation(vertex, -1);
             if (!allowCliffs)
             {
                 if (cell.GetNeighbor(vertex))
                 {
                     cell.GetNeighbor(vertex).ChangeVertexElevation(vertex.Opposite(), -1);
                 }
                 if (cell.GetNeighbor(vertex.Next()))
                 {
                     cell.GetNeighbor(vertex.Next()).ChangeVertexElevation(vertex.Previous2(), -1);
                 }
                 if (cell.GetNeighbor(vertex.Previous()))
                 {
                     cell.GetNeighbor(vertex.Previous()).ChangeVertexElevation(vertex.Next2(), -1);
                 }
             }
             stopWatch.Reset();
             stopWatch.Start();
         }
     }
     else if (activeMode == EditMode.rivers)
     {
         if (Input.GetMouseButton(1))
         {
             cell.RemoveRivers();
             Explosion(cell);
         }
         else if (isDrag)
         {
             SquareCell otherCell = cell.GetNeighbor(dragDirection.Opposite()); // work with brushes
             if (otherCell)
             {
                 otherCell.SetOutgoingRiver(dragDirection);
             }
         }
     }
     else if (activeMode == EditMode.roads)
     {
         float fracX = hitpoint.x - Mathf.Floor(hitpoint.x);
         float fracZ = hitpoint.z - Mathf.Floor(hitpoint.z);
         if (fracX > 0.25f && fracX < 0.75f || fracZ > 0.25f && fracZ < 0.75f)
         {
             GridDirection edge = squareGrid.GetEdge(hitpoint);
             if (Input.GetMouseButton(1))
             {
                 cell.RemoveRoad(edge);
                 Explosion(cell);
             }
             else
             {
                 cell.AddRoad(edge);
             }
         }
     }
     else if (activeMode == EditMode.water_level)
     {
         if (Input.GetMouseButton(0) && freshClick)
         {
             cell.WaterLevel++;
         }
         else if (Input.GetMouseButton(1) && freshClick)
         {
             cell.WaterLevel--;
         }
     }
     else if (activeMode == EditMode.building)
     {
         if (Input.GetMouseButton(0) && freshClick)
         {
             cell.UrbanLevel++;
         }
         if (Input.GetMouseButton(1) && freshClick)
         {
             cell.UrbanLevel = 0;
             Explosion(cell);
         }
     }
     else if (activeMode == EditMode.trees)
     {
         if (Input.GetMouseButton(0) && freshClick)
         {
             cell.PlantLevel++;
         }
         if (Input.GetMouseButton(1) && freshClick)
         {
             cell.PlantLevel = 0;
             Explosion(cell);
         }
     }
     else if (activeMode == EditMode.rocks)
     {
         if (Input.GetMouseButton(0) && freshClick)
         {
             cell.ScenaryObject = 1;
         }
         if (Input.GetMouseButton(1) && freshClick)
         {
             cell.ScenaryObject = 0;
             Explosion(cell);
         }
     }
     else if (activeMode == EditMode.mast)
     {
         if (Input.GetMouseButton(0) && freshClick)
         {
             cell.ScenaryObject = 2;
         }
         if (Input.GetMouseButton(1) && freshClick)
         {
             cell.ScenaryObject = 0;
             Explosion(cell);
         }
     }
     else if (activeMode == EditMode.lighthouse)
     {
         if (Input.GetMouseButton(0) && freshClick)
         {
             cell.ScenaryObject = 3;
         }
         if (Input.GetMouseButton(1) && freshClick)
         {
             cell.ScenaryObject = 0;
             Explosion(cell);
         }
     }
     else if (activeMode == EditMode.industry)
     {
         if (Input.GetMouseButton(0) && freshClick)
         {
             cell.Industry = (int)activeIndustry + 1;
         }
         if (Input.GetMouseButton(1) && freshClick)
         {
             cell.Industry = 0;
             Explosion(cell);
         }
     }
     else if (activeMode == EditMode.town)
     {
         if (cell.Town == null)
         {
             GameObject  town    = Instantiate(townPrefab);
             TownManager manager = town.GetComponent <TownManager>();
             town.transform.position = GridCoordinates.ToPosition(cell.coordinates) + new Vector3(0, cell.CentreElevation * GridMetrics.elevationStep, 0);
             manager.Init(cell);
             cell.Town = manager;
         }
     }
 }