Esempio n. 1
0
    public void SetBlock(int index, int blockID)
    {
        int paletteIndex = data.Get(index, indicesLength /*, true*/);

        if (paletteIndex < 0 || paletteIndex > palette.Length - 1)
        {
            Debug.LogError("paletteIndex: " + paletteIndex);
            return;
        }
        PaletteEntry current = palette[paletteIndex];

        // Whatever block is there will cease to exist in but a moment...
        current.refcount -= 1;

        // The following steps/choices *must* be ordered like they are.

        // --- Is the block-type already in the palette?
        int currentPaletteIndex = System.Array.FindIndex <PaletteEntry>(palette, entry => { return(entry.blockID.Equals(blockID)); });

        if (currentPaletteIndex != -1)
        {
            //Debug.LogError("Already in palette: " + type.ToString());
            // YES: Use the existing palette entry.
            data.Set(index, indicesLength, currentPaletteIndex);
            palette[currentPaletteIndex].refcount += 1;
            return;
        }

        // --- Can we overwrite the current palette entry?
        if (current.refcount == 0)
        {
            Debug.Log("Overwrite current palette entry");
            // YES, we can!
            current.blockID  = blockID;
            current.refcount = 1;
            return;
        }

        Debug.Log("Add new Palette:" + blockID.ToString());
        // --- A new palette entry is needed!

        // Get the first free palette entry, possibly growing the palette!
        int newEntry = NewPaletteEntry();

        palette[newEntry] = new PaletteEntry {
            refcount = 1, blockID = blockID
        };
        data.Set(index, indicesLength, newEntry /*, true*/);
        paletteCount += 1;
    }