예제 #1
0
    public void Load()
    {
        for (int x = xMin; x < xMax; x++)
        {
            cursorMatrix.Add(new List <Cursor>());
            for (int z = zMin; z < zMax; z++)
            {
                int        elevation    = VoxelController.GetElevation(x, z);
                GameObject cursorObject = Instantiate(cursorPrefab, new Vector3(0, 0, 0), Quaternion.identity);
                cursorObject.transform.parent = GameObject.Find("Cursors").transform;
                Cursor cursor = cursorObject.GetComponent <Cursor>();
                cursor.originalColor = Color.gray;
                cursor.xPos          = x;
                cursor.zPos          = z;
                cursor.yPos          = elevation;
                if (elevation < 0)
                {
                    cursor.useable = false;
                }
                cursorMatrix[x].Add(cursor);
            }
        }

        StartCoroutine(SetLoaded());
    }
예제 #2
0
    private IEnumerator SpawnTreasure()
    {
        print("spawning treasure");
        GameController.treasureCount++;
        int x = Random.Range(0, 19);
        int z = Random.Range(0, 19);

        while (!Helpers.GetTile(x, z).CanHaveTreasure())
        {
            x = Random.Range(0, 19);
            z = Random.Range(0, 19);
        }

        int y = VoxelController.GetElevation(x, z);

        GameObject treasure = Instantiate(Resources.Load("Treasure"), Vector3.zero, Quaternion.identity) as GameObject;
        Cursor     cursor   = Helpers.GetTile(x, z);

        cursor.standingTreasure = treasure.GetComponent <Treasure>();
        print(treasure.GetComponent <Treasure>());
        treasure.GetComponent <Treasure>().cursor = cursor;

        Helpers.SetTransformPosition(treasure.transform, x, y, z);
        MainCamera.Lock();
        MainCamera.CenterOnWorldPoint(treasure.transform.position);
        yield return(new WaitForSeconds(3f));

        MainCamera.Unlock();
        instance.ContinueNextTurn();
    }
예제 #3
0
    private static bool CanMoveToNextTile(Cursor target, Cursor origin, int allowance)
    {
        int targetElevation = VoxelController.GetElevation(target.xPos, target.zPos);
        int originElevation = VoxelController.GetElevation(origin.xPos, origin.zPos);
        int diff            = Mathf.Abs(targetElevation - originElevation);

        return(diff <= allowance);
    }
예제 #4
0
    public static List <Cursor> GetRadialTiles(int originX, int originZ, int maxHops, int maxHeightChange, bool allowOthers,
                                               int minDistance = 0, bool heightAugmented = false, bool forMovement = false)
    {
        print("max height diff: " + maxHeightChange);
        Cursor       origin = GetTile(originX, originZ);
        List <int[]> queue  = new List <int[]>();

        queue.Add(new int[] { originX, originZ, 0 });
        for (int i = 0; i < queue.Count; i++)
        {
            int[] entry      = queue[i];
            int   counter    = entry[2] + 1;
            int   hopAugment = heightAugmented ? origin.yPos : 0;
            if (counter > maxHops + hopAugment)
            {
                continue;
            }

            List <Cursor> neighbors = Neighbors(entry[0], entry[1]);

            List <int[]> newCells = new List <int[]>();

            foreach (Cursor cursor in neighbors)
            {
                if (allowOthers || !cursor.standingUnit || cursor.standingUnit == Unit.Subject())
                {
                    if (forMovement)
                    {
                        Unit unit = Helpers.GetTile(originX, originZ).standingUnit;
                        if (!CanMoveThere(unit, cursor))
                        {
                            continue;
                        }
                    }

                    int  elevation = VoxelController.GetElevation(cursor.xPos, cursor.zPos);
                    bool movable   = (!forMovement && Mathf.Abs(elevation - origin.yPos) <= maxHeightChange) || (elevation >= 0 && Helpers.CanMoveToNextTile(cursor, Helpers.GetTile(entry[0], entry[1]), maxHeightChange));
                    if (movable)
                    {
                        newCells.Add(new int[] { cursor.xPos, cursor.zPos, counter, elevation });
                    }
                }
            }

            for (int a = newCells.Count - 1; a >= 0; a--)
            {
                for (int g = 0; g < queue.Count; g++)
                {
                    if (newCells[a][0] == queue[g][0] &&
                        newCells[a][1] == queue[g][1] &&
                        newCells[a][2] <= queue[g][2])
                    {
                        newCells.RemoveAt(a);
                        break;
                    }
                }
            }

            for (int a = 0; a < newCells.Count; a++)
            {
                queue.Add(newCells[a]);
            }
        }

        HashSet <Cursor> cursors = new HashSet <Cursor>();

        foreach (int[] entry in queue)
        {
            int  distance              = Mathf.Abs(entry[0] - originX) + Mathf.Abs(entry[1] - originZ);
            bool satisfiesMinDistance  = distance >= minDistance;
            bool satisfiesHeightOffset = !heightAugmented;
            if (heightAugmented)
            {
                Cursor destination  = GetTile(entry[0], entry[1]);
                int    distanceDiff = distance - maxHops;
                int    heightDiff   = origin.yPos - destination.yPos;
                satisfiesHeightOffset = distanceDiff <= heightDiff;
            }

            if (satisfiesMinDistance && satisfiesHeightOffset)
            {
                cursors.Add(GetTile(entry[0], entry[1]));
            }
        }

        return(cursors.ToList());
    }
