mirror of
https://github.com/PorktoberRevolution/ReStocked
synced 2024-09-01 17:34:42 +00:00
Merge branch 'Wheels' into develop
This commit is contained in:
40
Source/Restock/Constraints/CopyPositionConstraint.cs
Normal file
40
Source/Restock/Constraints/CopyPositionConstraint.cs
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
40
Source/Restock/Constraints/CopyRotationConstraint.cs
Normal file
40
Source/Restock/Constraints/CopyRotationConstraint.cs
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
10
Source/Restock/Constraints/IConstraint.cs
Normal file
10
Source/Restock/Constraints/IConstraint.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Restock.Constraints
|
||||
{
|
||||
public interface IConstraint
|
||||
{
|
||||
void Update();
|
||||
}
|
||||
}
|
33
Source/Restock/Constraints/LookAtConstraint.cs
Normal file
33
Source/Restock/Constraints/LookAtConstraint.cs
Normal file
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
79
Source/Restock/ModuleRestockConstraints.cs
Normal file
79
Source/Restock/ModuleRestockConstraints.cs
Normal file
@ -0,0 +1,79 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Restock.Constraints;
|
||||
|
||||
namespace Restock
|
||||
{
|
||||
public class ModuleRestockConstraints : PartModule
|
||||
{
|
||||
public List<IConstraint> constraints;
|
||||
|
||||
public override void OnLoad(ConfigNode node)
|
||||
{
|
||||
base.OnLoad(node);
|
||||
|
||||
constraints = new List<IConstraint>();
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -44,6 +44,10 @@
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Constraints\CopyPositionConstraint.cs" />
|
||||
<Compile Include="Constraints\CopyRotationConstraint.cs" />
|
||||
<Compile Include="Constraints\IConstraint.cs" />
|
||||
<Compile Include="Constraints\LookAtConstraint.cs" />
|
||||
<Compile Include="InstallChecker.cs" />
|
||||
<Compile Include="MaterialModifiers\ColorPropertyMaterialModifier.cs" />
|
||||
<Compile Include="MaterialModifiers\FloatPropertyMaterialModifier.cs" />
|
||||
@ -55,6 +59,7 @@
|
||||
<Compile Include="ModuleRestockDepthMask.cs" />
|
||||
<Compile Include="ModuleRestockHeatEffects.cs" />
|
||||
<Compile Include="ModuleRestockISRUAnimation.cs" />
|
||||
<Compile Include="ModuleRestockConstraints.cs" />
|
||||
<Compile Include="ModuleRestockModifyFairingMaterials.cs" />
|
||||
<Compile Include="ModuleRestockModifyMaterials.cs" />
|
||||
<Compile Include="PartModuleExtensions.cs" />
|
||||
|
Reference in New Issue
Block a user