mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Copy scene when entering play mode
This commit is contained in:
@@ -60,7 +60,7 @@ namespace GlitchyEditor.EditWindows
|
||||
|
||||
ShowComponentEditor<TransformComponent>("Transform", entity, => ShowTransformComponentEditor);
|
||||
ShowComponentEditor<CameraComponent>("Camera", entity, => ShowCameraComponentEditor, => ShowComponentContextMenu<CameraComponent>);
|
||||
ShowComponentEditor<SpriterRendererComponent>("Sprite Renderer", entity, => ShowSpriteRendererComponentEditor, => ShowComponentContextMenu<SpriterRendererComponent>);
|
||||
ShowComponentEditor<SpriteRendererComponent>("Sprite Renderer", entity, => ShowSpriteRendererComponentEditor, => ShowComponentContextMenu<SpriteRendererComponent>);
|
||||
ShowComponentEditor<MeshRendererComponent>("Mesh Renderer", entity, => ShowMeshRendererComponentEditor, => ShowComponentContextMenu<MeshRendererComponent>);
|
||||
ShowComponentEditor<LightComponent>("Light", entity, => ShowLightComponentEditor, => ShowComponentContextMenu<LightComponent>);
|
||||
ShowComponentEditor<MeshComponent>("Mesh", entity, => ShowMeshComponentEditor, => ShowComponentContextMenu<MeshComponent>);
|
||||
@@ -117,18 +117,18 @@ namespace GlitchyEditor.EditWindows
|
||||
|
||||
private static void ShowNameComponentEditor(Entity entity)
|
||||
{
|
||||
if (!entity.HasComponent<DebugNameComponent>())
|
||||
if (!entity.HasComponent<NameComponent>())
|
||||
return;
|
||||
|
||||
char8[256] nameBuffer = default;
|
||||
|
||||
DebugNameComponent* component = entity.GetComponent<DebugNameComponent>();
|
||||
NameComponent* component = entity.GetComponent<NameComponent>();
|
||||
|
||||
String name = null;
|
||||
StringView name = null;
|
||||
|
||||
if(component != null)
|
||||
{
|
||||
name = component.DebugName;
|
||||
name = component.Name;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -142,11 +142,10 @@ namespace GlitchyEditor.EditWindows
|
||||
{
|
||||
if(component == null)
|
||||
{
|
||||
component = entity.AddComponent<DebugNameComponent>();
|
||||
component = entity.AddComponent<NameComponent>();
|
||||
}
|
||||
|
||||
component.DebugName.Clear();
|
||||
component.DebugName.Append(&nameBuffer);
|
||||
component.Name = StringView(&nameBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,7 +249,7 @@ namespace GlitchyEditor.EditWindows
|
||||
}
|
||||
}
|
||||
|
||||
private static void ShowSpriteRendererComponentEditor(Entity entity, SpriterRendererComponent* spriteRendererComponent)
|
||||
private static void ShowSpriteRendererComponentEditor(Entity entity, SpriteRendererComponent* spriteRendererComponent)
|
||||
{
|
||||
ColorRGBA spriteColor = ColorRGBA.LinearToSRGB(spriteRendererComponent.Color);
|
||||
if (ImGui.ColorEdit4("Color", ref spriteColor))
|
||||
@@ -548,7 +547,7 @@ namespace GlitchyEditor.EditWindows
|
||||
ImGui.Separator();
|
||||
|
||||
ShowComponentButton<CameraComponent>("Camera");
|
||||
ShowComponentButton<SpriterRendererComponent>("Sprite Renderer");
|
||||
ShowComponentButton<SpriteRendererComponent>("Sprite Renderer");
|
||||
ShowComponentButton<LightComponent>("Light");
|
||||
ShowComponentButton<Rigidbody2DComponent>("Rigidbody 2D");
|
||||
ShowComponentButton<BoxCollider2DComponent>("Box collider 2D");
|
||||
|
||||
@@ -301,11 +301,11 @@ namespace GlitchyEditor.EditWindows
|
||||
{
|
||||
String name = null;
|
||||
|
||||
var nameComponent = tree.Value.GetComponent<DebugNameComponent>();
|
||||
var nameComponent = tree.Value.GetComponent<NameComponent>();
|
||||
|
||||
if(nameComponent != null)
|
||||
{
|
||||
name = nameComponent.DebugName;
|
||||
name = scope:: .(nameComponent.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -501,13 +501,13 @@ namespace GlitchyEditor.EditWindows
|
||||
{
|
||||
Entity entity = .(entityId, _scene);
|
||||
|
||||
String name = null;
|
||||
StringView name = null;
|
||||
|
||||
var nameComponent = entity.GetComponent<DebugNameComponent>();
|
||||
var nameComponent = entity.GetComponent<NameComponent>();
|
||||
|
||||
if(nameComponent != null)
|
||||
{
|
||||
name = nameComponent.DebugName;
|
||||
name = nameComponent.Name;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -26,6 +26,9 @@ namespace GlitchyEditor
|
||||
get => _scene;
|
||||
set
|
||||
{
|
||||
if (_scene == value)
|
||||
return;
|
||||
|
||||
_scene = value;
|
||||
_entityHierarchyWindow.SetContext(_scene);
|
||||
}
|
||||
|
||||
@@ -27,7 +27,9 @@ namespace GlitchyEditor
|
||||
BlendState _opaqueBlendState ~ _.ReleaseRef();
|
||||
DepthStencilState _depthStencilState ~ _.ReleaseRef();
|
||||
|
||||
Scene _scene ~ delete _;
|
||||
Scene _activeScene ~ _?.ReleaseRef();
|
||||
Scene _editorScene ~ _?.ReleaseRef();
|
||||
|
||||
String _sceneFilePath = new String() ~ delete _;
|
||||
|
||||
public String SceneFilePath
|
||||
@@ -78,7 +80,8 @@ namespace GlitchyEditor
|
||||
|
||||
InitGraphics();
|
||||
|
||||
_scene = new Scene();
|
||||
_editorScene = new Scene();
|
||||
SetReference!(_activeScene, _editorScene);
|
||||
|
||||
_camera = EditorCamera(Vector3(3.5f, 1.25f, 2.75f), Quaternion.FromEulerAngles(MathHelper.ToRadians(40), MathHelper.ToRadians(25), 0), MathHelper.ToRadians(75), 0.1f, 1);
|
||||
_camera.RenderTarget = _cameraTarget;
|
||||
@@ -163,7 +166,7 @@ namespace GlitchyEditor
|
||||
|
||||
private void InitEditor()
|
||||
{
|
||||
_editor = new Editor(_scene, _contentManager);
|
||||
_editor = new Editor(_editorScene, _contentManager);
|
||||
_editor.SceneViewportWindow.ViewportSizeChanged.Add(new (s, e) => ViewportSizeChanged(s, e));
|
||||
_editor.CurrentCamera = &_camera;
|
||||
|
||||
@@ -176,6 +179,8 @@ namespace GlitchyEditor
|
||||
{
|
||||
_camera.Update(gameTime);
|
||||
|
||||
_editor.CurrentScene = _activeScene;
|
||||
|
||||
// Clear the swapchain-buffer
|
||||
RenderCommand.Clear(null, .Color | .Depth, .(0.2f, 0.2f, 0.2f), 1.0f, 0);
|
||||
|
||||
@@ -185,9 +190,9 @@ namespace GlitchyEditor
|
||||
RenderCommand.SetDepthStencilState(_depthStencilState);
|
||||
|
||||
if (_sceneState == .Edit)
|
||||
_scene.UpdateEditor(gameTime, _camera, _viewportTarget, scope => DebugDraw3D, scope => DebugDraw2D);
|
||||
_activeScene.UpdateEditor(gameTime, _camera, _viewportTarget, scope => DebugDraw3D, scope => DebugDraw2D);
|
||||
else if (_sceneState == .Play)
|
||||
_scene.UpdateRuntime(gameTime, _viewportTarget);
|
||||
_activeScene.UpdateRuntime(gameTime, _viewportTarget);
|
||||
|
||||
RenderCommand.UnbindRenderTargets();
|
||||
RenderCommand.SetRenderTarget(null, 0, true);
|
||||
@@ -226,9 +231,9 @@ namespace GlitchyEditor
|
||||
return Math.Clamp(1.5f - Vector3.Distance(_editor.CurrentCamera.Position, pos) / 50, 0, 1);
|
||||
}
|
||||
|
||||
for (var (entity, transform, camera) in _scene.[Friend]_ecsWorld.Enumerate<TransformComponent, CameraComponent>())
|
||||
for (var (entity, transform, camera) in _activeScene.[Friend]_ecsWorld.Enumerate<TransformComponent, CameraComponent>())
|
||||
{
|
||||
if (_editor.EntityHierarchyWindow.SelectedEntities.Contains(.(entity, _scene)))
|
||||
if (_editor.EntityHierarchyWindow.SelectedEntities.Contains(.(entity, _activeScene)))
|
||||
{
|
||||
DebugRenderer.DrawViewFrustum(transform.WorldTransform, camera.Camera.Projection, _camera.Projection * _camera.View, .White);
|
||||
}
|
||||
@@ -240,9 +245,9 @@ namespace GlitchyEditor
|
||||
//Renderer2D.DrawQuad(world, _iconCamera, .White, .(0, 0, 1, 1), entity.Index);
|
||||
}
|
||||
|
||||
for (var (entity, transform, light) in _scene.[Friend]_ecsWorld.Enumerate<TransformComponent, LightComponent>())
|
||||
for (var (entity, transform, light) in _activeScene.[Friend]_ecsWorld.Enumerate<TransformComponent, LightComponent>())
|
||||
{
|
||||
if (_editor.EntityHierarchyWindow.SelectedEntities.Contains(.(entity, _scene)))
|
||||
if (_editor.EntityHierarchyWindow.SelectedEntities.Contains(.(entity, _activeScene)))
|
||||
{
|
||||
Renderer.DrawRay(.Zero, .(0, 0, 20), ColorRGBA(light.SceneLight.Color, 1.0f), transform.WorldTransform);
|
||||
|
||||
@@ -343,18 +348,30 @@ namespace GlitchyEditor
|
||||
|
||||
private void OnScenePlay()
|
||||
{
|
||||
_sceneState = .Play;
|
||||
_editor.SceneViewportWindow.EditorMode = false;
|
||||
_sceneState = .Play;
|
||||
|
||||
_scene.OnRuntimeStart();
|
||||
using (Scene runtimeScene = new Scene())
|
||||
{
|
||||
_editorScene.CopyTo(runtimeScene);
|
||||
|
||||
runtimeScene.OnRuntimeStart();
|
||||
|
||||
SetReference!(_activeScene, runtimeScene);
|
||||
}
|
||||
|
||||
_editor.CurrentScene = _activeScene;
|
||||
}
|
||||
|
||||
private void OnSceneStop()
|
||||
{
|
||||
_scene.OnRuntimeStop();
|
||||
_activeScene.OnRuntimeStop();
|
||||
SetReference!(_activeScene, _editorScene);
|
||||
|
||||
_editor.SceneViewportWindow.EditorMode = true;
|
||||
_sceneState = .Edit;
|
||||
|
||||
_editor.CurrentScene = _activeScene;
|
||||
}
|
||||
|
||||
// Just for testing
|
||||
@@ -488,13 +505,17 @@ namespace GlitchyEditor
|
||||
/// Creates a new scene.
|
||||
private void NewScene()
|
||||
{
|
||||
OnSceneStop();
|
||||
|
||||
SceneFilePath = null;
|
||||
|
||||
delete _scene;
|
||||
_scene = new Scene();
|
||||
_editor.CurrentScene = _scene;
|
||||
_editorScene.ReleaseRef();
|
||||
_editorScene = new Scene();
|
||||
_editor.CurrentScene = _editorScene;
|
||||
var vpSize = _editor.SceneViewportWindow.ViewportSize;
|
||||
_scene.OnViewportResize((.)vpSize.X, (.)vpSize.Y);
|
||||
_editorScene.OnViewportResize((.)vpSize.X, (.)vpSize.Y);
|
||||
|
||||
SetReference!(_activeScene, _editorScene);
|
||||
|
||||
_camera.Position = .(-1.5f, 1.5f, -2.5f);
|
||||
_camera.RotationEuler = .(MathHelper.ToRadians(25), MathHelper.ToRadians(35), 0);
|
||||
@@ -530,7 +551,7 @@ namespace GlitchyEditor
|
||||
return;
|
||||
}
|
||||
|
||||
SceneSerializer serializer = scope .(_scene);
|
||||
SceneSerializer serializer = scope .(_editorScene);
|
||||
serializer.Serialize(SceneFilePath);
|
||||
}
|
||||
|
||||
@@ -565,16 +586,20 @@ namespace GlitchyEditor
|
||||
/// Loads the given scene file.
|
||||
private void LoadSceneFile(StringView filename)
|
||||
{
|
||||
OnSceneStop();
|
||||
|
||||
SceneFilePath = scope String(filename);
|
||||
|
||||
delete _scene;
|
||||
_scene = new Scene();
|
||||
_editor.CurrentScene = _scene;
|
||||
_editorScene.ReleaseRef();
|
||||
_editorScene = new Scene();
|
||||
_editor.CurrentScene = _editorScene;
|
||||
var vpSize = _editor.SceneViewportWindow.ViewportSize;
|
||||
_scene.OnViewportResize((.)vpSize.X, (.)vpSize.Y);
|
||||
_editorScene.OnViewportResize((.)vpSize.X, (.)vpSize.Y);
|
||||
|
||||
SceneSerializer serializer = scope .(_scene);
|
||||
SceneSerializer serializer = scope .(_editorScene);
|
||||
serializer.Deserialize(SceneFilePath);
|
||||
|
||||
SetReference!(_activeScene, _editorScene);
|
||||
}
|
||||
|
||||
private void DrawMainMenuBar()
|
||||
@@ -648,7 +673,7 @@ namespace GlitchyEditor
|
||||
_viewportTarget.Resize(sizeX, sizeY);
|
||||
_cameraTarget.Resize(sizeX, sizeY);
|
||||
|
||||
_scene.OnViewportResize(sizeX, sizeY);
|
||||
_activeScene.OnViewportResize(sizeX, sizeY);
|
||||
_camera.OnViewportResize(sizeX, sizeY);
|
||||
}
|
||||
|
||||
|
||||
@@ -229,160 +229,6 @@ namespace GlitchyEngine.Content
|
||||
return .Success;
|
||||
}
|
||||
|
||||
/*public static EcsEntity LoadModel(String filename, Material material, EcsWorld world,
|
||||
List<AnimationClip> outClips, StringView entityName = StringView())
|
||||
{
|
||||
CGLTF.Options options = .();
|
||||
CGLTF.Data* data;
|
||||
CGLTF.Result result = CGLTF.ParseFile(options, filename, out data);
|
||||
|
||||
Log.EngineLogger.Assert(result == .Success, "Failed to load model.");
|
||||
|
||||
result = CGLTF.LoadBuffers(options, data, filename);
|
||||
|
||||
Log.EngineLogger.Assert(result == .Success, "Failed to load buffers");
|
||||
|
||||
(EcsEntity entity, ?) = CreateEntity(world, entityName, .InvalidEntity);
|
||||
|
||||
for(var node in data.Scenes[0].Nodes)
|
||||
{
|
||||
NodesToEntities(data, node, entity, world, material, outClips);
|
||||
}
|
||||
|
||||
CGLTF.Free(data);
|
||||
|
||||
return entity;
|
||||
}*/
|
||||
|
||||
private static (EcsEntity Entity, TransformComponent* Transform) CreateEntity(EcsWorld world, StringView? name, EcsEntity parent)
|
||||
{
|
||||
EcsEntity entity = world.NewEntity();
|
||||
|
||||
var nameComponent = world.AssignComponent<DebugNameComponent>(entity);
|
||||
|
||||
if (name != null && name.Value.Ptr != null)
|
||||
{
|
||||
nameComponent.SetName(name.Value);
|
||||
}
|
||||
else
|
||||
{
|
||||
nameComponent.SetName("Unnamed Node");
|
||||
}
|
||||
|
||||
/////TODO: !!!!!!!!REPORT!!!!!!!!!!!!!
|
||||
// This works
|
||||
TransformComponent cmp = .();
|
||||
var childTransform = world.AssignComponent<TransformComponent>(entity, cmp);
|
||||
// This trashes the stack
|
||||
//var childTransform = world.AssignComponent<TransformComponent>(entity);
|
||||
childTransform.Parent = parent;
|
||||
|
||||
return (entity, childTransform);
|
||||
}
|
||||
|
||||
/*private static void NodesToEntities(CGLTF.Data* data, CGLTF.Node* node, EcsEntity parentEntity, EcsWorld world, Material material, List<AnimationClip> clips)
|
||||
{
|
||||
(EcsEntity entity, TransformComponent* childTransform) = CreateEntity(world, node.Name == null ? null : StringView(node.Name), parentEntity);
|
||||
|
||||
if(node.HasMatrix)
|
||||
{
|
||||
childTransform.LocalTransform = *(Matrix*)&node.Matrix;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(node.HasTranslation)
|
||||
childTransform.Position = *(Vector3*)&node.Translation;
|
||||
else
|
||||
childTransform.Position = .Zero;
|
||||
|
||||
if(node.HasRotation)
|
||||
childTransform.Rotation = *(Quaternion*)&node.Rotation;
|
||||
else
|
||||
childTransform.Rotation = .Identity;
|
||||
|
||||
if(node.HasScale)
|
||||
childTransform.Scale = *(Vector3*)&node.Scale;
|
||||
else
|
||||
childTransform.Scale = .(1, 1, 1);
|
||||
}
|
||||
|
||||
// Invert the Z-Axis of the root Node to convert the coordinate system from right-handed to left-handed
|
||||
if(parentEntity == .InvalidEntity)
|
||||
childTransform.Scale *= .(1, 1, -1);
|
||||
|
||||
Skeleton skeleton = null;
|
||||
|
||||
if(node.Skin != null)
|
||||
{
|
||||
skeleton = ExtractSkeleton(node.Skin);
|
||||
|
||||
LoadAnimationClips(data, node.Skin, skeleton, clips);
|
||||
}
|
||||
|
||||
if(node.Mesh != null)
|
||||
{
|
||||
// If we have only one primitive, add it directly to the entity
|
||||
if(node.Mesh.Primitives.Length == 1)
|
||||
{
|
||||
var mesh = world.AssignComponent<MeshComponent>(entity);
|
||||
|
||||
using (var geo = PrimitiveToGeoBinding(node.Mesh.Primitives[0]))
|
||||
{
|
||||
mesh.Mesh = Content.ManageAsset(geo);
|
||||
}
|
||||
|
||||
if(skeleton == null)
|
||||
{
|
||||
var meshRenderer = world.AssignComponent<MeshRendererComponent>(entity);
|
||||
meshRenderer.Material = material.Handle;
|
||||
}
|
||||
else
|
||||
{
|
||||
var meshRenderer = world.AssignComponent<SkinnedMeshRendererComponent>(entity);
|
||||
meshRenderer.Material = material;
|
||||
meshRenderer.Skeleton = skeleton;
|
||||
}
|
||||
}
|
||||
// otherwise one child-entity per primitive
|
||||
else
|
||||
{
|
||||
for(var primitive in node.Mesh.Primitives)
|
||||
{
|
||||
EcsEntity meshEntity = world.NewEntity();
|
||||
|
||||
var meshParent = world.AssignComponent<ParentComponent>(meshEntity);
|
||||
meshParent.Entity = entity;
|
||||
|
||||
var mesh = world.AssignComponent<MeshComponent>(meshEntity);
|
||||
|
||||
using (var geo = PrimitiveToGeoBinding(primitive))
|
||||
{
|
||||
mesh.Mesh = Content.ManageAsset(geo);
|
||||
}
|
||||
|
||||
if(skeleton == null)
|
||||
{
|
||||
var meshRenderer = world.AssignComponent<MeshRendererComponent>(meshEntity);
|
||||
meshRenderer.Material = material.Handle;
|
||||
}
|
||||
else
|
||||
{
|
||||
var meshRenderer = world.AssignComponent<SkinnedMeshRendererComponent>(meshEntity);
|
||||
meshRenderer.Material = material;
|
||||
meshRenderer.Skeleton = skeleton;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
skeleton?.ReleaseRef();
|
||||
|
||||
for(var child in node.Children)
|
||||
{
|
||||
NodesToEntities(data, child, entity, world, material, clips);
|
||||
}
|
||||
}*/
|
||||
|
||||
public static GeometryBinding PrimitiveToGeoBinding(CGLTF.Primitive primitive)
|
||||
{
|
||||
GeometryBinding binding = new GeometryBinding();
|
||||
|
||||
@@ -873,7 +873,7 @@ namespace GlitchyEngine.Renderer
|
||||
}
|
||||
}
|
||||
|
||||
public static void DrawSprite(Matrix transform, SpriterRendererComponent* spriteRenderer, uint32 entityId)
|
||||
public static void DrawSprite(Matrix transform, SpriteRendererComponent* spriteRenderer, uint32 entityId)
|
||||
{
|
||||
if (spriteRenderer.IsCircle)
|
||||
DrawCircle(transform, spriteRenderer.Sprite.Get() ?? s_whiteTexture, spriteRenderer.Color, 1.0f, spriteRenderer.UvTransform, entityId);
|
||||
|
||||
@@ -45,7 +45,7 @@ namespace GlitchyEngine.World
|
||||
}
|
||||
|
||||
[Component("Sprite Renderer")]
|
||||
struct SpriterRendererComponent// : IDisposableComponent
|
||||
struct SpriteRendererComponent// : IDisposableComponent
|
||||
{
|
||||
private AssetHandle<Texture2D> _sprite = .Invalid;
|
||||
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
using System;
|
||||
namespace GlitchyEngine.World
|
||||
{
|
||||
struct DebugNameComponent : IDisposableComponent
|
||||
{
|
||||
private String _debugName;
|
||||
|
||||
public String DebugName
|
||||
{
|
||||
get => _debugName;
|
||||
set mut => SetName(value);
|
||||
}
|
||||
|
||||
public void SetName(StringView name) mut
|
||||
{
|
||||
delete _debugName;
|
||||
|
||||
_debugName = new String(name);
|
||||
}
|
||||
|
||||
public void Dispose() mut
|
||||
{
|
||||
DeleteAndNullify!(_debugName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,6 +66,12 @@ namespace GlitchyEngine.World
|
||||
|
||||
public UUID UUID => GetComponent<IDComponent>().ID;
|
||||
|
||||
public StringView Name
|
||||
{
|
||||
get => GetComponent<NameComponent>().Name;
|
||||
set => GetComponent<NameComponent>().Name = value;
|
||||
}
|
||||
|
||||
public TransformComponent* Transform => GetComponent<TransformComponent>();
|
||||
|
||||
public T* AddComponent<T>(T value = T()) where T: struct, new
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
namespace GlitchyEngine.World
|
||||
{
|
||||
struct NameComponent : IDisposableComponent
|
||||
{
|
||||
private String _name;
|
||||
|
||||
public StringView Name
|
||||
{
|
||||
get => _name;
|
||||
set mut => SetName(value);
|
||||
}
|
||||
|
||||
public void SetName(StringView name) mut
|
||||
{
|
||||
if (_name == null)
|
||||
_name = new String(name);
|
||||
else
|
||||
_name.Set(name);
|
||||
}
|
||||
|
||||
public void Dispose() mut
|
||||
{
|
||||
DeleteAndNullify!(_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
namespace GlitchyEngine.World
|
||||
{
|
||||
/**
|
||||
* A Component that allows to specify a parent entity.
|
||||
*/
|
||||
public struct ParentComponent
|
||||
{
|
||||
public EcsEntity Entity;
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ namespace GlitchyEngine.World
|
||||
using internal ScriptableEntity;
|
||||
using internal GlitchyEngine.World;
|
||||
|
||||
class Scene
|
||||
class Scene : RefCounter
|
||||
{
|
||||
internal EcsWorld _ecsWorld = new .() ~ delete _;
|
||||
|
||||
@@ -26,6 +26,8 @@ namespace GlitchyEngine.World
|
||||
|
||||
private AssetHandle _gammaCorrectEffect;
|
||||
|
||||
private uint32 _viewportWidth, _viewportHeight;
|
||||
|
||||
// Maps ids to the entities they represent.
|
||||
private Dictionary<UUID, EcsEntity> _idToEntity = new .() ~ delete _;
|
||||
|
||||
@@ -56,7 +58,7 @@ namespace GlitchyEngine.World
|
||||
_onComponentAddedHandlers.Add(typeof(CameraComponent), (e, t, c) => {
|
||||
CameraComponent* cameraComponent = (.)c;
|
||||
|
||||
cameraComponent.Camera.SetViewportSize(e.Scene.ViewportWidth, e.Scene.ViewportHeight);
|
||||
cameraComponent.Camera.SetViewportSize(e.Scene._viewportWidth, e.Scene._viewportHeight);
|
||||
});
|
||||
|
||||
RenderTargetGroupDescription desc = .(100, 100,
|
||||
@@ -83,6 +85,59 @@ namespace GlitchyEngine.World
|
||||
{
|
||||
}
|
||||
|
||||
public void CopyTo(Scene target)
|
||||
{
|
||||
// Copy entities
|
||||
for (let sourceHandle in _ecsWorld.Enumerate())
|
||||
{
|
||||
Entity sourceEntity = .(sourceHandle, this);
|
||||
|
||||
target.CreateEntity(sourceEntity.Name, sourceEntity.UUID);
|
||||
}
|
||||
|
||||
// TODO: perhaps use reflection and comptime
|
||||
// Copy components
|
||||
//CopyComponents<TransformComponent>(this, target); /* Parent will be copied below*/
|
||||
CopyComponents<MeshRendererComponent>(this, target);
|
||||
CopyComponents<EditorComponent>(this, target);
|
||||
CopyComponents<SpriteRendererComponent>(this, target);
|
||||
CopyComponents<CameraComponent>(this, target);
|
||||
CopyComponents<NativeScriptComponent>(this, target);
|
||||
CopyComponents<LightComponent>(this, target);
|
||||
CopyComponents<Rigidbody2DComponent>(this, target);
|
||||
CopyComponents<BoxCollider2DComponent>(this, target);
|
||||
CopyComponents<CircleCollider2DComponent>(this, target);
|
||||
|
||||
// Copy transforms
|
||||
for (let (sourceHandle, sourceTransform) in _ecsWorld.Enumerate<TransformComponent>())
|
||||
{
|
||||
Entity sourceEntity = Entity(sourceHandle, this);
|
||||
Entity sourceParent = Entity(sourceTransform.Parent, this);
|
||||
|
||||
Entity targetEntity = target.GetEntityByID(sourceEntity.UUID);
|
||||
*targetEntity.Transform = *sourceTransform;
|
||||
|
||||
if (sourceParent.IsValid)
|
||||
{
|
||||
Entity targetParent = target.GetEntityByID(sourceParent.UUID);
|
||||
targetEntity.Parent = targetParent;
|
||||
}
|
||||
}
|
||||
|
||||
target.OnViewportResize(_viewportWidth, _viewportHeight);
|
||||
}
|
||||
|
||||
private static void CopyComponents<TComponent>(Scene source, Scene target) where TComponent : struct, new
|
||||
{
|
||||
for (let (sourceHandle, sourceComponent) in source._ecsWorld.Enumerate<TComponent>())
|
||||
{
|
||||
Entity sourceEntity = .(sourceHandle, source);
|
||||
|
||||
Entity targetEntity = target.GetEntityByID(sourceEntity.UUID);
|
||||
targetEntity.AddComponent<TComponent>(*sourceComponent);
|
||||
}
|
||||
}
|
||||
|
||||
b2Vec2 _gravity2D = .(0.0f, -9.8f);
|
||||
|
||||
static b2BodyType GetBox2DBodyType(Rigidbody2DComponent.BodyType bodyType)
|
||||
@@ -256,7 +311,7 @@ namespace GlitchyEngine.World
|
||||
// Sprite renderer
|
||||
Renderer2D.BeginScene(*primaryCamera, primaryCameraTransform, .BackToFront);
|
||||
|
||||
for (var (entity, transform, sprite) in _ecsWorld.Enumerate<TransformComponent, SpriterRendererComponent>())
|
||||
for (var (entity, transform, sprite) in _ecsWorld.Enumerate<TransformComponent, SpriteRendererComponent>())
|
||||
{
|
||||
Renderer2D.DrawSprite(transform.WorldTransform, sprite, entity.Index);
|
||||
}
|
||||
@@ -469,7 +524,7 @@ namespace GlitchyEngine.World
|
||||
// Sprite renderer
|
||||
Renderer2D.BeginScene(camera, .BackToFront);
|
||||
|
||||
for (var (entity, transform, sprite) in _ecsWorld.Enumerate<TransformComponent, SpriterRendererComponent>())
|
||||
for (var (entity, transform, sprite) in _ecsWorld.Enumerate<TransformComponent, SpriteRendererComponent>())
|
||||
{
|
||||
Renderer2D.DrawSprite(transform.WorldTransform, sprite, entity.Index);
|
||||
}
|
||||
@@ -502,12 +557,12 @@ namespace GlitchyEngine.World
|
||||
}
|
||||
|
||||
/// Creates a new Entity with the given name.
|
||||
public Entity CreateEntity(String name = "", UUID id = default)
|
||||
public Entity CreateEntity(StringView name = "", UUID id = default)
|
||||
{
|
||||
Entity entity = Entity(_ecsWorld.NewEntity(), this);
|
||||
entity.AddComponent<TransformComponent>();
|
||||
|
||||
let nameComponent = entity.AddComponent<DebugNameComponent>();
|
||||
let nameComponent = entity.AddComponent<NameComponent>();
|
||||
nameComponent.SetName(name.IsEmpty ? "Entity" : name);
|
||||
|
||||
// If no id is given generate a random one.
|
||||
@@ -549,13 +604,11 @@ namespace GlitchyEngine.World
|
||||
return .Err;
|
||||
}
|
||||
|
||||
private uint32 ViewportWidth, ViewportHeight;
|
||||
|
||||
/// Sets the size of the viewport into which the scene will be rendered.
|
||||
public void OnViewportResize(uint32 width, uint32 height)
|
||||
{
|
||||
ViewportWidth = width;
|
||||
ViewportHeight = height;
|
||||
_viewportWidth = width;
|
||||
_viewportHeight = height;
|
||||
|
||||
for (var (entity, cameraComponent) in _ecsWorld.Enumerate<CameraComponent>())
|
||||
{
|
||||
@@ -565,8 +618,8 @@ namespace GlitchyEngine.World
|
||||
}
|
||||
}
|
||||
|
||||
_compositeTarget.Resize(ViewportWidth, ViewportHeight);
|
||||
_cameraTarget.Resize(ViewportWidth, ViewportHeight);
|
||||
_compositeTarget.Resize(_viewportWidth, _viewportHeight);
|
||||
_cameraTarget.Resize(_viewportWidth, _viewportHeight);
|
||||
}
|
||||
|
||||
private void OnComponentAdded(Entity entity, Type componentType, void* component)
|
||||
|
||||
@@ -79,12 +79,12 @@ class SceneSerializer
|
||||
|
||||
SerializeComponent<EditorComponent>(writer, entity, "EditorComponent", scope (component) => {});
|
||||
|
||||
SerializeComponent<DebugNameComponent>(writer, entity, "NameComponent", scope (component) =>
|
||||
SerializeComponent<NameComponent>(writer, entity, "NameComponent", scope (component) =>
|
||||
{
|
||||
Serialize.Value(writer, "Name", component.DebugName);
|
||||
Serialize.Value(writer, "Name", component.Name);
|
||||
});
|
||||
|
||||
SerializeComponent<SpriterRendererComponent>(writer, entity, "SpriterRendererComponent", scope (component) =>
|
||||
SerializeComponent<SpriteRendererComponent>(writer, entity, "SpriterRendererComponent", scope (component) =>
|
||||
{
|
||||
Serialize.Value(writer, "Color", component.Color);
|
||||
Serialize.Value(writer, "IsCircle", component.IsCircle);
|
||||
@@ -317,7 +317,7 @@ class SceneSerializer
|
||||
case "EditorComponent":
|
||||
Try!(DeserializeComponent<EditorComponent>(reader, entity, scope (component) => { return .Ok; }));
|
||||
case "NameComponent":
|
||||
Try!(DeserializeComponent<DebugNameComponent>(reader, entity, scope (component) =>
|
||||
Try!(DeserializeComponent<NameComponent>(reader, entity, scope (component) =>
|
||||
{
|
||||
String name;
|
||||
|
||||
@@ -330,7 +330,7 @@ class SceneSerializer
|
||||
return .Ok;
|
||||
}));
|
||||
case "SpriterRendererComponent":
|
||||
Try!(DeserializeComponent<SpriterRendererComponent>(reader, entity, scope (component) =>
|
||||
Try!(DeserializeComponent<SpriteRendererComponent>(reader, entity, scope (component) =>
|
||||
{
|
||||
Try!(Deserialize.Value(reader, "Color", out component.Color));
|
||||
reader.EntryEnd();
|
||||
|
||||
@@ -20,6 +20,7 @@ namespace GlitchyEngine.World
|
||||
/// The frame when the transform was recalculated
|
||||
public uint Frame;
|
||||
|
||||
// TODO: probably use UUID
|
||||
public EcsEntity Parent
|
||||
{
|
||||
get => _parent;
|
||||
|
||||
Reference in New Issue
Block a user