Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/isadorasophia/murder into main
Browse files Browse the repository at this point in the history
  • Loading branch information
saint11 committed Dec 17, 2023
2 parents 8fdcf82 + 31b61a4 commit 26caf58
Show file tree
Hide file tree
Showing 19 changed files with 168 additions and 56 deletions.
14 changes: 14 additions & 0 deletions src/Murder.Editor/CustomEditors/CharacterEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ private void InitializeStage(Stage stage, ScriptInformation info)
stage.EditorHook.OnNodeSelected += SelectNode;
}

public override void UpdateEditor()
{
GameLogger.Verify(_script is not null);

if (!ActiveEditors.TryGetValue(_script.Guid, out var info))
{
GameLogger.Warning("Unitialized stage for particle editor?");
return;
}

Stage stage = info.Stage;
stage.Update();
}

public override void DrawEditor()
{
GameLogger.Verify(_script is not null);
Expand Down
2 changes: 2 additions & 0 deletions src/Murder.Editor/CustomEditors/CustomEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public abstract class CustomEditor : IDisposable
public abstract void OpenEditor(
ImGuiRenderer imGuiRenderer, RenderContext renderContext, object target, bool overwrite);

public virtual void UpdateEditor() { }

public abstract void DrawEditor();

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions src/Murder.Editor/CustomEditors/ParticleSystemEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public override void OpenEditor(ImGuiRenderer imGuiRenderer, RenderContext rende
}
}

public override void UpdateEditor()
{
base.UpdateEditor();
}

public override void DrawEditor()
{
GameLogger.Verify(Stages is not null);
Expand Down
10 changes: 10 additions & 0 deletions src/Murder.Editor/CustomEditors/PrefabAssetEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ protected override void OnSwitchAsset(ImGuiRenderer imGuiRenderer, RenderContext

public override IEntity? SelectedEntity => _lastOpenedEntity;

public override void UpdateEditor()
{
GameLogger.Verify(_asset is not null);

if (Stages.ContainsKey(_asset.Guid))
{
Stages[_asset.Guid].Update();
}
}

public override void DrawEditor()
{
GameLogger.Verify(Stages is not null);
Expand Down
37 changes: 27 additions & 10 deletions src/Murder.Editor/CustomEditors/SpriteEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Murder.Core;
using Murder.Core.Geometry;
using Murder.Core.Graphics;
using Murder.Core.Input;
using Murder.Core.Sounds;
using Murder.Diagnostics;
using Murder.Editor.Assets;
Expand Down Expand Up @@ -70,6 +71,19 @@ private void InitializeStage(Stage stage, SpriteInformation info)
stage.EditorHook.CurrentZoomLevel = 6;
}

public override void UpdateEditor()
{
GameLogger.Verify(_sprite is not null);

if (!ActiveEditors.TryGetValue(_sprite.Guid, out SpriteInformation? info))
{
GameLogger.Warning("Unitialized stage for particle editor?");
return;
}

info.Stage.Update();
}

public override void DrawEditor()
{
GameLogger.Verify(_sprite is not null);
Expand Down Expand Up @@ -213,7 +227,7 @@ private void SelectAnimation(SpriteInformation info, string animation)
info.SelectedAnimation = animation;
info.Stage.AddOrReplaceComponentOnEntity(
info.HelperId,
new AnimationOverloadComponent(animation, loop: true, ignoreFacing: true));
new AnimationOverloadComponent(animation, loop: true, ignoreFacing: true, startTime: info.Hook.Time));
}

private int _value = 0;
Expand Down Expand Up @@ -273,13 +287,13 @@ private void DrawTimeline(SpriteInformation info)
{
if (_sprite.Animations.TryGetValue(info.SelectedAnimation, out Animation selectedAnimation))
{
if (Game.Instance.IsPaused && ImGui.Button("\uf04b"))
if (info.Hook.IsPaused && (ImGui.Button("\uf04b") || Game.Input.Pressed(MurderInputButtons.Space)))
{
Game.Instance.Resume();
info.Hook.IsPaused = false;
}
else if (!Game.Instance.IsPaused && ImGui.Button("\uf04c"))
else if (!info.Hook.IsPaused && (ImGui.Button("\uf04c") || Game.Input.Pressed(MurderInputButtons.Space)))
{
Game.Instance.Pause();
info.Hook.IsPaused = true;
}

ImGui.SameLine();
Expand Down Expand Up @@ -370,18 +384,21 @@ private void DrawTimeline(SpriteInformation info)
}
}

float rate = (info.Hook.TimeSinceAnimationStarted % selectedAnimation.AnimationDuration) / selectedAnimation.AnimationDuration;

Vector2 arrowPosition = position + new Vector2(padding * 3 + (area.X - padding * 5) * rate, 0);
drawList.AddTriangleFilled(arrowPosition + new Vector2(-6, 0), arrowPosition + new Vector2(6, 0), arrowPosition + new Vector2(0, 20), arrowColor);
drawList.AddLine(arrowPosition, arrowPosition + new Vector2(0, area.Y), arrowColor);
float rate = (info.Hook.Time % selectedAnimation.AnimationDuration) / selectedAnimation.AnimationDuration;

if (new Rectangle(position, area).Contains(ImGui.GetMousePos()) && ImGui.IsMouseDown(ImGuiMouseButton.Left))
{
float mouseX = ImGui.GetMousePos().X - position.X;

info.AnimationProgress = Calculator.Clamp01(mouseX / (area.X - padding * 2));
rate = info.AnimationProgress;
info.Hook.Time = info.AnimationProgress * selectedAnimation.AnimationDuration;
}

Vector2 arrowPosition = position + new Vector2(padding * 3 + (area.X - padding * 5) * rate, 0);

drawList.AddTriangleFilled(arrowPosition + new Vector2(-6, 0), arrowPosition + new Vector2(6, 0), arrowPosition + new Vector2(0, 20), arrowColor);
drawList.AddLine(arrowPosition, arrowPosition + new Vector2(0, area.Y), arrowColor);
}
}
else
Expand Down
8 changes: 8 additions & 0 deletions src/Murder.Editor/CustomEditors/WorldAssetEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ protected override void InitializeStage(Stage stage, Guid guid)
float _entitiesPickerSize = 200;
float _entityInspectorSize = -1;

