Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development #25

Merged
merged 6 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Basalt.Core/Basalt.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.8.0</Version>
<Version>1.8.1</Version>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageIcon>BasaltLogoBg.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
2 changes: 1 addition & 1 deletion Basalt.Raylib/Basalt.Raylib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Version>1.8.0</Version>
<Version>1.8.1</Version>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageIcon>BasaltLogoBg.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
2 changes: 1 addition & 1 deletion Basalt/Basalt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Version>1.8.0</Version>
<Version>1.8.1</Version>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>Basalt</Title>
<PackageIcon>BasaltLogoBg.png</PackageIcon>
Expand Down
2 changes: 0 additions & 2 deletions Basalt/Common/Components/Rigidbody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@
/// Initializes a new instance of the <see cref="Rigidbody"/> class.
/// </summary>
/// <param name="entity">The entity that the rigidbody belongs to.</param>
public Rigidbody(Entity entity) : base(entity)

Check warning on line 41 in Basalt/Common/Components/Rigidbody.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'physicsEngine' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
var physics = Engine.Instance.GetEngineComponent<IPhysicsEngine>();
if (physics != null)
{
physicsEngine = physics;
if (physics is PhysicsEngine engine)
Entity.Transform.chunking = engine.chunking;
}
else
{
Expand Down
8 changes: 3 additions & 5 deletions Basalt/Common/Components/Transform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace Basalt.Common.Components
[SingletonComponent]
public sealed class Transform : Component
{
internal IChunkingMechanism? chunking;
public bool IsFixedPoint { get; set; } = false;
private Vector3 position;
/// <summary>
Expand All @@ -29,10 +28,9 @@ public Vector3 Position
return;
}

if (chunking != null)
{
chunking.MarkForUpdate(Entity);
}
if(Engine.Instance.EntityManager != null)
Engine.Instance.EntityManager.ChunkingMechanism.MarkForUpdate(Entity);


var offset = value - position;
position = value;
Expand Down
18 changes: 13 additions & 5 deletions Basalt/Common/Entities/EntityManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Basalt.Core.Common.Abstractions.Engine;
using Basalt.Common.Physics;
using Basalt.Core.Common.Abstractions.Engine;

namespace Basalt.Common.Entities
{
Expand All @@ -10,13 +11,15 @@ public class EntityManager
public int EntityCount => entities.Count;
private List<Entity> entities = new List<Entity>();
private readonly object lockObject = new object();
private readonly IEventBus eventBus;
/// <summary>
/// Gets the currently used Chunking Mechanism that groups entities together.
/// </summary>
public IChunkingMechanism ChunkingMechanism { get; private set; } = new Grid(32);

private List<Entity> queuedEntities = new List<Entity>();

public EntityManager(IEventBus eventBus)
public EntityManager()
{
this.eventBus = eventBus;
ChunkingMechanism = new Grid(32);
}

/// <summary>
Expand Down Expand Up @@ -71,5 +74,10 @@ public List<Entity> GetEntities()
return entities.FirstOrDefault(e => e.Id == id);
}
}

public void UseChunkingMechanism(IChunkingMechanism chunkingMechanism)
{
ChunkingMechanism = chunkingMechanism;
}
}
}
22 changes: 6 additions & 16 deletions Basalt/Common/Physics/PhysicsEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class PhysicsEngine : IPhysicsEngine
const float targetDeltaTime = 0.02f;
const int targetFrameTimeMs = 20;

internal IChunkingMechanism chunking;
//internal IChunkingMechanism chunking;
private IEventBus eventBus;
private ILogger? logger;
private bool ShouldRun = true;
Expand All @@ -33,17 +33,7 @@ public class PhysicsEngine : IPhysicsEngine
public PhysicsEngine()
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
{
chunking = new Grid(32);
}
/// <summary>
/// Initializes a new instance of the <see cref="CustomPhysics"/> class using the specified <see cref="IChunkingMechanism"/>.
/// </summary>
/// <param name="chunkingMechanism">The chunking mechanism used to optimize collision handling</param>
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public PhysicsEngine(IChunkingMechanism chunkingMechanism)
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
{
chunking = chunkingMechanism;

}

/// <summary>
Expand Down Expand Up @@ -86,11 +76,11 @@ public void Simulate()

eventBus?.TriggerEvent(BasaltConstants.PhysicsUpdateEventKey);

chunking.Update();
Engine.Instance.EntityManager.ChunkingMechanism.Update();

// Check for collisions

DetectCollisions(chunking.GetEntitiesChunked());
DetectCollisions(Engine.Instance.EntityManager.ChunkingMechanism.GetEntitiesChunked());

elapsedTime = DateTimeOffset.Now.ToUnixTimeMilliseconds() - startTime;

Expand Down Expand Up @@ -162,15 +152,15 @@ public void AddEntityToSimulation(object entity)
{
if (entity is Entity e)
{
chunking.AddEntity(e);
Engine.Instance.EntityManager.ChunkingMechanism.AddEntity(e);
}
}

public void RemoveEntityFromSimulation(object entity)
{
if (entity is Entity e)
{
chunking.RemoveEntity(e);
Engine.Instance.EntityManager.ChunkingMechanism.RemoveEntity(e);
}
}
}
Expand Down
17 changes: 15 additions & 2 deletions Basalt/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,23 @@ public bool Running

private Dictionary<Type, ComponentHolder> Components { get; set; } = new();

private EntityManager _entityManager;

/// <summary>
/// The entity manager that holds all the entities.
/// </summary>
public EntityManager EntityManager { get; private set; }
public EntityManager EntityManager
{
get
{
if(_entityManager == null)
{
_entityManager = new();
}
return _entityManager;
}
private set { _entityManager = value; }
}
private ILogger? _logger;

/// <summary>
Expand Down Expand Up @@ -109,7 +122,7 @@ public void Initialize()
}

// Initialize Entity Manager
EntityManager = new(GetEngineComponent<IEventBus>()!);
EntityManager = new();

// Move graphics engine and event bus to the front of the list
Components = Components.OrderBy(c => c.Key == typeof(IGraphicsEngine) ? 0 : c.Key == typeof(IEventBus) ? 1 : 2).ToDictionary(c => c.Key, c => c.Value);
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file. See [versionize](https://github.com/versionize/versionize) for commit guidelines.

<a name="1.8.1"></a>
## [1.8.1](https://www.github.com/thiagomvas/Basalt/releases/tag/v1.8.1) (2024-06-10)

### Bug Fixes

* Add reference to used ChunkingMechanism ([22a47e7](https://www.github.com/thiagomvas/Basalt/commit/22a47e7b5da6c250219e4942580a25d04ba5b669))

<a name="1.8.0"></a>
## [1.8.0](https://www.github.com/thiagomvas/Basalt/releases/tag/v1.8.0) (2024-06-10)

Expand Down
Loading