Skip to content

Instantly share code, notes, and snippets.

@Frooxius
Last active August 12, 2018 15:57
Show Gist options
  • Save Frooxius/db1ccdd68aafa2516220a31970b0d0ff to your computer and use it in GitHub Desktop.
Save Frooxius/db1ccdd68aafa2516220a31970b0d0ff to your computer and use it in GitHub Desktop.
Grab Instancer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FrooxEngine
{
[Category("Transform/Interaction")]
[SingleInstancePerSlot]
public class GrabInstancer : Component, IGrabbable
{
public readonly SyncRef<Slot> Template;
public readonly Sync<bool> ActivateRoot;
public readonly Sync<bool> EnableGrabbable;
public readonly Sync<int> GrabPriority;
int IGrabbable.GrabPriority => GrabPriority.Value;
public bool IsGrabbed => false;
public bool CanGrab => Enabled && Template.Target != null;
public bool Scalable => false;
public Grabber Grabber => null;
protected override void OnAwake()
{
ActivateRoot.Value = true;
EnableGrabbable.Value = true;
}
public IGrabbable Grab(Grabber grabber, Slot holdSlot)
{
var instance = Template.Target.Duplicate(Slot.Parent);
var grabbable = instance.GetComponentInChildren<IGrabbable>();
if (grabbable == null)
throw new Exception("The template doesn't have an IGrabbable component!");
if (grabbable is GrabInstancer)
throw new Exception("The template cannot be another GrabInstancer");
if (ActivateRoot.Value)
instance.ActiveSelf = true;
if (EnableGrabbable.Value)
grabbable.Enabled = true;
return grabbable.Grab(grabber, holdSlot);
}
public void Release(Grabber grabber)
{
throw new NotSupportedException("GrabInstancer cannot be grab released");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment