Convert ModuleRestockLookAtConstrain into ModuleRestockConstraints
New module is extendable and modular, allowing constraints to be run in the expected order without relying on possibly undefined behaviorpull/822/head
parent
073b7d2948
commit
931d891644
Binary file not shown.
@ -0,0 +1,40 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace Restock.Constraints
|
||||||
|
{
|
||||||
|
[System.Serializable]
|
||||||
|
public class CopyPositionConstraint : IConstraint
|
||||||
|
{
|
||||||
|
private readonly string moversName;
|
||||||
|
private readonly string targetName;
|
||||||
|
|
||||||
|
private readonly bool local = false;
|
||||||
|
|
||||||
|
// Cached components
|
||||||
|
private readonly Transform mover;
|
||||||
|
private readonly Transform target;
|
||||||
|
|
||||||
|
public CopyPositionConstraint(ConfigNode node, Part p)
|
||||||
|
{
|
||||||
|
node.TryGetValue("rotatorsName", ref moversName);
|
||||||
|
node.TryGetValue("targetName", ref targetName);
|
||||||
|
|
||||||
|
mover = p.FindModelTransform(moversName);
|
||||||
|
target = p.FindModelTransform(targetName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update()
|
||||||
|
{
|
||||||
|
if (mover == null || target == null) return;
|
||||||
|
|
||||||
|
if (local)
|
||||||
|
{
|
||||||
|
mover.localPosition = target.localPosition;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mover.position = target.position;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace Restock.Constraints
|
||||||
|
{
|
||||||
|
[System.Serializable]
|
||||||
|
public class CopyRotationConstraint : IConstraint
|
||||||
|
{
|
||||||
|
private readonly string rotatorsName;
|
||||||
|
private readonly string targetName;
|
||||||
|
|
||||||
|
private readonly bool local = false;
|
||||||
|
|
||||||
|
// Cached components
|
||||||
|
private readonly Transform target;
|
||||||
|
private readonly Transform rotator;
|
||||||
|
|
||||||
|
public CopyRotationConstraint(ConfigNode node, Part p)
|
||||||
|
{
|
||||||
|
node.TryGetValue("rotatorsName", ref rotatorsName);
|
||||||
|
node.TryGetValue("targetName", ref targetName);
|
||||||
|
|
||||||
|
rotator = p.FindModelTransform(rotatorsName);
|
||||||
|
target = p.FindModelTransform(targetName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update()
|
||||||
|
{
|
||||||
|
if (rotator == null || target == null) return;
|
||||||
|
|
||||||
|
if (local)
|
||||||
|
{
|
||||||
|
rotator.localRotation = target.localRotation;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
rotator.rotation = target.rotation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
using System;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace Restock.Constraints
|
||||||
|
{
|
||||||
|
public interface IConstraint
|
||||||
|
{
|
||||||
|
void Update();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace Restock.Constraints
|
||||||
|
{
|
||||||
|
[System.Serializable]
|
||||||
|
public class LookAtConstraint : IConstraint
|
||||||
|
{
|
||||||
|
private readonly string rotatorsName;
|
||||||
|
private readonly string targetName;
|
||||||
|
|
||||||
|
// Cached components
|
||||||
|
private readonly Transform target;
|
||||||
|
private readonly Transform rotator;
|
||||||
|
|
||||||
|
public LookAtConstraint(ConfigNode node, Part p)
|
||||||
|
{
|
||||||
|
node.TryGetValue("rotatorsName", ref rotatorsName);
|
||||||
|
node.TryGetValue("targetName", ref targetName);
|
||||||
|
|
||||||
|
rotator = p.FindModelTransform(rotatorsName);
|
||||||
|
target = p.FindModelTransform(targetName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update()
|
||||||
|
{
|
||||||
|
if (rotator == null || target == null) return;
|
||||||
|
|
||||||
|
var lookPos = target.position - rotator.position;
|
||||||
|
var rotation = Quaternion.LookRotation(lookPos, target.up);
|
||||||
|
rotator.rotation = rotation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue