2023-06-03 09:35:13 -04:00

85 lines
2.9 KiB
C#

using System;
using System.Collections.Generic;
using BindingFlags = System.Reflection.BindingFlags;
using UnityEngine;
using System.Collections;
using GatherAndDefend.LevelEditor;
public static class Extensions
{
public static Rect CalculateBounds(this Level level)
{
Rect bound = new Rect(0, 0, 0, 0);
foreach (var tilemap in level)
{
foreach (var tile in tilemap)
{
bound = bound.GetUpdatedBound((Vector3)tile.Position);
}
}
return bound;
}
public static Rect GetUpdatedBound(this Rect bound, Rect other)
{
var rect = bound;
rect.xMin = other.xMin < rect.xMin ? other.xMin : rect.xMin;
rect.yMin = other.yMin < rect.yMin ? other.yMin : rect.yMin;
rect.xMax = other.xMax > rect.xMax ? other.xMax : rect.xMax;
rect.yMax = other.yMax > rect.yMax ? other.yMax : rect.yMax;
return rect;
}
public static Rect GetUpdatedBound(this Rect bound, Vector2 position)
{
var rect = bound;
rect.xMin = position.x < rect.xMin ? position.x : rect.xMin;
rect.yMin = position.y < rect.yMin ? position.y : rect.yMin;
rect.xMax = position.x > rect.xMax ? position.x : rect.xMax;
rect.yMax = position.y > rect.yMax ? position.y : rect.yMax;
return rect;
}
public static int ToInt(this object jobj)
{
return int.Parse(jobj.ToString());
}
public static float ToFloat(this object jobj)
{
return float.Parse(jobj.ToString());
}
public static Vector3 ToVector3(this object jobj)
{
var p_enum = (jobj as IEnumerable).GetEnumerator();
float[] p_array = new float[3];
p_enum.MoveNext();
p_array[0] = float.Parse(p_enum.Current.ToString());
p_enum.MoveNext();
p_array[1] = float.Parse(p_enum.Current.ToString());
p_enum.MoveNext();
p_array[2] = float.Parse(p_enum.Current.ToString());
return new Vector3(p_array[0], p_array[1], p_array[2]);
}
public static bool ToBool(this object jobj) => bool.Parse(jobj.ToString());
public static GameObject Create(this GameObject prefab, Vector3 position, Quaternion rotation = default, Transform parent = null)
{
if (rotation == default) rotation = Quaternion.identity;
var instance = GameObject.Instantiate(prefab, position, rotation);
instance.transform.SetParent(parent);
instance.name = prefab.name;
return instance;
}
public static T GetComponentInChildren<T>(this Component obj, string name) where T : Component
{
foreach (var comp in obj.GetComponentsInChildren<T>())
{
if (comp.name == name) return comp;
}
return null;
}
public enum LevelObjectType { GameObject, Tile }
public static bool Approximately(this Vector3 vect, Vector3 other)
{
return Mathf.Approximately(Vector3.Distance(vect, other), 0);
}
}