Skip to content

Commit

Permalink
Merge pull request #587 from ExtendRealityLtd/more-features
Browse files Browse the repository at this point in the history
More features
  • Loading branch information
thestonefox authored May 1, 2023
2 parents 66306c9 + 498ff2d commit 8360db9
Show file tree
Hide file tree
Showing 27 changed files with 1,611 additions and 59 deletions.
8 changes: 8 additions & 0 deletions Runtime/Cast/Event.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Runtime/Cast/Event/Proxy.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions Runtime/Cast/Event/Proxy/PointsCastEventProxyEmitter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
namespace Zinnia.Cast.Event.Proxy
{
using System;
using UnityEngine;
using UnityEngine.Events;
using Zinnia.Event.Proxy;
using Zinnia.Extension;

/// <summary>
/// Emits a <see cref="UnityEvent"/> with a <see cref="PointsCast.EventData"/> payload whenever <see cref="SingleEventProxyEmitter{TValue,TEvent}.Receive"/> is called.
/// </summary>
public class PointsCastEventProxyEmitter : RestrictableSingleEventProxyEmitter<PointsCast.EventData, PointsCastEventProxyEmitter.UnityEvent>
{
/// <summary>
/// The types of <see cref="GameObject"/> that can be used for the rule source.
/// </summary>
public enum RuleSourceType
{
/// <summary>
/// Use the actual <see cref="Collider"/> hit as the source for the rule.
/// </summary>
Collider,
/// <summary>
/// Use the parent <see cref="Rigidbody"/> hit as the target for the rule.
/// </summary>
Rigidbody
}

[Tooltip("The source GameObject to apply to the RestrictableSingleEventProxyEmitter.ReceiveValidity.")]
[SerializeField]
private RuleSourceType ruleSource;
/// <summary>
/// The source <see cref="GameObject"/> to apply to the <see cref="RestrictableSingleEventProxyEmitter.ReceiveValidity"/>.
/// </summary>
public RuleSourceType RuleSource
{
get
{
return ruleSource;
}
set
{
ruleSource = value;
}
}

/// <summary>
/// Defines the event with the specified state.
/// </summary>
[Serializable]
public class UnityEvent : UnityEvent<PointsCast.EventData> { }

/// <inheritdoc />
protected override object GetTargetToCheck()
{
if (Payload == null || Payload.HitData == null)
{
return null;
}

RaycastHit hitData = (RaycastHit)Payload.HitData;
switch (RuleSource)
{
case RuleSourceType.Collider:
return hitData.collider.gameObject;
case RuleSourceType.Rigidbody:
return hitData.collider.GetContainingTransform().gameObject;
}

return null;
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Cast/Event/Proxy/PointsCastEventProxyEmitter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Runtime/Cast/FixedLineCast.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
namespace Zinnia.Cast
{
using System;
using UnityEngine;
using Zinnia.Extension;

/// <summary>
/// Casts a straight line in the direction of the origin for a fixed length.
/// </summary>
[Obsolete("Use `StraightLineCast.ShouldFixLength` instead.")]
public class FixedLineCast : StraightLineCast
{
[Tooltip("The current length of the cast.")]
Expand Down Expand Up @@ -43,6 +45,20 @@ public virtual void SetCurrentLength(EventData data)
}
}

/// <summary>
/// Increments the current length of the cast by the given value.
/// </summary>
/// <param name="value">The value to increment the length by.</param>
public virtual void IncrementCurrentLength(float value)
{
if (!this.IsValidState())
{
return;
}

CurrentLength += value;
}

/// <inheritdoc />
protected override void GeneratePoints()
{
Expand Down
36 changes: 10 additions & 26 deletions Runtime/Cast/ParabolicLineCast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,6 @@ public virtual void SetMaximumLengthY(float value)
MaximumLength = new Vector2(MaximumLength.x, value);
}

protected override void OnEnable()
{
base.OnEnable();
curvePoints.Add(default);
curvePoints.Add(default);
curvePoints.Add(default);
curvePoints.Add(default);
}

protected override void OnDisable()
{
base.OnDisable();
curvePoints.Clear();
}

/// <inheritdoc />
protected override void DoCastPoints()
{
Expand All @@ -153,7 +138,8 @@ protected override void DoCastPoints()
/// <returns>The collision point or the point being the furthest away on the cast line if nothing is hit.</returns>
protected virtual Vector3 ProjectForward()
{
float rotation = Vector3.Dot(Vector3.up, Origin.transform.forward.normalized);
Vector3 actualForward = GetTrackedForward(Origin.transform.forward);
float rotation = Vector3.Dot(Vector3.up, actualForward.normalized);
float length = MaximumLength.x;

if ((rotation * 100f) > HeightLimitAngle)
Expand All @@ -162,7 +148,7 @@ protected virtual Vector3 ProjectForward()
length = MaximumLength.x * controllerRotationOffset * controllerRotationOffset;
}

Ray ray = new Ray(Origin.transform.position, Origin.transform.forward);
Ray ray = new Ray(Origin.transform.position, actualForward);
bool hasCollided = PhysicsCast.Raycast(PhysicsCast, ray, out RaycastHit hitData, length, Physics.IgnoreRaycastLayer);

// Adjust the cast length if something is blocking it.
Expand Down Expand Up @@ -195,8 +181,8 @@ protected virtual Vector3 ProjectDown(Vector3 downwardOrigin)

if (downRayHit)
{
point = ray.GetPoint(hitData.distance);
TargetHit = hitData;
RaycastHit actualHitData = GetActualTargetHit(hitData, downRayHit);
point = actualHitData.point;
}

return point;
Expand Down Expand Up @@ -260,13 +246,11 @@ protected virtual void GeneratePoints(Vector3 forward, Vector3 down)
forward = DestinationPointOverride != null ? (Vector3)DestinationPointOverride : forward;
down = DestinationPointOverride != null ? (Vector3)DestinationPointOverride : down;

if (curvePoints.Count >= 4)
{
curvePoints[0] = Origin.transform.position;
curvePoints[1] = forward + (Vector3.up * CurveOffset);
curvePoints[2] = down;
curvePoints[3] = down;
}
curvePoints.Clear();
curvePoints.Add(Origin.transform.position);
curvePoints.Add(forward + (Vector3.up * CurveOffset));
curvePoints.Add(down);
curvePoints.Add(down);

points.Clear();
foreach (Vector3 generatedPoint in BezierCurveGenerator.GeneratePoints(SegmentCount, curvePoints))
Expand Down
78 changes: 78 additions & 0 deletions Runtime/Cast/PointsCast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public void Clear()
[Serializable]
public class UnityEvent : UnityEvent<EventData> { }

[Header("Cast Settings")]
[Tooltip("The origin point for the cast.")]
[SerializeField]
private GameObject origin;
Expand Down Expand Up @@ -179,6 +180,40 @@ public RuleContainer TargetPointValidity
targetPointValidity = value;
}
}
[Tooltip("The amount of distance the cursor has to move before the destination of the cursor is updated to a new position.")]
[SerializeField]
private float cursorLockThreshold;
/// <summary>
/// The amount of distance the cursor has to move before the destination of the cursor is updated to a new position.
/// </summary>
public float CursorLockThreshold
{
get
{
return cursorLockThreshold;
}
set
{
cursorLockThreshold = value;
}
}
[Tooltip("The duration it takes to transition previous destination point to the current actual target point.")]
[SerializeField]
private float transitionDuration;
/// <summary>
/// The duration it takes to transition previous destination point to the current actual target point.
/// </summary>
public float TransitionDuration
{
get
{
return transitionDuration;
}
set
{
transitionDuration = value;
}
}

/// <summary>
/// An override for the destination location point in world space.
Expand All @@ -188,6 +223,7 @@ public RuleContainer TargetPointValidity
/// <summary>
/// Emitted whenever the cast result changes.
/// </summary>
[Header("Cast Events")]
public UnityEvent ResultsChanged = new UnityEvent();

/// <summary>
Expand Down Expand Up @@ -226,6 +262,14 @@ protected set
/// The data to emit with an event.
/// </summary>
protected readonly EventData eventData = new EventData();
/// <summary>
/// The origin forward that is being tracked by the drag delay.
/// </summary>
protected Vector3? trackedOriginForward = null;
/// <summary>
/// The reference to the output velocity.
/// </summary>
protected Vector3 outVelocity = Vector3.zero;

/// <summary>
/// Clears <see cref="Origin"/>.
Expand Down Expand Up @@ -313,16 +357,50 @@ public virtual void Process()

protected virtual void OnEnable()
{
points.Clear();
OnAfterTargetHitChange();
}

protected virtual void OnDisable()
{
TargetHit = null;
trackedOriginForward = null;
IsTargetHitValid = false;
points.Clear();
ClearDestinationPointOverride();
}

/// <summary>
///
/// </summary>
/// <param name="orignForward"></param>
protected virtual Vector3 GetTrackedForward(Vector3 orignForward)
{
trackedOriginForward = TransitionDuration.ApproxEquals(0f) || trackedOriginForward == null ? orignForward : Vector3.SmoothDamp((Vector3)trackedOriginForward, orignForward, ref outVelocity, TransitionDuration, Mathf.Infinity, Time.deltaTime);
return (Vector3)trackedOriginForward;
}

/// <summary>
///
/// </summary>
/// <param name="actualHitData"></param>
/// <param name="hasCollided"></param>
/// <returns></returns>
protected virtual RaycastHit GetActualTargetHit(RaycastHit actualHitData, bool hasCollided)
{
RaycastHit newTargetHit = actualHitData;
if (CursorLockThreshold > 0f && TargetHit != null)
{
RaycastHit existingTargetHit = (RaycastHit)TargetHit;
if (Vector3.Distance(actualHitData.point, existingTargetHit.point) <= CursorLockThreshold)
{
newTargetHit = existingTargetHit;
}
}
TargetHit = hasCollided ? newTargetHit : (RaycastHit?)null;
return newTargetHit;
}

/// <summary>
/// Called after <see cref="TargetHit"/> has been changed.
/// </summary>
Expand Down
Loading

0 comments on commit 8360db9

Please sign in to comment.