Skip to content

Commit

Permalink
Merge pull request #28 from thiagomvas/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
thiagomvas authored Jun 18, 2024
2 parents 1d2c3ae + 55bcc75 commit efc87b4
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 54 deletions.
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.9.0</Version>
<Version>1.9.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.9.0</Version>
<Version>1.9.1</Version>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageIcon>BasaltLogoBg.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
13 changes: 11 additions & 2 deletions Basalt.TestField/Components/TestTrigger.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Basalt.Common.Components;
using Basalt.Common.Entities;
using Basalt.Raylib.Components;
using System.Drawing;
using System.Numerics;

namespace Basalt.TestField.Components
{
Expand All @@ -10,12 +12,19 @@ public TestTrigger(Entity entity) : base(entity)
{
}

public override void OnStart()
{
Entity.AddComponent(new RaylibParticleSystem(Entity) { Looping = true, ModelCacheKey = "cube" });
var ps = Entity.GetComponent<RaylibParticleSystem>();
ps.UpdateDefaults(new() { Color = Color.RebeccaPurple, Velocity = new Vector3(0, 1f, 0) });
ps.OnStartEvent(this, EventArgs.Empty);
}

public override void OnCollision(Collider other)
{
if (other.Entity.Id == "entity.player")
{
Console.WriteLine($"Trigger collided with {other.Entity.Id}");
Entity.GetComponent<ModelRenderer>().ModelCacheKey = "sphere";

}
}
}
Expand Down
22 changes: 8 additions & 14 deletions Basalt.TestField/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Basalt.Raylib.Sound;
using Basalt.Raylib.Utils;
using Basalt.TestField;
using Basalt.TestField.Components;
using Basalt.Types;
using Raylib_cs;
using System.Numerics;
Expand Down Expand Up @@ -65,20 +66,13 @@

Engine.CreateEntity(player);

var emitter = new Entity();
emitter.Transform.Position = new Vector3(0, 10, 0);
emitter.AddComponent(new RaylibParticleSystem(emitter) { ModelCacheKey = "cube" });
Engine.CreateEntity(emitter);

var ps = emitter.GetComponent<RaylibParticleSystem>()!;

ps.SubscribeOnParticleReset((ref Particle p) =>
{
p.Velocity = new(Random.Shared.NextSingle() * 10 - 5, Random.Shared.NextSingle() * 10 - 5, Random.Shared.NextSingle() * 10 - 5);
// Apply random rotation
p.Rotation = Quaternion.CreateFromYawPitchRoll(Random.Shared.NextSingle() * MathF.PI * 2, Random.Shared.NextSingle() * MathF.PI * 2, Random.Shared.NextSingle() * MathF.PI * 2);

});
var trigger = new Entity();
trigger.AddComponent(new ModelRenderer(trigger) { ModelCacheKey = "cube", Size = new Vector3(1f), ColorTint = Color.Green });
trigger.AddComponent(new BoxCollider(trigger) { Size = new Vector3(1, 1, 1), IsTrigger = true });
trigger.AddComponent(new TestTrigger(trigger));
trigger.AddComponent(new Rigidbody(trigger) { IsKinematic = true });
trigger.Transform.Position = new Vector3(0, 1, 5);
Engine.CreateEntity(trigger);

TestingUtils.SetupTestingScene(250);
TestingUtils.SetupDebugInfo();
24 changes: 0 additions & 24 deletions Basalt.Tests/Integration/EntityIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,30 +169,6 @@ public void EntitySerializeToJson_WhenComponentHasEntityReference_ShouldSerializ
Assert.That(json, Does.Contain("entity.target"));
}

[Test]
public void EntityDeserializeFromJson_WhenComponentHasEntityReference_ShouldDeserializeReference()
{
// Arrange
var target = new Entity();
target.Id = "entity.target";
var entity = new Entity();
entity.Id = "entity.testing";
entity.AddComponent(new TestComponent(entity) { Target = target });

// Act
Engine.Instance.Initialize();
Engine.CreateEntity(target);
var json = entity.SerializeToJson();
var newEntity = Entity.DeserializeFromJson(json);
Engine.CreateEntity(newEntity);

// Assert
Assert.IsNotNull(newEntity, "Deserialization failed, result entity was null");
Assert.IsNotNull(newEntity.GetComponent<TestComponent>(), "Test component was null");
Assert.IsTrue(newEntity.GetComponent<TestComponent>().HasStarted, "Component did not initialize");
Assert.IsNotNull(newEntity.GetComponent<TestComponent>().Target, "Target was null");
Assert.That(newEntity.GetComponent<TestComponent>().Target.Id, Is.EqualTo(target.Id), "Ids are different");
}

[Test]
public void EntityDeserializeToJson_WhenMissingIdField_ShouldGenerateNew()
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.9.0</Version>
<Version>1.9.1</Version>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Title>Basalt</Title>
<PackageIcon>BasaltLogoBg.png</PackageIcon>
Expand Down
2 changes: 2 additions & 0 deletions Basalt/Common/Components/BaseParticleSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public void UnsubscribeOnParticleReset(ParticleUpdateDelegate update)
/// <param name="particle">The new default particle value</param>
public void UpdateDefaults(Particle particle)
{
particle.Position = Entity.Transform.Position;
defaults = particle;
}

Expand All @@ -198,6 +199,7 @@ public void Reset()
for (int i = 0; i < _particles.Length; i++)
{
_particles[i] = defaults;
_particles[i].Lifetime = ParticleLifetime / _length * i;
}
}

