From 3cbb67db7b798b669bc595f690446ca25fceb939 Mon Sep 17 00:00:00 2001 From: Aristeas <94058548+Jnick-24@users.noreply.github.com> Date: Thu, 6 Jun 2024 22:34:09 -0500 Subject: [PATCH] movestroids --- .../AsteroidEntities/AsteroidSpawner.cs | 9 +++-- .../DynamicAsteroids/AsteroidSettings.cs | 35 ++++++++++++++++--- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidEntities/AsteroidSpawner.cs b/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidEntities/AsteroidSpawner.cs index 54bfc633..6641351d 100644 --- a/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidEntities/AsteroidSpawner.cs +++ b/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidEntities/AsteroidSpawner.cs @@ -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()) @@ -32,6 +32,7 @@ public void UpdateTick() AsteroidSettings.AsteroidSpawnRadius * AsteroidSettings.AsteroidSpawnRadius * 1.1) { _asteroids.Remove(asteroid); + asteroid.Close(); continue; } } @@ -39,7 +40,11 @@ public void UpdateTick() 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++; } diff --git a/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidSettings.cs b/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidSettings.cs index 141aeb93..56d883f6 100644 --- a/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidSettings.cs +++ b/Dynamic Asteroids/Data/Scripts/DynamicAsteroids/AsteroidSettings.cs @@ -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 = { @@ -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)) @@ -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; + } } }