Rigs, routes and decisions ​

csharp
public sealed class Mouse : MonoBehaviour
{
    public string Art;
    public Cell Hole;
    public Cell Target;
    private Rig rig;
    private Route route;
    private Decisions decisions;

    private void Start()
    {
        rig = Rig.Create("Mouse", transform)
            .Part("body", Path.Combine(Art, "body.obj"), Path.Combine(Art, "mouse.png"))
            .Part("tail", Path.Combine(Art, "tail.obj"), Path.Combine(Art, "mouse.png"), "body", new Vector3(0f, 0.1f, -0.2f));
        rig.Clip("run", 0.3f)
            .Key("body", 0f, AnimationChannel.OffsetY, 0f)
            .Key("body", 0.15f, AnimationChannel.OffsetY, 0.06f)
            .Swing("tail", AnimationChannel.Yaw, 25f, 0f, 2f);
        rig.Play("run");
        route = new Route(Kitchen.PosOf(Hole, 0f)).Hop(Kitchen.PosOf(Target, 1f), 0.4f);
        decisions = new Decisions("my.addon/mouse");
        decisions.Decided += (key, value) => Debug.Log(key + " = " + value);
    }

    private void Update()
    {
        decisions.Tick(Time.deltaTime);
        float run = Mathf.Max(0f, Kitchen.RoundTime - 20f) * 2.5f;
        rig.Root.transform.position = route.At(run);
        rig.Root.transform.rotation = Quaternion.LookRotation(route.HeadingAt(run));
        if (run >= route.Length && Kitchen.IsHost)
            decisions.Decide("trip0", "arrived");
    }

    private void OnDestroy()
    {
        decisions.Dispose();
    }
}

Twenty seconds into the round a mouse runs out of its hole, its body bobbing and its tail swinging, and hops onto a counter. Its place comes from the round clock, so it is in the same place on every machine. The host decides when it has arrived, and every machine hears it once.

Rig ​

csharp
public sealed class Rig
{
    public static Rig Create(string name, Transform parent = null);
    public GameObject Root { get; }
    public Transform this[string part] { get; }
    public Rig Part(string part, string objPath, string pngPath, string attachTo = null, Vector3 at = default, Vector3 euler = default, float scale = 1f);
    public Rig Part(string part, Mesh mesh, Material material, string attachTo = null, Vector3 at = default, Vector3 euler = default, float scale = 1f);
    public Rig Part(string part, GameObject model, string attachTo = null, Vector3 at = default, Vector3 euler = default, float scale = 1f);
    public RigClip Clip(string name, float length, bool loop = true);
    public void Play(string clip, float fade = 0.15f, float speed = 1f);
    public void Sample(string clip, float seconds);
    public string Playing { get; }
    public float Speed { get; set; }
    public float Time { get; }
}
  • Part: a mesh with its pivot at the OBJ's origin, hung on the root or on the part named by attachTo, at at and turned by euler. A GameObject with no mesh is a joint for other parts.
  • Clip: a clip of length seconds. A looping clip eases from its last key back to its first; one that doesn't loop holds its last pose.
  • Play: plays a clip from its start and cross-fades from the pose before. Playing the clip that is already playing changes only its speed.
  • Sample: poses the rig at an exact time in a clip and stops it playing by itself. Drive gates and carts this way from the round clock, so every machine shows the same pose.

RigClip ​

csharp
public sealed class RigClip
{
    public string Name { get; }
    public float Length { get; }
    public bool Loop { get; }
    public RigClip Key(string part, float time, AnimationChannel channel, float value);
    public RigClip Swing(string part, AnimationChannel channel, float amplitude, float phase = 0f, float cycles = 1f);
    public float Value(string part, AnimationChannel channel, float time);
}

A channel's value is its keyed value, eased from key to key, or its rest value without keys, plus its swings. Offsets are in metres, turns in degrees from the part's own place, and Scale and ScaleY multiply its size. The channels are the animation channels. A swing is a sine of amplitude either way, cycles times over the clip; whole cycles keep a looping clip seamless.

Route ​

csharp
public sealed class Route
{
    public Route(Vector3 start);
    public static Route Through(params Vector3[] path);
    public Route To(Vector3 point);
    public Route Hop(Vector3 point, float height);
    public Route Then(Route other);
    public Route Reversed();
    public float Length { get; }
    public int Count { get; }
    public Vector3 this[int index] { get; }
    public float DistanceTo(int index);
    public Vector3 At(float distance);
    public Vector3 HeadingAt(float distance);
    public bool Hopping(float distance);
}
  • Legs are straight lines between points. Length and DistanceTo measure along the floor, so a hop up onto a counter is no longer than walking to it.
  • Hop: a leg that arcs height metres over the straight line, for jumping onto or off a counter.
  • At, HeadingAt: the position and the flat facing at a distance along the route, clamped to its ends. Work the distance out from Kitchen.RoundTime.
  • Reversed: the same path back, hops included.

Decisions ​

csharp
public sealed class Decisions : IDisposable
{
    public Decisions(string kind);
    public string Kind { get; }
    public bool IsHost { get; }
    public int Count { get; }
    public bool TryGet(string key, out string value);
    public bool Decide(string key, string value);
    public void Ask(string key, string value);
    public void Tick(float deltaSeconds);
    public event Action<string, string> Decided;
    public event Action<string, string, ulong> Asked;
}

Some things can't come from the round clock alone: a chef shooed the rat, the rat got the plate. The host decides them once, and every machine sees the same outcome.

  • kind: the Lobby message kind, prefixed with your addon id. Make one set per level; a restart makes a new set.
  • Decide: the host decides a key. The first decision stands. False on a guest, and for a key already decided.
  • Ask: a guest asks the host to decide, for example when its player pressed a button. On the host it raises Asked at once.
  • Decided: raised once for each key on every machine: on the host when it decides, on a guest when the host's word arrives. Guests take decisions only from the lobby's host.
  • Tick: call it every frame. Online, the host sends all its decisions again every second, so a guest whose kitchen loads late still gets them.

Offline this machine is the host. Keys and values are one line without tabs.