Skip to content

Commit

Permalink
Added Remove object method to star system, to add the child objects o…
Browse files Browse the repository at this point in the history
…f the deleted object to its parent
  • Loading branch information
8vogt committed Feb 20, 2021
1 parent 9879d44 commit 747a960
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
30 changes: 12 additions & 18 deletions SEWorldGenPlugin/Generator/MyStarSystemGenerator.Networking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,29 +207,23 @@ private static void SendRemoveSystemObjectServer(Guid objectId, ulong callbackId
MySystemObject o = Static.StarSystem.GetObjectById(objectId);
if (o != null && o.DisplayName != Static.StarSystem.CenterObject.DisplayName)
{
MySystemObject parent = Static.StarSystem.GetObjectById(o.ParentId);
if (parent != null)
if(o.Type == MySystemObjectType.ASTEROIDS)
{
if(o.Type == MySystemObjectType.ASTEROIDS)
var asteroids = o as MySystemAsteroids;
if (MyAsteroidObjectsManager.Static.AsteroidObjectProviders.ContainsKey(asteroids.AsteroidTypeName))
{
var asteroids = o as MySystemAsteroids;
if (MyAsteroidObjectsManager.Static.AsteroidObjectProviders.ContainsKey(asteroids.AsteroidTypeName))
bool removed = MyAsteroidObjectsManager.Static.AsteroidObjectProviders[asteroids.AsteroidTypeName].RemoveInstance(asteroids);
if (removed)
{
bool removed = MyAsteroidObjectsManager.Static.AsteroidObjectProviders[asteroids.AsteroidTypeName].RemoveInstance(asteroids);
if (removed)
{
parent.ChildObjects.Remove(o);
PluginEventHandler.Static.RaiseStaticEvent(SendSimpleActionCallbackClient, true, callbackId, clientId);
return;
}
PluginEventHandler.Static.RaiseStaticEvent(SendSimpleActionCallbackClient, Static.StarSystem.RemoveObject(objectId), callbackId, clientId);
return;
}
}
else
{
parent.ChildObjects.Remove(o);
PluginEventHandler.Static.RaiseStaticEvent(SendSimpleActionCallbackClient, true, callbackId, clientId);
return;
}
}
else
{
PluginEventHandler.Static.RaiseStaticEvent(SendSimpleActionCallbackClient, Static.StarSystem.RemoveObject(objectId), callbackId, clientId);
return;
}
}
PluginEventHandler.Static.RaiseStaticEvent(SendSimpleActionCallbackClient, false, callbackId, clientId);
Expand Down
10 changes: 9 additions & 1 deletion SEWorldGenPlugin/Generator/MyStarSystemGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public override void LoadData()

StarSystem = data;

MyPluginLog.Log("Loaded system data. Checking Asteroid objects");

if(StarSystem != null && StarSystem.CenterObject != null)
{
foreach (var obj in StarSystem.GetAllObjects())
Expand All @@ -111,11 +113,17 @@ public override void LoadData()
var asteroid = obj as MySystemAsteroids;

var provider = MyAsteroidObjectsManager.Static.AsteroidObjectProviders[asteroid.AsteroidTypeName];
provider.TryLoadObject(asteroid);
if (!provider.TryLoadObject(asteroid))
{
MyPluginLog.Log("No data found associated with asteroid object " + asteroid.DisplayName + " (" + asteroid.Id + "), Removing it.", LogLevel.WARNING);
StarSystem.RemoveObject(asteroid.Id);
}
}
}
}

MyPluginLog.Log("All asteroid objects checked");

m_planets = new List<MyPlanetGeneratorDefinition>();
m_moons = new List<MyPlanetGeneratorDefinition>();
m_suns = new List<MyPlanetGeneratorDefinition>();
Expand Down
25 changes: 25 additions & 0 deletions SEWorldGenPlugin/ObjectBuilders/MyObjectBuilder_SystemData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,31 @@ public bool ObjectExists(Guid id)
{
return GetObjectById(id) != null;
}

/// <summary>
/// Removes the object with the given id. Cant be the center object
/// </summary>
/// <param name="id">Id of the object</param>
/// <returns>True, if object was successfully removed.</returns>
public bool RemoveObject(Guid id)
{
if (id == CenterObject.Id) return false;
if (!ObjectExists(id)) return false;

var obj = GetObjectById(id);
var parent = GetObjectById(obj.ParentId);

if (parent == null) return false;

foreach(var child in obj.GetAllChildren())
{
parent.ChildObjects.Add(child);
obj.ChildObjects.Remove(child);
}
parent.ChildObjects.Remove(obj);

return true;
}
}

/// <summary>
Expand Down

0 comments on commit 747a960

Please sign in to comment.