-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
139 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidEntities/AsteroidSpawner.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Sandbox.ModAPI; | ||
using VRageMath; | ||
|
||
namespace DynamicAsteroids.AsteroidEntities | ||
{ | ||
internal class AsteroidSpawner | ||
{ | ||
private List<AsteroidEntity> _asteroids; | ||
|
||
public void Init() | ||
{ | ||
_asteroids = new List<AsteroidEntity>(AsteroidSettings.MaxAsteroidCount); | ||
} | ||
|
||
public void Close() | ||
{ | ||
_asteroids.Clear(); | ||
} | ||
|
||
public void UpdateTick() | ||
{ | ||
Vector3D playerPosition = MyAPIGateway.Session?.Player?.GetPosition() ?? Vector3D.MaxValue; | ||
|
||
if (playerPosition == Vector3D.MaxValue || !AsteroidSettings.SpawnAsteroidsAtPoint(playerPosition)) | ||
return; | ||
|
||
foreach (var asteroid in _asteroids.ToArray()) | ||
{ | ||
if (Vector3D.DistanceSquared(asteroid.PositionComp.GetPosition(), playerPosition) > | ||
AsteroidSettings.AsteroidSpawnRadius * AsteroidSettings.AsteroidSpawnRadius * 1.1) | ||
{ | ||
_asteroids.Remove(asteroid); | ||
continue; | ||
} | ||
} | ||
|
||
int asteroidsSpawned = 0; | ||
while (_asteroids.Count < AsteroidSettings.MaxAsteroidCount && asteroidsSpawned < 10) | ||
{ | ||
_asteroids.Add(AsteroidEntity.CreateAsteroid(playerPosition + RandVector()*AsteroidSettings.AsteroidSpawnRadius, RandAsteroidSize, Vector3D.Zero)); | ||
|
||
asteroidsSpawned++; | ||
} | ||
} | ||
|
||
private Vector3D RandVector() | ||
{ | ||
var theta = MainSession.I.Rand.NextDouble() * 2.0 * Math.PI; | ||
var phi = Math.Acos(2.0 * MainSession.I.Rand.NextDouble() - 1.0); | ||
var sinPhi = Math.Sin(phi); | ||
return Math.Pow(MainSession.I.Rand.NextDouble(), 1/3d) * new Vector3D(sinPhi * Math.Cos(theta), sinPhi * Math.Sin(theta), Math.Cos(phi)); | ||
} | ||
|
||
private float RandAsteroidSize => (float) (MainSession.I.Rand.NextDouble()*MainSession.I.Rand.NextDouble()*MainSession.I.Rand.NextDouble()*500) + 1.5f; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using VRageMath; | ||
|
||
namespace DynamicAsteroids | ||
{ | ||
internal static class AsteroidSettings | ||
{ | ||
public static int MaxAsteroidCount = 500; | ||
public static int AsteroidSpawnRadius = 10000; | ||
|
||
public static SpawnableArea[] ValidSpawnLocations = | ||
{ | ||
new SpawnableArea | ||
{ | ||
CenterPosition = new Vector3D(148001024.50, 1024.50, 1024.50), | ||
Normal = new Vector3D(1, 10, 0.5).Normalized(), | ||
Radius = 60268000 * 2.5, | ||
InnerRadius = 60268000 * 1.2, | ||
HeightFromCenter = 25000, | ||
} | ||
}; | ||
|
||
public static bool SpawnAsteroidsAtPoint(Vector3D point) | ||
{ | ||
foreach (var area in ValidSpawnLocations) | ||
if (area.ContainsPoint(point)) | ||
return true; | ||
return false; | ||
} | ||
} | ||
|
||
internal class SpawnableArea | ||
{ | ||
public Vector3D CenterPosition; | ||
public Vector3D Normal; | ||
public double Radius; | ||
public double InnerRadius; | ||
public double HeightFromCenter; | ||
|
||
public bool ContainsPoint(Vector3D point) | ||
{ | ||
point -= CenterPosition; | ||
double pointDistanceSq = point.LengthSquared(); | ||
|
||
if (pointDistanceSq > Radius * Radius || pointDistanceSq < InnerRadius * InnerRadius) | ||
return false; | ||
|
||
// TODO: Normal and HeightFromCenter | ||
|
||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters