Ejemplo n.º 1
0
    public List <Tile> TilesInRange(Tile center, int range)
    {
        //Return tiles rnage steps from center, http://www.redblobgames.com/grids/hexagons/#range
        List <Tile> ret = new List <Tile>();

        if (center == null)
        {
            return(ret);
        }
        CubeIndex o;

        for (int dx = -range; dx <= range; dx++)
        {
            for (int dy = Mathf.Max(-range, -dx - range); dy <= Mathf.Min(range, -dx + range); dy++)
            {
                o = new CubeIndex(dx, dy, -dx - dy) + center.Index;
                if (grid.ContainsKey(o.ToString()))
                {
                    ret.Add(grid[o.ToString()]);
                }
            }
        }

        return(ret);
    }
Ejemplo n.º 2
0
    //distance between peek of ground and lowest cube in the same column, return stop position(in row)
    int findDistance(int col, int groundPeek)
    {
        CubeIndex index = getLowestCubeIndexInRow(col);
        int       row   = anchor.row + index.row;

        return(groundPeek + 1 - index.row);
    }
Ejemplo n.º 3
0
    private void StartThreadLightAreaCalculation(CubeIndex start, int lightRange)
    {
        Thread myThread = new Thread(() => CalculateNewLightArea(start, lightRange));

        myThread.Start();
        Debug.Log("Started new Thread to calculate light area");
    }
Ejemplo n.º 4
0
    public HashSet <CubeIndex> TilesInMoveRange(CubeIndex startIndex, int range)
    {
        // Set of hexes
        HashSet <CubeIndex> visited = new HashSet <CubeIndex>();

        // Add start to visited
        visited.Add(startIndex);
        // Array of arrays of hexes
        var fringes = new List <List <CubeIndex> >();

        fringes.Add(new List <CubeIndex>()
        {
            startIndex
        });

        for (int i = 1; i <= range; i++)
        {
            fringes.Add(new List <CubeIndex>());
            foreach (var index in fringes[i - 1])
            {
                foreach (var neighbour in Neighbours(index))
                {
                    if (neighbour.Passable)                     // && neighbour.visibility != Tile.Visibility.Hidden
                    {
                        visited.Add(neighbour.index);
                        fringes[i].Add(neighbour.index);
                    }
                }
            }
        }

        return(visited);
    }
Ejemplo n.º 5
0
    private void CalculateNewLightArea(CubeIndex index, int lightRange)
    {
        // First iterate through current light area
        foreach (var tile in lightArea)
        {
            // And remove one from the tiles light counter
            _lightManager.litTiles[tile]--;
        }
        // Calculate the new indiviual light area at the new position
        lightArea = _grid.TilesInRange(index, lightRange);
        foreach (var tile in lightArea)
        {
            // If light manager include this tile yet
            if (!_lightManager.litTiles.ContainsKey(tile))
            {
                // Add it with one light source
                _lightManager.litTiles[tile] = 1;
            }
            // If it already exists in the lit list
            else
            {
                // Add one to the light source counter
                _lightManager.litTiles[tile]++;
            }
        }
        LightRaySent = true;
        EventHandler.current.LightSourcesUpdated();

        Debug.Log("Calculated new Light Area with range: " + lightRange);
    }
Ejemplo n.º 6
0
 public void CalculateNewMoveArea(CubeIndex index, int moveRange)
 {
     moveArea           = _grid.TilesInMoveRange(index, moveRange);
     MoveAreaCalculated = true; // Triggers event to update highlghted area
     areaCalcStarted    = false;
     Debug.Log("Calculated new Move Area with range: " + moveRange);
 }
Ejemplo n.º 7
0
 public void ChangeTileDestination(CubeIndex index)
 {
     if (onTileDestinationChanged != null)
     {
         onTileDestinationChanged(index);
     }
 }
Ejemplo n.º 8
0
 public void ChangeVisibility(CubeIndex index, Tile.SelectionStatus visibility)
 {
     if (onVisibilityChanged != null)
     {
         onVisibilityChanged(index, visibility);
     }
 }
Ejemplo n.º 9
0
    public static OffsetIndex CubeToOddFlat(CubeIndex c)
    {
        OffsetIndex o;

        o.col = c.x;
        o.row = c.z + (c.x - (c.x & 1)) / 2;
        return(o);
    }
