Skip to content

Commit

Permalink
zzre: Fix box-shaped triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
Helco committed Jan 24, 2024
1 parent 0284b26 commit 19bac12
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 35 deletions.
8 changes: 4 additions & 4 deletions zzio/scn/Trigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Trigger : ISceneSection
public string s = "";
public Vector3 // TODO: Move Collider out
pos,
size; // only if type == Box
end; // only if type == Box
public float radius; // only if type == Sphere

public void Read(Stream stream)
Expand All @@ -44,12 +44,12 @@ public void Read(Stream stream)
ii4 = reader.ReadUInt32();
s = reader.ReadZString();
pos = reader.ReadVector3();
size = new Vector3();
end = new Vector3();
radius = 0.0f;
switch (colliderType)
{
case TriggerColliderType.Box:
size = reader.ReadVector3();
end = reader.ReadVector3();
break;
case TriggerColliderType.Sphere:
radius = reader.ReadSingle();
Expand All @@ -76,7 +76,7 @@ public void Write(Stream stream)
switch (colliderType)
{
case TriggerColliderType.Box:
writer.Write(size);
writer.Write(end);
break;
case TriggerColliderType.Sphere:
writer.Write(radius);
Expand Down
2 changes: 1 addition & 1 deletion zzre/game/systems/TriggerActivation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private bool ShouldBeActive(Location location, Trigger trigger)
case TriggerColliderType.Point: return false;

case TriggerColliderType.Box:
var box = new Box(Vector3.Zero, trigger.size);
var box = Box.FromMinMax(trigger.pos, trigger.end);
if (!box.Intersects(location, playerPos))
return false;
break;
Expand Down
39 changes: 12 additions & 27 deletions zzre/tools/sceneeditor/SceneEditor.ISelectable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ private class SelectionComponent : BaseDisposable
private readonly SceneEditor editor;
private readonly ITagContainer diContainer;
private readonly Camera camera;
private readonly DebugLineRenderer boxBoundsRenderer;
private readonly DebugLineRenderer sphereBoundsRenderer;
private readonly DebugLineRenderer boundsRenderer;

private IRenderable? activeBoundsRenderer = null;
private ISelectable[] lastPotentials = Array.Empty<ISelectable>();
private int lastPotentialI;

Expand All @@ -72,25 +70,21 @@ public SelectionComponent(ITagContainer diContainer)
var fbArea = diContainer.GetTag<FramebufferArea>();
var mouseEventArea = diContainer.GetTag<MouseEventArea>();

boxBoundsRenderer = new DebugLineRenderer(diContainer);
boxBoundsRenderer.Material.LinkTransformsTo(camera);
boxBoundsRenderer.Material.World.Value = Matrix4x4.Identity;
sphereBoundsRenderer = new DebugLineRenderer(diContainer);
sphereBoundsRenderer.Material.LinkTransformsTo(camera);
sphereBoundsRenderer.Material.World.Value = Matrix4x4.Identity;
boundsRenderer = new DebugLineRenderer(diContainer);
boundsRenderer.Material.LinkTransformsTo(camera);
boundsRenderer.Material.World.Value = Matrix4x4.Identity;

editor.Window.OnContent += HandleGizmos;
editor.OnLoadScene += () => editor.Selected = null;
editor.OnNewSelection += HandleNewSelection;
fbArea.OnRender += HandleRender;
fbArea.OnRender += boundsRenderer.Render;
mouseEventArea.OnButtonUp += HandleClick;
}

protected override void DisposeManaged()
{
base.DisposeManaged();
boxBoundsRenderer.Dispose();
sphereBoundsRenderer.Dispose();
boundsRenderer.Dispose();
}

private void HandleGizmos()
Expand All @@ -114,25 +108,16 @@ private void HandleGizmos()
private void HandleNewSelection(ISelectable? newSelected)
{
var bounds = newSelected?.RenderedBounds;
if (bounds is OrientedBox boxBounds)
{
boxBoundsRenderer.Clear();
boxBoundsRenderer.AddBox(boxBounds, IColor.Red);
activeBoundsRenderer = boxBoundsRenderer;
}
boundsRenderer.Clear();
if (bounds is OrientedBox orientedBoxBounds)
boundsRenderer.AddBox(orientedBoxBounds, IColor.Red);
else if (bounds is Box boxBounds)
boundsRenderer.AddBox(boxBounds, IColor.Red);
else if (bounds is Sphere sphereBounds)
{
sphereBoundsRenderer.Clear();
sphereBoundsRenderer.AddDiamondSphere(sphereBounds, IColor.Red);
activeBoundsRenderer = sphereBoundsRenderer;
}
else
activeBoundsRenderer = null;
boundsRenderer.AddDiamondSphere(sphereBounds, IColor.Red);
editor.fbArea.IsDirty = true;
}

private void HandleRender(CommandList cl) => activeBoundsRenderer?.Render(cl);

private void HandleClick(MouseButton button, Vector2 pos)
{
if (button != MouseButton.Left || ImGuizmo.IsOver() || ImGuizmo.IsUsing())
Expand Down
7 changes: 4 additions & 3 deletions zzre/tools/sceneeditor/SceneEditor.Trigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ private class Trigger : BaseDisposable, ISelectable

public IRaycastable? RenderedBounds => SceneTrigger.colliderType switch
{
TriggerColliderType.Box => new Box(Vector3.Zero, SceneTrigger.size).TransformToWorld(Location),
TriggerColliderType.Box => Box.FromMinMax(Location.GlobalPosition, Location.GlobalPosition + SceneTrigger.end - SceneTrigger.pos),
TriggerColliderType.Sphere => new Sphere(Location.GlobalPosition, SceneTrigger.radius),
TriggerColliderType.Point => null,
_ => throw new NotImplementedException("Unknown TriggerColliderType")
};
public float ViewSize => SceneTrigger.colliderType switch
{
TriggerColliderType.Box => SceneTrigger.size.MaxComponent(),
TriggerColliderType.Box => (SceneTrigger.end - SceneTrigger.pos).MaxComponent(),
TriggerColliderType.Sphere => SceneTrigger.radius * 2f,
TriggerColliderType.Point => PointTriggerSize * 2f,
_ => throw new NotImplementedException("Unknown TriggerColliderType")
Expand Down Expand Up @@ -75,7 +75,8 @@ public void Content()
break;

case TriggerColliderType.Box:
InputFloat3("Size", ref SceneTrigger.size);
var size = SceneTrigger.end - SceneTrigger.pos;
InputFloat3("Size", ref size);
break;
}
}
Expand Down

0 comments on commit 19bac12

Please sign in to comment.