diff --git a/Distribution/Restock/GameData/ReStock/Patches/Ground/restock-wheels.cfg b/Distribution/Restock/GameData/ReStock/Patches/Ground/restock-wheels.cfg index 4d1ce4f1..da309570 100644 --- a/Distribution/Restock/GameData/ReStock/Patches/Ground/restock-wheels.cfg +++ b/Distribution/Restock/GameData/ReStock/Patches/Ground/restock-wheels.cfg @@ -32,15 +32,15 @@ MODULE { - name = ModuleRestockLookAtConstraint + name = ModuleRestockConstraints - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = Suspension1 targetName = Suspension2 } - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = Suspension2 targetName = Suspension1 @@ -130,82 +130,69 @@ MODULE { - name = FXModuleLookAtConstraint + name = ModuleRestockConstraints - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = Link1 targetName = Link1Target } - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = Link2 targetName = Link2Target } - } - - MODULE - { - name = FXModuleConstrainPosition - - matchRotation = true - matchPosition = false - CONSTRAINFX + COPYROTATIONCONSTRAINT { - moversName = LowerBracket + rotatorsName = LowerBracket targetName = Base } - CONSTRAINFX + COPYROTATIONCONSTRAINT { - moversName = SteeringPivotFollower + rotatorsName = SteeringPivotFollower targetName = SteeringPivot } - CONSTRAINFX + COPYROTATIONCONSTRAINT { - moversName = WheelPivotFollower + rotatorsName = WheelPivotFollower targetName = WheelPivot } - } - - MODULE - { - name = FXModuleLookAtConstraint - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = ShockCylinder targetName = ShockPiston } - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = ShockPiston targetName = ShockCylinder } - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = SteeringPiston1 targetName = SteeringCylinder1 } - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = SteeringCylinder1 targetName = SteeringPiston1 } - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = SteeringPiston2 targetName = SteeringCylinder2 } - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = SteeringCylinder2 targetName = SteeringPiston2 @@ -241,27 +228,27 @@ MODULE { - name = ModuleRestockLookAtConstraint + name = ModuleRestockConstraints - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = DamperCylinder targetName = DamperPiston } - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = DamperPiston targetName = DamperCylinder } - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = SteeringCylinder1 targetName = SteeringPiston1 } - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = SteeringPiston1 targetName = SteeringCylinder1 diff --git a/Distribution/Restock/GameData/ReStock/PatchesMH/Ground/restock-mh-wheels.cfg b/Distribution/Restock/GameData/ReStock/PatchesMH/Ground/restock-mh-wheels.cfg index 397e8269..2c7b6ea2 100644 --- a/Distribution/Restock/GameData/ReStock/PatchesMH/Ground/restock-mh-wheels.cfg +++ b/Distribution/Restock/GameData/ReStock/PatchesMH/Ground/restock-mh-wheels.cfg @@ -103,67 +103,51 @@ MODULE { - name = ModuleAdvancedLookAtConstraint + name = ModuleRestockConstraints - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = Link1 targetName = Link1Target } - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = Link2 targetName = Link2Target } - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = SuspensionPiston targetName = SuspensionPistonTarget } - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = SuspensionCylinder targetName = SuspensionCylinderTarget } - } - - MODULE - { - name = FXModuleConstrainPosition - - matchRotation = true - matchPosition = false - CONSTRAINFX + COPYROTATIONCONSTRAINT { - moversName = LowerBracket + rotatorsName = LowerBracket targetName = UpperBracket } - CONSTRAINFX + COPYROTATIONCONSTRAINT { - moversName = SteeringPivotFollower + rotatorsName = SteeringPivotFollower targetName = SteeringPivot } - } - - MODULE - { - name = FXModuleConstrainPosition - - matchRotation = false - matchPosition = true - CONSTRAINFX + COPYPOSITIONCONSTRAINT { moversName = WheelMesh targetName = WheelPivotFollower } - CONSTRAINFX + COPYPOSITIONCONSTRAINT { moversName = WheelMeshBusted targetName = WheelPivotFollower diff --git a/Distribution/Restock/GameData/ReStock/Plugins/Restock.dll b/Distribution/Restock/GameData/ReStock/Plugins/Restock.dll index bf2d6b2b..a2c920e4 100644 Binary files a/Distribution/Restock/GameData/ReStock/Plugins/Restock.dll and b/Distribution/Restock/GameData/ReStock/Plugins/Restock.dll differ diff --git a/Distribution/RestockPlus/GameData/ReStockPlus/Parts/Ground/restock-wheel-1-T.cfg b/Distribution/RestockPlus/GameData/ReStockPlus/Parts/Ground/restock-wheel-1-T.cfg index f1a1c19a..5710273e 100644 --- a/Distribution/RestockPlus/GameData/ReStockPlus/Parts/Ground/restock-wheel-1-T.cfg +++ b/Distribution/RestockPlus/GameData/ReStockPlus/Parts/Ground/restock-wheel-1-T.cfg @@ -138,15 +138,15 @@ PART MODULE { - name = ModuleRestockLookAtConstraint + name = ModuleRestockConstraints - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = Suspension1 targetName = Suspension2 } - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = Suspension2 targetName = Suspension1 diff --git a/Distribution/RestockPlus/GameData/ReStockPlus/Parts/Ground/restock-wheel-4.cfg b/Distribution/RestockPlus/GameData/ReStockPlus/Parts/Ground/restock-wheel-4.cfg index d8574555..e585a789 100644 --- a/Distribution/RestockPlus/GameData/ReStockPlus/Parts/Ground/restock-wheel-4.cfg +++ b/Distribution/RestockPlus/GameData/ReStockPlus/Parts/Ground/restock-wheel-4.cfg @@ -235,67 +235,51 @@ PART MODULE { - name = ModuleRestockLookAtConstraint + name = ModuleRestockConstraints - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = Link1 targetName = Link1Target } - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = Link2 targetName = Link2Target } - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = SuspensionPiston targetName = SuspensionPistonTarget } - CONSTRAINLOOKFX + LOOKATCONSTRAINT { rotatorsName = SuspensionCylinder targetName = SuspensionCylinderTarget } - } - - MODULE - { - name = FXModuleConstrainPosition - - matchRotation = true - matchPosition = false - CONSTRAINFX + COPYROTATIONCONSTRAINT { - moversName = LowerBracket + rotatorsName = LowerBracket targetName = UpperBracket } - CONSTRAINFX + COPYROTATIONCONSTRAINT { - moversName = SteeringPivotFollower + rotatorsName = SteeringPivotFollower targetName = SteeringPivot } - } - - MODULE - { - name = FXModuleConstrainPosition - - matchRotation = false - matchPosition = true - CONSTRAINFX + COPYPOSITIONCONSTRAINT { moversName = WheelMesh targetName = WheelPivotFollower } - CONSTRAINFX + COPYPOSITIONCONSTRAINT { moversName = WheelMeshBusted targetName = WheelPivotFollower diff --git a/Source/Restock/Constraints/CopyPositionConstraint.cs b/Source/Restock/Constraints/CopyPositionConstraint.cs new file mode 100644 index 00000000..dceb4734 --- /dev/null +++ b/Source/Restock/Constraints/CopyPositionConstraint.cs @@ -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; + } + } + } +} \ No newline at end of file diff --git a/Source/Restock/Constraints/CopyRotationConstraint.cs b/Source/Restock/Constraints/CopyRotationConstraint.cs new file mode 100644 index 00000000..99b71b6f --- /dev/null +++ b/Source/Restock/Constraints/CopyRotationConstraint.cs @@ -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; + } + } + } +} \ No newline at end of file diff --git a/Source/Restock/Constraints/IConstraint.cs b/Source/Restock/Constraints/IConstraint.cs new file mode 100644 index 00000000..76b8ffa6 --- /dev/null +++ b/Source/Restock/Constraints/IConstraint.cs @@ -0,0 +1,10 @@ +using System; +using UnityEngine; + +namespace Restock.Constraints +{ + public interface IConstraint + { + void Update(); + } +} \ No newline at end of file diff --git a/Source/Restock/Constraints/LookAtConstraint.cs b/Source/Restock/Constraints/LookAtConstraint.cs new file mode 100644 index 00000000..ef31cc9f --- /dev/null +++ b/Source/Restock/Constraints/LookAtConstraint.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/Source/Restock/ModuleRestockConstraints.cs b/Source/Restock/ModuleRestockConstraints.cs new file mode 100644 index 00000000..3ea46a90 --- /dev/null +++ b/Source/Restock/ModuleRestockConstraints.cs @@ -0,0 +1,79 @@ +using System.Collections.Generic; +using System.Linq; +using Restock.Constraints; + +namespace Restock +{ + public class ModuleRestockConstraints : PartModule + { + public List constraints; + + public override void OnLoad(ConfigNode node) + { + base.OnLoad(node); + + constraints = new List(); + var cnodes = node.GetNodes(); + this.Log($"Loading {cnodes.Length} constraints"); + + foreach (var cnode in cnodes) + { + switch (cnode.name) + { + //LookAtConstraint + case "CONSTRAINLOOKFX": + case "LOOKATCONSTRAINT": + constraints.Add(new LookAtConstraint(cnode, this.part)); + break; + + //CopyPositionConstraint + case "COPYPOSITIONCONSTRAINT": + constraints.Add(new CopyPositionConstraint(cnode, this.part)); + break; + + //CopyRotationConstraint + case "COPYROTATIONCONSTRAINT": + constraints.Add(new CopyRotationConstraint(cnode, this.part)); + break; + + //Unknown + default: + this.LogError($"Unknown constraint type \"{cnode.name}\""); + break; + } + } + + this.Log($"Loaded {constraints.Count} constraints"); + } + + public override void OnStart(StartState state) + { + if (HighLogic.LoadedSceneIsFlight || HighLogic.LoadedSceneIsEditor) + { + if (constraints == null || constraints.Count == 0) + { + ConfigNode cfg; + foreach (UrlDir.UrlConfig pNode in GameDatabase.Instance.GetConfigs("PART")) + { + if (pNode.name.Replace("_", ".") == part.partInfo.name) + { + cfg = pNode.config; + ConfigNode node = cfg.GetNodes("MODULE").Single(n => n.GetValue("name") == moduleName); + OnLoad(node); + } + } + } + } + } + + void LateUpdate() + { + if (constraints == null) return; + + foreach (var constraint in constraints) + { + constraint.Update(); + } + } + } +} \ No newline at end of file diff --git a/Source/Restock/ModuleRestockLookAtConstraint.cs b/Source/Restock/ModuleRestockLookAtConstraint.cs deleted file mode 100644 index 8ef71f2c..00000000 --- a/Source/Restock/ModuleRestockLookAtConstraint.cs +++ /dev/null @@ -1,95 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using UnityEngine; - -namespace Restock -{ - - public class ModuleRestockLookAtConstraint : PartModule - { - - [System.Serializable] - public class LookConstraint - { - string rotatorsName; - string targetName; - - // Cached components - Transform target; - Transform rotator; - Part part; - - public LookConstraint(ConfigNode node, Part p) - { - node.TryGetValue("rotatorsName", ref rotatorsName); - node.TryGetValue("targetName", ref targetName); - part = p; - rotator = p.FindModelTransform(rotatorsName); - target = p.FindModelTransform(targetName); - } - - public void UpdateRotators() - { - if (rotator != null && target != null) - { - Vector3 targetPostition = new Vector3(target.position.x, - target.position.y, - target.position.z); - - Vector3 lookPos = target.position - rotator.position; - var rotation = Quaternion.LookRotation(lookPos, target.up); - rotator.rotation = rotation; - } - } - } - - public List constraints; - - public override void OnLoad(ConfigNode node) - { - base.OnLoad(node); - constraints = new List(); - ConfigNode[] cnodes = node.GetNodes("CONSTRAINLOOKFX"); - Debug.Log(String.Format("[ModuleAdvancedLookAtConstraint]: Loading {0} constraints", cnodes.Length)); - - for (int i = 0; i < cnodes.Length; i++) - { - constraints.Add(new LookConstraint(cnodes[i], this.part)); - } - - Debug.Log(String.Format("[ModuleAdvancedLookAtConstraint]: Loaded {0} constraints", constraints.Count)); - } - public override void OnStart(StartState state) - { - if (HighLogic.LoadedSceneIsFlight || HighLogic.LoadedSceneIsEditor) - { - if (constraints == null || constraints.Count == 0) - { - ConfigNode cfg; - foreach (UrlDir.UrlConfig pNode in GameDatabase.Instance.GetConfigs("PART")) - { - if (pNode.name.Replace("_", ".") == part.partInfo.name) - { - cfg = pNode.config; - ConfigNode node = cfg.GetNodes("MODULE").Single(n => n.GetValue("name") == moduleName); - OnLoad(node); - } - } - } - } - } - - void LateUpdate() - { - if (constraints != null) - { - for (int i = 0; i < constraints.Count; i++) - { - constraints[i].UpdateRotators(); - } - } - } - } -} diff --git a/Source/Restock/Restock.csproj b/Source/Restock/Restock.csproj index c4dd63fa..8c469675 100644 --- a/Source/Restock/Restock.csproj +++ b/Source/Restock/Restock.csproj @@ -44,6 +44,10 @@ + + + + @@ -55,7 +59,7 @@ - +