Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cart
Created February 1, 2018 06:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cart/0736138c02f75eb081e2b4b7b3f2d545 to your computer and use it in GitHub Desktop.
Save cart/0736138c02f75eb081e2b4b7b3f2d545 to your computer and use it in GitHub Desktop.
A collection of C# Node extensions for Godot
using Godot;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace HighHat.Scene.Nodes
{
public static class NodeExtensions
{
private static Regex _nameRegex = new Regex("@*(?<Name>[\\w|\\d]+)@*.*");
public static T FindClosestToTarget<T>(this IEnumerable<T> spatials, Vector3 target, Func<T, bool> predicate = null) where T : Spatial
{
T closest = null;
float? closestDistance = null;
foreach (var spatial in spatials)
{
if (predicate != null && predicate(spatial) == false)
{
continue;
}
var distance = (spatial.GlobalTransform.origin - target).Length();
if (closestDistance == null || distance < closestDistance.Value)
{
closestDistance = distance;
closest = spatial;
}
}
return closest;
}
public static void RemoveFromParent(this Node node)
{
var parent = node.GetParent();
if (parent != null)
{
parent.RemoveChild(node);
}
}
public static void AddToCurrentLevel(this Spatial spatial)
{
Game.Instance.LevelManager.CurrentLevel.AddChild(spatial);
}
public static void SetBoneAttachmentRotation(this Spatial spatial)
{
spatial.Rotation = new Vector3(-Mathf.PI / 2.0f, 0, 0);
}
public static string GetOriginalName(this Node node)
{
var match = _nameRegex.Match(node.Name);
if (match.Success)
{
return match.Groups["Name"].Captures[0].Value;
}
return null;
}
public static void MakeMaterialOverrideUnique(this MeshInstance meshInstance)
{
meshInstance.MaterialOverride = (Material)meshInstance.MaterialOverride.Duplicate();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment