|
| 1 | +using System.Collections; |
| 2 | +using System.Reflection; |
| 3 | +using COTL_API.Guid; |
| 4 | +using UnityEngine; |
| 5 | +using UnityEngine.AddressableAssets; |
| 6 | +using UnityEngine.ResourceManagement.AsyncOperations; |
| 7 | + |
| 8 | +namespace COTL_API.CustomEnemy; |
| 9 | + |
| 10 | +public static partial class CustomEnemyManager |
| 11 | +{ |
| 12 | + internal static Dictionary<Enemy, CustomEnemy> CustomEnemyList { get; } = []; //customEnemy Class list |
| 13 | + public static Dictionary<Enemy, GameObject> CustomEnemyPrefabList { get; } = []; //prefabs used to spawn enemies |
| 14 | + public static List<UnitObject> CustomSpawnedEnemies { get; } = []; |
| 15 | + |
| 16 | + public static Enemy Add(CustomEnemy customEnemy) |
| 17 | + { |
| 18 | + var guid = TypeManager.GetModIdFromCallstack(Assembly.GetCallingAssembly()); |
| 19 | + |
| 20 | + var innerType = GuidManager.GetEnumValue<Enemy>(guid, customEnemy.InternalName); |
| 21 | + customEnemy.enemyType = innerType; |
| 22 | + customEnemy.ModPrefix = guid; |
| 23 | + |
| 24 | + CustomEnemyList.Add(innerType, customEnemy); |
| 25 | + LogWarning($"Added: {innerType} {customEnemy.InternalName} {customEnemy.ModPrefix}"); |
| 26 | + |
| 27 | + var enemyPrefab = BuildEnemyPrefab(customEnemy); |
| 28 | + |
| 29 | + return innerType; |
| 30 | + } |
| 31 | + |
| 32 | + public static IEnumerator BuildEnemyPrefab(CustomEnemy customEnemy) |
| 33 | + { |
| 34 | + |
| 35 | + var opHandle = Addressables.LoadAssetAsync<GameObject>(customEnemy.EnemyToMimic); |
| 36 | + yield return opHandle; |
| 37 | + |
| 38 | + if (opHandle.Status == AsyncOperationStatus.Succeeded) |
| 39 | + { |
| 40 | + var loadedPrefab = opHandle.Result; |
| 41 | + LogInfo("Successfully loaded enemy prefab: " + loadedPrefab.name); |
| 42 | + |
| 43 | + //var obj = Instantiate(loadedPrefab); |
| 44 | + //obj.transform.position = PlayerFarming.Instance.transform.position; |
| 45 | + |
| 46 | + //TODO: set attributes, spine, controller, etc on the custom enemy |
| 47 | + CustomEnemyPrefabList.Add(customEnemy.enemyType, loadedPrefab); |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + LogError("Failed to load prefab at path: " + customEnemy.EnemyToMimic); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public static UnitObject? Spawn(Enemy enemyType, Vector3 position) |
| 56 | + { |
| 57 | + //check if enemyType is in CustomEnemyPrefabList |
| 58 | + if (!CustomEnemyPrefabList.ContainsKey(enemyType)) |
| 59 | + { |
| 60 | + LogWarning($"No custom enemy prefab found for {enemyType}!"); |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + var prefab = CustomEnemyPrefabList[enemyType]; |
| 65 | + var obj = UnityEngine.Object.Instantiate(prefab, position, Quaternion.identity); |
| 66 | + var unitObject = obj.GetComponent<UnitObject>(); |
| 67 | + |
| 68 | + if (unitObject != null) |
| 69 | + { |
| 70 | + CustomSpawnedEnemies.Add(unitObject); |
| 71 | + return unitObject; |
| 72 | + } |
| 73 | + LogWarning("A spawned custom enemy does not have a UnitObject component! " + prefab); |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments