Skip to content

Commit

Permalink
Fix bug with blob prefab
Browse files Browse the repository at this point in the history
  • Loading branch information
Feiryn committed Sep 16, 2024
1 parent 3ea21e7 commit ec5b475
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 54 deletions.
101 changes: 55 additions & 46 deletions LethalMon/Behaviours/BlobTamedBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,62 +73,71 @@ internal static void AddPhysicsSectionToPrefab()
{
if (PhysicsRegionAdded) return;

var enemyType = Utils.GetEnemyType(Utils.Enemy.Blob);
if (enemyType == null)
var enemyTypes = Utils.GetEnemyTypes(Utils.Enemy.Blob);
if (enemyTypes.Length == 0)
{
LethalMon.Log("Unable to get blob prefab.", LethalMon.LogType.Error);
LethalMon.Log("Unable to get blob types.", LethalMon.LogType.Error);
return;
}

LethalMon.Log("Tamed Blob: CreatePhysicsRegion");

// Root object
foreach (var enemyType in enemyTypes)
{
if (enemyType.enemyPrefab == null)
{
LethalMon.Log("Unable to get blob prefab.", LethalMon.LogType.Error);
continue;
}

// Root object
#if _DEBUG
GameObject rootObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
GameObject rootObject = GameObject.CreatePrimitive(PrimitiveType.Cube);

if (rootObject.TryGetComponent(out MeshRenderer meshRenderer))
{
meshRenderer.material = new Material(Shader.Find("HDRP/Lit")) { color = Color.red };
meshRenderer.enabled = true;
}
rootObject.name = PhysicsObjectName;
var rootCollider = rootObject.GetComponent<BoxCollider>();
if (rootObject.TryGetComponent(out MeshRenderer meshRenderer))
{
meshRenderer.material = new Material(Shader.Find("HDRP/Lit")) { color = Color.red };
meshRenderer.enabled = true;
}
rootObject.name = PhysicsObjectName;
var rootCollider = rootObject.GetComponent<BoxCollider>();
#else
var rootObject = new GameObject(PhysicsObjectName);
var rootCollider = rootObject.AddComponent<BoxCollider>();
var rootObject = new GameObject(PhysicsObjectName);
var rootCollider = rootObject.AddComponent<BoxCollider>();
#endif
rootObject.layer = (int)Utils.LayerMasks.Mask.Room;
rootObject.transform.SetParent(enemyType.enemyPrefab.transform, false);
rootCollider.isTrigger = false;

// ItemDropCollider
var itemDropColliderObject = new GameObject("ItemDropCollider");
itemDropColliderObject.layer = (int)Utils.LayerMasks.Mask.Triggers;
var itemDropCollider = itemDropColliderObject.AddComponent<BoxCollider>();
itemDropCollider.isTrigger = true;
itemDropColliderObject.transform.localScale = new(1f, 1.5f, 1f);
itemDropColliderObject.transform.SetParent(rootObject.transform, false);

var physicsRegionObject = new GameObject("PlayerPhysicsRegion");
physicsRegionObject.layer = (int)Utils.LayerMasks.Mask.Triggers;
var physicsCollider = physicsRegionObject.AddComponent<BoxCollider>();
physicsCollider.isTrigger = true;
physicsRegionObject.transform.localScale = new(1f, 1.5f, 1f);
physicsRegionObject.transform.SetParent(rootObject.transform, false);

var physicsRegion = physicsRegionObject.AddComponent<PlayerPhysicsRegion>();
physicsRegion.parentNetworkObject = enemyType.enemyPrefab.GetComponent<NetworkObject>();
physicsRegion.physicsTransform = enemyType.enemyPrefab.gameObject.transform;
physicsRegion.allowDroppingItems = true;
physicsRegion.itemDropCollider = itemDropCollider;
physicsRegion.physicsCollider = physicsCollider;
physicsRegion.disablePhysicsRegion = false;
physicsRegion.priority = 1;
physicsRegion.maxTippingAngle = 360;

rootObject.SetActive(false);

PhysicsRegionAdded = true;
rootObject.layer = (int)Utils.LayerMasks.Mask.Room;
rootObject.transform.SetParent(enemyType.enemyPrefab.transform, false);
rootCollider.isTrigger = false;

// ItemDropCollider
var itemDropColliderObject = new GameObject("ItemDropCollider");
itemDropColliderObject.layer = (int)Utils.LayerMasks.Mask.Triggers;
var itemDropCollider = itemDropColliderObject.AddComponent<BoxCollider>();
itemDropCollider.isTrigger = true;
itemDropColliderObject.transform.localScale = new(1f, 1.5f, 1f);
itemDropColliderObject.transform.SetParent(rootObject.transform, false);

var physicsRegionObject = new GameObject("PlayerPhysicsRegion");
physicsRegionObject.layer = (int)Utils.LayerMasks.Mask.Triggers;
var physicsCollider = physicsRegionObject.AddComponent<BoxCollider>();
physicsCollider.isTrigger = true;
physicsRegionObject.transform.localScale = new(1f, 1.5f, 1f);
physicsRegionObject.transform.SetParent(rootObject.transform, false);

var physicsRegion = physicsRegionObject.AddComponent<PlayerPhysicsRegion>();
physicsRegion.parentNetworkObject = enemyType.enemyPrefab.GetComponent<NetworkObject>();
physicsRegion.physicsTransform = enemyType.enemyPrefab.gameObject.transform;
physicsRegion.allowDroppingItems = true;
physicsRegion.itemDropCollider = itemDropCollider;
physicsRegion.physicsCollider = physicsCollider;
physicsRegion.disablePhysicsRegion = false;
physicsRegion.priority = 1;
physicsRegion.maxTippingAngle = 360;

rootObject.SetActive(false);

PhysicsRegionAdded = true;
}
}

internal override void OnEscapedFromBall(PlayerControllerB playerWhoThrewBall)
Expand Down
10 changes: 2 additions & 8 deletions LethalMon/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,10 @@ public static bool IsHost
#region Enemy
public static List<EnemyType> EnemyTypes => Resources.FindObjectsOfTypeAll<EnemyType>().ToList();

public static EnemyType? GetEnemyType(Enemy enemy)
public static EnemyType[] GetEnemyTypes(Enemy enemy)
{
var enemyName = enemy.ToString();
foreach(var enemyType in EnemyTypes)
{
if (enemyType.name == enemyName)
return enemyType;
}

return null;
return EnemyTypes.Where(e => e.name == enemyName).ToArray();
}

public static void OpenDoorsAsEnemyAroundPosition(Vector3 position)
Expand Down

0 comments on commit ec5b475

Please sign in to comment.