A game where you get to play as a slime, made with Godot.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.7 KiB

public static class GodotExtensions
{
public static T GetParentOrThrow<T>(this Node node)
where T : class
{
var parent = node.GetParent();
if (parent == null) throw new InvalidOperationException($"Parent of {node} is null");
if (parent is not T result) throw new InvalidCastException($"Parent of {node} is {node.GetType()}, not {typeof(T)}");
return result;
}
public static T GetNodeOrThrow<T>(this Node parent, NodePath path)
where T : class
{
var node = parent.GetNodeOrNull(path);
if (node == null) throw new InvalidOperationException($"Could not find node {path} from {parent}");
if (node is not T result) throw new InvalidCastException($"Node {path} from {parent} is {node.GetType()}, not {typeof(T)}");
return result;
}
public static (Corner, Corner) GetCorners(this Side side)
=> side switch {
Side.Left => (Corner.BottomLeft , Corner.TopLeft ),
Side.Top => (Corner.TopLeft , Corner.TopRight ),
Side.Right => (Corner.TopRight , Corner.BottomRight),
Side.Bottom => (Corner.BottomRight, Corner.BottomLeft ),
_ => throw new ArgumentException($"Invalid Side value '{side}'", nameof(side)),
};
public static Side GetOpposite(this Side side)
=> side switch {
Side.Left => Side.Right,
Side.Top => Side.Bottom,
Side.Right => Side.Left,
Side.Bottom => Side.Top,
_ => throw new ArgumentException($"Invalid Side value '{side}'", nameof(side)),
};
public static Corner GetOpposite(this Corner corner)
=> corner switch {
Corner.TopLeft => Corner.BottomRight,
Corner.TopRight => Corner.BottomLeft,
Corner.BottomRight => Corner.TopLeft,
Corner.BottomLeft => Corner.TopRight,
_ => throw new ArgumentException($"Invalid Corner value '{corner}'", nameof(corner)),
};
}