45 lines
1.3 KiB
C#
45 lines
1.3 KiB
C#
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
|
|
namespace DDD
|
|
{
|
|
public interface ICustomerFactory
|
|
{
|
|
Task<GameObject> CreateAsync(CustomerSpawnArgs args);
|
|
}
|
|
|
|
public interface ICustomerInitializer
|
|
{
|
|
void Initialize(string customerDataId, CustomerData customerData);
|
|
}
|
|
|
|
public struct CustomerSpawnArgs
|
|
{
|
|
public string CustomerDataId;
|
|
public CustomerData CustomerData;
|
|
public Vector3 Position;
|
|
public Quaternion Rotation;
|
|
public Transform Parent;
|
|
}
|
|
|
|
public class CustomerFactory : ICustomerFactory
|
|
{
|
|
private GameObject _customerPrefab;
|
|
|
|
public async Task<GameObject> CreateAsync(CustomerSpawnArgs args)
|
|
{
|
|
if (!_customerPrefab)
|
|
{
|
|
_customerPrefab = await AssetManager.LoadAsset<GameObject>(DataConstants.CustomerNpcPrefab);
|
|
}
|
|
|
|
var newCustomer = Object.Instantiate(_customerPrefab, args.Position, args.Rotation, args.Parent);
|
|
|
|
if (newCustomer.TryGetComponent(out ICustomerInitializer initializer))
|
|
{
|
|
initializer.Initialize(args.CustomerDataId, args.CustomerData);
|
|
}
|
|
return newCustomer;
|
|
}
|
|
}
|
|
} |