This commit is contained in:
Simon Lübeß
2022-11-16 13:09:26 +01:00
parent a42544f171
commit 0155a38ae6
8 changed files with 367 additions and 21 deletions
+26 -1
View File
@@ -3,6 +3,7 @@ using GlitchyEngine.Renderer;
using System;
using System.Collections;
using Box2D;
using GlitchyEngine.Core;
namespace GlitchyEngine.World
{
@@ -24,6 +25,9 @@ namespace GlitchyEngine.World
private Effect _gammaCorrectEffect ~ _.ReleaseRef();
// Maps ids to the entities they represent.
private Dictionary<UUID, EcsEntity> _idToEntity = new .() ~ delete _;
public Entity ActiveCamera => {
Entity cameraEntity = .();
@@ -205,6 +209,8 @@ namespace GlitchyEngine.World
}
}
renderTarget = _cameraTarget..AddRef();
// 3D render
Renderer.BeginScene(*primaryCamera, primaryCameraTransform, renderTarget, _compositeTarget);
@@ -468,13 +474,20 @@ namespace GlitchyEngine.World
}
/// Creates a new Entity with the given name.
public Entity CreateEntity(String name = "")
public Entity CreateEntity(String name = "", UUID id = default)
{
Entity entity = Entity(_ecsWorld.NewEntity(), this);
entity.AddComponent<TransformComponent>();
let nameComponent = entity.AddComponent<DebugNameComponent>();
nameComponent.SetName(name.IsEmpty ? "Entity" : name);
// If no id is given generate a random one.
IDComponent idComponent = (id == default) ? IDComponent() : IDComponent(id);
entity.AddComponent<IDComponent>(idComponent);
_idToEntity.Add(idComponent.ID, entity.Handle);
return entity;
}
@@ -485,6 +498,8 @@ namespace GlitchyEngine.World
*/
public void DestroyEntity(Entity entity, bool destroyChildren = false)
{
_idToEntity.Remove(entity.UUID);
if (destroyChildren)
{
for (Entity child in entity.EnumerateChildren)
@@ -496,6 +511,16 @@ namespace GlitchyEngine.World
_ecsWorld.RemoveEntity(entity.Handle);
}
public Result<Entity> GetEntityByID(UUID id)
{
if (_idToEntity.TryGetValue(id, let ecsEntity))
{
return .Ok(Entity(ecsEntity, this));
}
return .Err;
}
private uint32 ViewportWidth, ViewportHeight;
/// Sets the size of the viewport into which the scene will be rendered.