Skip to content

Commit 868e11a

Browse files
author
LoneWandererProductions
committed
Sync to new version
1 parent c328f94 commit 868e11a

13 files changed

Lines changed: 1395 additions & 105 deletions

‎Solaris/Aurora.xaml.cs‎

Lines changed: 270 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,255 @@ public int StepHeight
160160

161161
#endregion
162162

163+
#region Avatar & Dynamic Sorting Dependency Properties
164+
165+
/// <summary>
166+
/// The current tile index of the avatar actor.
167+
/// </summary>
168+
public static readonly DependencyProperty AvatarPositionProperty = DependencyProperty.Register(
169+
nameof(AvatarPosition), typeof(int), typeof(Aurora),
170+
new PropertyMetadata(-1, OnAvatarPositionChanged));
171+
172+
/// <summary>
173+
/// Enables sorting dynamic actors directly on LayerOne alongside standing props.
174+
/// </summary>
175+
public static readonly DependencyProperty UseUnifiedLayerOneProperty = DependencyProperty.Register(
176+
nameof(UseUnifiedLayerOne), typeof(bool), typeof(Aurora),
177+
new PropertyMetadata(true));
178+
179+
/// <summary>
180+
/// Gets or sets the avatar position.
181+
/// </summary>
182+
/// <value>
183+
/// The avatar position.
184+
/// </value>
185+
public int AvatarPosition
186+
{
187+
get => (int)GetValue(AvatarPositionProperty);
188+
set => SetValue(AvatarPositionProperty, value);
189+
}
190+
191+
/// <summary>
192+
/// Gets or sets a value indicating whether [use unified layer one].
193+
/// </summary>
194+
/// <value>
195+
/// <c>true</c> if [use unified layer one]; otherwise, <c>false</c>.
196+
/// </value>
197+
public bool UseUnifiedLayerOne
198+
{
199+
get => (bool)GetValue(UseUnifiedLayerOneProperty);
200+
set => SetValue(UseUnifiedLayerOneProperty, value);
201+
}
202+
203+
/// <summary>
204+
/// Called when [avatar position changed].
205+
/// </summary>
206+
/// <param name="d">The d.</param>
207+
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
208+
private static void OnAvatarPositionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
209+
{
210+
var control = (Aurora)d;
211+
control.MarkLayerDirty(DirtyFlags.FullRedraw);
212+
control.RenderDirty();
213+
}
214+
215+
#endregion
216+
217+
#region Actor & Avatar Dependency Properties
218+
219+
/// <summary>
220+
/// Liste oder Dictionary aller dynamischen Einheiten (Gegner, NPCs, Verbündete).
221+
/// </summary>
222+
public static readonly DependencyProperty AuroraActorsProperty = DependencyProperty.Register(
223+
nameof(AuroraActors), typeof(Dictionary<int, GameActor>), typeof(Aurora),
224+
new PropertyMetadata(null, OnActorsChanged));
225+
226+
/// <summary>
227+
/// Gets or sets the aurora actors.
228+
/// </summary>
229+
/// <value>
230+
/// The aurora actors.
231+
/// </value>
232+
public Dictionary<int, GameActor>? AuroraActors
233+
{
234+
get => (Dictionary<int, GameActor>?)GetValue(AuroraActorsProperty);
235+
set => SetValue(AuroraActorsProperty, value);
236+
}
237+
238+
/// <summary>
239+
/// Called when [actors changed].
240+
/// </summary>
241+
/// <param name="d">The d.</param>
242+
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
243+
private static void OnActorsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
244+
{
245+
var control = (Aurora)d;
246+
if (e.NewValue is Dictionary<int, GameActor> actors)
247+
{
248+
foreach (var actor in actors.Values)
249+
{
250+
actor.UpdateBuffer();
251+
}
252+
}
253+
control.MarkLayerDirty(DirtyFlags.FullRedraw);
254+
control.RenderDirty();
255+
}
256+
257+
/// <summary>
258+
/// Called when the player's avatar is assigned or replaced.
259+
/// </summary>
260+
/// <param name="d">The d.</param>
261+
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
262+
private static void OnAvatarChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
263+
{
264+
var control = (Aurora)d;
265+
control.UpdateAvatarBuffer();
266+
control.MarkLayerDirty(DirtyFlags.FullRedraw);
267+
control.RenderDirty();
268+
}
269+
270+
/// <summary>
271+
/// Converts the allocated avatar bitmap into a fast unmanaged buffer.
272+
/// </summary>
273+
private void UpdateAvatarBuffer()
274+
{
275+
_avatarBuffer?.Dispose();
276+
_avatarBuffer = AuroraAvatar != null ? UnmanagedImageBuffer.FromBitmap(AuroraAvatar) : null;
277+
}
278+
279+
#endregion
280+
281+
#region Fog of War Dependency Properties
282+
283+
/// <summary>
284+
/// Status of the Warring Mist by tile index (0 = Unexplored, 1 = Explored, 2 = Visible).
285+
/// </summary>
286+
public static readonly DependencyProperty AuroraFogProperty = DependencyProperty.Register(
287+
nameof(AuroraFog), typeof(byte[]), typeof(Aurora),
288+
new PropertyMetadata(null, OnFogChanged));
289+
290+
/// <summary>
291+
/// Gets or sets the aurora fog.
292+
/// </summary>
293+
/// <value>
294+
/// The aurora fog.
295+
/// </value>
296+
public byte[]? AuroraFog
297+
{
298+
get => (byte[]?)GetValue(AuroraFogProperty);
299+
set => SetValue(AuroraFogProperty, value);
300+
}
301+
302+
/// <summary>
303+
/// Called when [fog changed].
304+
/// </summary>
305+
/// <param name="d">The d.</param>
306+
/// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
307+
/// <returns></returns>
308+
private static void OnFogChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
309+
{
310+
var control = (Aurora)d;
311+
control.MarkLayerDirty(DirtyFlags.FullRedraw);
312+
control.RenderDirty();
313+
}
314+
315+
#endregion
316+
317+
/// <summary>
318+
/// Assigns a new list of texture layers to a tile and re-renders it.
319+
/// </summary>
320+
/// <param name="tileIndex">The 1D tile index.</param>
321+
/// <param name="tiles">The new list of texture IDs on this tile.</param>
322+
public void ChangeTiles(int tileIndex, List<int> tiles)
323+
{
324+
if (tileIndex < 0 || tileIndex >= AuroraWidth * AuroraHeight) return;
325+
326+
AuroraMap ??= new Dictionary<int, List<int>>();
327+
328+
if (tiles == null || tiles.Count == 0)
329+
{
330+
AuroraMap.Remove(tileIndex);
331+
}
332+
else
333+
{
334+
AuroraMap[tileIndex] = new List<int>(tiles);
335+
}
336+
337+
MarkTileDirty(tileIndex, DirtyFlags.TileMap);
338+
RenderDirty();
339+
}
340+
341+
/// <summary>
342+
/// Berechnet anhand einer 4-Bit-Nachbarschaftsmaske (N=1, E=2, S=4, W=8)
343+
/// die passenden Übergangs-/Autotiling-Texturen für eine Kachel und aktualisiert sie.
344+
/// Verhindert Zeilenüberläufe an den Rändern (Flat Map Bounds).
345+
/// </summary>
346+
/// <param name="tileIndex">Der Kachelindex.</param>
347+
/// <param name="targetBaseTextureId">Die Textur-ID des Basis-Geländes (z. B. Wasser oder Straße).</param>
348+
/// <param name="getTransitionTextureId">Optionale Lookup-Funktion: (bitmask) => texturId.</param>
349+
public void UpdateTileTransitions(
350+
int tileIndex,
351+
int targetBaseTextureId,
352+
Func<byte, int?>? getTransitionTextureId = null)
353+
{
354+
if (tileIndex < 0 || tileIndex >= AuroraWidth * AuroraHeight || AuroraMap == null)
355+
return;
356+
357+
int x = tileIndex % AuroraWidth;
358+
int y = tileIndex / AuroraWidth;
359+
360+
// 4-Bit Richtungs-Maske: Nord (1), Ost (2), Süd (4), West (8)
361+
byte mask = 0;
362+
363+
// Nord (y > 0)
364+
if (y > 0 && HasMatchingTexture(tileIndex - AuroraWidth, targetBaseTextureId))
365+
mask |= 1;
366+
367+
// Ost (x < Width - 1: kein Zeilenumbruch!)
368+
if (x < AuroraWidth - 1 && HasMatchingTexture(tileIndex + 1, targetBaseTextureId))
369+
mask |= 2;
370+
371+
// Süd (y < Height - 1)
372+
if (y < AuroraHeight - 1 && HasMatchingTexture(tileIndex + AuroraWidth, targetBaseTextureId))
373+
mask |= 4;
374+
375+
// West (x > 0: kein Zeilenübertrag!)
376+
if (x > 0 && HasMatchingTexture(tileIndex - 1, targetBaseTextureId))
377+
mask |= 8;
378+
379+
if (getTransitionTextureId != null)
380+
{
381+
var resolvedTransition = getTransitionTextureId(mask);
382+
if (resolvedTransition.HasValue)
383+
{
384+
if (!AuroraMap.TryGetValue(tileIndex, out var layers))
385+
{
386+
layers = new List<int>();
387+
AuroraMap[tileIndex] = layers;
388+
}
389+
390+
// Fügt die Übergangstextur auf oberster Ebene hinzu oder tauscht sie aus
391+
if (!layers.Contains(resolvedTransition.Value))
392+
{
393+
layers.Add(resolvedTransition.Value);
394+
MarkTileDirty(tileIndex, DirtyFlags.TileMap);
395+
RenderDirty();
396+
}
397+
}
398+
}
399+
}
400+
401+
/// <summary>
402+
/// Prüft sicher, ob eine Kachel eine bestimmte Textur-ID enthält.
403+
/// </summary>
404+
private bool HasMatchingTexture(int checkTileIndex, int matchTextureId)
405+
{
406+
if (AuroraMap == null || !AuroraMap.TryGetValue(checkTileIndex, out var list))
407+
return false;
408+
409+
return list.Contains(matchTextureId);
410+
}
411+
163412
/// <summary>
164413
/// The third layer
165414
/// </summary>
@@ -180,6 +429,11 @@ public int StepHeight
180429
/// </summary>
181430
public Viewport ActiveViewport { get; } = new();
182431

432+
/// <summary>
433+
/// The avatar buffer
434+
/// </summary>
435+
private UnmanagedImageBuffer? _avatarBuffer;
436+
183437
/// <inheritdoc />
184438
/// <summary>
185439
/// Initializes a new instance of the <see cref="T:Solaris.Aurora" /> class.
@@ -384,16 +638,6 @@ private static void OnMapChanged(DependencyObject d, DependencyPropertyChangedEv
384638
control.RenderDirty();
385639
}
386640

387-
/// <summary>
388-
/// Called when [avatar changed].
389-
/// </summary>
390-
/// <param name="d">The d.</param>
391-
/// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
392-
private static void OnAvatarChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
393-
{
394-
// Optional hook: If you need to immediately draw the avatar when it's assigned, do it here.
395-
}
396-
397641
/// <summary>
398642
/// Called when [movement changed].
399643
/// </summary>
@@ -406,13 +650,12 @@ private static async void OnMovementChanged(DependencyObject d, DependencyProper
406650

407651
try
408652
{
409-
// Safely await the animation without blocking the main UI thread
410-
await Helper.DisplayMovement(control, movement, control.AuroraAvatar, control.AuroraWidth,
411-
control.AuroraHeight, control.AuroraTextureSize);
653+
await Helper.DisplayMovement(
654+
control, movement, control.AuroraAvatar, control.AuroraWidth,
655+
control.AuroraHeight, control.AuroraTextureSize, control.UseUnifiedLayerOne);
412656
}
413657
catch (Exception ex)
414658
{
415-
// Log or handle animation failure gracefully rather than crashing the app silently
416659
Trace.WriteLine($"Movement animation failed: {ex.Message}");
417660
}
418661
}
@@ -479,11 +722,13 @@ private static void OnAddDisplayChanged(DependencyObject d, DependencyPropertyCh
479722
var control = (Aurora)d;
480723
var value = (KeyValuePair<int, int>)e.NewValue;
481724

482-
var newBmp = Helper.AddDisplay(control.AuroraWidth, control.AuroraHeight, control.AuroraTextureSize,
483-
control.AuroraTextures, control._thirdLayer, value);
725+
var newBmp = Helper.AddDisplay(
726+
control.AuroraWidth, control.AuroraHeight, control.AuroraTextureSize,
727+
control.AuroraTextures, control._thirdLayer, value,
728+
control.ActiveViewport, control.AuroraElevation, control.StepHeight);
729+
484730
control._thirdLayer = newBmp;
485731
control.LayerThree.Source = newBmp.UpdateWriteableBitmap(control.LayerThree.Source as WriteableBitmap);
486-
487732
control.MarkTileDirty(value.Key, DirtyFlags.Overlays);
488733
}
489734