Ejemplo n.º 10
0
 public GameTile GameTileAt(CubeIndex index)
 {
     if (tilesDictionary.ContainsKey(index.ToString()))
     {
         return(tilesDictionary[index.ToString()]);
     }
     return(null);
 }
Ejemplo n.º 11
0
 public int IdOfGameTileAt(CubeIndex index)
 {
     if (tilesDictionary.ContainsKey(index.ToString()))
     {
         return(tilesDictionary[index.ToString()].id);
     }
     return(-1);
 }
Ejemplo n.º 12
0
        public static Tile operator +(Tile one, Tile two)
        {
            //Tile ret = new Tile();
            CubeIndex index = one.index + two.index;
            Tile      ret   = one.grid.TileAt(index);

            return(ret);
        }
Ejemplo n.º 13
0
 public Tile TileAt(CubeIndex index)
 {
     if (grid.ContainsKey(index.ToString()))
     {
         return(grid[index.ToString()]);
     }
     return(null);
 }
Ejemplo n.º 14
0
    public static float3 ToWorldPos(this CubeIndex index, float outerRadius)
    {
        float3 pos = new float3(0, 0, 0);

        pos.x = outerRadius * 3.0f / 2.0f * index.x;
        pos.z = outerRadius * Mathf.Sqrt(3.0f) * (index.y + index.x / 2.0f);
        return(pos);
    }
Ejemplo n.º 15
0
    public static OffsetIndex CubeToOddPointy(CubeIndex c)
    {
        OffsetIndex o;

        o.row = c.z;
        o.col = c.x + (c.z - (c.z & 1)) / 2;
        return(o);
    }
Ejemplo n.º 16
0
    void moveCol(int x)
    {
        CubeIndex newPos = new CubeIndex(anchor.row, anchor.col + x);

        if (inBlockBoard(newPos))
        {
            Anchor = newPos;
        }
    }
Ejemplo n.º 17
0
    public void StartThreadMoveAreaCalculation(CubeIndex start, int moveRange)
    {
        Thread myThread = new Thread(() => CalculateNewMoveArea(start, moveRange));

        myThread.Start();
        Debug.Log("Started new Thread to calculate move area");
        areaCalcStarted    = true;
        MoveAreaCalculated = false;
    }
Ejemplo n.º 18
0
    public static Tile TileAt(Dictionary <string, Tile> grid, int x, int y, int z)
    {
        CubeIndex index = new CubeIndex(x, y, z);

        if (grid.ContainsKey(index.ToString()))
        {
            return(grid[index.ToString()]);
        }
        return(null);
    }
Ejemplo n.º 19
0
    protected override void OnUpdate()
    {
        NativeQueue <TileData> nativeQueue = new NativeQueue <TileData>(Allocator.TempJob);

        CopyTileJob copyTileJob = new CopyTileJob {
            nativeQueue = nativeQueue.ToConcurrent()
        };
        JobHandle jobHandle = copyTileJob.Schedule(this);

        jobHandle.Complete();

        NativeArray <TileData> nativeArray = new NativeArray <TileData>(nativeQueue.Count, Allocator.TempJob);

        NativeQueueToArrayJob nativeQueueToArrayJob = new NativeQueueToArrayJob {
            nativeQueue = nativeQueue,
            nativeArray = nativeArray,
        };

        jobHandle = nativeQueueToArrayJob.Schedule();
        jobHandle.Complete();

        nativeQueue.Dispose();

        var camera = Camera.main;

        //if (camera != null && Input.GetMouseButtonDown(0))
        {
            var mousePosition = Input.mousePosition;
            var cameraRay     = camera.ScreenPointToRay(mousePosition);

            Entities.WithAll <HexTileHightlightComponent>().ForEach((Entity entity) => {
                PostUpdateCommands.RemoveComponent <HexTileHightlightComponent>(entity);
            });
            var deltaTime = Time.deltaTime;
            if (UnityEngine.Physics.Raycast(cameraRay, out var closestHit, float.PositiveInfinity))
            {
                Vector3   mapPos = new Vector3(closestHit.point.x, 0, closestHit.point.z);
                CubeIndex index  = HexUtils.FromPosition(mapPos, Bootstrap.Defines.TileRadius);
                Debug.DrawLine(Vector3.zero, closestHit.point, Color.red, 2.5f);
                for (int i = 0; i < nativeArray.Length; i++)
                {
                    if (nativeArray[i].index.Equals(index))
                    {
                        PostUpdateCommands.AddComponent(nativeArray[i].entity, new HexTileHightlightComponent());
                        break;
                    }
                }
            }
        }

        nativeArray.Dispose();
    }