예제 #5
0
    public static List <int[]> DeriveShortestPath(int xPos, int zPos, int originX, int originZ, Unit requestor)
    {
        List <int[]> queue        = new List <int[]>();
        List <int[]> shortestPath = new List <int[]>();

        queue.Add(new int[] { xPos, zPos, 0, VoxelController.GetElevation(xPos, zPos) });
        for (int i = 0; i < queue.Count; i++)
        {
            int[] entry   = queue[i];
            int   counter = entry[2] + 1;

            List <Cursor> neighbors = Helpers.Neighbors(entry[0], entry[1]);

            List <int[]> newCells = new List <int[]>();

            foreach (Cursor cursor in neighbors)
            {
                if (!cursor.standingUnit || cursor.standingUnit == requestor)
                {
                    int  elevation = VoxelController.GetElevation(cursor.xPos, cursor.zPos);
                    bool movable   = elevation >= 0 && Helpers.CanMoveToNextTile(cursor, Helpers.GetTile(entry[0], entry[1]), requestor.JumpHeight());

                    if (movable)
                    {
                        newCells.Add(new int[] { cursor.xPos, cursor.zPos, counter, elevation });
                    }
                }
            }

            bool reachedDestination = false;

            for (int a = 0; a < newCells.Count; a++)
            {
                if (newCells[a][0] == originX && newCells[a][1] == originZ)
                {
                    reachedDestination = true;
                    break;
                }
            }

            for (int a = newCells.Count - 1; a >= 0; a--)
            {
                for (int g = 0; g < queue.Count; g++)
                {
                    if (newCells[a][0] == queue[g][0] &&
                        newCells[a][1] == queue[g][1] &&
                        newCells[a][2] >= queue[g][2])
                    {
                        newCells.RemoveAt(a);
                        break;
                    }
                }
            }

            for (int a = 0; a < newCells.Count; a++)
            {
                queue.Add(newCells[a]);
            }

            if (reachedDestination)
            {
                queue.Reverse();
                int firstIndex = queue.FindIndex(r => (r[0] == originX && r[1] == originZ));
                shortestPath.Add(queue[firstIndex]);

                int[] previousElement = queue[firstIndex];

                for (int b = firstIndex; b < queue.Count; b++)
                {
                    int[] currentElement = queue[b];

                    if (
                        (
                            (
                                currentElement[0] == previousElement[0] - 1 &&
                                currentElement[1] == previousElement[1]
                            ) ||
                            (
                                currentElement[0] == previousElement[0] + 1 &&
                                currentElement[1] == previousElement[1]
                            ) ||
                            (
                                currentElement[0] == previousElement[0] &&
                                currentElement[1] == previousElement[1] + 1
                            ) ||
                            (
                                currentElement[0] == previousElement[0] &&
                                currentElement[1] == previousElement[1] - 1
                            )
                        ) && currentElement[2] == previousElement[2] - 1
                        )
                    {
                        shortestPath.Add(currentElement);
                        previousElement = currentElement;
                    }
                }

                break;
            }
        }

        return(shortestPath);
    }