Skip to content

Commit

Permalink
attempt 2
Browse files Browse the repository at this point in the history
  • Loading branch information
InvalidArgument3 committed Jun 14, 2024
1 parent e0d577c commit 0aceeb7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,30 @@ public static AsteroidEntity CreateAsteroid(Vector3D position, float size, Vecto
var ent = new AsteroidEntity();
Log.Info($"Creating AsteroidEntity at Position: {position}, Size: {size}, InitialVelocity: {initialVelocity}, Type: {type}");

if (entityId.HasValue)
// Server-side: Generate a unique ID if none is provided
if (MyAPIGateway.Session.IsServer)
{
ent.EntityId = entityId.Value;
if (entityId.HasValue)
{
ent.EntityId = entityId.Value;
}
else
{
ent.EntityId = MyEntityIdentifier.AllocateId(); // Generate a unique ID
}
}
else
{
// Client-side: Use the provided ID
if (entityId.HasValue)
{
ent.EntityId = entityId.Value;
}
else
{
Log.Info("Client did not receive a valid entity ID. Skipping asteroid creation.");
return null;
}
}

try
Expand Down
42 changes: 12 additions & 30 deletions Dynamic Asteroids/Data/Scripts/DynamicAsteroids/MainSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,34 +150,15 @@ private void OnMessageReceived(byte[] message)
var asteroidMessage = MyAPIGateway.Utilities.SerializeFromBinary<AsteroidNetworkMessage>(message);
Log.Info($"Client: Deserialized asteroid message");

Log.Info($"Client: Received message to create/remove asteroid:");
Log.Info($"Position: {asteroidMessage.GetPosition()}");
Log.Info($"Size: {asteroidMessage.Size}");
Log.Info($"InitialVelocity: {asteroidMessage.GetVelocity()}");
Log.Info($"AngularVelocity: {asteroidMessage.GetAngularVelocity()}");
Log.Info($"Type: {asteroidMessage.GetType()}");
Log.Info($"IsSubChunk: {asteroidMessage.IsSubChunk}");
Log.Info($"EntityId: {asteroidMessage.EntityId}");
Log.Info($"IsRemoval: {asteroidMessage.IsRemoval}");
Log.Info($"IsInitialCreation: {asteroidMessage.IsInitialCreation}");
Log.Info($"Rotation: {asteroidMessage.GetRotation()}");

// Check if asteroid already exists
var existingAsteroid = MyEntities.GetEntityById(asteroidMessage.EntityId) as AsteroidEntity;
if (existingAsteroid != null)
{
Log.Info($"Client: Asteroid with ID {asteroidMessage.EntityId} already exists. Skipping creation.");
return;
}

// Handle removal of asteroids
if (asteroidMessage.IsRemoval)
{
var asteroid = MyEntities.GetEntityById(asteroidMessage.EntityId) as AsteroidEntity;
if (asteroid != null)
{
Log.Info($"Client: Removing asteroid with ID {asteroidMessage.EntityId}");
MyEntities.Remove(asteroid);
asteroid.Close();
MyEntities.Remove(asteroid); // Ensure the entity is removed from MyEntities
Log.Info($"Client: Removed asteroid with ID {asteroidMessage.EntityId}");
}
else
Expand All @@ -199,9 +180,12 @@ private void OnMessageReceived(byte[] message)
asteroidMessage.GetType(),
asteroidMessage.GetRotation(),
asteroidMessage.EntityId);
asteroid.Physics.AngularVelocity = asteroidMessage.GetAngularVelocity();
MyEntities.Add(asteroid);
Log.Info($"Client: Created initial asteroid with ID {asteroid.EntityId}");
if (asteroid != null)
{
asteroid.Physics.AngularVelocity = asteroidMessage.GetAngularVelocity();
MyEntities.Add(asteroid);
Log.Info($"Client: Created initial asteroid with ID {asteroid.EntityId}");
}
}
else
{
Expand All @@ -213,14 +197,12 @@ private void OnMessageReceived(byte[] message)
asteroidMessage.GetType(),
asteroidMessage.GetRotation(),
asteroidMessage.EntityId);
if (asteroid == null)
if (asteroid != null)
{
Log.Info("Failed to create asteroid, skipping");
return;
asteroid.Physics.AngularVelocity = asteroidMessage.GetAngularVelocity();
MyEntities.Add(asteroid);
Log.Info($"Client: Created asteroid with ID {asteroid.EntityId}");
}
asteroid.Physics.AngularVelocity = asteroidMessage.GetAngularVelocity();
MyEntities.Add(asteroid);
Log.Info($"Client: Created asteroid with ID {asteroid.EntityId}");
}
}
catch (Exception ex)
Expand Down

0 comments on commit 0aceeb7

Please sign in to comment.