Ejemplo n.º 20
0
    void destroyCurrentBlockAndAddCubesToBlockBoard()
    {
        CubeIndex stopIndex = currentBlock.getStopIndex();

        foreach (var blockCube in currentBlock.getCubes())
        {
            blockCube.index.row       += stopIndex.row; blockCube.index.col += stopIndex.col;
            blockCube.transform.parent = transform;
            cubes.Add(blockCube);
        }
        currentBlock.transform.DetachChildren();
        Destroy(currentBlock.gameObject);
    }
Ejemplo n.º 21
0
    public override bool Equals(object obj)
    {
        if (obj == null)
        {
            return(false);
        }
        CubeIndex o = (CubeIndex)obj;

        if ((System.Object)o == null)
        {
            return(false);
        }
        return((x == o.x) && (y == o.y) && (z == o.z));
    }
Ejemplo n.º 22
0
    public CubeIndex getLowestCubeIndexInRow(int col)
    {
        int       lowest = Int32.MaxValue;
        CubeIndex result = null;

        foreach (var cube in cubes)
        {
            if (cube.index.col == col && lowest > cube.index.row)
            {
                lowest = cube.index.row;
                result = cube.index;
            }
        }
        return(result);
    }
Ejemplo n.º 23
0
    static List <Hex> GridBetweenPoints(Grid grid, Hex start, Hex end)
    {
        List <Hex> hexes = new List <Hex> ();

        CubeIndex startCoord = start.CubeIndex;
        CubeIndex endCoord   = end.CubeIndex;

        int startX = 0;
        int endX   = 0;

        if (startCoord.x < endCoord.x)
        {
            startX = startCoord.x;
            endX   = endCoord.x;
        }
        else
        {
            startX = endCoord.x;
            endX   = startCoord.x;
        }

        int startY = 0;
        int endY   = 0;

        if (startCoord.z < endCoord.z)
        {
            startY = startCoord.z;
            endY   = endCoord.z;
        }
        else
        {
            startY = endCoord.z;
            endY   = startCoord.z;
        }


        for (int i = startX; i < endX; i++)
        {
            for (int j = startY; j < endY; j++)
            {
                hexes.Add(grid.TileAt(new CubeIndex(i, j)).GetComponent <Hex> ());
            }
        }

        return(hexes);
    }
Ejemplo n.º 24
0
    public Tile Neighbour(Tile tile, HexDirection direction)
    {
        if (HexDirection.None == direction || tile == null)
        {
            return(null);
        }
        CubeIndex o = tile.Index + direction;

        if (grid.TryGetValue(o.ToString(), out var ret))
        {
            return(ret);
        }
        else
        {
            return(null);
        }
    }
Ejemplo n.º 25
0
    private void SendLightRays(int lightRange)
    {
        CubeIndex index = new CubeIndex(0, 0, 0);

        if (bonfire)
        {
            index = bonfire.currTile.index;
        }
        if (lightObject)
        {
            index = lightObject.currTile.index;
        }
        if (gameObject.layer == 20) // Item?
        {
            index = this.transform.parent.GetComponent <Tile>().index;
        }
        //StartThreadLightAreaCalculation(lightObject.currTile.index, lightRange);
        CalculateNewLightArea(index, lightRange);
    }
Ejemplo n.º 26
0
    //find highest stop position
    public void calcStopPosition()
    {
        List <int> cols = getListBlockCols();
        List <int> highestColsInBoard = blockBoard.getHighestCubeInCols(cols);

        int col         = 0;
        int highestStop = Int32.MinValue;

        foreach (var groundPeek in highestColsInBoard)
        {
            var stopRow = findDistance(col, groundPeek);
            if (highestStop < stopRow)
            {
                highestStop = stopRow;
            }
            col++;
        }
        stopIndex = new CubeIndex(highestStop, anchor.col);
    }
