Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
InvalidArgument3 committed Jun 10, 2024
1 parent d4b2f1c commit eaa4b7c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void SplitAsteroid()
}

// Send a removal message before closing
var removalMessage1 = new AsteroidNetworkMessage(PositionComp.GetPosition(), Size, Vector3D.Zero, Vector3D.Zero, Type, false, EntityId, true);
var removalMessage1 = new AsteroidNetworkMessage(PositionComp.GetPosition(), Size, Vector3D.Zero, Vector3D.Zero, Type, false, EntityId, true, false);
var removalMessageBytes1 = MyAPIGateway.Utilities.SerializeToBinary(removalMessage1);
MyAPIGateway.Multiplayer.SendMessageToOthers(1337, removalMessageBytes1);

Expand All @@ -124,13 +124,13 @@ public void SplitAsteroid()
subChunk.Physics.AngularVelocity = newAngularVelocity;

// Send a network message to clients
var message = new AsteroidNetworkMessage(newPos, newSize, newVelocity, newAngularVelocity, Type, true, subChunk.EntityId, false);
var message = new AsteroidNetworkMessage(newPos, newSize, newVelocity, newAngularVelocity, Type, true, subChunk.EntityId, false, true);
var messageBytes = MyAPIGateway.Utilities.SerializeToBinary(message);
MyAPIGateway.Multiplayer.SendMessageToOthers(1337, messageBytes);
}

// Send a removal message before closing
var removalMessage2 = new AsteroidNetworkMessage(PositionComp.GetPosition(), Size, Vector3D.Zero, Vector3D.Zero, Type, false, EntityId, true);
var removalMessage2 = new AsteroidNetworkMessage(PositionComp.GetPosition(), Size, Vector3D.Zero, Vector3D.Zero, Type, false, EntityId, true, false);
var removalMessageBytes2 = MyAPIGateway.Utilities.SerializeToBinary(removalMessage2);
MyAPIGateway.Multiplayer.SendMessageToOthers(1337, removalMessageBytes2);

Expand Down Expand Up @@ -225,6 +225,16 @@ private void Init(Vector3D position, float size, Vector3D initialVelocity, Aster
Physics.AngularVelocity = RandVector() * AsteroidSettings.GetRandomAngularVelocity(MainSession.I.Rand); // Set initial angular velocity

Log.Info($"Asteroid model {ModelString} loaded successfully with initial angular velocity: {Physics.AngularVelocity}");

// Ensure the entity is added to the physics world
if (MyAPIGateway.Session.IsServer)
{
SyncFlag = true;
}
else
{
CreatePhysics();
}
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,23 @@ public void Close()
Log.Info("Closing AsteroidSpawner");
_asteroids?.Clear();
}

public void UpdateTick()
{
if (!MyAPIGateway.Session.IsServer)
return;

try
{
//Log.Info("UpdateTick called in AsteroidSpawner");

// Get all players on the server
List<IMyPlayer> players = new List<IMyPlayer>();
MyAPIGateway.Players.GetPlayers(players);

foreach (var player in players)
{
Vector3D playerPosition = player.GetPosition();
//Log.Info($"Player {player.DisplayName} position: {playerPosition}");

if (!AsteroidSettings.PlayerCanSeeRings(playerPosition))
{
//Log.Info("Player cannot see rings");
continue;
}

Expand All @@ -65,7 +60,7 @@ public void UpdateTick()
_asteroids.Remove(asteroid);

// Send a network message to clients for removal
var removalMessage = new AsteroidNetworkMessage(asteroid.PositionComp.GetPosition(), asteroid.Size, Vector3D.Zero, Vector3D.Zero, asteroid.Type, false, asteroid.EntityId, true);
var removalMessage = new AsteroidNetworkMessage(asteroid.PositionComp.GetPosition(), asteroid.Size, Vector3D.Zero, Vector3D.Zero, asteroid.Type, false, asteroid.EntityId, true, false);
var removalMessageBytes = MyAPIGateway.Utilities.SerializeToBinary(removalMessage);
MyAPIGateway.Multiplayer.SendMessageToOthers(1337, removalMessageBytes);

Expand All @@ -92,12 +87,12 @@ public void UpdateTick()
AsteroidType type = AsteroidSettings.GetRandomAsteroidType(MainSession.I.Rand);

Log.Info($"Spawning asteroid at {newPosition} with velocity {newVelocity} of type {type}");
var asteroid = AsteroidEntity.CreateAsteroid(newPosition, RandAsteroidSize, newVelocity, type);
var asteroid = AsteroidEntity.CreateAsteroid(newPosition, AsteroidSettings.GetRandomAsteroidSize(MainSession.I.Rand), newVelocity, type);
_asteroids.Add(asteroid);
asteroidsSpawned++;

// Send a network message to clients
var message = new AsteroidNetworkMessage(newPosition, RandAsteroidSize, newVelocity, Vector3D.Zero, type, false, asteroid.EntityId, false);
var message = new AsteroidNetworkMessage(newPosition, asteroid.Size, newVelocity, Vector3D.Zero, type, false, asteroid.EntityId, false, true);
var messageBytes = MyAPIGateway.Utilities.SerializeToBinary(message);
MyAPIGateway.Multiplayer.SendMessageToOthers(1337, messageBytes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ public struct AsteroidNetworkMessage
[ProtoMember(8)]
public bool IsRemoval;

public AsteroidNetworkMessage(Vector3D position, float size, Vector3D initialVelocity, Vector3D angularVelocity, AsteroidType type, bool isSubChunk, long entityId, bool isRemoval)
[ProtoMember(9)]
public bool IsInitialCreation;

public AsteroidNetworkMessage(Vector3D position, float size, Vector3D initialVelocity, Vector3D angularVelocity, AsteroidType type, bool isSubChunk, long entityId, bool isRemoval, bool isInitialCreation)
{
Position = position;
Size = size;
Expand All @@ -41,6 +44,7 @@ public AsteroidNetworkMessage(Vector3D position, float size, Vector3D initialVel
IsSubChunk = isSubChunk;
EntityId = entityId;
IsRemoval = isRemoval;
IsInitialCreation = isInitialCreation;
}
}
}
10 changes: 9 additions & 1 deletion Dynamic Asteroids/Data/Scripts/DynamicAsteroids/MainSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ private void OnMessageReceived(byte[] message)
asteroid.Close();
}
}
else if (asteroidMessage.IsInitialCreation)
{
var asteroid = AsteroidEntity.CreateAsteroid(asteroidMessage.Position, asteroidMessage.Size, asteroidMessage.InitialVelocity, asteroidMessage.Type);
asteroid.Physics.AngularVelocity = asteroidMessage.AngularVelocity;
MyEntities.Add(asteroid);
}
else
{
if (asteroidMessage.IsSubChunk)
Expand All @@ -130,7 +136,9 @@ private void OnMessageReceived(byte[] message)
else
{
// Create the regular asteroid on the client
AsteroidEntity.CreateAsteroid(asteroidMessage.Position, asteroidMessage.Size, asteroidMessage.InitialVelocity, asteroidMessage.Type);
var asteroid = AsteroidEntity.CreateAsteroid(asteroidMessage.Position, asteroidMessage.Size, asteroidMessage.InitialVelocity, asteroidMessage.Type);
asteroid.Physics.AngularVelocity = asteroidMessage.AngularVelocity;
MyEntities.Add(asteroid);
}
}
}
Expand Down

0 comments on commit eaa4b7c

Please sign in to comment.