Initial Commit
This commit is contained in:
commit
ee5c2f922d
2255 changed files with 547750 additions and 0 deletions
37
Assets/Scripts/Util/AnimatorExtensions.cs
Normal file
37
Assets/Scripts/Util/AnimatorExtensions.cs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
using UnityEngine;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
public static class AnimatorExtensions
|
||||
{
|
||||
public static void SetFloatSafe(this Animator animator, int hash, float value)
|
||||
{
|
||||
if (animator.Exists(hash))
|
||||
{
|
||||
animator.SetFloat(hash, value);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetBoolSafe(this Animator animator, int hash, bool value)
|
||||
{
|
||||
if (animator.Exists(hash))
|
||||
{
|
||||
animator.SetBool(hash, value);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool Exists(this Animator animator, int hash)
|
||||
{
|
||||
var paramCount = animator.parameterCount;
|
||||
var p = animator.parameters;
|
||||
for (var i = 0; i < paramCount; i++)
|
||||
{
|
||||
if (p[i].nameHash == hash)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Util/AnimatorExtensions.cs.meta
Normal file
3
Assets/Scripts/Util/AnimatorExtensions.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 75904a32e3924e4f938be95e21e2c1f7
|
||||
timeCreated: 1534032964
|
||||
7
Assets/Scripts/Util/AssetManifest.cs
Normal file
7
Assets/Scripts/Util/AssetManifest.cs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
namespace DefaultNamespace.Util
|
||||
{
|
||||
public static class AssetManifest
|
||||
{
|
||||
public const string HealthKit = "health_kit";
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Util/AssetManifest.cs.meta
Normal file
3
Assets/Scripts/Util/AssetManifest.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ea150cb272204883ae17521841b8c5c3
|
||||
timeCreated: 1643460924
|
||||
56
Assets/Scripts/Util/AsyncOperationWrapper.cs
Normal file
56
Assets/Scripts/Util/AsyncOperationWrapper.cs
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
using System;
|
||||
using UnityEngine.ResourceManagement.AsyncOperations;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
public struct AsyncOperationWrapper<T> where T : class
|
||||
{
|
||||
private readonly T obj;
|
||||
private readonly AsyncOperationHandle<T>? handle;
|
||||
|
||||
public AsyncOperationWrapper(T obj)
|
||||
{
|
||||
this.obj = obj;
|
||||
handle = new AsyncOperationHandle<T>();
|
||||
}
|
||||
|
||||
public AsyncOperationWrapper(AsyncOperationHandle<T> handle)
|
||||
{
|
||||
this.handle = handle;
|
||||
obj = default;
|
||||
}
|
||||
|
||||
public bool IsEmpty => !HasObject && !handle.HasValue;
|
||||
|
||||
private bool HasObject => obj != null;
|
||||
|
||||
public AsyncOperationStatus Status => obj != null ? AsyncOperationStatus.Succeeded : handle?.Status ?? AsyncOperationStatus.None;
|
||||
|
||||
public bool IsValid => HasObject || (handle?.IsValid() ?? false);
|
||||
|
||||
public bool IsDone => HasObject || (handle?.IsDone ?? false);
|
||||
|
||||
public float PercentComplete => HasObject ? 1 : handle?.PercentComplete ?? 0;
|
||||
|
||||
public Exception OperationException => HasObject ? null : handle?.OperationException ?? null;
|
||||
|
||||
public T Result => obj ?? handle?.Result;
|
||||
|
||||
public event Action<AsyncOperationWrapper<T>> Completed
|
||||
{
|
||||
add
|
||||
{
|
||||
if (HasObject)
|
||||
{
|
||||
value.Invoke(this);
|
||||
}
|
||||
else if (handle.HasValue)
|
||||
{
|
||||
var thisCopy = this;
|
||||
handle.Value.Completed += operationHandle => value.Invoke(thisCopy);
|
||||
}
|
||||
}
|
||||
remove { }
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Util/AsyncOperationWrapper.cs.meta
Normal file
3
Assets/Scripts/Util/AsyncOperationWrapper.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 595613121c854ee1932f2d5dfb593f24
|
||||
timeCreated: 1532261040
|
||||
60
Assets/Scripts/Util/BufferVector2.cs
Normal file
60
Assets/Scripts/Util/BufferVector2.cs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Assets.Scripts.Util
|
||||
{
|
||||
public struct BufferVector2 : IBufferElementData
|
||||
{
|
||||
public bool Equals(BufferVector2 other)
|
||||
{
|
||||
return x.Equals(other.x) && y.Equals(other.y);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return obj is BufferVector2 other && Equals(other);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
return (x.GetHashCode() * 397) ^ y.GetHashCode();
|
||||
}
|
||||
}
|
||||
|
||||
public float x;
|
||||
public float y;
|
||||
|
||||
public BufferVector2(float x, float y)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public static implicit operator Vector2(BufferVector2 val)
|
||||
{
|
||||
return new Vector2(val.x, val.y);
|
||||
}
|
||||
|
||||
public static bool operator ==(BufferVector2 lhs, BufferVector2 rhs)
|
||||
{
|
||||
return lhs.x == rhs.x && lhs.y == rhs.y;
|
||||
}
|
||||
|
||||
public static bool operator !=(BufferVector2 lhs, BufferVector2 rhs)
|
||||
{
|
||||
return lhs.x != rhs.x || lhs.y != rhs.y;
|
||||
}
|
||||
|
||||
public static BufferVector2 operator -(BufferVector2 lhs, BufferVector2 rhs)
|
||||
{
|
||||
return new BufferVector2(lhs.x - rhs.x, lhs.y - rhs.y);
|
||||
}
|
||||
|
||||
public static BufferVector2 operator +(BufferVector2 lhs, BufferVector2 rhs)
|
||||
{
|
||||
return new BufferVector2(lhs.x + rhs.x, lhs.y + rhs.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Util/BufferVector2.cs.meta
Normal file
11
Assets/Scripts/Util/BufferVector2.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 901cd711bd6f8304683ae2b44feea6b2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
217
Assets/Scripts/Util/EntityCommandBufferExtensions.cs
Normal file
217
Assets/Scripts/Util/EntityCommandBufferExtensions.cs
Normal file
|
|
@ -0,0 +1,217 @@
|
|||
using Events;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
public static class EntityCommandBufferExtensions
|
||||
{
|
||||
public delegate void ModifyData<T>(ref T data) where T : struct, IComponentData;
|
||||
|
||||
public static void PostEvent<T>(this EntityCommandBuffer buffer) where T : struct, IComponentData
|
||||
{
|
||||
var entity = buffer.CreateEntity();
|
||||
buffer.AddComponent(entity, new EventData());
|
||||
buffer.AddComponent(entity, new T());
|
||||
}
|
||||
|
||||
public static void PostEvent<T>(this EntityCommandBuffer buffer, T eventComponent) where T : struct, IComponentData
|
||||
{
|
||||
var entity = buffer.CreateEntity();
|
||||
buffer.AddComponent(entity, new EventData());
|
||||
buffer.AddComponent(entity, eventComponent);
|
||||
}
|
||||
|
||||
public static void PostEvent<T>(this EntityManager entityManager) where T : struct, IComponentData
|
||||
{
|
||||
entityManager.CreateEntity(ComponentType.ReadWrite<EventData>(), ComponentType.ReadWrite<T>());
|
||||
}
|
||||
|
||||
public static void PostEvent<T>(this EntityManager entityManager, T eventComponent) where T : struct, IComponentData
|
||||
{
|
||||
var entity = entityManager.CreateEntity(ComponentType.ReadWrite<EventData>(), ComponentType.ReadWrite<T>());
|
||||
entityManager.SetComponentData(entity, eventComponent);
|
||||
}
|
||||
|
||||
public static void PostEntityEvent
|
||||
<T>(this EntityCommandBuffer buffer, EntityManager entityManager, Entity entity) where T : struct, IComponentData
|
||||
{
|
||||
if (!entityManager.HasComponent<T>(entity))
|
||||
{
|
||||
buffer.AddComponent(entity, new T());
|
||||
}
|
||||
}
|
||||
|
||||
public static void PostEntityEvent
|
||||
<T>(this EntityCommandBuffer buffer, EntityManager entityManager, Entity entity, T eventComponent) where T : struct, IComponentData
|
||||
{
|
||||
if (entityManager.HasComponent<T>(entity))
|
||||
{
|
||||
buffer.SetComponent(entity, eventComponent);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.AddComponent(entity, eventComponent);
|
||||
}
|
||||
}
|
||||
|
||||
public static void PostEntityEvent<T>(this EntityManager entityManager, Entity entity) where T : struct, IComponentData
|
||||
{
|
||||
if (!entityManager.HasComponent<T>(entity))
|
||||
{
|
||||
entityManager.AddComponentData(entity, new T());
|
||||
}
|
||||
}
|
||||
|
||||
public static void PostEntityEvent<T>(this EntityManager manager, Entity entity, T eventComponent) where T : struct, IComponentData
|
||||
{
|
||||
if (manager.HasComponent<T>(entity))
|
||||
{
|
||||
manager.SetComponentData(entity, eventComponent);
|
||||
}
|
||||
else
|
||||
{
|
||||
manager.AddComponentData(entity, eventComponent);
|
||||
}
|
||||
}
|
||||
|
||||
public static void PostEntityEvent<T>(this EntityManager entityManager, Entity entity, ModifyData<T> eAction) where T : struct, IComponentData
|
||||
{
|
||||
if (entityManager.HasComponent<T>(entity))
|
||||
{
|
||||
var data = entityManager.GetComponentData<T>(entity);
|
||||
eAction.Invoke(ref data);
|
||||
entityManager.SetComponentData(entity, data);
|
||||
}
|
||||
else
|
||||
{
|
||||
var data = new T();
|
||||
eAction.Invoke(ref data);
|
||||
entityManager.AddComponentData(entity, data);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool HasComponents<T, TK>(this EntityManager manager, Entity entity)
|
||||
{
|
||||
return manager.HasComponent<T>(entity) && manager.HasComponent<TK>(entity);
|
||||
}
|
||||
|
||||
public static bool HasComponents<T, TK, TH>(this EntityManager manager, Entity entity)
|
||||
{
|
||||
return manager.HasComponent<T>(entity) && manager.HasComponent<TK>(entity) && manager.HasComponent<TH>(entity);
|
||||
}
|
||||
|
||||
public static bool HasComponents<T, TK, TH, TL>(this EntityManager manager, Entity entity)
|
||||
{
|
||||
return manager.HasComponent<T>(entity) &&
|
||||
manager.HasComponent<TK>(entity) &&
|
||||
manager.HasComponent<TH>(entity) &&
|
||||
manager.HasComponent<TL>(entity);
|
||||
}
|
||||
|
||||
public static bool HasComponents<T, TK, TH, TL, TM>(this EntityManager manager, Entity entity)
|
||||
{
|
||||
return manager.HasComponent<T>(entity) &&
|
||||
manager.HasComponent<TK>(entity) &&
|
||||
manager.HasComponent<TH>(entity) &&
|
||||
manager.HasComponent<TL>(entity) &&
|
||||
manager.HasComponent<TM>(entity);
|
||||
}
|
||||
|
||||
public static bool HasComponents<T, TK, TH, TL, TM, TR>(this EntityManager manager, Entity entity)
|
||||
{
|
||||
return manager.HasComponent<T>(entity) &&
|
||||
manager.HasComponent<TK>(entity) &&
|
||||
manager.HasComponent<TH>(entity) &&
|
||||
manager.HasComponent<TL>(entity) &&
|
||||
manager.HasComponent<TM>(entity) &&
|
||||
manager.HasComponent<TR>(entity);
|
||||
}
|
||||
|
||||
public static bool HasComponents(this EntityManager manager, Entity entity, params Type[] types)
|
||||
{
|
||||
return types.All(t => manager.HasComponent(entity, ComponentType.FromTypeIndex(TypeManager.GetTypeIndex(t))));
|
||||
}
|
||||
|
||||
public static bool HasComponents(this EntityManager manager, Entity entity, params ComponentType[] types)
|
||||
{
|
||||
return types.All(t => manager.HasComponent(entity, t));
|
||||
}
|
||||
|
||||
public static void CopyFrom<T>(this EntityCommandBuffer buffer, Entity entity, GameObjectEntity o) where T : struct, IComponentData
|
||||
{
|
||||
buffer.AddComponent(entity, o.GetComponent<ComponentDataProxy<T>>().Value);
|
||||
}
|
||||
|
||||
public static void DestroyEntityWithObjects(this EntityManager entityManager, Entity entity)
|
||||
{
|
||||
if (entityManager.HasComponent<Transform>(entity))
|
||||
{
|
||||
Object.Destroy(entityManager.GetComponentObject<Transform>(entity).gameObject);
|
||||
}
|
||||
if (entityManager.Exists(entity))
|
||||
{
|
||||
entityManager.DestroyEntity(entity);
|
||||
}
|
||||
}
|
||||
|
||||
public static void KeepData
|
||||
<T>(this EntityCommandBuffer buffer, EntityManager entityManager, bool keep, Entity entity, T data) where T : struct, IComponentData
|
||||
{
|
||||
var hasComponent = entityManager.HasComponent<T>(entity);
|
||||
if (!keep && hasComponent)
|
||||
{
|
||||
buffer.RemoveComponent<T>(entity);
|
||||
}
|
||||
else if (keep && !hasComponent)
|
||||
{
|
||||
buffer.AddComponent(entity, data);
|
||||
}
|
||||
else if (keep)
|
||||
{
|
||||
buffer.SetComponent(entity, data);
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryGetComponentData<T>(this EntityManager entityManager, Entity entity, out T componetData)
|
||||
where T : struct, IComponentData
|
||||
{
|
||||
if (entityManager.HasComponent<T>(entity))
|
||||
{
|
||||
componetData = entityManager.GetComponentData<T>(entity);
|
||||
return true;
|
||||
}
|
||||
|
||||
componetData = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool TryGetSharedComponentData
|
||||
<T>(this EntityManager entityManager, Entity entity, out T componetData) where T : struct, ISharedComponentData
|
||||
{
|
||||
if (entityManager.HasComponent<T>(entity))
|
||||
{
|
||||
componetData = entityManager.GetSharedComponentData<T>(entity);
|
||||
return true;
|
||||
}
|
||||
|
||||
componetData = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool TryGetComponentObject<T>(this EntityManager entityManager, Entity entity, out T componetData)
|
||||
{
|
||||
if (entityManager.HasComponent<T>(entity))
|
||||
{
|
||||
componetData = entityManager.GetComponentObject<T>(entity);
|
||||
return true;
|
||||
}
|
||||
|
||||
componetData = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 10a8d83969f5477d9d3e6c5bee97d662
|
||||
timeCreated: 1532171570
|
||||
462
Assets/Scripts/Util/EnumerableExtensions.cs
Normal file
462
Assets/Scripts/Util/EnumerableExtensions.cs
Normal file
|
|
@ -0,0 +1,462 @@
|
|||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
public static class EnumerableExtensions
|
||||
{
|
||||
public static void Fill<T>(this T[] buffer, T val)
|
||||
{
|
||||
for (var i = 0; i < buffer.Length; i++)
|
||||
{
|
||||
buffer[i] = val;
|
||||
}
|
||||
}
|
||||
|
||||
public static int FindMinIndex<T>(this IReadOnlyList<T> list, Func<T, float> valueGetter)
|
||||
{
|
||||
var closestValue = float.MaxValue;
|
||||
var closestIndex = -1;
|
||||
|
||||
for (var i = 0; i < list.Count; i++)
|
||||
{
|
||||
var val = valueGetter.Invoke(list[i]);
|
||||
if (val < closestValue || closestIndex < 0)
|
||||
{
|
||||
closestValue = val;
|
||||
closestIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
return closestIndex;
|
||||
}
|
||||
|
||||
public static int FindMinIndex<T>(this NativeList<T> list, Func<T, float> valueGetter) where T : struct
|
||||
{
|
||||
var closestValue = float.MaxValue;
|
||||
var closestIndex = -1;
|
||||
|
||||
for (var i = 0; i < list.Length; i++)
|
||||
{
|
||||
var val = valueGetter.Invoke(list[i]);
|
||||
if (val < closestValue || closestIndex < 0)
|
||||
{
|
||||
closestValue = val;
|
||||
closestIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
return closestIndex;
|
||||
}
|
||||
|
||||
public static T MinValue<T>(this IEnumerable<T> list, Func<T, float> valueGetter)
|
||||
{
|
||||
var closestDist = float.PositiveInfinity;
|
||||
T obj = default;
|
||||
var isEmpty = true;
|
||||
foreach (var val in list)
|
||||
{
|
||||
var dist = valueGetter(val);
|
||||
if (dist <= closestDist)
|
||||
{
|
||||
closestDist = dist;
|
||||
obj = val;
|
||||
}
|
||||
|
||||
isEmpty = false;
|
||||
}
|
||||
|
||||
if (isEmpty)
|
||||
{
|
||||
throw new ArgumentException();
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
public static int FindIndex<T>(this NativeList<T> val, Predicate<T> predicate) where T : struct
|
||||
{
|
||||
return FindIndex((NativeArray<T>)val, predicate);
|
||||
}
|
||||
|
||||
public static int FindIndex<T>(this NativeArray<T> val, Predicate<T> predicate) where T : struct
|
||||
{
|
||||
for (var i = 0; i < val.Length; i++)
|
||||
{
|
||||
if (predicate.Invoke(val[i]))
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static void Reverse<T>(this NativeList<T> val) where T : struct
|
||||
{
|
||||
Reverse((NativeArray<T>)val);
|
||||
}
|
||||
|
||||
public static void Reverse<T>(this NativeArray<T> val) where T : struct
|
||||
{
|
||||
var tmp = new NativeArray<T>(val.Length, Allocator.Temp);
|
||||
for (var i = 0; i < val.Length; i++)
|
||||
{
|
||||
tmp[val.Length - i - 1] = val[i];
|
||||
}
|
||||
val.CopyFrom(tmp);
|
||||
tmp.Dispose();
|
||||
}
|
||||
|
||||
public static NativeArray<T> Fill<T>(this NativeArray<T> val, T v) where T : struct
|
||||
{
|
||||
for (var i = 0; i < val.Length; i++)
|
||||
{
|
||||
val[i] = v;
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
public static IEnumerator<T> Enumerate<T>(this NativeList<T> val) where T : struct
|
||||
{
|
||||
for (var i = 0; i < val.Length; i++)
|
||||
{
|
||||
yield return val[i];
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerator<T> Enumerate<T>(this NativeArray<T> val) where T : struct
|
||||
{
|
||||
for (var i = 0; i < val.Length; i++)
|
||||
{
|
||||
yield return val[i];
|
||||
}
|
||||
}
|
||||
|
||||
public static bool CheckIterate<T>(this IEnumerable<T> list) where T : struct
|
||||
{
|
||||
return list.Any();
|
||||
}
|
||||
|
||||
public static void Iterate<T>(this IEnumerable<T> list) where T : struct
|
||||
{
|
||||
foreach (var unused in list)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<(NativeArray<T>, int)> NativeForEach<T>(this NativeArray<T> list, Func<T, T> func) where T : struct
|
||||
{
|
||||
for (var i = 0; i < list.Length; i++)
|
||||
{
|
||||
list[i] = func.Invoke(list[i]);
|
||||
yield return (list, i);
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<(NativeSlice<T>, int)> NativeForEach<T>(this NativeSlice<T> list, Func<T, T> func) where T : struct
|
||||
{
|
||||
for (var i = 0; i < list.Length; i++)
|
||||
{
|
||||
list[i] = func.Invoke(list[i]);
|
||||
yield return (list, i);
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<(NativeArray<T>, int)> NativeForEach
|
||||
<T>(this IEnumerable<(NativeArray<T> array, int index)> list, Func<T, T> func) where T : struct
|
||||
{
|
||||
foreach (var tuple in list)
|
||||
{
|
||||
var array = tuple.array;
|
||||
array[tuple.index] = func.Invoke(tuple.array[tuple.index]);
|
||||
yield return tuple;
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<(NativeSlice<T>, int)> NativeForEach
|
||||
<T>(this IEnumerable<(NativeSlice<T> array, int index)> list, Func<T, T> func) where T : struct
|
||||
{
|
||||
foreach (var tuple in list)
|
||||
{
|
||||
var array = tuple.array;
|
||||
array[tuple.index] = func.Invoke(tuple.array[tuple.index]);
|
||||
yield return tuple;
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<(NativeArray<T>, int)> NativeWhere<T>(this NativeArray<T> list, Predicate<T> predicate) where T : struct
|
||||
{
|
||||
for (var i = 0; i < list.Length; i++)
|
||||
{
|
||||
var eVal = list[i];
|
||||
if (predicate.Invoke(eVal))
|
||||
{
|
||||
yield return (list, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<(NativeSlice<T>, int)> NativeWhere<T>(this NativeSlice<T> list, Predicate<T> predicate) where T : struct
|
||||
{
|
||||
for (var i = 0; i < list.Length; i++)
|
||||
{
|
||||
var eVal = list[i];
|
||||
if (predicate.Invoke(eVal))
|
||||
{
|
||||
yield return (list, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static (DynamicBuffer<T>, int)? NativeFirstOrOptional<T>(this DynamicBuffer<T> buffer, Predicate<T> predicate) where T : struct
|
||||
{
|
||||
for (var i = 0; i < buffer.Length; i++)
|
||||
{
|
||||
var eVal = buffer[i];
|
||||
if (predicate.Invoke(eVal))
|
||||
{
|
||||
return (buffer, i);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static (NativeSlice<T>, int)? NativeFirstOrOptional<T>(this NativeSlice<T> list, Predicate<T> predicate) where T : struct
|
||||
{
|
||||
for (var i = 0; i < list.Length; i++)
|
||||
{
|
||||
var eVal = list[i];
|
||||
if (predicate.Invoke(eVal))
|
||||
{
|
||||
return (list, i);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static (NativeArray<T>, int)? NativeFirstOrOptional<T>(this NativeArray<T> list, Predicate<T> predicate) where T : struct
|
||||
{
|
||||
for (var i = 0; i < list.Length; i++)
|
||||
{
|
||||
var eVal = list[i];
|
||||
if (predicate.Invoke(eVal))
|
||||
{
|
||||
return (list, i);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static bool NativeModify<T>(this (DynamicBuffer<T>, int)? val, Func<T, T> func) where T : struct
|
||||
{
|
||||
if (val.HasValue)
|
||||
{
|
||||
var list = val.Value.Item1;
|
||||
var index = val.Value.Item2;
|
||||
list[index] = func.Invoke(list[index]);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool NativeModify<T>(this (NativeSlice<T>, int)? val, Func<T, T> func) where T : struct
|
||||
{
|
||||
if (val.HasValue)
|
||||
{
|
||||
var list = val.Value.Item1;
|
||||
var index = val.Value.Item2;
|
||||
list[index] = func.Invoke(list[index]);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool NativeModify<T>(this (NativeArray<T>, int)? val, Func<T, T> func) where T : struct
|
||||
{
|
||||
if (val.HasValue)
|
||||
{
|
||||
var list = val.Value.Item1;
|
||||
var index = val.Value.Item2;
|
||||
list[index] = func.Invoke(list[index]);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool EmptyPredicate<T>(T val)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public static DynamicBufferEnumerator<T> Begin<T>(this DynamicBuffer<T> val) where T : struct, IBufferElementData
|
||||
{
|
||||
return new DynamicBufferEnumerator<T> { Predicate = EmptyPredicate, Buffer = val, Index = 0 };
|
||||
}
|
||||
|
||||
public static DynamicBufferEnumerator<T> Where<T>(this DynamicBufferEnumerator<T> val, Predicate<T> func) where T : struct, IBufferElementData
|
||||
{
|
||||
return new DynamicBufferEnumerator<T>
|
||||
{
|
||||
Buffer = val.Buffer, Index = 0, Predicate = v => (val.Predicate == null || val.Predicate.Invoke(v)) && func.Invoke(v)
|
||||
};
|
||||
}
|
||||
|
||||
public static float Sum<T>(this DynamicBufferEnumerator<T> val, Func<T, float> sum) where T : struct, IBufferElementData
|
||||
{
|
||||
float final = 0;
|
||||
|
||||
for (var i = val.Index; i < val.Buffer.Length; i++)
|
||||
{
|
||||
if (val.Predicate(val.Buffer[i]))
|
||||
{
|
||||
final += sum.Invoke(val.Buffer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return final;
|
||||
}
|
||||
|
||||
public static int Sum<T>(this DynamicBufferEnumerator<T> val, Func<T, int> sum) where T : struct, IBufferElementData
|
||||
{
|
||||
var final = 0;
|
||||
|
||||
for (var i = val.Index; i < val.Buffer.Length; i++)
|
||||
{
|
||||
if (val.Predicate(val.Buffer[i]))
|
||||
{
|
||||
final += sum.Invoke(val.Buffer[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return final;
|
||||
}
|
||||
|
||||
public static bool Any<T>(this DynamicBufferEnumerator<T> val, Predicate<T> predicate) where T : struct, IBufferElementData
|
||||
{
|
||||
for (var i = val.Index; i < val.Buffer.Length; i++)
|
||||
{
|
||||
if (val.Predicate(val.Buffer[i]) && predicate.Invoke(val.Buffer[i]))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static T FirstOrDefault<T>(this DynamicBufferEnumerator<T> val, Predicate<T> predicate) where T : struct, IBufferElementData
|
||||
{
|
||||
for (var i = val.Index; i < val.Buffer.Length; i++)
|
||||
{
|
||||
if (val.Predicate(val.Buffer[i]) && predicate.Invoke(val.Buffer[i]))
|
||||
{
|
||||
return val.Buffer[i];
|
||||
}
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
public static int IndexOf<T>(this DynamicBufferEnumerator<T> val, Predicate<T> predicate) where T : struct, IBufferElementData
|
||||
{
|
||||
for (var i = val.Index; i < val.Buffer.Length; i++)
|
||||
{
|
||||
if (val.Predicate(val.Buffer[i]) && predicate.Invoke(val.Buffer[i]))
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static void Fill<T>(this DynamicBuffer<T> buffer, int amount) where T : struct, IBufferElementData
|
||||
{
|
||||
for (var i = 0; i < amount; i++)
|
||||
{
|
||||
buffer.Add(default);
|
||||
}
|
||||
}
|
||||
|
||||
public static NativeHashMapEnum<T, TK> GetEnumerator<T, TK>(this NativeMultiHashMap<T, TK> map, T key)
|
||||
where T : struct, IEquatable<T> where TK : struct
|
||||
{
|
||||
return new NativeHashMapEnum<T, TK> { map = map, key = key };
|
||||
}
|
||||
|
||||
public class EnumeratorEndException : Exception
|
||||
{
|
||||
}
|
||||
|
||||
public struct DynamicBufferEnumerator<T> where T : struct, IBufferElementData
|
||||
{
|
||||
public int Index;
|
||||
public DynamicBuffer<T> Buffer;
|
||||
public Predicate<T> Predicate;
|
||||
}
|
||||
|
||||
public struct NativeHashMapEnum<T, TK> : IEnumerable<TK> where T : struct, IEquatable<T> where TK : struct
|
||||
{
|
||||
public NativeMultiHashMap<T, TK> map;
|
||||
public T key;
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
public IEnumerator<TK> GetEnumerator()
|
||||
{
|
||||
return new NativeHashMapIt<T, TK> { map = map, key = key };
|
||||
}
|
||||
}
|
||||
|
||||
public struct NativeHashMapIt<T, TK> : IEnumerator<TK> where T : struct, IEquatable<T> where TK : struct
|
||||
{
|
||||
public T key;
|
||||
public TK current;
|
||||
public NativeMultiHashMapIterator<T> it;
|
||||
public NativeMultiHashMap<T, TK> map;
|
||||
public bool isNotFirst;
|
||||
public bool hasMore;
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (!isNotFirst)
|
||||
{
|
||||
hasMore = map.TryGetFirstValue(key, out current, out it);
|
||||
isNotFirst = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
hasMore = map.TryGetNextValue(out current, ref it);
|
||||
}
|
||||
|
||||
return hasMore;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
isNotFirst = false;
|
||||
}
|
||||
|
||||
object IEnumerator.Current => Current;
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
public TK Current => current;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Util/EnumerableExtensions.cs.meta
Normal file
3
Assets/Scripts/Util/EnumerableExtensions.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: f6c6847d1d87416e80d4bebfdc15ee5c
|
||||
timeCreated: 1534087731
|
||||
308
Assets/Scripts/Util/GameObjectExtensions.cs
Normal file
308
Assets/Scripts/Util/GameObjectExtensions.cs
Normal file
|
|
@ -0,0 +1,308 @@
|
|||
using Attributes;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Trive.Mono.Utils
|
||||
{
|
||||
public static class GameObjectExtensions
|
||||
{
|
||||
public static void SetLayerRecursive(this GameObject gameObject, int layer)
|
||||
{
|
||||
if (gameObject == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
gameObject.layer = layer;
|
||||
foreach (Transform child in gameObject.transform)
|
||||
{
|
||||
if (child == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
child.gameObject.SetLayerRecursive(layer);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetStaticRecursive(this GameObject gameObject, bool isStatic)
|
||||
{
|
||||
if (gameObject == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
gameObject.isStatic = isStatic;
|
||||
foreach (Transform child in gameObject.transform)
|
||||
{
|
||||
if (child == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
child.gameObject.SetStaticRecursive(isStatic);
|
||||
}
|
||||
}
|
||||
|
||||
public static Bounds GetColliderBounds(this GameObject gameObject, bool includeTriggers)
|
||||
{
|
||||
var bounds = new Bounds(gameObject.transform.position, Vector3.zero);
|
||||
foreach (Transform child in gameObject.transform)
|
||||
{
|
||||
var colliders = child.GetComponents<Collider>();
|
||||
foreach (var collider in colliders)
|
||||
{
|
||||
if (!collider.enabled)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (!includeTriggers && !collider.isTrigger)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
bounds.Encapsulate(collider.bounds);
|
||||
}
|
||||
}
|
||||
|
||||
return bounds;
|
||||
}
|
||||
|
||||
public static Bounds GetRendererBounds(this GameObject gameObject)
|
||||
{
|
||||
var renderers = gameObject.GetComponentsInChildren<Renderer>();
|
||||
if (renderers.Length <= 0)
|
||||
{
|
||||
return new Bounds(gameObject.transform.position, Vector3.zero);
|
||||
}
|
||||
var bounds = renderers[0].bounds;
|
||||
for (var i = 1; i < renderers.Length; i++)
|
||||
{
|
||||
bounds.Encapsulate(renderers[i].bounds);
|
||||
}
|
||||
return bounds;
|
||||
}
|
||||
|
||||
public static Bounds GetMeshRendererBounds(this GameObject gameObject, bool includeInactive = false)
|
||||
{
|
||||
var renderers = gameObject.GetComponentsInChildren<MeshRenderer>(includeInactive);
|
||||
if (renderers.Length <= 0)
|
||||
{
|
||||
return new Bounds(gameObject.transform.position, Vector3.zero);
|
||||
}
|
||||
var bounds = renderers[0].bounds;
|
||||
for (var i = 1; i < renderers.Length; i++)
|
||||
{
|
||||
bounds.Encapsulate(renderers[i].bounds);
|
||||
}
|
||||
return bounds;
|
||||
}
|
||||
|
||||
private static Component GetComponent(Transform parent, Type componentType, string name, bool global, bool includeHidden)
|
||||
{
|
||||
var head = new Stack<Transform>();
|
||||
Component cmp;
|
||||
if (IsComp(parent, componentType, name, out cmp))
|
||||
{
|
||||
return cmp;
|
||||
}
|
||||
|
||||
for (var i = 0; i < parent.childCount; i++)
|
||||
{
|
||||
head.Push(parent.GetChild(i));
|
||||
}
|
||||
|
||||
while (head.Count > 0)
|
||||
{
|
||||
var h = head.Pop();
|
||||
if (includeHidden || h.gameObject.activeInHierarchy)
|
||||
{
|
||||
if (IsComp(h, componentType, name, out cmp))
|
||||
{
|
||||
return cmp;
|
||||
}
|
||||
if (global || !h.name.StartsWith("$"))
|
||||
{
|
||||
for (var i = 0; i < h.childCount; i++)
|
||||
{
|
||||
head.Push(h.GetChild(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static bool IsComp(Transform parent, Type componentType, string name, out Component cmp)
|
||||
{
|
||||
if (parent.name == name)
|
||||
{
|
||||
cmp = parent.GetComponent(componentType);
|
||||
return cmp != null;
|
||||
}
|
||||
|
||||
cmp = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
private static T CheckComp<T>(T comp, string name, bool optional) where T : Component
|
||||
{
|
||||
if (comp == null && !optional)
|
||||
{
|
||||
throw new MissingComponentException("Missing Component with name: " + name);
|
||||
}
|
||||
return comp;
|
||||
}
|
||||
|
||||
public static T FindChildrenGroup<T>(this Component t, bool global = true, bool includeHidden = false) where T : struct
|
||||
{
|
||||
return FindChildrenGroup<T>(t.transform, global, includeHidden);
|
||||
}
|
||||
|
||||
public static T FindChildrenGroup<T>(this GameObject t, bool global = true, bool includeHidden = false) where T : struct
|
||||
{
|
||||
return FindChildrenGroup<T>(t.transform, global, includeHidden);
|
||||
}
|
||||
|
||||
public static T FindChildrenGroup<T>(this Transform t, bool global = true, bool includeHidden = false) where T : struct
|
||||
{
|
||||
var group = new T();
|
||||
var fields = typeof(T).GetFields();
|
||||
foreach (var fieldInfo in fields)
|
||||
{
|
||||
if (!typeof(Component).IsAssignableFrom(fieldInfo.FieldType))
|
||||
{
|
||||
throw new ArrayTypeMismatchException($"{fieldInfo.Name} Field must be of type Component");
|
||||
}
|
||||
var rootComponentAttribute = fieldInfo.GetCustomAttribute<RootComponentAttribute>();
|
||||
if (rootComponentAttribute != null)
|
||||
{
|
||||
var rootComp = t.GetComponent(fieldInfo.FieldType);
|
||||
if (rootComp == null)
|
||||
{
|
||||
throw new MissingComponentException($"Could not find root component for field {fieldInfo.Name}");
|
||||
}
|
||||
fieldInfo.SetValueDirect(__makeref(group), rootComp);
|
||||
continue;
|
||||
}
|
||||
|
||||
var optinalAttribute = fieldInfo.GetCustomAttribute<OptionalComponentAttribute>();
|
||||
var comp = GetComponent(t, fieldInfo.FieldType, $"${fieldInfo.Name}", global, includeHidden);
|
||||
if (comp == null)
|
||||
{
|
||||
throw new MissingComponentException($"Could not find component of type {fieldInfo.FieldType} for field {fieldInfo.Name}");
|
||||
}
|
||||
fieldInfo.SetValueDirect(__makeref(group), comp);
|
||||
}
|
||||
|
||||
return group;
|
||||
}
|
||||
|
||||
public static Entity ConvertToEntity(this GameObject gameObject, World world)
|
||||
{
|
||||
var settings = new GameObjectConversionSettings(
|
||||
world,
|
||||
GameObjectConversionUtility.ConversionFlags.AddEntityGUID | GameObjectConversionUtility.ConversionFlags.AssignName);
|
||||
var entity = GameObjectConversionUtility.ConvertGameObjectHierarchy(gameObject, settings);
|
||||
|
||||
foreach (var com in gameObject.GetComponents<Component>())
|
||||
{
|
||||
if (com is GameObjectEntity || com is ConvertToEntity || com is ComponentDataProxyBase || com is StopConvertToEntity)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
world.EntityManager.AddComponentObject(entity, com);
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
#region NonOptional
|
||||
|
||||
public static T FindChild<T>(this GameObject obj, string name) where T : Component
|
||||
{
|
||||
return (T)CheckComp(GetComponent(obj.transform, typeof(T), name, false, false), name, false);
|
||||
}
|
||||
|
||||
public static T FindChild<T>(this GameObject obj, string name, bool includeHidden) where T : Component
|
||||
{
|
||||
return (T)CheckComp(GetComponent(obj.transform, typeof(T), name, false, includeHidden), name, false);
|
||||
}
|
||||
|
||||
public static T FindChild<T>(this Component obj, string name) where T : Component
|
||||
{
|
||||
return (T)CheckComp(GetComponent(obj.transform, typeof(T), name, false, false), name, false);
|
||||
}
|
||||
|
||||
public static T FindChild<T>(this Component obj, string name, bool includeHidden) where T : Component
|
||||
{
|
||||
return (T)CheckComp(GetComponent(obj.transform, typeof(T), name, false, includeHidden), name, false);
|
||||
}
|
||||
|
||||
public static T FindChildGlobal<T>(this GameObject obj, string name) where T : Component
|
||||
{
|
||||
return (T)CheckComp(GetComponent(obj.transform, typeof(T), name, true, false), name, false);
|
||||
}
|
||||
|
||||
public static T FindChildGlobal<T>(this GameObject obj, string name, bool includeHidden) where T : Component
|
||||
{
|
||||
return (T)CheckComp(GetComponent(obj.transform, typeof(T), name, true, includeHidden), name, false);
|
||||
}
|
||||
|
||||
public static T FindChildGlobal<T>(this Component obj, string name) where T : Component
|
||||
{
|
||||
return (T)CheckComp(GetComponent(obj.transform, typeof(T), name, true, false), name, false);
|
||||
}
|
||||
|
||||
public static T FindChildGlobal<T>(this Component obj, string name, bool includeHidden) where T : Component
|
||||
{
|
||||
return (T)CheckComp(GetComponent(obj.transform, typeof(T), name, true, includeHidden), name, false);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Optional
|
||||
|
||||
public static T TryFindChild<T>(this GameObject obj, string name) where T : Component
|
||||
{
|
||||
return (T)CheckComp(GetComponent(obj.transform, typeof(T), name, false, false), name, true);
|
||||
}
|
||||
|
||||
public static T TryFindChild<T>(this GameObject obj, string name, bool includeHidden) where T : Component
|
||||
{
|
||||
return (T)CheckComp(GetComponent(obj.transform, typeof(T), name, false, includeHidden), name, true);
|
||||
}
|
||||
|
||||
public static T TryFindChild<T>(this Component obj, string name) where T : Component
|
||||
{
|
||||
return (T)CheckComp(GetComponent(obj.transform, typeof(T), name, false, false), name, true);
|
||||
}
|
||||
|
||||
public static T TryFindChild<T>(this Component obj, string name, bool includeHidden) where T : Component
|
||||
{
|
||||
return (T)CheckComp(GetComponent(obj.transform, typeof(T), name, false, includeHidden), name, true);
|
||||
}
|
||||
|
||||
public static T TryFindChildGlobal<T>(this GameObject obj, string name) where T : Component
|
||||
{
|
||||
return (T)CheckComp(GetComponent(obj.transform, typeof(T), name, true, false), name, true);
|
||||
}
|
||||
|
||||
public static T TryFindChildGlobal<T>(this GameObject obj, string name, bool includeHidden) where T : Component
|
||||
{
|
||||
return (T)CheckComp(GetComponent(obj.transform, typeof(T), name, true, includeHidden), name, true);
|
||||
}
|
||||
|
||||
public static T TryFindChildGlobal<T>(this Component obj, string name) where T : Component
|
||||
{
|
||||
return (T)CheckComp(GetComponent(obj.transform, typeof(T), name, true, false), name, true);
|
||||
}
|
||||
|
||||
public static T TryFindChildGlobal<T>(this Component obj, string name, bool includeHidden) where T : Component
|
||||
{
|
||||
return (T)CheckComp(GetComponent(obj.transform, typeof(T), name, true, includeHidden), name, true);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Util/GameObjectExtensions.cs.meta
Normal file
11
Assets/Scripts/Util/GameObjectExtensions.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2bd6c9c915da5bf49aac60b70aa076fe
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
30
Assets/Scripts/Util/GuidExtensions.cs
Normal file
30
Assets/Scripts/Util/GuidExtensions.cs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Assets.Scripts.Util
|
||||
{
|
||||
public static class GuidExtensions
|
||||
{
|
||||
public static bool IsValid(this Guid guid)
|
||||
{
|
||||
return guid == Guid.Empty;
|
||||
}
|
||||
|
||||
public static bool EqualsTo(this Guid guid, Hash128 hash)
|
||||
{
|
||||
return guid.ToString("N").Equals(hash.ToString());
|
||||
}
|
||||
|
||||
public static Hash128 ToHash128(this Guid guid)
|
||||
{
|
||||
var hash = Hash128.Parse(guid.ToString());
|
||||
return hash;
|
||||
}
|
||||
|
||||
public static Guid ToGuid(this Hash128 hash)
|
||||
{
|
||||
var guid = Guid.ParseExact(hash.ToString(), "N");
|
||||
return guid;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Util/GuidExtensions.cs.meta
Normal file
11
Assets/Scripts/Util/GuidExtensions.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: eca3bd3eabdd38a43a25de5a664a5744
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
98
Assets/Scripts/Util/Optional.cs
Normal file
98
Assets/Scripts/Util/Optional.cs
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
using DefaultNamespace;
|
||||
using System;
|
||||
|
||||
namespace Assets.Scripts.Util
|
||||
{
|
||||
[Serializable]
|
||||
public struct Optional<T> where T : struct
|
||||
{
|
||||
private bool1 hasValue;
|
||||
internal T value;
|
||||
|
||||
public Optional(T value)
|
||||
{
|
||||
this.value = value;
|
||||
hasValue = true;
|
||||
}
|
||||
|
||||
public Optional(T? value)
|
||||
{
|
||||
hasValue = value.HasValue;
|
||||
this.value = hasValue ? value.Value : default;
|
||||
}
|
||||
|
||||
public bool HasValue => hasValue;
|
||||
|
||||
public T Value
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!hasValue)
|
||||
{
|
||||
throw new InvalidOperationException("No Value");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public T GetValueOrDefault()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
public T GetValueOrDefault(T defaultValue)
|
||||
{
|
||||
if (!hasValue)
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public override bool Equals(object other)
|
||||
{
|
||||
if (!hasValue)
|
||||
{
|
||||
return other == null;
|
||||
}
|
||||
if (other == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return value.Equals(other);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
if (!hasValue)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return value.GetHashCode();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if (!hasValue)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
return value.ToString();
|
||||
}
|
||||
|
||||
public static implicit operator Optional<T>(T? value)
|
||||
{
|
||||
return new Optional<T>(value);
|
||||
}
|
||||
|
||||
public static implicit operator Optional<T>(T value)
|
||||
{
|
||||
return new Optional<T>(value);
|
||||
}
|
||||
|
||||
public static explicit operator T(Optional<T> value)
|
||||
{
|
||||
return value.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Util/Optional.cs.meta
Normal file
11
Assets/Scripts/Util/Optional.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9b6be8063ba034e4da3351772b061653
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
87
Assets/Scripts/Util/PIDFloat.cs
Normal file
87
Assets/Scripts/Util/PIDFloat.cs
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Trive.Core
|
||||
{
|
||||
[Serializable]
|
||||
public struct PIDFloat
|
||||
{
|
||||
public float pFactor, iFactor, dFactor;
|
||||
private float integral;
|
||||
private float lastError;
|
||||
|
||||
public PIDFloat(Vector3 factors)
|
||||
{
|
||||
pFactor = factors.x;
|
||||
iFactor = factors.y;
|
||||
dFactor = factors.z;
|
||||
integral = 0;
|
||||
lastError = 0;
|
||||
}
|
||||
|
||||
public PIDFloat(float pFactor, float iFactor, float dFactor)
|
||||
{
|
||||
this.pFactor = pFactor;
|
||||
this.iFactor = iFactor;
|
||||
this.dFactor = dFactor;
|
||||
integral = 0;
|
||||
lastError = 0;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
integral = 0;
|
||||
lastError = 0;
|
||||
}
|
||||
|
||||
public void SetFactors(float pFactor, float iFactor, float dFactor)
|
||||
{
|
||||
this.pFactor = pFactor;
|
||||
this.iFactor = iFactor;
|
||||
this.dFactor = dFactor;
|
||||
}
|
||||
|
||||
public void SetFactors(Vector3 factors)
|
||||
{
|
||||
SetFactors(factors.x, factors.y, factors.y);
|
||||
}
|
||||
|
||||
public PIDFloat Update(float setpoint, float actual, float timeFrame, out float val)
|
||||
{
|
||||
var present = setpoint - actual;
|
||||
var newIntegral = integral + present * timeFrame;
|
||||
var deriv = (present - lastError) / timeFrame;
|
||||
val = present * pFactor + newIntegral * iFactor + deriv * dFactor;
|
||||
return new PIDFloat(pFactor, iFactor, dFactor) { lastError = present, integral = newIntegral };
|
||||
}
|
||||
|
||||
public float Update(float setpoint, float actual, float timeFrame)
|
||||
{
|
||||
return Update(setpoint, actual, timeFrame, ref lastError, ref integral);
|
||||
}
|
||||
|
||||
public float Update(float setpoint, float actual, float timeFrame, ref float lastError, ref float integral)
|
||||
{
|
||||
var present = setpoint - actual;
|
||||
integral += present * timeFrame;
|
||||
var deriv = (present - lastError) / timeFrame;
|
||||
var val = present * pFactor + integral * iFactor + deriv * dFactor;
|
||||
lastError = present;
|
||||
return val;
|
||||
}
|
||||
|
||||
public float UpdateAngle(float setpoint, float actual, float timeFrame)
|
||||
{
|
||||
return UpdateAngle(setpoint, actual, timeFrame, ref integral, ref lastError);
|
||||
}
|
||||
|
||||
public float UpdateAngle(float setpoint, float actual, float timeFrame, ref float integral, ref float lastError)
|
||||
{
|
||||
var present = Mathf.DeltaAngle(actual, setpoint);
|
||||
integral += present * timeFrame;
|
||||
var deriv = Mathf.DeltaAngle(lastError, present) / timeFrame;
|
||||
lastError = present;
|
||||
return present * pFactor + integral * iFactor + deriv * dFactor;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Util/PIDFloat.cs.meta
Normal file
11
Assets/Scripts/Util/PIDFloat.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6a4d73948338d6c4d912ef7774414490
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
36
Assets/Scripts/Util/ParticleSystemFactory.cs
Normal file
36
Assets/Scripts/Util/ParticleSystemFactory.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using Zenject;
|
||||
using Hash128 = Unity.Entities.Hash128;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
public class ParticleSystemFactory : IFactory<Hash128, AsyncOperationWrapper<GameObject>>
|
||||
{
|
||||
private readonly Dictionary<Hash128, AsyncOperationWrapper<GameObject>> loadingSystems =
|
||||
new Dictionary<Hash128, AsyncOperationWrapper<GameObject>>();
|
||||
|
||||
private readonly Dictionary<Hash128, ParticleSystem> particleSystems = new Dictionary<Hash128, ParticleSystem>();
|
||||
|
||||
public AsyncOperationWrapper<GameObject> Create(Hash128 param)
|
||||
{
|
||||
if (particleSystems.TryGetValue(param, out var system))
|
||||
{
|
||||
return new AsyncOperationWrapper<GameObject>(system.gameObject);
|
||||
}
|
||||
if (loadingSystems.TryGetValue(param, out var systemLoading))
|
||||
{
|
||||
return systemLoading;
|
||||
}
|
||||
systemLoading = new AsyncOperationWrapper<GameObject>(Addressables.InstantiateAsync(param.ToString()));
|
||||
systemLoading.Completed += operation =>
|
||||
{
|
||||
loadingSystems.Remove(param);
|
||||
particleSystems.Add(param, operation.Result.GetComponent<ParticleSystem>());
|
||||
};
|
||||
loadingSystems.Add(param, systemLoading);
|
||||
return systemLoading;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Util/ParticleSystemFactory.cs.meta
Normal file
3
Assets/Scripts/Util/ParticleSystemFactory.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: f8770bef9921407abebfd61785902be2
|
||||
timeCreated: 1533834014
|
||||
20
Assets/Scripts/Util/ProjectilePool.cs
Normal file
20
Assets/Scripts/Util/ProjectilePool.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
public class ProjectilePool
|
||||
{
|
||||
public Stack<GameObject> projectiles = new Stack<GameObject>();
|
||||
|
||||
public void AddToPool(GameObject projectile)
|
||||
{
|
||||
projectiles.Push(projectile);
|
||||
}
|
||||
|
||||
public GameObject GetFree()
|
||||
{
|
||||
return projectiles.Peek();
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Util/ProjectilePool.cs.meta
Normal file
3
Assets/Scripts/Util/ProjectilePool.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2dfb62cf08974039aeef5fb451693f5c
|
||||
timeCreated: 1531047558
|
||||
31
Assets/Scripts/Util/RandomExtensions.cs
Normal file
31
Assets/Scripts/Util/RandomExtensions.cs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
public static class RandomExtensions
|
||||
{
|
||||
public static double NextGaussian(this Random r, double mu = 0, double sigma = 1)
|
||||
{
|
||||
var u1 = r.NextDouble();
|
||||
var u2 = r.NextDouble();
|
||||
|
||||
var rand_std_normal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2);
|
||||
|
||||
var rand_normal = mu + sigma * rand_std_normal;
|
||||
|
||||
return rand_normal;
|
||||
}
|
||||
|
||||
public static void Shuffle<T>(this IList<T> list)
|
||||
{
|
||||
var n = list.Count;
|
||||
while (n > 1)
|
||||
{
|
||||
n--;
|
||||
var k = UnityEngine.Random.Range(0, n + 1);
|
||||
(list[k], list[n]) = (list[n], list[k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Util/RandomExtensions.cs.meta
Normal file
3
Assets/Scripts/Util/RandomExtensions.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9601f156f427404286a395e18e36fefd
|
||||
timeCreated: 1534025100
|
||||
73
Assets/Scripts/Util/RectExtensions.cs
Normal file
73
Assets/Scripts/Util/RectExtensions.cs
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
using UnityEngine;
|
||||
|
||||
namespace Trive.Assets.Scripts.Utils.Extensions
|
||||
{
|
||||
public static class RectExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Scales a rect by a given amount around its center point
|
||||
/// </summary>
|
||||
/// <param name="rect">
|
||||
/// The given rect
|
||||
/// </param>
|
||||
/// <param name="scale">
|
||||
/// The scale factor
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The given rect scaled around its center
|
||||
/// </returns>
|
||||
public static Rect ScaleSizeBy(this Rect rect, float scale)
|
||||
{
|
||||
return rect.ScaleSizeBy(scale, rect.center);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scales a rect by a given amount and around a given point
|
||||
/// </summary>
|
||||
/// <param name="rect">
|
||||
/// The rect to size
|
||||
/// </param>
|
||||
/// <param name="scale">
|
||||
/// The scale factor
|
||||
/// </param>
|
||||
/// <param name="pivotPoint">
|
||||
/// The point to scale around
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// The rect, scaled around the given pivot point
|
||||
/// </returns>
|
||||
public static Rect ScaleSizeBy(this Rect rect, float scale, Vector2 pivotPoint)
|
||||
{
|
||||
var result = rect;
|
||||
|
||||
//"translate" the top left to something like an origin
|
||||
result.x -= pivotPoint.x;
|
||||
result.y -= pivotPoint.y;
|
||||
|
||||
//Scale the rect
|
||||
result.xMin *= scale;
|
||||
result.yMin *= scale;
|
||||
result.xMax *= scale;
|
||||
result.yMax *= scale;
|
||||
|
||||
//"translate" the top left back to its original position
|
||||
result.x += pivotPoint.x;
|
||||
result.y += pivotPoint.y;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Rect Encapculate(this Rect rect, Vector2 point)
|
||||
{
|
||||
var min = rect.min;
|
||||
var max = rect.max;
|
||||
|
||||
min.x = Mathf.Min(min.x, point.x);
|
||||
min.y = Mathf.Min(min.y, point.y);
|
||||
max.x = Mathf.Max(max.x, point.x);
|
||||
max.y = Mathf.Max(max.y, point.y);
|
||||
|
||||
return new Rect(min.x, min.y, max.x - min.x, max.y - min.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Util/RectExtensions.cs.meta
Normal file
11
Assets/Scripts/Util/RectExtensions.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9254042b261d9ae40aa5720fc7f69c9f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
171
Assets/Scripts/Util/SoundLibrary.cs
Normal file
171
Assets/Scripts/Util/SoundLibrary.cs
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
[CreateAssetMenu(menuName = "Audio/Sound Library")]
|
||||
public class SoundLibrary : ScriptableObject
|
||||
{
|
||||
[SerializeField, HideInInspector] private AudoClipWrapper[] audoClips;
|
||||
[SerializeField] private Vector2 randomPitchRange = new Vector2(1, 1);
|
||||
[SerializeField] private Vector2 randomVolumeRange = new Vector2(1, 1);
|
||||
[SerializeField, Range(0, 1)] private float volume = 1;
|
||||
private int lastIndex;
|
||||
private List<AudoClipWrapper> shuffleClips;
|
||||
|
||||
private void Reset()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
var clips = Selection.objects.OfType<AudioClip>().ToArray();
|
||||
if (clips.Length > 0)
|
||||
{
|
||||
audoClips = new AudoClipWrapper[clips.Length];
|
||||
for (var i = 0; i < audoClips.Length; i++)
|
||||
{
|
||||
audoClips[i] = new AudoClipWrapper(clips[i]);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (shuffleClips != null)
|
||||
{
|
||||
shuffleClips = audoClips.OrderBy(c => Random.value).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public void SwitchRandomClip(AudioSource source)
|
||||
{
|
||||
if (!CheckSource(source))
|
||||
{
|
||||
return;
|
||||
}
|
||||
var wrapper = RandomWrapper();
|
||||
source.volume = wrapper.Volume;
|
||||
source.clip = wrapper.Clip;
|
||||
source.pitch = RandomPitch();
|
||||
}
|
||||
|
||||
public void SwitchShuffledClip(AudioSource source, float audoMul = 1)
|
||||
{
|
||||
if (!CheckSource(source))
|
||||
{
|
||||
return;
|
||||
}
|
||||
var wrapper = ShuffledWrapper();
|
||||
source.volume = wrapper.Volume * audoMul * RandomVolume();
|
||||
source.clip = wrapper.Clip;
|
||||
source.pitch = RandomPitch();
|
||||
}
|
||||
|
||||
public void PlayRandomOneShot(AudioSource source, float audoMul = 1)
|
||||
{
|
||||
if (!CheckSource(source))
|
||||
{
|
||||
return;
|
||||
}
|
||||
var wrapper = RandomWrapper();
|
||||
source.pitch = RandomPitch();
|
||||
source.PlayOneShot(wrapper.Clip, wrapper.Volume * audoMul * RandomVolume());
|
||||
}
|
||||
|
||||
public void PlayShuffledOneShot(AudioSource source, float audoMul = 1)
|
||||
{
|
||||
if (!CheckSource(source))
|
||||
{
|
||||
return;
|
||||
}
|
||||
var wrapper = ShuffledWrapper();
|
||||
source.pitch = RandomPitch();
|
||||
source.PlayOneShot(wrapper.Clip, wrapper.Volume * audoMul * RandomVolume());
|
||||
}
|
||||
|
||||
public void PlayShuffledOneShotOverrideVolume(AudioSource source, float audoMul = 1)
|
||||
{
|
||||
if (!CheckSource(source))
|
||||
{
|
||||
return;
|
||||
}
|
||||
var wrapper = ShuffledWrapper();
|
||||
source.pitch = RandomPitch();
|
||||
source.PlayOneShot(wrapper.Clip, wrapper.Volume * audoMul);
|
||||
}
|
||||
|
||||
private bool CheckSource(AudioSource source)
|
||||
{
|
||||
if (source == null)
|
||||
{
|
||||
Debug.LogError("Trying to play on null Audio Source");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public float RandomVolume()
|
||||
{
|
||||
return Random.Range(randomVolumeRange.x, randomVolumeRange.y) * volume;
|
||||
}
|
||||
|
||||
public float RandomPitch()
|
||||
{
|
||||
return Random.Range(randomPitchRange.x, randomPitchRange.y);
|
||||
}
|
||||
|
||||
public AudoClipWrapper ShuffledWrapper()
|
||||
{
|
||||
if (shuffleClips == null || shuffleClips.Count != audoClips.Length)
|
||||
{
|
||||
shuffleClips = audoClips.OrderBy(c => Random.value).ToList();
|
||||
}
|
||||
if (lastIndex >= shuffleClips.Count)
|
||||
{
|
||||
lastIndex = 0;
|
||||
shuffleClips = audoClips.OrderBy(c => Random.value).ToList();
|
||||
}
|
||||
|
||||
var wrapper = shuffleClips[lastIndex];
|
||||
lastIndex++;
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
public AudoClipWrapper RandomWrapper()
|
||||
{
|
||||
var totalWeight = audoClips.Sum(c => c.Weight);
|
||||
var randomWeight = Random.Range(0, totalWeight);
|
||||
foreach (var clip in audoClips)
|
||||
{
|
||||
if (randomWeight < clip.Weight)
|
||||
{
|
||||
return clip;
|
||||
}
|
||||
randomWeight -= clip.Weight;
|
||||
}
|
||||
|
||||
Debug.LogError("Total Weight is lower than the sum of element's weights");
|
||||
return audoClips[Random.Range(0, audoClips.Length)];
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class AudoClipWrapper
|
||||
{
|
||||
[SerializeField] private AudioClip clip;
|
||||
[SerializeField, Range(0, 1)] private float volume = 1;
|
||||
[SerializeField] private int weight = 1;
|
||||
|
||||
public AudoClipWrapper(AudioClip clip)
|
||||
{
|
||||
this.clip = clip;
|
||||
}
|
||||
|
||||
public AudioClip Clip => clip;
|
||||
|
||||
public float Volume => volume;
|
||||
|
||||
public int Weight => Mathf.Max(1, weight);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Util/SoundLibrary.cs.meta
Normal file
11
Assets/Scripts/Util/SoundLibrary.cs.meta
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 7aa5acded53affd4aa559c6aa5c2fdda
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
69
Assets/Scripts/Util/SoundManager.cs
Normal file
69
Assets/Scripts/Util/SoundManager.cs
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
using Events;
|
||||
using System;
|
||||
using Unity.Entities;
|
||||
using UnityEngine;
|
||||
using Zenject;
|
||||
|
||||
public class SoundManager
|
||||
{
|
||||
[Serializable]
|
||||
public class Settings
|
||||
{
|
||||
public float DefaultSpatialBlend;
|
||||
}
|
||||
|
||||
[Inject] private readonly Settings settings;
|
||||
|
||||
public void PlayClip(EntityCommandBuffer buffer, SoundLibrary library, Vector3 point)
|
||||
{
|
||||
PlayClip(buffer, library, point, settings.DefaultSpatialBlend);
|
||||
}
|
||||
|
||||
public void PlayClip(EntityCommandBuffer buffer, SoundLibrary library, Vector3 point, float spatialBlend)
|
||||
{
|
||||
PlayClip(buffer, library, point, spatialBlend, 1);
|
||||
}
|
||||
|
||||
public void PlayClip(EntityCommandBuffer buffer, SoundLibrary library, Vector3 point, float spatialBlend, float volume)
|
||||
{
|
||||
var randomWrapper = library.RandomWrapper();
|
||||
var entity = buffer.CreateEntity();
|
||||
buffer.AddComponent(entity, new EventData());
|
||||
buffer.AddSharedComponent(
|
||||
entity,
|
||||
new SoundEvent
|
||||
{
|
||||
Clip = randomWrapper.Clip,
|
||||
Volume = library.RandomVolume() * randomWrapper.Volume * volume,
|
||||
Pitch = library.RandomPitch(),
|
||||
SpatialBlend = spatialBlend,
|
||||
Point = point
|
||||
});
|
||||
}
|
||||
|
||||
public void PlayClip(EntityManager manager, SoundLibrary library, Vector3 point)
|
||||
{
|
||||
PlayClip(manager, library, point, settings.DefaultSpatialBlend);
|
||||
}
|
||||
|
||||
public void PlayClip(EntityManager manager, SoundLibrary library, Vector3 point, float spatialBlend)
|
||||
{
|
||||
PlayClip(manager, library, point, spatialBlend, 1);
|
||||
}
|
||||
|
||||
public void PlayClip(EntityManager manager, SoundLibrary library, Vector3 point, float spatialBlend, float volume)
|
||||
{
|
||||
var randomWrapper = library.RandomWrapper();
|
||||
var e = manager.CreateEntity(ComponentType.ReadWrite<EventData>());
|
||||
manager.AddSharedComponentData(
|
||||
e,
|
||||
new SoundEvent
|
||||
{
|
||||
Clip = randomWrapper.Clip,
|
||||
Volume = library.RandomVolume() * randomWrapper.Volume * volume,
|
||||
Pitch = library.RandomPitch(),
|
||||
SpatialBlend = spatialBlend,
|
||||
Point = point
|
||||
});
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Util/SoundManager.cs.meta
Normal file
3
Assets/Scripts/Util/SoundManager.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9a1763989aa54b2294e6b26186d49832
|
||||
timeCreated: 1531311777
|
||||
39
Assets/Scripts/Util/Vector2Extension .cs
Normal file
39
Assets/Scripts/Util/Vector2Extension .cs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
using UnityEngine;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
public static class Vector2Extension
|
||||
{
|
||||
public static Vector2 Rotate(this Vector2 v, float degrees)
|
||||
{
|
||||
var sin = Mathf.Sin(degrees * Mathf.Deg2Rad);
|
||||
var cos = Mathf.Cos(degrees * Mathf.Deg2Rad);
|
||||
|
||||
var tx = v.x;
|
||||
var ty = v.y;
|
||||
v.x = cos * tx - sin * ty;
|
||||
v.y = sin * tx + cos * ty;
|
||||
return v;
|
||||
}
|
||||
|
||||
public static bool InRange(this Vector2Int pos, Vector2Int size)
|
||||
{
|
||||
return pos.x >= 0 && pos.x < size.x && pos.y >= 0 && pos.y < size.y;
|
||||
}
|
||||
|
||||
public static Vector2Int Translate(this Vector2Int pos, int x, int y)
|
||||
{
|
||||
return new Vector2Int(pos.x + x, pos.y + y);
|
||||
}
|
||||
|
||||
public static Vector2Int To2DIndex(this int value, int width)
|
||||
{
|
||||
return new Vector2Int(value % width, Mathf.FloorToInt(value / (float)width));
|
||||
}
|
||||
|
||||
public static int ToIndex(this Vector2Int value, int width)
|
||||
{
|
||||
return value.y * width + value.x;
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Util/Vector2Extension .cs.meta
Normal file
3
Assets/Scripts/Util/Vector2Extension .cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 10ef5c2746944348990b1ea8bc16bfa3
|
||||
timeCreated: 1534020675
|
||||
20
Assets/Scripts/Util/ZenjectExtensions.cs
Normal file
20
Assets/Scripts/Util/ZenjectExtensions.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
using Unity.Entities;
|
||||
using Zenject;
|
||||
|
||||
namespace Util
|
||||
{
|
||||
public static class ZenjectExtensions
|
||||
{
|
||||
public static IfNotBoundBinder FromEcs<TContract>(this ConcreteIdBinderGeneric<TContract> binder) where TContract : ComponentSystemBase
|
||||
{
|
||||
return binder.FromMethod(
|
||||
c =>
|
||||
{
|
||||
var contract = (TContract)c.Container.Resolve<World>().GetExistingSystem(typeof(TContract));
|
||||
c.Container.Inject(contract);
|
||||
return contract;
|
||||
})
|
||||
.NonLazy();
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Util/ZenjectExtensions.cs.meta
Normal file
3
Assets/Scripts/Util/ZenjectExtensions.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 360241bc87ca4e979b1fa0e0bd34cfa7
|
||||
timeCreated: 1596621538
|
||||
35
Assets/Scripts/Util/bool1.cs
Normal file
35
Assets/Scripts/Util/bool1.cs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace DefaultNamespace
|
||||
{
|
||||
[Serializable]
|
||||
public struct bool1
|
||||
{
|
||||
[SerializeField] private byte _value;
|
||||
|
||||
public bool1(bool value)
|
||||
{
|
||||
_value = (byte)(value ? 1 : 0);
|
||||
}
|
||||
|
||||
public static implicit operator bool1(bool value)
|
||||
{
|
||||
return new bool1(value);
|
||||
}
|
||||
|
||||
public static implicit operator bool(bool1 value)
|
||||
{
|
||||
return value._value != 0;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
if (_value == 0)
|
||||
{
|
||||
return "false";
|
||||
}
|
||||
return "true";
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Util/bool1.cs.meta
Normal file
3
Assets/Scripts/Util/bool1.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 8531868cc3f349778297854bc882be60
|
||||
timeCreated: 1531433194
|
||||
Loading…
Add table
Add a link
Reference in a new issue