Ejemplo n.º 27
0
    public List <GameTile> GameTilesInRange(GameTile center, int range)
    {
        //Return tiles range steps from center, http://www.redblobgames.com/tilesDictionarys/hexagons/#range
        List <GameTile> ret = new List <GameTile>();
        CubeIndex       o;

        for (int dx = -range; dx <= range; dx++)
        {
            for (int dy = Mathf.Max(-range, -dx - range); dy <= Mathf.Min(range, -dx + range); dy++)
            {
                o = new CubeIndex(dx, dy, -dx - dy) + center.index;
                if (tilesDictionary.ContainsKey(o.ToString()))
                {
                    ret.Add(tilesDictionary[o.ToString()]);
                }
            }
        }
        return(ret);
    }
Ejemplo n.º 28
0
    public bool IsOceanTileBySettings(GameTile tile)
    {
        CubeIndex index           = tile.index;
        int       maxMapDimension = Mathf.Max(mapWidth, mapHeight);
        int       coordsSum       = Mathf.Abs(index.x) + Mathf.Abs(index.y) + Mathf.Abs(index.z);

        if (maxMapDimension <= oceanLayers)
        {
            oceanLayers = maxMapDimension - 1;
        }

        for (int i = 0; i < oceanLayers; i++)
        {
            if (coordsSum == (maxMapDimension - i) * 2)
            {
                return(true);
            }
        }

        return(false);
    }
Ejemplo n.º 29
0
    // Return a List of Locations representing the found path
    public List <CubeIndex> FindPath()
    {
        List <CubeIndex> path    = new List <CubeIndex>();
        CubeIndex        current = goal;

        // path.Add(current);

        while (!current.Equals(start))
        {
            if (!cameFrom.ContainsKey(current))
            {
                MonoBehaviour.print("cameFrom does not contain current.");
                return(new List <CubeIndex>());
            }
            path.Add(current);
            current = cameFrom[current];
        }
        // path.Add(start);
        path.Reverse();
        return(path);
    }
Ejemplo n.º 30
0
        public void MoveRows(int count)
        {
            Dictionary <Bubble, Tile> tmp = new Dictionary <Bubble, Tile>();
            CubeIndex down = new CubeIndex(-1 * count, 2 * count, -1 * count);

            //sort by row inverted
            foreach (var tile in Tiles)
            {
                var bubble  = Get(tile);
                var newTile = grid.TileAt(tile.Index + down);
                tmp.Add(bubble, newTile);
            }

            b2t.Clear();
            t2b.Clear();

            foreach (var bubble in tmp.Keys)
            {
                var tile = tmp[bubble];
                Attach(bubble, tile);
            }
        }
Ejemplo n.º 31
0
 public static OffsetIndex CubeToOddFlat(CubeIndex c)
 {
     OffsetIndex o;
     o.col = c.x;
     o.row = c.z + (c.x - (c.x&1)) / 2;
     return o;
 }
Ejemplo n.º 32
0
 public int Distance(CubeIndex a, CubeIndex b)
 {
     return Mathf.Abs(a.x - b.x) + Mathf.Abs(a.y - b.y) + Mathf.Abs(a.z - b.z);
 }
Ejemplo n.º 33
0
 public static OffsetIndex CubeToOddPointy(CubeIndex c)
 {
     OffsetIndex o;
     o.row = c.z;
     o.col = c.x + (c.z - (c.z&1)) / 2;
     return o;
 }
Ejemplo n.º 34
0
 public List<Tile> Neighbours(CubeIndex index)
 {
     return Neighbours(TileAt(index));
 }
Ejemplo n.º 35
0
 public Tile TileAt(CubeIndex index)
 {
     if(grid.ContainsKey(index.ToString()))
        return grid[index.ToString()];
     return null;
 }
Ejemplo n.º 36
0
    public List<Tile> TilesInRange(Tile center, int range)
    {
        //Return tiles rnage steps from center, http://www.redblobgames.com/grids/hexagons/#range
        List<Tile> ret = new List<Tile>();
        CubeIndex o;

        for(int dx = -range; dx <= range; dx++){
            for(int dy = Mathf.Max(-range, -dx-range); dy <= Mathf.Min(range, -dx+range); dy++){
                o = new CubeIndex(dx, dy, -dx-dy) + center.index;
                if(grid.ContainsKey(o.ToString()))
                    ret.Add(grid[o.ToString()]);
            }
        }
        return ret;
    }
Ejemplo n.º 37
0
 public List<Tile> TilesInRange(CubeIndex index, int range)
 {
     return TilesInRange(TileAt(index), range);
 }