104 lines
3.3 KiB
C#
104 lines
3.3 KiB
C#
using System;
|
|
using AibisDream.Framework;
|
|
using AibisDream.Kit;
|
|
using AibisDream.Utility;
|
|
using UnityEngine;
|
|
using Object = UnityEngine.Object;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class MobileObjectFactory
|
|
{
|
|
private readonly Transform _spawner;
|
|
|
|
private readonly SimpleObjectPool<AbsMobileObject> _animaPool;
|
|
private readonly SimpleObjectPool<AbsMobileObject> _spritePool;
|
|
private readonly SimpleObjectPool<AbsMobileObject> _objectPool;
|
|
|
|
public MobileObjectFactory(Transform spawner)
|
|
{
|
|
_spawner = spawner;
|
|
|
|
var anima = ResourceKit.LoadAssetSync<GameObject>(ConstRef.MobileAnima);
|
|
_animaPool =
|
|
new SimpleObjectPool<AbsMobileObject>(() => InstantiateObject(anima, spawner), ResetObject);
|
|
|
|
var sprite = ResourceKit.LoadAssetSync<GameObject>(ConstRef.MobileSprite);
|
|
_spritePool =
|
|
new SimpleObjectPool<AbsMobileObject>(() => InstantiateObject(sprite, spawner), ResetObject);
|
|
|
|
var objectPrefab = ResourceKit.LoadAssetSync<GameObject>(ConstRef.MobileObject);
|
|
_objectPool =
|
|
new SimpleObjectPool<AbsMobileObject>(() => InstantiateObject(objectPrefab, spawner), ResetObject);
|
|
}
|
|
|
|
public void AddRecycleMobiles(AbsMobileObject[] unRecycledObjects)
|
|
{
|
|
foreach (var mobileObject in unRecycledObjects)
|
|
{
|
|
mobileObject.OnDisposed += mobileObject switch
|
|
{
|
|
AnimaMobileObject => o => _animaPool.Recycle(o),
|
|
SpriteMobileObject => o => _spritePool.Recycle(o),
|
|
_ => o => _objectPool.Recycle(o)
|
|
};
|
|
}
|
|
}
|
|
|
|
private AbsMobileObject InstantiateObject(GameObject prefab, Transform spawner)
|
|
{
|
|
var obj = Object.Instantiate(prefab, spawner).transform;
|
|
obj.transform.position = spawner.position;
|
|
return obj.GetComponent<AbsMobileObject>();
|
|
}
|
|
|
|
private void ResetObject(AbsMobileObject mobileObject)
|
|
{
|
|
mobileObject.transform.position = _spawner.position;
|
|
}
|
|
|
|
public AbsMobileObject CreateMobileObject(MobileObjectData data)
|
|
{
|
|
var pool = data.mobileType switch
|
|
{
|
|
MobileType.Anima => _animaPool,
|
|
MobileType.Sprite => _spritePool,
|
|
MobileType.Object => _objectPool,
|
|
_ => throw new ArgumentOutOfRangeException()
|
|
};
|
|
|
|
var res = pool.Allocate();
|
|
res.Init(data);
|
|
res.OnDisposed += o => pool.Recycle(o);
|
|
return res;
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public struct MobileObjectData
|
|
{
|
|
public string itemName;
|
|
public Vector2 direction;
|
|
public Vector2 speed;
|
|
public MobileType mobileType;
|
|
public string[] assetAddressArray;
|
|
public Vector2 offset;
|
|
[CustomSortingLayer] public int sortingLayerId;
|
|
public int sortingOrder;
|
|
[HideInInspector] public bool isDistanceMode;
|
|
[HideInInspector] public float existTime;
|
|
[HideInInspector] public float existDistance;
|
|
|
|
public int weight;
|
|
|
|
public bool flipX;
|
|
public bool flipY;
|
|
}
|
|
|
|
public enum MobileType
|
|
{
|
|
Anima,
|
|
Sprite,
|
|
Object
|
|
}
|
|
} |