Skip to content

Commit

Permalink
movestroids
Browse files Browse the repository at this point in the history
  • Loading branch information
ari-steas committed Jun 7, 2024
1 parent ecf18aa commit 3cbb67d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void UpdateTick()
{
Vector3D playerPosition = MyAPIGateway.Session?.Player?.GetPosition() ?? Vector3D.MaxValue;

if (playerPosition == Vector3D.MaxValue || !AsteroidSettings.SpawnAsteroidsAtPoint(playerPosition))
if (playerPosition == Vector3D.MaxValue || !AsteroidSettings.PlayerCanSeeRings(playerPosition))
return;

foreach (var asteroid in _asteroids.ToArray())
Expand All @@ -32,14 +32,19 @@ public void UpdateTick()
AsteroidSettings.AsteroidSpawnRadius * AsteroidSettings.AsteroidSpawnRadius * 1.1)
{
_asteroids.Remove(asteroid);
asteroid.Close();
continue;
}
}

int asteroidsSpawned = 0;
while (_asteroids.Count < AsteroidSettings.MaxAsteroidCount && asteroidsSpawned < 10)
{
_asteroids.Add(AsteroidEntity.CreateAsteroid(playerPosition + RandVector()*AsteroidSettings.AsteroidSpawnRadius, RandAsteroidSize, Vector3D.Zero));
Vector3D newPosition = playerPosition + RandVector()*AsteroidSettings.AsteroidSpawnRadius;
Vector3D newVelocity;
if (!AsteroidSettings.CanSpawnAsteroidAtPoint(newPosition, out newVelocity))
continue;
_asteroids.Add(AsteroidEntity.CreateAsteroid(newPosition, RandAsteroidSize, newVelocity));

asteroidsSpawned++;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using VRageMath;
using System;
using VRageMath;

namespace DynamicAsteroids
{
internal static class AsteroidSettings
{
public static int MaxAsteroidCount = 500;
public static int MaxAsteroidCount = 1000;
public static int AsteroidSpawnRadius = 10000;
public static int AsteroidVelocityBase = 80;

public static SpawnableArea[] ValidSpawnLocations =
{
Expand All @@ -15,11 +17,26 @@ internal static class AsteroidSettings
Normal = new Vector3D(1, 10, 0.5).Normalized(),
Radius = 60268000 * 2.5,
InnerRadius = 60268000 * 1.2,
HeightFromCenter = 25000,
HeightFromCenter = 1000,
}
};

public static bool SpawnAsteroidsAtPoint(Vector3D point)
public static bool CanSpawnAsteroidAtPoint(Vector3D point, out Vector3D velocity)
{
foreach (var area in ValidSpawnLocations)
{
if (area.ContainsPoint(point))
{
velocity = area.VelocityAtPoint(point);
return true;
}
}

velocity = Vector3D.Zero;
return false;
}

public static bool PlayerCanSeeRings(Vector3D point)
{
foreach (var area in ValidSpawnLocations)
if (area.ContainsPoint(point))
Expand All @@ -41,12 +58,20 @@ public bool ContainsPoint(Vector3D point)
point -= CenterPosition;
double pointDistanceSq = point.LengthSquared();

// squared is more performant
if (pointDistanceSq > Radius * Radius || pointDistanceSq < InnerRadius * InnerRadius)
return false;

// TODO: Normal and HeightFromCenter
// Calculate HeightFromCenter
if (Math.Abs(Vector3D.Dot(point, Normal)) > HeightFromCenter)
return false;

return true;
}

public Vector3D VelocityAtPoint(Vector3D point)
{
return -(point - CenterPosition).Cross(Normal).Normalized() * AsteroidSettings.AsteroidVelocityBase;
}
}
}

0 comments on commit 3cbb67d

Please sign in to comment.