//Adds a parent to the dictionary of parents if the parent isn't a key yet.
    //if the temperature or maxDepth parameters have changed from what was noted
    //the temperature effect on this tile is recalculated and the temperature of this tile is changed
    public void ModifyLocalTemp(HexTemperature relevantParent, int depth, int maxDepth, float temperature)
    {
        if (!tempParents.ContainsKey(relevantParent)) //A new parent is added to this tile
        {
            float newTempMod = HeatCalculation(depth, maxDepth, temperature);
            tempParents.Add(relevantParent, new DepthTemp(depth, maxDepth, temperature, newTempMod));
            myTemperature += HeatCalculation(depth, maxDepth, temperature);
        }
        else if (depth > maxDepth) //The tile falls outside the zone of tiles it can be in
        {
            myTemperature -= tempParents[relevantParent].tempMod;
            tempParents.Remove(relevantParent);
        }
        else if (maxDepth != tempParents[relevantParent].maxDepth || temperature != tempParents[relevantParent].parentTemp)
        {
            //an existing tile is altered
            float newTempMod = HeatCalculation(depth, maxDepth, temperature);
            myTemperature = (myTemperature + newTempMod) - tempParents[relevantParent].tempMod;
            tempParents[relevantParent] = new DepthTemp(depth, maxDepth, temperature, newTempMod);
        }

        //change the colour of the tile to comply with the temperature
        float tempShift = myTemperature / 40;
        Color tempColor = Color.Lerp(Color.green, Color.red, tempShift);

        renderer.material.color = tempColor;
    }
Exemple #2
0
    //A list containing every value of the HexagonDirection Enum for easy iteration

    //Returns all tiles which can be reached from the start tile with the range of maxDepth
    public HashSet <HexTemperature> HeatArea(HexTemperature start, float temperature, int maxDepth, HashSet <HexTemperature> existing = null, List <HexTemperature> border = null)
    {
        open         = new List <HexTemperature>();
        checkedNodes = new HashSet <HexTemperature>();
        HashSet <HexTemperature> output = new HashSet <HexTemperature>();

        if (border == null)
        {
            open.Add(start);
            checkedNodes.Add(start);
            output.Add(start);
            start.ModifyLocalTemp(start, 0, maxDepth, temperature);
        }
        else
        {
            open         = border;
            checkedNodes = existing;
        }

        int currentDepth = 0;

        //Take the current node, find its neighbours and update their temperature
        for (int x = 0; x < open.Count; x++)
        {
            List <HexagonTile> neighbours = new List <HexagonTile>();
            neighbours   = Controller.GetAllNeighbours(open[x].MyHex);
            currentDepth = 1 + open[x].tempParents[start].myDepth;

            //add the 6 neighbours of the node that's currently being checked.
            foreach (HexagonTile futureTile in neighbours)
            {
                if (futureTile == null || checkedNodes.Contains(futureTile.MyTemp))
                {
                    continue;
                }
                else
                {
                    HexTemperature futureTemp = futureTile.MyTemp;
                    if (currentDepth + 1 <= maxDepth && !futureTile.blocked)
                    {
                        open.Add(futureTemp);
                    }

                    output.Add(futureTemp);
                    futureTemp.ModifyLocalTemp(start, currentDepth, maxDepth, temperature);
                    checkedNodes.Add(futureTemp);
                }
            }
        }
        return(output);
    }
Exemple #3
0
    private void Start()
    {
        // http://answers.unity3d.com/questions/421509/2d-hexagonal-grid-beginner.html
        // Get the offsets.
        offsetX = Radius * 1.5f;
        offsetZ = Radius * Mathf.Sqrt(3);

        MyTemp       = gameObject.GetComponent <HexTemperature>();
        MyTemp.MyHex = this;

        blocked = false;
        visible = true;
        UpdatePosition();
    }
    public GameObject CreateTile(Point p)
    {
        // Create the gameobject.
        GameObject newTile = Instantiate(tilePrefab, tilesContainer.transform);

        newTile.name = p.ToString();

        // Set the tile position for the script.
        HexagonTile tile = newTile.GetComponent <HexagonTile>();

        tile.TileX = p.X;
        tile.TileZ = p.Y;

        HexTemperature hexTemp = newTile.GetComponent <HexTemperature>();

        hexTemp.Pathing = HeatSearch;
        hexTemp.Field   = this;
        hexTemp.MyHex   = tile;

        tile.MyTemp = hexTemp;

        tiles[p] = newTile;
        return(newTile);
    }