cleanup code
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
using System.Numerics;
|
||||
using Windows.Foundation;
|
||||
using Windows.Storage;
|
||||
using Windows.Storage.Pickers;
|
||||
using Windows.UI;
|
||||
using Microsoft.Graphics.Canvas;
|
||||
using Microsoft.Graphics.Canvas.Text;
|
||||
using Microsoft.Graphics.Canvas.UI.Xaml;
|
||||
@@ -7,21 +11,19 @@ using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Input;
|
||||
using ReactorMaintenance.Simulation;
|
||||
using Windows.Foundation;
|
||||
using Windows.Storage;
|
||||
using Windows.Storage.Pickers;
|
||||
using WinRT.Interop;
|
||||
|
||||
namespace ReactorMaintenance.Win2D;
|
||||
|
||||
public sealed partial class MainWindow : Window
|
||||
{
|
||||
private readonly SimulationEngine _simulation = new();
|
||||
private LevelState _level;
|
||||
private EditorTool _selectedTool = EditorTool.Floor;
|
||||
private GridPosition? _selectedCell;
|
||||
private StorageFile? _currentFile;
|
||||
private bool _painting;
|
||||
private sealed record CanvasLayout(double CellSize, double OriginX, double OriginY)
|
||||
{
|
||||
public Rect CellRect(int x, int y)
|
||||
{
|
||||
return new(OriginX + x * CellSize, OriginY + y * CellSize, CellSize, CellSize);
|
||||
}
|
||||
}
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
@@ -36,9 +38,7 @@ public sealed partial class MainWindow : Window
|
||||
private void ToolPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
if (ToolPicker.SelectedItem is EditorTool tool)
|
||||
{
|
||||
_selectedTool = tool;
|
||||
}
|
||||
}
|
||||
|
||||
private void New_Click(object sender, RoutedEventArgs e)
|
||||
@@ -58,9 +58,7 @@ public sealed partial class MainWindow : Window
|
||||
|
||||
var file = await picker.PickSingleFileAsync();
|
||||
if (file is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var json = await FileIO.ReadTextAsync(file);
|
||||
_level = LevelSerializer.Deserialize(json);
|
||||
@@ -84,9 +82,7 @@ public sealed partial class MainWindow : Window
|
||||
}
|
||||
|
||||
if (file is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await FileIO.WriteTextAsync(file, LevelSerializer.Serialize(_level));
|
||||
_currentFile = file;
|
||||
@@ -116,9 +112,7 @@ public sealed partial class MainWindow : Window
|
||||
private void LevelCanvas_PointerMoved(object sender, PointerRoutedEventArgs e)
|
||||
{
|
||||
if (_painting)
|
||||
{
|
||||
PaintAt(e.GetCurrentPoint(LevelCanvas).Position);
|
||||
}
|
||||
}
|
||||
|
||||
private void LevelCanvas_PointerReleased(object sender, PointerRoutedEventArgs e)
|
||||
@@ -130,9 +124,7 @@ public sealed partial class MainWindow : Window
|
||||
private void PaintAt(Point point)
|
||||
{
|
||||
if (!TryGetGridPosition(point, out var position))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_selectedCell = position;
|
||||
_level = LevelEditor.Apply(_level, position, _selectedTool);
|
||||
@@ -155,46 +147,38 @@ public sealed partial class MainWindow : Window
|
||||
private void DrawCells(CanvasDrawingSession drawing, CanvasLayout layout)
|
||||
{
|
||||
for (var y = 0; y < _level.Height; y++)
|
||||
for (var x = 0; x < _level.Width; x++)
|
||||
{
|
||||
for (var x = 0; x < _level.Width; x++)
|
||||
var position = new GridPosition(x, y);
|
||||
var cell = _level.GetCell(position);
|
||||
var rect = layout.CellRect(x, y);
|
||||
|
||||
drawing.FillRectangle(rect, CellColor(cell));
|
||||
|
||||
if (cell.HasPipe)
|
||||
{
|
||||
var position = new GridPosition(x, y);
|
||||
var cell = _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));
|
||||
var pipeColor = cell.Pipe switch
|
||||
{
|
||||
var center = new Vector2((float)(rect.X + rect.Width / 2), (float)(rect.Y + rect.Height / 2));
|
||||
var pipeColor = cell.Pipe switch
|
||||
{
|
||||
PipeMedium.Coolant => Colors.DeepSkyBlue,
|
||||
PipeMedium.Fuel => Colors.Goldenrod,
|
||||
PipeMedium.Pressure => Colors.LightSteelBlue,
|
||||
_ => Colors.Transparent
|
||||
};
|
||||
drawing.DrawLine(new Vector2((float)rect.X + 6, center.Y), new Vector2((float)(rect.X + rect.Width - 6), center.Y), pipeColor, Math.Max(3, (float)rect.Width / 7));
|
||||
drawing.DrawLine(new Vector2(center.X, (float)rect.Y + 6), new Vector2(center.X, (float)(rect.Y + rect.Height - 6)), pipeColor, Math.Max(3, (float)rect.Width / 7));
|
||||
}
|
||||
|
||||
if (cell.LeakRate > 0)
|
||||
{
|
||||
drawing.DrawCircle(new Vector2((float)(rect.X + rect.Width - 10), (float)(rect.Y + 10)), 5, Colors.OrangeRed, 2);
|
||||
}
|
||||
|
||||
if (cell.Hazards.Fire)
|
||||
{
|
||||
drawing.FillCircle(new Vector2((float)(rect.X + rect.Width * 0.5), (float)(rect.Y + rect.Height * 0.5)), (float)rect.Width * 0.24f, Colors.OrangeRed);
|
||||
}
|
||||
|
||||
if (_selectedCell == position)
|
||||
{
|
||||
drawing.DrawRectangle(rect, Colors.White, 3);
|
||||
}
|
||||
|
||||
DrawCellGlyph(drawing, cell, rect);
|
||||
PipeMedium.Coolant => Colors.DeepSkyBlue,
|
||||
PipeMedium.Fuel => Colors.Goldenrod,
|
||||
PipeMedium.Pressure => Colors.LightSteelBlue,
|
||||
_ => Colors.Transparent
|
||||
};
|
||||
drawing.DrawLine(new((float)rect.X + 6, center.Y), new((float)(rect.X + rect.Width - 6), center.Y), pipeColor, Math.Max(3, (float)rect.Width / 7));
|
||||
drawing.DrawLine(new(center.X, (float)rect.Y + 6), new(center.X, (float)(rect.Y + rect.Height - 6)), pipeColor, Math.Max(3, (float)rect.Width / 7));
|
||||
}
|
||||
|
||||
if (cell.LeakRate > 0)
|
||||
drawing.DrawCircle(new((float)(rect.X + rect.Width - 10), (float)(rect.Y + 10)), 5, Colors.OrangeRed, 2);
|
||||
|
||||
if (cell.Hazards.Fire)
|
||||
drawing.FillCircle(new((float)(rect.X + rect.Width * 0.5), (float)(rect.Y + rect.Height * 0.5)), (float)rect.Width * 0.24f, Colors.OrangeRed);
|
||||
|
||||
if (_selectedCell == position)
|
||||
drawing.DrawRectangle(rect, Colors.White, 3);
|
||||
|
||||
DrawCellGlyph(drawing, cell, rect);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,19 +186,17 @@ public sealed partial class MainWindow : Window
|
||||
{
|
||||
var text = cell.Kind switch
|
||||
{
|
||||
CellKind.Reactor => "R",
|
||||
CellKind.CoolingPump => "C",
|
||||
CellKind.Generator => "G",
|
||||
CellKind.PressureRegulator => "P",
|
||||
CellKind.Reactor => "R",
|
||||
CellKind.CoolingPump => "C",
|
||||
CellKind.Generator => "G",
|
||||
CellKind.PressureRegulator => "P",
|
||||
CellKind.DiagnosticTerminal => "D",
|
||||
CellKind.ControlTerminal => "T",
|
||||
_ => string.Empty
|
||||
CellKind.ControlTerminal => "T",
|
||||
_ => string.Empty
|
||||
};
|
||||
|
||||
if (string.IsNullOrEmpty(text))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
using var format = new CanvasTextFormat
|
||||
{
|
||||
@@ -254,7 +236,7 @@ public sealed partial class MainWindow : Window
|
||||
var layout = GetLayout();
|
||||
var x = (int)((point.X - layout.OriginX) / layout.CellSize);
|
||||
var y = (int)((point.Y - layout.OriginY) / layout.CellSize);
|
||||
position = new GridPosition(x, y);
|
||||
position = new(x, y);
|
||||
return _level.InBounds(position);
|
||||
}
|
||||
|
||||
@@ -266,30 +248,26 @@ public sealed partial class MainWindow : Window
|
||||
size = Math.Max(20, size);
|
||||
var originX = Math.Max(0, (availableWidth - size * _level.Width) / 2);
|
||||
var originY = Math.Max(0, (availableHeight - size * _level.Height) / 2);
|
||||
return new CanvasLayout(size, originX, originY);
|
||||
return new(size, originX, originY);
|
||||
}
|
||||
|
||||
private static Windows.UI.Color CellColor(CellState cell)
|
||||
private static Color CellColor(CellState cell)
|
||||
{
|
||||
if (cell.Kind == CellKind.Wall)
|
||||
{
|
||||
return ColorHelper.FromArgb(255, 54, 61, 68);
|
||||
}
|
||||
|
||||
if (cell.Hazards.Fire)
|
||||
{
|
||||
return ColorHelper.FromArgb(255, 91, 39, 30);
|
||||
}
|
||||
|
||||
return cell.Kind switch
|
||||
{
|
||||
CellKind.Reactor => ColorHelper.FromArgb(255, 61, 76, 82),
|
||||
CellKind.CoolingPump => ColorHelper.FromArgb(255, 25, 79, 96),
|
||||
CellKind.Generator => ColorHelper.FromArgb(255, 86, 75, 35),
|
||||
CellKind.PressureRegulator => ColorHelper.FromArgb(255, 70, 78, 98),
|
||||
CellKind.Reactor => ColorHelper.FromArgb(255, 61, 76, 82),
|
||||
CellKind.CoolingPump => ColorHelper.FromArgb(255, 25, 79, 96),
|
||||
CellKind.Generator => ColorHelper.FromArgb(255, 86, 75, 35),
|
||||
CellKind.PressureRegulator => ColorHelper.FromArgb(255, 70, 78, 98),
|
||||
CellKind.DiagnosticTerminal => ColorHelper.FromArgb(255, 39, 84, 62),
|
||||
CellKind.ControlTerminal => ColorHelper.FromArgb(255, 80, 61, 91),
|
||||
_ => ColorHelper.FromArgb(255, 31, 36, 40)
|
||||
CellKind.ControlTerminal => ColorHelper.FromArgb(255, 80, 61, 91),
|
||||
_ => ColorHelper.FromArgb(255, 31, 36, 40)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -298,29 +276,15 @@ public sealed partial class MainWindow : Window
|
||||
LevelNameText.Text = _level.Name;
|
||||
TurnText.Text = _level.Global.Turn.ToString();
|
||||
StatusText.Text = _level.Global.Status;
|
||||
GlobalText.Text =
|
||||
$"Power: {_level.Global.Power}/10\n" +
|
||||
$"Cooling: {_level.Global.Cooling}/10\n" +
|
||||
$"Core Heat: {_level.Global.CoreHeat}/10\n" +
|
||||
$"Facility Stability: {_level.Global.FacilityStability}/10";
|
||||
GlobalText.Text = $"Power: {_level.Global.Power}/10\n" + $"Cooling: {_level.Global.Cooling}/10\n" + $"Core Heat: {_level.Global.CoreHeat}/10\n" + $"Facility Stability: {_level.Global.FacilityStability}/10";
|
||||
|
||||
if (_selectedCell is { } position && _level.InBounds(position))
|
||||
{
|
||||
var cell = _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" + $"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}";
|
||||
}
|
||||
else
|
||||
{
|
||||
CellText.Text = "No cell selected.";
|
||||
}
|
||||
|
||||
ForecastList.ItemsSource = _level.Forecasts;
|
||||
}
|
||||
@@ -328,20 +292,68 @@ public sealed partial class MainWindow : Window
|
||||
private static LevelState BuildStarterLevel()
|
||||
{
|
||||
var level = LevelState.Create("Cooling Sector B", 16, 12);
|
||||
level = level.SetCell(new GridPosition(3, 5), new CellState { Kind = CellKind.CoolingPump, Pipe = PipeMedium.Coolant, Flow = 5, Pressure = 5, Powered = true });
|
||||
level = level.SetCell(new GridPosition(4, 5), new CellState { Pipe = PipeMedium.Coolant, Flow = 5, Pressure = 7 });
|
||||
level = level.SetCell(new GridPosition(5, 5), new CellState { Pipe = PipeMedium.Coolant, Flow = 3, Pressure = 8, LeakRate = 2, Integrity = 4 });
|
||||
level = level.SetCell(new GridPosition(6, 5), new CellState { Pipe = PipeMedium.Coolant, Flow = 3, Pressure = 7 });
|
||||
level = level.SetCell(new GridPosition(8, 5), new CellState { Kind = CellKind.Reactor, Hazards = new HazardState { Heat = 6, Stability = 8 } });
|
||||
level = level.SetCell(new GridPosition(2, 8), new CellState { Kind = CellKind.Generator, Pipe = PipeMedium.Fuel, Flow = 4, Pressure = 6, Powered = true });
|
||||
level = level.SetCell(new GridPosition(11, 4), new CellState { Kind = CellKind.DiagnosticTerminal, Powered = true });
|
||||
level = level.SetCell(new GridPosition(12, 8), new CellState { Kind = CellKind.ControlTerminal, Powered = true });
|
||||
level = level.SetCell(new(3, 5), new()
|
||||
{
|
||||
Kind = CellKind.CoolingPump,
|
||||
Pipe = PipeMedium.Coolant,
|
||||
Flow = 5,
|
||||
Pressure = 5,
|
||||
Powered = true
|
||||
});
|
||||
level = level.SetCell(new(4, 5), new()
|
||||
{
|
||||
Pipe = PipeMedium.Coolant,
|
||||
Flow = 5,
|
||||
Pressure = 7
|
||||
});
|
||||
level = level.SetCell(new(5, 5), new()
|
||||
{
|
||||
Pipe = PipeMedium.Coolant,
|
||||
Flow = 3,
|
||||
Pressure = 8,
|
||||
LeakRate = 2,
|
||||
Integrity = 4
|
||||
});
|
||||
level = level.SetCell(new(6, 5), new()
|
||||
{
|
||||
Pipe = PipeMedium.Coolant,
|
||||
Flow = 3,
|
||||
Pressure = 7
|
||||
});
|
||||
level = level.SetCell(new(8, 5), new()
|
||||
{
|
||||
Kind = CellKind.Reactor,
|
||||
Hazards = new()
|
||||
{
|
||||
Heat = 6,
|
||||
Stability = 8
|
||||
}
|
||||
});
|
||||
level = level.SetCell(new(2, 8), new()
|
||||
{
|
||||
Kind = CellKind.Generator,
|
||||
Pipe = PipeMedium.Fuel,
|
||||
Flow = 4,
|
||||
Pressure = 6,
|
||||
Powered = true
|
||||
});
|
||||
level = level.SetCell(new(11, 4), new()
|
||||
{
|
||||
Kind = CellKind.DiagnosticTerminal,
|
||||
Powered = true
|
||||
});
|
||||
level = level.SetCell(new(12, 8), new()
|
||||
{
|
||||
Kind = CellKind.ControlTerminal,
|
||||
Powered = true
|
||||
});
|
||||
return level with { Forecasts = new SimulationEngine().Forecast(level) };
|
||||
}
|
||||
|
||||
private sealed record CanvasLayout(double CellSize, double OriginX, double OriginY)
|
||||
{
|
||||
public Rect CellRect(int x, int y) =>
|
||||
new(OriginX + x * CellSize, OriginY + y * CellSize, CellSize, CellSize);
|
||||
}
|
||||
}
|
||||
private readonly SimulationEngine _simulation = new();
|
||||
private StorageFile? _currentFile;
|
||||
private LevelState _level;
|
||||
private bool _painting;
|
||||
private GridPosition? _selectedCell;
|
||||
private EditorTool _selectedTool = EditorTool.Floor;
|
||||
}
|
||||
Reference in New Issue
Block a user