Refactor cells for dual tile rendering

This commit is contained in:
2026-05-08 22:05:02 +02:00
parent 9c7d661e8c
commit 40038302de
9 changed files with 225 additions and 76 deletions

View File

@@ -25,6 +25,11 @@ public sealed partial class MainWindow
{
return new(OriginX + x * CellSize, OriginY + y * CellSize, CellSize, CellSize);
}
public Rect DualTileRect(int x, int y)
{
return new(OriginX + (x - 0.5) * CellSize, OriginY + (y - 0.5) * CellSize, CellSize, CellSize);
}
}
public MainWindow()
@@ -157,12 +162,20 @@ public sealed partial class MainWindow
var layout = GetLayout();
drawing.Clear(ColorHelper.FromArgb(255, 16, 18, 21));
DrawCells(drawing, layout);
DrawTerrain(drawing, layout);
DrawCellOverlays(drawing, layout);
DrawGrid(drawing, layout);
DrawRobot(drawing, layout);
}
private void DrawCells(CanvasDrawingSession drawing, CanvasLayout layout)
private void DrawTerrain(CanvasDrawingSession drawing, CanvasLayout layout)
{
for (var y = 0; y <= m_Level.Height; y++)
for (var x = 0; x <= m_Level.Width; x++)
DrawDualTerrainTile(drawing, layout.DualTileRect(x, y), GetDualTileMask(x, y));
}
private void DrawCellOverlays(CanvasDrawingSession drawing, CanvasLayout layout)
{
for (var y = 0; y < m_Level.Height; y++)
for (var x = 0; x < m_Level.Width; x++)
@@ -171,8 +184,6 @@ public sealed partial class MainWindow
var cell = m_Level.GetCell(position);
var rect = layout.CellRect(x, y);
drawing.FillRectangle(rect, CellColor(cell));
if (cell.HasPipe)
{
var center = new Vector2((float)(rect.X + rect.Width / 2), (float)(rect.Y + rect.Height / 2));
@@ -195,31 +206,93 @@ public sealed partial class MainWindow
if (m_SelectedCell == position)
drawing.DrawRectangle(rect, Colors.White, 3);
DrawCellGlyph(drawing, cell, rect);
DrawCellProp(drawing, cell, rect);
}
}
private void DrawCellGlyph(CanvasDrawingSession drawing, CellState cell, Rect rect)
private void DrawDualTerrainTile(CanvasDrawingSession drawing, Rect rect, int floorMask)
{
var text = cell.Kind switch {
ECellKind.Reactor => "R",
ECellKind.CoolingPump => "C",
ECellKind.Generator => "G",
ECellKind.PressureRegulator => "P",
ECellKind.DiagnosticTerminal => "D",
ECellKind.ControlTerminal => "T",
var wallColor = ColorHelper.FromArgb(255, 54, 61, 68);
var floorColor = ColorHelper.FromArgb(255, 31, 36, 40);
drawing.FillRectangle(rect, wallColor);
if (floorMask == 0)
return;
if (floorMask == c_AllFloorCorners)
{
drawing.FillRectangle(rect, floorColor);
return;
}
var halfWidth = rect.Width / 2;
var halfHeight = rect.Height / 2;
if ((floorMask & c_TopLeftFloor) != 0)
drawing.FillRectangle(new(rect.X, rect.Y, halfWidth, halfHeight), floorColor);
if ((floorMask & c_TopRightFloor) != 0)
drawing.FillRectangle(new(rect.X + halfWidth, rect.Y, halfWidth, halfHeight), floorColor);
if ((floorMask & c_BottomLeftFloor) != 0)
drawing.FillRectangle(new(rect.X, rect.Y + halfHeight, halfWidth, halfHeight), floorColor);
if ((floorMask & c_BottomRightFloor) != 0)
drawing.FillRectangle(new(rect.X + halfWidth, rect.Y + halfHeight, halfWidth, halfHeight), floorColor);
var center = new Vector2((float)(rect.X + halfWidth), (float)(rect.Y + halfHeight));
if (floorMask is c_TopLeftFloor or c_TopRightFloor or c_BottomLeftFloor or c_BottomRightFloor)
drawing.FillCircle(center, (float)Math.Min(rect.Width, rect.Height) * 0.18f, floorColor);
}
private int GetDualTileMask(int x, int y)
{
var mask = 0;
if (GetTerrainOrWall(x - 1, y - 1) == ECellTerrain.Floor)
mask |= c_TopLeftFloor;
if (GetTerrainOrWall(x, y - 1) == ECellTerrain.Floor)
mask |= c_TopRightFloor;
if (GetTerrainOrWall(x - 1, y) == ECellTerrain.Floor)
mask |= c_BottomLeftFloor;
if (GetTerrainOrWall(x, y) == ECellTerrain.Floor)
mask |= c_BottomRightFloor;
return mask;
}
private ECellTerrain GetTerrainOrWall(int x, int y)
{
var position = new GridPosition(x, y);
return m_Level.InBounds(position) ? m_Level.GetCell(position).Terrain : ECellTerrain.Wall;
}
private void DrawCellProp(CanvasDrawingSession drawing, CellState cell, Rect rect)
{
var text = cell.Prop switch {
ECellProp.Reactor => "R",
ECellProp.CoolingPump => "C",
ECellProp.Generator => "G",
ECellProp.PressureRegulator => "P",
ECellProp.DiagnosticTerminal => "D",
ECellProp.ControlTerminal => "T",
_ => string.Empty
};
if (string.IsNullOrEmpty(text))
return;
var propRect = new Rect(rect.X + rect.Width * 0.18, rect.Y + rect.Height * 0.18, rect.Width * 0.64, rect.Height * 0.64);
drawing.FillRoundedRectangle(propRect, 4, 4, PropColor(cell.Prop));
drawing.DrawRoundedRectangle(propRect, 4, 4, ColorHelper.FromArgb(210, 12, 14, 16), 2);
using var format = new CanvasTextFormat();
format.FontSize = Math.Max(14, (float)rect.Width * 0.42f);
format.FontSize = Math.Max(14, (float)rect.Width * 0.34f);
format.HorizontalAlignment = CanvasHorizontalAlignment.Center;
format.VerticalAlignment = CanvasVerticalAlignment.Center;
drawing.DrawText(text, rect, Colors.White, format);
drawing.DrawText(text, propRect, Colors.White, format);
}
private void DrawGrid(CanvasDrawingSession drawing, CanvasLayout layout)
@@ -265,22 +338,16 @@ public sealed partial class MainWindow
return new(size, originX, originY);
}
private static Color CellColor(CellState cell)
private static Color PropColor(ECellProp prop)
{
if (cell.Kind == ECellKind.Wall)
return ColorHelper.FromArgb(255, 54, 61, 68);
if (cell.Hazards.Fire)
return ColorHelper.FromArgb(255, 91, 39, 30);
return cell.Kind switch {
ECellKind.Reactor => ColorHelper.FromArgb(255, 61, 76, 82),
ECellKind.CoolingPump => ColorHelper.FromArgb(255, 25, 79, 96),
ECellKind.Generator => ColorHelper.FromArgb(255, 86, 75, 35),
ECellKind.PressureRegulator => ColorHelper.FromArgb(255, 70, 78, 98),
ECellKind.DiagnosticTerminal => ColorHelper.FromArgb(255, 39, 84, 62),
ECellKind.ControlTerminal => ColorHelper.FromArgb(255, 80, 61, 91),
_ => ColorHelper.FromArgb(255, 31, 36, 40)
return prop switch {
ECellProp.Reactor => ColorHelper.FromArgb(255, 61, 76, 82),
ECellProp.CoolingPump => ColorHelper.FromArgb(255, 25, 79, 96),
ECellProp.Generator => ColorHelper.FromArgb(255, 86, 75, 35),
ECellProp.PressureRegulator => ColorHelper.FromArgb(255, 70, 78, 98),
ECellProp.DiagnosticTerminal => ColorHelper.FromArgb(255, 39, 84, 62),
ECellProp.ControlTerminal => ColorHelper.FromArgb(255, 80, 61, 91),
_ => Colors.Transparent
};
}
@@ -294,7 +361,7 @@ public sealed partial class MainWindow
if (m_SelectedCell is { } position && m_Level.InBounds(position))
{
var cell = m_Level.GetCell(position);
CellText.Text = $"Position: {position.X},{position.Y}\n" + $"Kind: {cell.Kind}\n" + $"Pipe: {cell.Pipe}\n" + $"Flow: {cell.Flow}, Pressure: {cell.Pressure}\n" + $"Integrity: {cell.Integrity}, Leak: {cell.LeakRate}\n" + $"Heat: {cell.Hazards.Heat}, Smoke: {cell.Hazards.Smoke}\n" + $"Fuel Vapor: {cell.Hazards.FuelVapor}, Fuel: {cell.Hazards.LiquidFuel}\n" + $"Coolant: {cell.Hazards.CoolantPooling}, Charge: {cell.Hazards.ElectricalCharge}";
CellText.Text = $"Position: {position.X},{position.Y}\n" + $"Terrain: {cell.Terrain}\n" + $"Prop: {cell.Prop}\n" + $"Pipe: {cell.Pipe}\n" + $"Flow: {cell.Flow}, Pressure: {cell.Pressure}\n" + $"Integrity: {cell.Integrity}, Leak: {cell.LeakRate}\n" + $"Heat: {cell.Hazards.Heat}, Smoke: {cell.Hazards.Smoke}\n" + $"Fuel Vapor: {cell.Hazards.FuelVapor}, Fuel: {cell.Hazards.LiquidFuel}\n" + $"Coolant: {cell.Hazards.CoolantPooling}, Charge: {cell.Hazards.ElectricalCharge}";
}
else
CellText.Text = "No cell selected.";
@@ -306,7 +373,7 @@ public sealed partial class MainWindow
{
var level = LevelState.Create("Cooling Sector B", 16, 12);
level = level.SetCell(new(3, 5), new() {
Kind = ECellKind.CoolingPump,
Prop = ECellProp.CoolingPump,
Pipe = EPipeMedium.Coolant,
Flow = 5,
Pressure = 5,
@@ -330,31 +397,36 @@ public sealed partial class MainWindow
Pressure = 7
});
level = level.SetCell(new(8, 5), new() {
Kind = ECellKind.Reactor,
Prop = ECellProp.Reactor,
Hazards = new() {
Heat = 6,
Stability = 8
}
});
level = level.SetCell(new(2, 8), new() {
Kind = ECellKind.Generator,
Prop = ECellProp.Generator,
Pipe = EPipeMedium.Fuel,
Flow = 4,
Pressure = 6,
Powered = true
});
level = level.SetCell(new(11, 4), new() {
Kind = ECellKind.DiagnosticTerminal,
Prop = ECellProp.DiagnosticTerminal,
Powered = true
});
level = level.SetCell(new(12, 8), new() {
Kind = ECellKind.ControlTerminal,
Prop = ECellProp.ControlTerminal,
Powered = true
});
return level with { Forecasts = new SimulationEngine().Forecast(level) };
}
private readonly SimulationEngine m_Simulation = new();
private const int c_TopLeftFloor = 1;
private const int c_TopRightFloor = 2;
private const int c_BottomLeftFloor = 4;
private const int c_BottomRightFloor = 8;
private const int c_AllFloorCorners = c_TopLeftFloor | c_TopRightFloor | c_BottomLeftFloor | c_BottomRightFloor;
private StorageFile? m_CurrentFile;
private LevelState m_Level;
private bool m_Painting;