You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ReStocked/Source/Restock/ModuleRestockConstraints.cs

73 lines
2.3 KiB
C#

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;
}
}
this.Log($"Loaded {constraints.Count} constraints");
}
public override void OnStart(StartState state)
{
if (HighLogic.LoadedSceneIsFlight || HighLogic.LoadedSceneIsEditor)
{
if (constraints == null || constraints.Count == 0)
{
foreach (UrlDir.UrlConfig pNode in GameDatabase.Instance.GetConfigs("PART"))
{
if (pNode.name.Replace("_", ".") == part.partInfo.name)
{
var cfg = pNode.config;
var node = cfg.GetNodes("MODULE").Single(n => n.GetValue("name") == moduleName);
OnLoad(node);
}
}
}
}
}
private void LateUpdate()
{
if (constraints == null) return;
foreach (var constraint in constraints)
{
constraint.Update();
}
}
}
}