Skip to content

Commit ab8f6bc

Browse files
Custom Enemy part 1
1 parent 1055c83 commit ab8f6bc

4 files changed

Lines changed: 110 additions & 5 deletions

File tree

COTL_API/COTL_API.csproj

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@
1313
<PackageIcon>icon.png</PackageIcon>
1414
</PropertyGroup>
1515

16-
<Import Project="../COTL_API.Common.props"/>
16+
<Import Project="../COTL_API.Common.props" />
1717

1818
<ItemGroup>
19-
<PackageReference Include="CultOfTheLamb.GameLibs" Version="1.5.15.979-r.0"/>
19+
<PackageReference Include="CultOfTheLamb.GameLibs" Version="1.5.15.979-r.0" />
2020
<Reference Include="UnifyLibrary">
2121
<HintPath>..\lib\UnifyLibrary.dll</HintPath>
2222
</Reference>
23-
<None Include="../README.md" Pack="true" PackagePath="/"/>
24-
<None Include="../LICENSE" Pack="true" PackagePath="/"/>
25-
<None Include="../icon.png" Pack="true" PackagePath="/"/>
23+
<None Include="../README.md" Pack="true" PackagePath="/" />
24+
<None Include="../LICENSE" Pack="true" PackagePath="/" />
25+
<None Include="../icon.png" Pack="true" PackagePath="/" />
26+
</ItemGroup>
27+
28+
<ItemGroup>
29+
<Folder Include="CustomEnemy\" />
2630
</ItemGroup>
2731
</Project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using Spine.Unity;
3+
4+
namespace COTL_API.CustomEnemy;
5+
6+
public abstract class CustomEnemy
7+
{
8+
internal string ModPrefix = "";
9+
public abstract string InternalName { get; }
10+
11+
public Enemy enemyType = Enemy.None;
12+
13+
public virtual string EnemyToMimic => "Assets/Prefabs/Enemies/All/Enemy Forest Archer Tennis.prefab";
14+
15+
public virtual SkeletonDataAsset? SpineOverride => null;
16+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System;
2+
3+
namespace COTL_API.CustomEnemy;
4+
5+
public class CustomEnemyController
6+
{
7+
//TODO: this is the AI for the enemy, to do things like attacking, phase changes, priorities, etc
8+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

Comments
 (0)