@@ -497,11 +742,12 @@ private static void OnRemoveDisplayChanged(DependencyObject d, DependencyPropert
497742
var control = (Aurora)d;
498743
var value = (int)e.NewValue;
499744

500-
var newBmp = Helper.RemoveDisplay(control.AuroraWidth, control.AuroraTextureSize, control._thirdLayer,
501-
value);
502-
control._thirdLayer = newBmp;
503-
control.LayerThree.Source = newBmp.UpdateWriteableBitmap(control.LayerThree.Source as WriteableBitmap);
745+
var newBmp = Helper.RemoveDisplay(
746+
control.AuroraWidth, control.AuroraTextureSize, control._thirdLayer, value,
747+
control.ActiveViewport, control.AuroraElevation, control.StepHeight);
504748

749+
control._thirdLayer = newBmp;
750+
control.LayerThree.Source = newBmp?.UpdateWriteableBitmap(control.LayerThree.Source as WriteableBitmap);
505751
control.MarkTileDirty(value, DirtyFlags.Overlays);
506752
}
507753

@@ -623,7 +869,9 @@ private void UpdateMapAndBitmap()
623869

624870
var newBitmap = Helper.GenerateImage(
625871
AuroraWidth, AuroraHeight, AuroraTextureSize, AuroraTextures, AuroraMap,
626-
ActiveViewport, AuroraElevation, StepHeight);
872+
ActiveViewport, AuroraElevation, StepHeight,
873+
AvatarPosition, _avatarBuffer, AuroraActors, AuroraFog);
874+
627875
ReplaceBitmapLayerOne(newBitmap);
628876
}
629877

‎Solaris/DirtyFlags.cs‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,33 @@ namespace Solaris
1616
[Flags]
1717
public enum DirtyFlags
1818
{
19+
/// <summary>
20+
/// The none
21+
/// </summary>
1922
None = 0,
20-
TileMap = 1 << 0, // Layer 1: Base terrain / static tiles
21-
Grid = 1 << 1, // Layer 2: Polaris grid overlay
22-
Overlays = 1 << 2, // Layer 3: Selection, glyphs, dynamic cursors
23+
/// <summary>
24+
/// Layer 1: Base terrain / static tiles
25+
/// </summary>
26+
TileMap = 1 << 0,
27+
28+
/// <summary>
29+
/// Layer 2: Polaris grid overlay
30+
/// </summary>
31+
Grid = 1 << 1,
32+
33+
/// <summary>
34+
/// Layer 3: Selection, glyphs, dynamic cursors
35+
/// </summary>
36+
Overlays = 1 << 2,
37+
38+
/// <summary>
39+
/// The viewport
40+
/// </summary>
2341
Viewport = 1 << 3,
42+
43+
/// <summary>
44+
/// The full redraw
45+
/// </summary>
2446
FullRedraw = TileMap | Grid | Overlays | Viewport
2547
}
2648
}

0 commit comments

Comments
 (0)