Skip to content

Instantly share code, notes, and snippets.

@egocarib
Last active December 26, 2019 07:52
Show Gist options
  • Save egocarib/b07dee6d5a6b38eed2888425a65ebc51 to your computer and use it in GitHub Desktop.
Save egocarib/b07dee6d5a6b38eed2888425a65ebc51 to your computer and use it in GitHub Desktop.
ScreenBuffer scrapBuffer = ScreenBuffer.GetScrapBuffer2(true); //copy the scrapbuffer
TileMaker tileMaker = new TileMaker("Great Magma Crab"); //you can pass a GameObject or a blueprint string to the TileMaker
tileMaker.WriteTileToBuffer(scrapBuffer, 10, 20); //enter x,y coordinates where you want to draw the tile on screen
Popup._TextConsole.DrawBuffer(scrapBuffer, null, false); //draw the scrapbuffer
using XRL.UI;
using XRL.Core;
using XRL.World;
using XRL.World.Parts;
using ConsoleLib.Console;
using GameObject = XRL.World.GameObject;
public class TileMaker
{
public string Tile;
public char DetailColorChar;
public char ForegroundColorChar;
public char BackgroundColorChar;
public UnityEngine.Color DetailColor;
public UnityEngine.Color ForegroundColor;
public UnityEngine.Color BackgroundColor;
public TileMaker(string blueprintString)
{
GameObject go = null;
if (GameObjectFactory.Factory.Blueprints.ContainsKey(blueprintString))
{
go = GameObjectFactory.Factory.CreateObject(blueprintString);
}
this.Initialize(go, false);
if (go != null)
{
go.Destroy();
}
}
public TileMaker(GameObject go)
{
this.Initialize(go);
}
public bool WriteTileToBuffer(ScreenBuffer scrapBuffer, int x, int y)
{
if (scrapBuffer == null || x < 0 || x >= scrapBuffer.Width || y < 0 || y >= scrapBuffer.Height)
{
return false;
}
scrapBuffer[x, y].SetBackground(this.BackgroundColorChar);
scrapBuffer[x, y].SetForeground(this.ForegroundColorChar);
scrapBuffer[x, y].TileLayerBackground[0] = this.DetailColor;
scrapBuffer[x, y].TileLayerForeground[0] = this.ForegroundColor;
scrapBuffer[x, y].Tile = this.Tile;
return true;
}
private void Initialize(GameObject go, bool renderOK = true)
{
this.Tile = string.Empty;
this.DetailColorChar = 'k';
this.ForegroundColorChar = 'y';
this.BackgroundColorChar = 'k';
this.DetailColor = ConsoleLib.Console.ColorUtility.ColorMap['k'];
this.ForegroundColor = ConsoleLib.Console.ColorUtility.ColorMap['y'];
this.BackgroundColor = ConsoleLib.Console.ColorUtility.ColorMap['k'];
//gather render data for GameObject similar to how the game does it in Cell.cs
Render pRender = go?.pRender;
if (pRender == null || pRender.Tile == null || !pRender.Visible || Globals.RenderMode != RenderModeType.Tiles)
{
return;
}
RenderEvent renderData = new RenderEvent();
Examiner examinerPart = go.GetPart<Examiner>();
if (examinerPart != null && !string.IsNullOrEmpty(examinerPart.UnknownTile) && !go.Understood())
{
renderData.Tile = examinerPart.UnknownTile;
}
else
{
renderData.Tile = go.pRender.Tile;
}
if (!string.IsNullOrEmpty(pRender.TileColor))
{
renderData.ColorString = pRender.TileColor;
}
else
{
renderData.ColorString = pRender.ColorString;
}
if (renderOK) //we can't render blueprint-created objects, because the game will throw errors trying to check their current cell
{
go.Render(renderData);
}
//renderData.Tile can be null if something has a temporary character replacement, like the up arrow from flying
this.Tile = !string.IsNullOrEmpty(renderData.Tile) ? renderData.Tile : pRender.Tile;
//save render data in our custom TileColorData format, using logic similar to QudItemListElement.InitFrom()
if (!string.IsNullOrEmpty(pRender.DetailColor))
{
this.DetailColor = ConsoleLib.Console.ColorUtility.ColorMap[pRender.DetailColor[0]];
this.DetailColorChar = pRender.DetailColor[0];
}
if (!string.IsNullOrEmpty(pRender.TileColor))
{
for (int i = 0; i < pRender.TileColor.Length; i++)
{
if (pRender.TileColor[i] == '&' && i < pRender.TileColor.Length - 1)
{
if (pRender.TileColor[i + 1] == '&')
{
i++;
}
else
{
this.ForegroundColor = ConsoleLib.Console.ColorUtility.ColorMap[pRender.TileColor[i + 1]];
this.ForegroundColorChar = pRender.TileColor[i + 1];
}
}
if (pRender.TileColor[i] == '^' && i < pRender.TileColor.Length - 1)
{
if (pRender.TileColor[i + 1] == '^')
{
i++;
}
else
{
this.BackgroundColor = ConsoleLib.Console.ColorUtility.ColorMap[pRender.TileColor[i + 1]];
this.BackgroundColorChar = pRender.TileColor[i + 1];
}
}
}
}
else if (!string.IsNullOrEmpty(pRender.ColorString))
{
for (int j = 0; j < pRender.ColorString.Length; j++)
{
if (pRender.ColorString[j] == '&' && j < pRender.ColorString.Length - 1)
{
if (pRender.ColorString[j + 1] == '&')
{
j++;
}
else
{
this.ForegroundColor = ConsoleLib.Console.ColorUtility.ColorMap[pRender.ColorString[j + 1]];
this.ForegroundColorChar = pRender.ColorString[j + 1];
}
}
if (pRender.ColorString[j] == '^' && j < pRender.ColorString.Length - 1)
{
if (pRender.ColorString[j + 1] == '^')
{
j++;
}
else
{
this.BackgroundColor = ConsoleLib.Console.ColorUtility.ColorMap[pRender.ColorString[j + 1]];
this.BackgroundColorChar = pRender.ColorString[j + 1];
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment