Skip to content

Commit b99227f

Browse files
committed
Improved enemy spawning in small buildings
1 parent c22f854 commit b99227f

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

Assets/Scripts/Game/Questing/Actions/CreateFoe.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,24 @@ bool PlaceFoeWilderness(GameObject[] gameObjects, bool placeEnemyBehindPlayer =
258258
bool PlaceFoeFreely(GameObject[] gameObjects, Transform parent, float minDistance = 5f, float maxDistance = 20f, bool placeEnemyBehindPlayer = true, bool lineOfSight = true, int spawnTries = 15)
259259
{
260260
const float maxFloorDistance = 4f;
261+
const float overlapSphereRadius = 0.65f;
262+
263+
// override min/max distance in small interior spaces.
264+
var dfInterior = GameObject.FindObjectOfType<DaggerfallInterior>();
265+
if(dfInterior){
266+
Bounds bounds = new Bounds (dfInterior.transform.position, Vector3.one);
267+
Renderer[] renderers = dfInterior.GetComponentsInChildren<Renderer> ();
268+
foreach (Renderer renderer in renderers)
269+
{
270+
bounds.Encapsulate (renderer.bounds);
271+
}
272+
Vector3 playerPos = GameManager.Instance.PlayerController.transform.position;
273+
Vector3 boundsMin = bounds.min;
274+
Vector3 boundsMax = bounds.max;
275+
boundsMin.y = boundsMax.y = playerPos.y;
276+
maxDistance = Mathf.Max(Vector3.Distance(playerPos, boundsMin), Vector3.Distance(playerPos, boundsMax));
277+
minDistance = Mathf.Min(minDistance, maxDistance/4f);
278+
}
261279

262280
// Must have received a valid array
263281
if (gameObjects == null || gameObjects.Length == 0)
@@ -303,7 +321,8 @@ bool PlaceFoeFreely(GameObject[] gameObjects, Transform parent, float minDistanc
303321

304322
// Ensure this is open space
305323
spawnPos = floorHit.point + enemyCC.center + (enemyCC.height/2f + .5f)*Vector3.up;
306-
Collider[] colliders = Physics.OverlapCapsule(spawnPos - enemyCC.height/2f * Vector3.up, spawnPos + enemyCC.height/2f * Vector3.up, enemyCC.radius, DFULayerMasks.CorporealMask);
324+
// Collider[] colliders = Physics.OverlapCapsule(spawnPos - enemyCC.height/2f * Vector3.up, spawnPos + enemyCC.height/2f * Vector3.up, enemyCC.radius, DFULayerMasks.CorporealMask);
325+
Collider[] colliders = Physics.OverlapSphere(spawnPos, overlapSphereRadius);
307326
if (colliders.Length > 0)
308327
continue;
309328

Assets/Scripts/Game/Utility/FoeSpawner.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,24 @@ bool PlaceFoeFreely(GameObject[] gameObjects, float minDistance = 5f, float maxD
119119
{
120120
const float separationDistance = 1.25f;
121121
const float maxFloorDistance = 4f;
122+
const float overlapSphereRadius = 0.65f;
123+
124+
// override min/max distance in small interior spaces.
125+
var dfInterior = FindObjectOfType<DaggerfallInterior>();
126+
if(dfInterior){
127+
Bounds bounds = new Bounds (dfInterior.transform.position, Vector3.one);
128+
Renderer[] renderers = dfInterior.GetComponentsInChildren<Renderer> ();
129+
foreach (Renderer renderer in renderers)
130+
{
131+
bounds.Encapsulate (renderer.bounds);
132+
}
133+
Vector3 playerPos = GameManager.Instance.PlayerController.transform.position;
134+
Vector3 boundsMin = bounds.min;
135+
Vector3 boundsMax = bounds.max;
136+
boundsMin.y = boundsMax.y = playerPos.y;
137+
maxDistance = Mathf.Max(Vector3.Distance(playerPos, boundsMin), Vector3.Distance(playerPos, boundsMax));
138+
minDistance = Mathf.Min(minDistance, maxDistance/4f);
139+
}
122140

123141
// Must have received a valid array
124142
if (gameObjects == null || gameObjects.Length == 0)
@@ -159,7 +177,8 @@ bool PlaceFoeFreely(GameObject[] gameObjects, float minDistance = 5f, float maxD
159177

160178
// Ensure this is open space
161179
spawnPos = floorHit.point + enemyCC.center + (enemyCC.height/2f + .5f)*Vector3.up;
162-
Collider[] colliders = Physics.OverlapCapsule(spawnPos - enemyCC.height/2f * Vector3.up, spawnPos + enemyCC.height/2f * Vector3.up, enemyCC.radius, DFULayerMasks.CorporealMask);
180+
// Collider[] colliders = Physics.OverlapCapsule(spawnPos - enemyCC.height/2f * Vector3.up, spawnPos + enemyCC.height/2f * Vector3.up, enemyCC.radius, DFULayerMasks.CorporealMask);
181+
Collider[] colliders = Physics.OverlapSphere(spawnPos, overlapSphereRadius);
163182
if (colliders.Length > 0)
164183
continue;
165184

0 commit comments

Comments
 (0)