inz-00/Assets/Scripts/Common/Extensions.cs
2019-11-17 16:23:29 +01:00

52 lines
1.7 KiB
C#

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Assets.Common
{
public static class UnityPointExtensions
{
public static Vector3 ToVector3(this Point p)
{
return new Vector3((float)p.x, 0, (float)p.y);
}
}
public static class IEnumerableExtensions
{
public static T RandomElement<T>(this IEnumerable<T> collection, System.Random generator)
{
var count = collection.Count();
return collection.ElementAt(generator.Next(count));
}
public static IEnumerable<T> RotateRight<T>(this IEnumerable<T> collection, int count = 1)
{
var diff = collection.Count() - count;
var head = collection.Take(diff);
var tail = collection.Skip(diff).Take(count);
return tail.Concat(head);
}
public static IEnumerable<T> RotateLeft<T>(this IEnumerable<T> collection, int count = 1)
{
var diff = collection.Count() - count;
var head = collection.Take(count);
var tail = collection.Skip(count).Take(diff);
return tail.Concat(head);
}
}
public static class HasMetadataExtensions
{
public static bool HasProperty(this IHasMetadata subject, string name) => subject.Metadata.HasProperty(name);
public static bool HasProperty<T>(this IHasMetadata subject, string name) => subject.Metadata.HasProperty<T>(name);
public static void SetProperty<T>(this IHasMetadata subject, string name, T property) => subject.Metadata.SetProperty(name, property);
public static T GetProperty<T>(this IHasMetadata subject, string name) => subject.Metadata.GetProperty<T>(name);
}
}