♻️ migrated the InteractionAreas currently used to the new format and removed the old code

This commit is contained in:
2025-11-04 17:20:15 +01:00
parent 8d616735f4
commit 8b77ca9bc5
15 changed files with 132 additions and 70 deletions
@@ -7,13 +7,13 @@ namespace Babushka.scripts.CSharp.Common.CharacterControls;
public partial class InteractionArea2D : Node2D
{
[Export] private Area2D _area;
[Export] private Label _label;
[Export] private bool _active = true;
[Export] private bool _useOutline = true;
[Export] private ShaderMaterial _outlineMaterial;
[Export] private CanvasItem? _spriteToOutline; // keep to not break old usages. TODO: remove later
[Export] private CanvasItem[] _spritesToOutline;
[Export] private CanvasItem[] _spritesToOutline = [];
[Export] private bool _showLabel = true;
[Export] private int _id = -1; // TODO: remove
@@ -38,21 +38,7 @@ public partial class InteractionArea2D : Node2D
{
if (_useOutline)
{
try
{
// support old implementations of the script. If the sprite to outline is set, add it to the array
if (_spriteToOutline != null)
{
Array.Resize(ref _spritesToOutline, _spritesToOutline.Length + 1);
_spritesToOutline[^1] = _spriteToOutline;
}
_backupMaterials = _spritesToOutline.Select(s => s.Material).ToArray();
}
catch (Exception exception)
{
GD.PrintErr($"No sprite to outline found on: {GetParent().Name}" + exception.Message);
}
_backupMaterials = _spritesToOutline.Select(s => s.Material).ToArray();
}
}
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Godot;
namespace Babushka.scripts.CSharp.Common.Util;
@@ -23,4 +24,54 @@ public static class NodeExtension
}
throw new Exception($"Parent of type {typeof(T)} not found for node {self.Name}");
}
//acts like Unity's GetComponent<T> / GetComponentInChildren<T>
// only works with Godot's built-in types.
public static T GetChildByType<T>(this Node node, bool recursive = true)
where T : Node
{
int childCount = node.GetChildCount();
for (int i = 0; i < childCount; i++)
{
Node child = node.GetChild(i);
if (child is T childT)
return childT;
if (recursive && child.GetChildCount() > 0)
{
T recursiveResult = child.GetChildByType<T>(true);
if (recursiveResult != null)
return recursiveResult;
}
}
return null;
}
/// <summary>
/// Another reimplementation of Unity's GetComponent<T>.
/// Verified to work with all types, also derived ones, but only when used from within a scene and at runtime.
/// </summary>
/// <param name="node"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T GetComponent<T>(Node node)
{
if (node is T)
{
return (T)(object)node;
}
foreach (Node child in node.GetChildren())
{
if (child is T)
{
return (T)(object)child;
}
}
return (T)(object)null;
}
}