diff --git a/Distribution/Restock/GameData/ReStock/Patches/Ground/restock-wheels.cfg b/Distribution/Restock/GameData/ReStock/Patches/Ground/restock-wheels.cfg index abd29cc7..4d1ce4f1 100644 --- a/Distribution/Restock/GameData/ReStock/Patches/Ground/restock-wheels.cfg +++ b/Distribution/Restock/GameData/ReStock/Patches/Ground/restock-wheels.cfg @@ -32,7 +32,7 @@ MODULE { - name = ModuleAdvancedLookAtConstraint + name = ModuleRestockLookAtConstraint CONSTRAINLOOKFX { @@ -241,7 +241,7 @@ MODULE { - name = ModuleAdvancedLookAtConstraint + name = ModuleRestockLookAtConstraint CONSTRAINLOOKFX { diff --git a/Distribution/Restock/GameData/ReStock/Plugins/Restock.dll b/Distribution/Restock/GameData/ReStock/Plugins/Restock.dll index 57c0b585..bf2d6b2b 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 663f25a2..f1a1c19a 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,7 +138,7 @@ PART MODULE { - name = ModuleAdvancedLookAtConstraint + name = ModuleRestockLookAtConstraint CONSTRAINLOOKFX { 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 5cf5cc37..d8574555 100644 --- a/Distribution/RestockPlus/GameData/ReStockPlus/Parts/Ground/restock-wheel-4.cfg +++ b/Distribution/RestockPlus/GameData/ReStockPlus/Parts/Ground/restock-wheel-4.cfg @@ -235,7 +235,7 @@ PART MODULE { - name = ModuleAdvancedLookAtConstraint + name = ModuleRestockLookAtConstraint CONSTRAINLOOKFX { diff --git a/Source/Restock/ModuleRestockLookAtConstraint.cs b/Source/Restock/ModuleRestockLookAtConstraint.cs new file mode 100644 index 00000000..8ef71f2c --- /dev/null +++ b/Source/Restock/ModuleRestockLookAtConstraint.cs @@ -0,0 +1,95 @@ +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 da8753e9..c4dd63fa 100644 --- a/Source/Restock/Restock.csproj +++ b/Source/Restock/Restock.csproj @@ -55,6 +55,7 @@ +