public override void UpdateEditor()
{
GameLogger.Verify(Stages.ContainsKey(_asset!.Guid));

Stage currentStage = Stages[_asset.Guid];
currentStage.Update();
}

public override void DrawEditor()
{
GameLogger.Verify(Stages.ContainsKey(_asset!.Guid));
Expand Down
7 changes: 7 additions & 0 deletions src/Murder.Editor/EditorScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ public override void ReloadImpl()
_initializedEditors = false;
}

public override void Update()
{
base.Update();

UpdateSelectedEditor();
}

public override void Draw()
{
// We don't need to draw the world when in the editor scene
Expand Down
13 changes: 13 additions & 0 deletions src/Murder.Editor/EditorScene_Editors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ public void Dispose()

private bool _initializedEditors = false;

private CustomEditorInstance? _lastActiveEditorInstance = null;

private void UpdateSelectedEditor()
{
if (_lastActiveEditorInstance is null)
{
return;
}

_lastActiveEditorInstance.Editor.UpdateEditor();
}

private void DrawAssetEditors()
{
GameAsset? closeTab = null;
Expand Down Expand Up @@ -104,6 +116,7 @@ private void DrawSelectedAsset(GameAsset asset)
ImGui.Spacing();

CustomEditorInstance? customEditor = GetOrCreateAssetEditor(asset);
_lastActiveEditorInstance = customEditor;

// Draw the editor header
if (ImGui.BeginChild("Asset Editor", new System.Numerics.Vector2(-1, -1), ImGuiChildFlags.None))
Expand Down
27 changes: 15 additions & 12 deletions src/Murder.Editor/Stage/Stage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ private void InitializeDrawAndWorld()
_world.Start();
}

public void Update()
{
// Only update the stage if it's active.
if (Game.Instance.IsActive)
{
_world.Update();
}

if (Game.NowUnscaled >= _targetFixedUpdateTime)
{
_world.FixedUpdate();
_targetFixedUpdateTime = Game.NowUnscaled + Game.FixedDeltaTime;
}
}

public void Draw(Rectangle? rectToDrawStage = null)
{
if (!_calledStart)
Expand Down Expand Up @@ -188,18 +203,6 @@ private static void DrawTextRoundedRect(ImDrawListPtr drawList, Vector2 position

private void DrawWorld()
{
// Only update the stage if it's active.
if (Game.Instance.IsActive)
{
_world.Update();
}

if (Game.NowUnscaled >= _targetFixedUpdateTime)
{
_world.FixedUpdate();
_targetFixedUpdateTime = Game.NowUnscaled + Game.FixedDeltaTime;
}

_renderContext.Begin();
_world.Draw(_renderContext);
_world.DrawGui(_renderContext);
Expand Down
9 changes: 4 additions & 5 deletions src/Murder.Editor/Systems/Editor/TimelineSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bang.Contexts;
using Bang;
using Bang.Contexts;
using Bang.Entities;
using Bang.Systems;
using Murder.Components;
Expand All @@ -21,13 +22,11 @@ public void Update(Context context)
return;
}

if (context.World.GetEntitiesWith(typeof(SpriteComponent)).FirstOrDefault() is not Entity entity)
if (hook.IsPaused)
{
return;
}

SpriteComponent sprite = entity.GetSprite();

hook.TimeSinceAnimationStarted = Game.Now - sprite.AnimationStartedTime;
hook.Time += Game.DeltaTime;
}
}
10 changes: 8 additions & 2 deletions src/Murder.Editor/Systems/EntitiesPlacerSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Murder.Components;
using Murder.Core;
using Murder.Core.Geometry;
using Murder.Core.Graphics;
using Murder.Core.Input;
using Murder.Editor.Attributes;
using Murder.Editor.Components;
Expand All @@ -22,12 +23,11 @@ namespace Murder.Editor.Systems
[WorldEditor(startActive: true)]
[Watch(typeof(IsPlacingComponent))]
[Filter(ContextAccessorFilter.AllOf, ContextAccessorKind.Read, typeof(IsPlacingComponent))]
internal class EntitiesPlacerSystem : IUpdateSystem, IReactiveSystem
internal class EntitiesPlacerSystem : IUpdateSystem, IReactiveSystem, IMurderRenderSystem
{
public void Update(Context context)
{
EditorHook hook = context.World.GetUnique<EditorComponent>().EditorHook;
DrawCreateEmptyEntity(context.World, hook);

if (!hook.IsMouseOnStage || hook.EntityToBePlaced is null)
{
Expand Down Expand Up @@ -146,5 +146,11 @@ private bool DrawCreateEmptyEntity(World world, EditorHook hook)

return true;
}

public void Draw(RenderContext render, Context context)
{
EditorHook hook = context.World.GetUnique<EditorComponent>().EditorHook;
DrawCreateEmptyEntity(context.World, hook);
}
}
}
14 changes: 10 additions & 4 deletions src/Murder.Editor/Systems/SpriteRenderDebugSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Murder.Core.Geometry;
using Murder.Core.Graphics;
using Murder.Editor.Components;
using Murder.Editor.Utilities;
using Murder.Helpers;
using Murder.Messages;
using Murder.Services;
Expand All @@ -25,7 +26,7 @@ internal class SpriteRenderDebugSystem : IMurderRenderSystem
{
public void Draw(RenderContext render, Context context)
{
var hook = context.World.GetUnique<EditorComponent>().EditorHook;
EditorHook hook = context.World.GetUnique<EditorComponent>().EditorHook;

foreach (var e in context.Entities)
{
Expand Down Expand Up @@ -169,6 +170,12 @@ public void Draw(RenderContext render, Context context)
scale -= Ease.ElasticIn(.9f - modifier * .9f) * scale;
}

float overrideCurrentTime = -1;
if (hook is TimelineEditorHook timeline)
{
overrideCurrentTime = timeline.Time;
}

FrameInfo frameInfo = RenderServices.DrawSprite(
batch,
asset.Guid,
Expand All @@ -183,7 +190,7 @@ public void Draw(RenderContext render, Context context)
Color = baseColor,
Outline = e.HasComponent<IsSelectedComponent>() ? Color.White.FadeAlpha(0.65f) : null,
},
new AnimationInfo(animationId, start) with { UseScaledTime = true });
new AnimationInfo(animationId, start) with { OverrideCurrentTime = overrideCurrentTime });

if (frameInfo.Complete && overload != null)
{
Expand Down Expand Up @@ -224,9 +231,8 @@ public void Draw(RenderContext render, Context context)
Color = baseColor * reflection.Alpha,
Scale = scale * new Vector2(1, -1),
},
new AnimationInfo(animationId, start) with { UseScaledTime = true });
new AnimationInfo(animationId, start) with { OverrideCurrentTime = overrideCurrentTime });
}

}
}

Expand Down
4 changes: 3 additions & 1 deletion src/Murder.Editor/Utilities/Stage/TimelineEditorHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

internal class TimelineEditorHook : EditorHook
{
public float TimeSinceAnimationStarted = 0;
public float Time = 0;

public bool IsPaused = false;
}
13 changes: 10 additions & 3 deletions src/Murder/Components/Agents/AnimationOverloadComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public AnimationOverloadComponent() { }
public AnimationOverloadComponent(string animationId, bool loop, bool ignoreFacing) : this(animationId, -1, loop, ignoreFacing)
{ }

public AnimationOverloadComponent(string animationId, bool loop, bool ignoreFacing, float startTime) : this(animationId, -1, loop, ignoreFacing, startTime)
{ }

public AnimationOverloadComponent(string animationId, float duration, bool loop, bool ignoreFacing, int current, float sortOffset, Guid customSprite) :
this(ImmutableArray.Create(animationId), duration, loop, ignoreFacing, current, sortOffset, customSprite)
{ }
Expand All @@ -89,16 +92,20 @@ public AnimationOverloadComponent(ImmutableArray<string> animations, float durat
this(animations, duration, loop, ignoreFacing, current, sortOffset, customSprite, Game.Now)
{ }

public AnimationOverloadComponent(string animationId, float duration, bool loop, bool ignoreFacing)
public AnimationOverloadComponent(string animationId, float duration, bool loop, bool ignoreFacing, float startTime)
{
_animationId = ImmutableArray.Create(animationId);
_animationId = [animationId];
Duration = duration;
Loop = loop;
IgnoreFacing = ignoreFacing;

Start = Game.Now;
Start = startTime;
_customSprite = Guid.Empty;
}

public AnimationOverloadComponent(string animationId, float duration, bool loop, bool ignoreFacing) : this(animationId, duration, loop, ignoreFacing, Game.Now)
{
}

public AnimationOverloadComponent(string animationId, Guid customSprite, float start, bool loop, bool ignoreFacing) :
this(ImmutableArray.Create(animationId), customSprite, start, loop, ignoreFacing)
Expand Down
Loading

0 comments on commit 26caf58

Please sign in to comment.