Exemple #1
0
    public static void ShowHeightMap(float startY = 0.5f)
    {
        float stepSize = (TerrainGen.gridY) * TerrainGen.hexSize / 2;

        for (int x = 0; x < TerrainGen.gridX; x++)
        {
            for (int z = 0; z < TerrainGen.gridZ; z++)
            {
                //
                Vector2Int hex       = new Vector2Int(x, z);
                float      hexHeight = TerrainGen.GetHexHeight(hex);
                int        steps     = Mathf.RoundToInt((hexHeight - startY) / (TerrainGen.hexSize / 2));
                steps = 5 + Mathf.Abs(steps % 10);
                Set(hex, (HexHighlight)steps);
            }
        }
    }
    void TargetNode(ref PathNode nodeA, PathType type)
    {
        int offSet = 0;

        if (nodeA.z % 2 == 0)
        {
            offSet = 6;
        }
        PathNode nodeB;

        for (int i = 0; i < 6; i++)
        {
            Vector3Int checkPos = Vector3Int.zero;
            checkPos.x = Mathf.Clamp(nodeA.x + checkOffSets[i + offSet].x, 0, gridX - 1);
            checkPos.z = Mathf.Clamp(nodeA.z + checkOffSets[i + offSet].z, 0, gridZ - 1);

            nodeB = pathGrids[type][checkPos.x, checkPos.z];
            if (nodeB.cost != null && nodeB.cost < nodeA.cost)
            {
                RaycastHit hit;
                Vector3    rayPos = TerrainGen.GetHexPosition(nodeB.positionVec);
                rayPos.y += 1;
                Physics.Raycast(rayPos, Vector3.down, out hit, 5f);
                if (debug)
                {
                    Debug.DrawRay(rayPos, Vector3.down, Color.blue, 0.5f);
                }
                if (hit.collider != null)
                {
                    BuildingBase buildingBase = null;
                    buildingBase = hit.collider.transform.parent.gameObject.GetComponent <BuildingBase>();
                    if (buildingBase != null && buildingBase.HasProperty(BuildingProperties.blocking))
                    {
                        Debug.DrawRay(hit.collider.transform.parent.gameObject.transform.position, Vector3.up, Color.magenta, 8f);
                        nodeB.cost = 100000;
                        if (nodeA.target == nodeB.position)
                        {
                            i            = 0;
                            nodeA.target = Vector2Int.zero;
                            nodeA.cost   = 10000;
                        }
                        continue;
                    }
                }
                float heightDif = TerrainGen.GetHexHeight(nodeA.positionVec) - TerrainGen.GetHexHeight(nodeB.positionVec);
                heightDif = heightDif / TerrainGen.hexSize / 2;
                if (type == PathType.flight)
                {
                    heightDif = 0;
                }
                switch (Mathf.RoundToInt(heightDif))
                {
                case 0:
                    if (nodeB.cost + walkCost < nodeA.cost)
                    {
                        nodeA.target = nodeB.position;
                        nodeA.cost   = nodeB.cost + walkCost;
                    }
                    break;

                case 1:
                    if (nodeB.cost + climbCost < nodeA.cost)
                    {
                        nodeA.target = nodeB.position;
                        nodeA.cost   = nodeB.cost + climbCost;
                    }
                    break;
                }
            }
        }
    }
    bool MakeTargetNode(ref PathNode nodeA, ref PathNode nodeB, PathType type)
    {
        if (nodeA.cost == null)
        {
            return(true);
        }
        RaycastHit hit;
        Vector3    rayPos = TerrainGen.GetHexPosition(nodeA.positionVec);

        rayPos.y += 1;
        Physics.Raycast(rayPos, Vector3.down, out hit, 5f);
        if (debug)
        {
            Debug.DrawRay(rayPos, Vector3.down, Color.magenta, 0.5f);
        }
        if (hit.collider != null)
        {
            //Debug.Log("Found Target:" + hit.collider.name);
            BuildingBase building = hit.collider.transform.parent.GetComponent <BuildingBase>();
            if (building != null)
            {
                Debug.Log("Found a building");
                if (nodeB.target == nodeA.position)
                {
                    nodeB.cost   = 100000;
                    nodeB.target = Vector2Int.zero;
                    TargetNode(ref nodeB, type);
                    return(false);
                }
            }
        }
        float heightDiff;

        heightDiff = TerrainGen.GetHexHeight(nodeB.positionVec) - TerrainGen.GetHexHeight(nodeA.positionVec);
        heightDiff = heightDiff / (TerrainGen.hexSize / 2 * TerrainGen.instance.yMult);

        //Debug.Log("HeightDif: " + heightDiff);

        switch (Mathf.Abs(Mathf.RoundToInt(heightDiff)))
        {
        case 0:
            if (nodeB.cost == null || nodeB.cost > nodeA.cost + walkCost)
            {
                //Debug.Log("Adding level");
                nodeB.target = nodeA.position;
                if (nodeA.cost != null)
                {
                    nodeB.cost = nodeA.cost + walkCost;
                }
                else
                {
                    return(true);
                }
            }
            break;

        case 1:
            if (nodeB.cost == null || nodeB.cost > nodeA.cost + climbCost)
            {
                //Debug.Log("Adding other");
                nodeB.target = nodeA.position;
                if (nodeA.cost != null)
                {
                    nodeB.cost = nodeA.cost + climbCost;
                }
                else
                {
                    return(true);
                }
            }
            break;

        default:
            if (nodeB.cost == null || nodeB.cost > nodeA.cost + 100)
            {
                //Debug.Log("Adding other");
                nodeB.target = nodeA.position;
                if (nodeA.cost != null)
                {
                    nodeB.cost = nodeA.cost + 10000;
                }
                else
                {
                    nodeB.cost = 100;
                }
            }
            break;
        }
        return(false);
    }
    public void GeneratePath(PathType type, Vector3Int destination)
    {
        goal             = destination;
        PathNode[,] grid = new PathNode[gridX, gridZ];
        for (int x = 0; x < gridX; x++)
        {
            for (int z = 0; z < gridZ; z++)
            {
                if (TerrainGen.GetHexHeight(new Vector2Int(x, z)) != -1)
                {
                    grid[x, z] = new PathNode(x, z);
                }
            }
        }
        grid[destination.x, destination.z].cost    = 0;
        grid[destination.x, destination.z].targetX = destination.x;
        grid[destination.x, destination.z].targetZ = destination.z;
        List <PathNode> calcingNodes = new List <PathNode>();

        calcingNodes.Add(new PathNode(destination.x, destination.z, -1));
        PathNode node;
        PathNode nextNode;
        float    heightDiff;

        while (calcingNodes.Count > 0)
        {
            node = calcingNodes[0];
            Vector3Int checkPos = Vector3Int.zero;
            int        offSet   = 0;
            if (node.z % 2 == 0)
            {
                offSet += 6;
            }
            for (int i = 0; i < 6; i++)
            {
                checkPos.x = Mathf.Clamp(node.x + checkOffSets[i + offSet].x, 0, gridX - 1);
                checkPos.z = Mathf.Clamp(node.z + checkOffSets[i + offSet].z, 0, gridZ - 1);
                nextNode   = grid[checkPos.x, checkPos.z];
                if (nextNode.cost == null && !calcingNodes.Contains(nextNode))
                {
                    calcingNodes.Add(nextNode);
                }
                MakeTargetNode(ref node, ref nextNode, type);
            }

            calcingNodes.RemoveAt(0);
            calcingNodes.Sort();
            if (calcingNodes.Count > 1000)
            {
                Debug.LogError("Calc overload!");
                break;
            }
        }
        if (pathGrids.ContainsKey(type))
        {
            pathGrids[type] = grid;
        }
        else
        {
            pathGrids.Add(type, grid);
        }
        generated = true;
    }
    public static Dictionary <string, List <Vector2Int> > GetHexesInRange(Vector3 position, int range, bool arc = false, float offSet = 0.05f)
    {
        Dictionary <string, List <Vector2Int> > hexesInRange;

        hexesInRange = new Dictionary <string, List <Vector2Int> >();

        List <Vector2Int> hexInRange   = new List <Vector2Int>();
        List <Vector2Int> goodHexes    = new List <Vector2Int>();
        List <Vector2Int> blockedHexes = new List <Vector2Int>();
        Vector3Int        hexPos       = TerrainGen.GetGridPosition(position);
        Vector2Int        gridPos      = new Vector2Int(hexPos.x, hexPos.z);

        hexInRange = TerrainGen.GetHexInRange(gridPos, range);
        Vector3 towerPos = position;

        towerPos.y += offSet;

        foreach (Vector2Int newPos in hexInRange)
        {
            if (!MyMath.IsWithin(hexPos.x, -1, TerrainGen.gridX) || !MyMath.IsWithin(hexPos.y, -1, TerrainGen.gridZ))
            {
                continue;
            }
            RaycastHit[] hits;
            bool         good = false;
            BuildingBase building;
            Vector3      castPos = TerrainGen.GetHexPosition(newPos.x, newPos.y);
            if (position.y > TerrainGen.GetHexHeight(newPos))
            {
                castPos.y += 0.25f;
            }
            else
            {
                castPos.y += 0.05f;
            }
            if (!arc)
            {
                //Physics.RayC
                float   lineLenght = Vector3.Distance(towerPos, castPos);
                Vector3 faceDir    = MyMath.GetDirectionRatio(towerPos, castPos);
                hits = Physics.RaycastAll(towerPos, -faceDir, lineLenght);
                good = true;
                foreach (RaycastHit hit in hits)
                {
                    building = hit.transform.GetComponentInParent <BuildingBase>();
                    if (building != null)
                    {
                        if (!building.Ethereal)
                        {
                            good = false;
                        }
                    }
                    else
                    {
                        good = false;
                    }
                }
                if (good)
                {
                    goodHexes.Add(newPos);
                }
                else
                {
                    blockedHexes.Add(newPos);
                }
            }
            else
            {
                goodHexes.Add(newPos);
            }
        }
        hexesInRange.Add("good", goodHexes);
        hexesInRange.Add("bad", blockedHexes);

        return(hexesInRange);
    }
    void MoveCamera()
    {
        if (Input.GetAxisRaw("Horizontal") > 0 || (Input.mousePosition.x > Screen.width - scrollBorder && Input.mousePosition.x < Screen.width + overBorder))
        {
            camHandle.transform.Translate(new Vector3(panSpeed, 0, 0), Space.Self);
        }
        else if (Input.GetAxisRaw("Horizontal") < 0 || (Input.mousePosition.x < scrollBorder && Input.mousePosition.x + overBorder > 0))
        {
            camHandle.transform.Translate(new Vector3(-panSpeed, 0, 0), Space.Self);
        }
        if (Input.GetAxisRaw("Vertical") > 0 || (Input.mousePosition.y > Screen.height - scrollBorder && Input.mousePosition.y < Screen.height + overBorder))
        {
            Quaternion saveRotation  = camHandle.transform.rotation;
            Quaternion rotation      = camHandle.transform.rotation;
            Vector3    eulerRotation = rotation.eulerAngles;
            eulerRotation.x = 0;
            if (eulerRotation.z != 0)
            {
                eulerRotation.y += eulerRotation.z;
                eulerRotation.z  = 0;
            }
            rotation.eulerAngles = eulerRotation;

            camHandle.rotation = rotation;
            camHandle.transform.Translate(new Vector3(0, 0, panSpeed), Space.Self);
            camHandle.rotation = saveRotation;
        }
        else if (Input.GetAxisRaw("Vertical") < 0 || (Input.mousePosition.y < scrollBorder && Input.mousePosition.y + overBorder > 0))
        {
            Quaternion saveRotation  = camHandle.transform.rotation;
            Quaternion rotation      = camHandle.transform.rotation;
            Vector3    eulerRotation = rotation.eulerAngles;
            eulerRotation.x = 0;
            if (eulerRotation.z != 0)
            {
                eulerRotation.y += eulerRotation.z;
                eulerRotation.z  = 0;
            }
            rotation.eulerAngles = eulerRotation;

            camHandle.rotation = rotation;
            camHandle.transform.Translate(new Vector3(0, 0, -panSpeed), Space.Self);
            camHandle.rotation = saveRotation;
        }
        Vector3 pos   = camHandle.position;
        float   lastY = pos.y;

        pos.y = TerrainGen.GetHexHeight(TerrainGen.GetGridPosition(pos));
        if (pos.y == -1)
        {
            pos.y = lastY;
        }
        camHandle.transform.position = pos;



        if (mouseCheck[2] != 0)
        {
            if (mouseCheck[2] > 0)
            {
                gameCam.transform.Translate(new Vector3(0, 0, panSpeed), Space.Self);
            }
            else
            {
                gameCam.transform.Translate(new Vector3(0, 0, -panSpeed), Space.Self);
            }
            //

            Vector3 camPos = gameCam.transform.localPosition;
            camPos.z = Mathf.Clamp(camPos.z, -40, -2.5f);
            camPos.y = Mathf.Clamp(camPos.y, 5f, 20);
            gameCam.transform.localPosition = camPos;
        }

        Vector3 menuPos = GetScreenPosition(hexHook.transform.position);

        menuPos.x += 60;
        triMenu.PlaceMenu(menuPos);
        Vector2 detailPos = GetScreenPosition(hexHook.transform.position);

        detailPos.x -= 120;
        triMenu.hexDetails.SetPosition(detailPos);
    }