public bool SetTile(int2 worldTilePosition, int id)
    {
        if (!TileUtil.BoundaryCheck(worldTilePosition, mapSize))
        {
            return(false);
        }

        int index = TileUtil.To1DIndex(worldTilePosition, mapSize);

        if (tiles[index] == id)
        {
            return(false);
        }

        int2 chunkPosition = TileUtil.WorldTileToChunk(worldTilePosition, chunkSize);

        if (chunks.TryGetValue(chunkPosition, out TileChunk chunk))
        {
            chunk.SetMeshDirty();
        }
        else
        {
            TileChunk newChunk = GenerateChunk(chunkPosition);
            newChunk.SetMeshDirty();
        }

        if (tileInformations[id].isSolid)
        {
            fluidQueue.Enqueue(new Tuple <int, float>(index, 0.0f));
        }

        LightEmission beforeEmission = tileInformations[tiles[index]].emission;

        tiles[index] = id;

        SetEmission(worldTilePosition, tileInformations[id].emission);
        CheckTileToUpdateLight(worldTilePosition, id, beforeEmission);

        return(true);
    }
 public void SetEmission(int2 worldTilePosition, LightEmission emission)
 {
     SetLight(worldTilePosition, emission.r, LightType.R);
     SetLight(worldTilePosition, emission.g, LightType.G);
     SetLight(worldTilePosition, emission.b, LightType.B);
 }
    void CheckTileToUpdateLight(int2 worldTilePosition, int id, LightEmission beforeEmission)
    {
        if (id == 0)
        {
            foreach (int2 direction in TileUtil.Direction4)
            {
                int2 neighborPosition = worldTilePosition + direction;

                if (!TileUtil.BoundaryCheck(neighborPosition, mapSize))
                {
                    continue;
                }

                for (int i = 0; i < 4; i++)
                {
                    LightType lightType     = (LightType)i;
                    int       neighborLight = GetLight(neighborPosition, lightType);

                    if (neighborLight <= 0)
                    {
                        continue;
                    }

                    switch (lightType)
                    {
                    case LightType.S:
                        sunLightPropagationQueue.Enqueue(neighborPosition);
                        break;

                    case LightType.R:
                        torchRedLightPropagationQueue.Enqueue(neighborPosition);
                        break;

                    case LightType.G:
                        torchGreenLightPropagationQueue.Enqueue(neighborPosition);
                        break;

                    case LightType.B:
                        torchBlueLightPropagationQueue.Enqueue(neighborPosition);
                        break;
                    }
                }
            }
        }
        else
        {
            int sunLight = GetLight(worldTilePosition, LightType.S);
            SetLight(worldTilePosition, 0, LightType.S);
            sunLightRemovalQueue.Enqueue(new Tuple <int2, int>(worldTilePosition, sunLight));

            foreach (int2 direction in TileUtil.Direction4)
            {
                int2 neighborPosition = worldTilePosition + direction;

                if (!TileUtil.BoundaryCheck(neighborPosition, mapSize))
                {
                    continue;
                }

                for (int i = 1; i < 4; i++)
                {
                    LightType lightType     = (LightType)i;
                    int       neighborLight = GetLight(neighborPosition, lightType);

                    if (neighborLight <= 0)
                    {
                        continue;
                    }

                    switch (lightType)
                    {
                    case LightType.R:
                        torchRedLightRemovalQueue.Enqueue(new Tuple <int2, int>(worldTilePosition, neighborLight));
                        break;

                    case LightType.G:
                        torchGreenLightRemovalQueue.Enqueue(new Tuple <int2, int>(worldTilePosition, neighborLight));
                        break;

                    case LightType.B:
                        torchBlueLightRemovalQueue.Enqueue(new Tuple <int2, int>(worldTilePosition, neighborLight));
                        break;
                    }
                }
            }
        }

        if (tileInformations[id].emission.r > 0)
        {
            torchRedLightPropagationQueue.Enqueue(worldTilePosition);
        }

        if (tileInformations[id].emission.g > 0)
        {
            torchGreenLightPropagationQueue.Enqueue(worldTilePosition);
        }

        if (tileInformations[id].emission.b > 0)
        {
            torchBlueLightPropagationQueue.Enqueue(worldTilePosition);
        }

        if (beforeEmission.r > 0)
        {
            torchRedLightRemovalQueue.Enqueue(new Tuple <int2, int>(worldTilePosition, beforeEmission.r));
        }

        if (beforeEmission.g > 0)
        {
            torchGreenLightRemovalQueue.Enqueue(new Tuple <int2, int>(worldTilePosition, beforeEmission.g));
        }

        if (beforeEmission.b > 0)
        {
            torchBlueLightRemovalQueue.Enqueue(new Tuple <int2, int>(worldTilePosition, beforeEmission.b));
        }
    }