Skip to content

Commit

Permalink
command to add roid areas
Browse files Browse the repository at this point in the history
  • Loading branch information
InvalidArgument3 committed Jun 18, 2024
1 parent bfa63c1 commit d1f2f99
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Sandbox.ModAPI;
using VRageMath;
using System.Collections.Generic;
using System.Linq;

namespace DynamicAsteroids
{
Expand Down Expand Up @@ -445,6 +446,28 @@ private static int[] ReadIntArray(string value)
}
return array;
}

public static void AddSpawnableArea(string name, Vector3D center, double radius)
{
ValidSpawnLocations.Add(new SpawnableArea
{
Name = name,
CenterPosition = center,
Radius = radius
});
SaveSettings();
}

public static void RemoveSpawnableArea(string name)
{
var area = ValidSpawnLocations.FirstOrDefault(a => a.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
if (area != null)
{
ValidSpawnLocations.Remove(area);
SaveSettings();
}
}

}

public class SpawnableArea
Expand All @@ -463,5 +486,6 @@ public Vector3D VelocityAtPoint(Vector3D point)
{
return (point - CenterPosition).Normalized() * AsteroidSettings.AsteroidVelocityBase;
}

}
}
51 changes: 51 additions & 0 deletions Dynamic Asteroids/Data/Scripts/DynamicAsteroids/MainSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public override void LoadData()
}

MyAPIGateway.Multiplayer.RegisterMessageHandler(32000, OnMessageReceived);
MyAPIGateway.Utilities.MessageEntered += OnMessageEntered;
}
catch (Exception ex)
{
Expand All @@ -66,6 +67,7 @@ protected override void UnloadData()
AsteroidSettings.SaveSettings(); // Save settings to the config file

MyAPIGateway.Multiplayer.UnregisterMessageHandler(32000, OnMessageReceived);
MyAPIGateway.Utilities.MessageEntered -= OnMessageEntered;
}
catch (Exception ex)
{
Expand All @@ -76,6 +78,55 @@ protected override void UnloadData()
I = null;
}

private void OnMessageEntered(string messageText, ref bool sendToOthers)
{
if (messageText.StartsWith("/dynamicasteroids") || messageText.StartsWith("/dn"))
{
var args = messageText.Split(' ');
if (args.Length > 1)
{
switch (args[1].ToLower())
{
case "createspawnarea":
double radius;
if (args.Length == 3 && double.TryParse(args[2], out radius))
{
CreateSpawnArea(radius);
sendToOthers = false;
}
break;

case "removespawnarea":
if (args.Length == 3)
{
RemoveSpawnArea(args[2]);
sendToOthers = false;
}
break;
}
}
}
}

private void CreateSpawnArea(double radius)
{
var player = MyAPIGateway.Session.Player;
if (player == null) return;

var position = player.GetPosition();
var name = $"Area_{position.GetHashCode()}";
AsteroidSettings.AddSpawnableArea(name, position, radius);
Log.Info($"Created spawn area '{name}' at {position} with radius {radius}");
MyAPIGateway.Utilities.ShowMessage("DynamicAsteroids", $"Created spawn area '{name}' at {position} with radius {radius}");
}

private void RemoveSpawnArea(string name)
{
AsteroidSettings.RemoveSpawnableArea(name);
Log.Info($"Removed spawn area '{name}'");
MyAPIGateway.Utilities.ShowMessage("DynamicAsteroids", $"Removed spawn area '{name}'");
}

public override void UpdateAfterSimulation()
{
try
Expand Down

0 comments on commit d1f2f99

Please sign in to comment.