Expand Down
41 changes: 33 additions & 8 deletions Basalt/Common/Entities/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public class Entity
private List<ComponentDto> componentDtos = new();
private List<Component> components = new();

[JsonIgnore]
internal bool created = false;
[JsonIgnore]
private int componentCount = 0;

[JsonProperty("Id")]
public string Id { get; set; } = System.Guid.NewGuid().ToString();
/// <summary>
Expand Down Expand Up @@ -113,6 +118,9 @@ public string SerializeToJson()
/// </summary>
/// <param name="json">The json string to deserialize from</param>
/// <returns>An entity instance from the JSON string</returns>
/// <remarks>
/// It is not recommended to rely on deserialization to keep references as they will break.
/// </remarks>
public static Entity DeserializeFromJson(string json)
{
JObject jObject = JObject.Parse(json);
Expand Down Expand Up @@ -208,6 +216,11 @@ public void AddComponent(Component component, bool overwrite = false)
break;
}

if(created)
component.OnStartEvent(this, EventArgs.Empty);

componentCount++;

}

/// <summary>
Expand Down Expand Up @@ -244,6 +257,7 @@ private void ForceAddComponent(Component component)
// Handle other cases if necessary
break;
}
componentCount++;
}

/// <summary>
Expand All @@ -257,6 +271,7 @@ public void RemoveComponent(Component component)
if (component.GetType() == typeof(Rigidbody))
Rigidbody = null;

componentCount--;
}

/// <summary>
Expand All @@ -273,8 +288,9 @@ public void RemoveComponent(Component component)
if (typeof(T) == typeof(Collider))
return Collider as T;

foreach (var component in components)
for (int i = 0; i < components.Count; i++)
{
Component? component = components[i];
if (component is T match)
{
return match;
Expand Down Expand Up @@ -344,13 +360,16 @@ public void Destroy()
{
Destroyed = true;
Engine.RemoveEntity(this);
foreach (var child in Children)
for (int i = 0; i < Children.Count; i++)
{
Entity? child = Children[i];
child.Destroy();
}

foreach (var component in components)
var count = componentCount;
for (int i = 0; i < count; i++)
{
Component? component = components[i];
component.onDestroy();
}
}
Expand All @@ -361,8 +380,9 @@ public void Destroy()
public void Create()
{
Engine.CreateEntity(this);
foreach (var child in Children)
for (int i = 0; i < Children.Count; i++)
{
Entity? child = Children[i];
child.Create();
}
}
Expand All @@ -371,8 +391,10 @@ public void CallOnCollision(Collider other)
{
if (Destroyed)
return;
foreach (var component in components)
component.OnCollision(other);

var count = componentCount;
for (int i = 0; i < count; i++)
components[i].OnCollision(other);
}

private static Type? ByName(string name)
Expand All @@ -394,8 +416,10 @@ public void CallOnCollision(Collider other)

internal void CallStart()
{
foreach (var component in components)
var count = componentCount;
for (int i = 0; i < count; i++)
{
Component? component = components[i];
if (!component.started)
{
var dependencyAttribute = component.GetType().GetCustomAttribute<ComponentDependentOnAttribute>();
Expand Down Expand Up @@ -431,8 +455,9 @@ public Entity Clone()
{
var result = new Entity();
result.Id = Id + Guid.NewGuid().ToString();
foreach (var component in components)
for (int i = 0; i < components.Count; i++)
{
Component? component = components[i];
var c = Activator.CreateInstance(component.GetType(), result) as Component;
foreach (var prop in c.GetType().GetProperties())
{
Expand Down
5 changes: 2 additions & 3 deletions Basalt/Common/Events/EventBus.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
using Basalt.Core.Common.Abstractions.Engine;
using System.Collections.Concurrent;
namespace Basalt.Common.Events
{
/// <summary>
/// Represents an event bus that allows subscribing to and notifying observers of events.
/// </summary>
public class EventBus : IEventBus
{
private readonly List<IObserver> observers;
private readonly object lockObject;

// Game Events
private Dictionary<string, EventHandler> eventHandlers = new Dictionary<string, EventHandler>();
private ConcurrentDictionary<string, EventHandler> eventHandlers = new ConcurrentDictionary<string, EventHandler>();
/// <summary>
/// Initializes a new instance of the <see cref="EventBus"/> class.
/// </summary>
public EventBus()
{
observers = new List<IObserver>();
lockObject = new object();
}

Expand Down
1 change: 1 addition & 0 deletions Basalt/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public static void CreateEntity(Entity entity)
Instance.Logger?.LogDebug($"Creating entity {entity.Id}...");
Instance.EntityManager.AddEntity(entity);
Instance.GetEngineComponent<IPhysicsEngine>()?.AddEntityToSimulation(entity);
entity.created = true;
entity.CallStart();
}

Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

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.9.1"></a>
## [1.9.1](https://www.github.com/thiagomvas/Basalt/releases/tag/v1.9.1) (2024-06-18)

### Bug Fixes

* Adding components during some events would crash the engine ([c13a6cf](https://www.github.com/thiagomvas/Basalt/commit/c13a6cf34094cee8841c5e2cbace019422ca04f2))
* Components are now initialized when added after entity creation ([7aab969](https://www.github.com/thiagomvas/Basalt/commit/7aab9696b8da0ddda0076eef0be87d29e28461ec))
* Particle Lifetime and starting position now work as expected ([7d19af5](https://www.github.com/thiagomvas/Basalt/commit/7d19af5b30834bbce56df88c45e4697cf361e610))

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

Expand Down

0 comments on commit efc87b4

Please sign in to comment.