| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- using Microsoft.Xna.Framework;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace OpenVIII
- {
- /// <summary>
- /// More Extensions
- /// </summary>
- public static class PointEx
- {
- #region Methods
- /// <see cref="https://stackoverflow.com/questions/4615410/random-element-of-listt-from-linq-sql"/>
- public static T Random<T>(this IEnumerable<T> input) => input.ElementAt(Memory.Random.Next(input.Count()));
- public static T RandomOrDefault<T>(this IEnumerable<T> input) => input.ElementAtOrDefault(Memory.Random.Next(input.Count()));
- /// <summary>
- /// Rotate a Vector2 by degrees
- /// </summary>
- /// <param name="v"></param>
- /// <param name="degrees"></param>
- /// <returns></returns>
- /// <see cref="https://answers.unity.com/questions/661383/whats-the-most-efficient-way-to-rotate-a-vector2-o.html"/>
- public static Vector2 Rotate(this Vector2 v, float degrees)
- {
- var rad = MathHelper.ToRadians(degrees);
- var sin = (float)Math.Sin(rad);
- var cos = (float)Math.Cos(rad);
- var tx = v.X;
- var ty = v.Y;
- v.X = (cos * tx) - (sin * ty);
- v.Y = (sin * tx) + (cos * ty);
- return v;
- }
- public static Point Offset(this ref Point source, Point offset)
- {
- source = (source.ToVector2() + offset.ToVector2()).ToPoint();
- return source;
- }
- public static Point Offset(this ref Point source, int x,int y)
- {
- var offset = new Point(x, y);
- return source.Offset(offset);
- }
- public static Point Offset(this ref Point source, Vector2 offset)
- {
- return source = (source.ToVector2() + offset).ToPoint();
- }
- public static Point Transform(this Point point, Matrix matrix)
- {
- point = Vector2.Transform(point.ToVector2(), Matrix.Invert(matrix)).RoundedPoint();
- return point;
- }
- public static Rectangle Scale(this Rectangle source, Matrix matrix)
- {
- var scale = Memory.Scale();
- var loc = source.Location.ToVector2();
- source.Offset(matrix.Translation.X, matrix.Translation.Y);
- source.Location = Vector2.Transform(loc, Matrix.Invert(matrix)).RoundedPoint();
- source.Size = (source.Size.ToVector2() * scale).RoundedPoint();
- return source;
- }
- public static Rectangle Scale(this Rectangle source, Vector2 scale)
- {
- source.Location = (source.Location.ToVector2() * scale).RoundedPoint();
- source.Size = (source.Size.ToVector2() * scale).RoundedPoint();
- return source;
- }
- public static Rectangle Scale(this Rectangle source)
- {
- var scale = Memory.Scale();
- source.Location = (source.Location.ToVector2() * scale).RoundedPoint();
- source.Size = (source.Size.ToVector2() * scale).RoundedPoint();
- return source;
- }
- public static Point Scale(this Point source, Vector2 scale)
- {
- source = (source.ToVector2() * scale).RoundedPoint();
- return source;
- }
- public static Point Scale(this Point source)
- {
- var scale = Memory.Scale();
- source = (source.ToVector2() * scale).RoundedPoint();
- return source;
- }
- public static Point RoundedPoint(this Vector2 v) => new Point((int)Math.Round(v.X), (int)Math.Round(v.Y));
- public static Point CeilingPoint(this Vector2 v) => v.Ceiling().ToPoint();
- public static Point FloorPoint(this Vector2 v) => v.Floor().ToPoint();
- public static Vector2 Round(this Vector2 v) => new Vector2((float)Math.Round(v.X), (float)Math.Round(v.Y));
- public static Vector2 Ceiling(this Vector2 v) => new Vector2((float)Math.Ceiling(v.X), (float)Math.Ceiling(v.Y));
- public static Vector2 Abs(this Vector2 v) => new Vector2((float)Math.Abs(v.X), (float)Math.Abs(v.Y));
- public static Vector2 Floor(this Vector2 v) => new Vector2((float)Math.Floor(v.X), (float)Math.Floor(v.Y));
- public static Vector2 FloorOrCeiling(this Vector2 v, Vector2 target)
- {
- float X, Y;
- X = v.X < target.X ? (float)Math.Ceiling(v.X) : (float)Math.Floor(v.X);
- Y = v.Y < target.Y ? (float)Math.Ceiling(v.Y) : (float)Math.Floor(v.Y);
- return new Vector2(X, Y);
- }
- /// <summary>
- /// Count how many flags set in enum.
- /// </summary>
- /// <param name="statuses">varible you need to number of flags set.</param>
- /// <returns>Count</returns>
- /// <see cref="https://stackoverflow.com/questions/677204/counting-the-number-of-flags-set-on-an-enumeration"/>
- public static uint Count(this Kernel.JunctionStatuses statuses)
- {
- var v = (uint)statuses;
- v = v - ((v >> 1) & 0x55555555); // reuse input as temporary
- v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
- return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // Count
- }
- /// <summary>
- /// Count how many flags set in enum.
- /// </summary>
- /// <param name="element">varible you need to number of flags set.</param>
- /// <returns>Count</returns>
- /// <see cref="https://stackoverflow.com/questions/677204/counting-the-number-of-flags-set-on-an-enumeration"/>
- public static uint Count(this Kernel.Element element)
- {
- var v = (uint)element;
- v = v - ((v >> 1) & 0x55555555); // reuse input as temporary
- v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
- return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // Count
- }
- /// <summary>
- /// Count how many flags set in enum.
- /// </summary>
- /// <param name="element">varible you need to number of flags set.</param>
- /// <returns>Count</returns>
- /// <see cref="https://stackoverflow.com/questions/677204/counting-the-number-of-flags-set-on-an-enumeration"/>
- public static uint Count(this Kernel.BattleOnlyStatuses element)
- {
- var v = (uint)element;
- v = v - ((v >> 1) & 0x55555555); // reuse input as temporary
- v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
- return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // Count
- }
- /// <summary>
- /// Count how many flags set in enum.
- /// </summary>
- /// <param name="element">varible you need to number of flags set.</param>
- /// <returns>Count</returns>
- /// <see cref="https://stackoverflow.com/questions/677204/counting-the-number-of-flags-set-on-an-enumeration"/>
- public static uint Count(this Kernel.PersistentStatuses element)
- {
- var v = (uint)element;
- v = v - ((v >> 1) & 0x55555555); // reuse input as temporary
- v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
- return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // Count
- }
- public static Characters ToCharacters(this Faces.ID id)
- {
- if ((byte)id > 10)
- return Characters.Blank;
- return (Characters)id;
- }
- public static GFs ToGFs(this Faces.ID id)
- {
- if ((byte)id < 16 || (byte)id > 31)
- return GFs.Blank;
- return (GFs)(id - 16);
- }
- public static Faces.ID ToFacesID(this Characters id) => (Faces.ID)id;
- public static Faces.ID ToFacesID(this GFs id)
- {
- if (GFs.All == id || GFs.Blank == id)
- return Faces.ID.Blank;
- return (Faces.ID)(id + 16);
- }
- #endregion Methods
- }
- }
|