diff --git a/Assets/CineboxAndrew b/Assets/CineboxAndrew index 4691b8f2..9bb51cac 160000 --- a/Assets/CineboxAndrew +++ b/Assets/CineboxAndrew @@ -1 +1 @@ -Subproject commit 4691b8f2e0b1e266bb917ae402c874bdc150d16e +Subproject commit 9bb51cacb47afca6ecda55304bff0af55d18f821 diff --git a/Distribution/Restock/GameData/ReStock/Assets/Structural/restock-launchclamp-1.mu b/Distribution/Restock/GameData/ReStock/Assets/Structural/restock-launchclamp-1.mu new file mode 100644 index 00000000..1f0394d1 Binary files /dev/null and b/Distribution/Restock/GameData/ReStock/Assets/Structural/restock-launchclamp-1.mu differ diff --git a/Distribution/Restock/GameData/ReStock/Patches/Structural/restock-structural-launchclamp.cfg b/Distribution/Restock/GameData/ReStock/Patches/Structural/restock-structural-launchclamp.cfg new file mode 100644 index 00000000..37fca7ea --- /dev/null +++ b/Distribution/Restock/GameData/ReStock/Patches/Structural/restock-structural-launchclamp.cfg @@ -0,0 +1,41 @@ +// Patches applying art changes to +// Contents: + + +@PART[launchClamp1] +{ + @author = Andrew Cassidy + !mesh = DELETE + MODEL + { + model = ReStock/Assets/Structural/restock-launchclamp-1 + } + + !MODULE[LaunchClamp] {} + + MODULE { + name = ModuleRestockLaunchClamp + trf_towerPivot_name = obj_girdercap + trf_towerStretch_name = obj_stretch + trf_towerGirder_name = obj_girder + trf_towerYoke_name = obj_yoke + trf_anchor_name = obj_ground + trf_animationRoot_name = restock-clamp-1 + anim_decouple_name = deploy + } + + MODULE + { + name = ModuleAnimateGeneric + animationName = extend + startEventGUIName = #LOC_RestockPlus_light_rotate_on + endEventGUIName = #LOC_RestockPlus_light_rotate_off + actionGUIName = #LOC_RestockPlus_light_rotate_toggle + allowDeployLimit = true + revClampDirection = false + revClampSpeed = true + revClampPercent = true + eventAvailableFlight = false + eventAvailableEVA = false + } +} \ No newline at end of file diff --git a/Distribution/Restock/GameData/ReStock/Plugins/Restock.dll b/Distribution/Restock/GameData/ReStock/Plugins/Restock.dll index 752515e0..074fb7ae 100644 Binary files a/Distribution/Restock/GameData/ReStock/Plugins/Restock.dll and b/Distribution/Restock/GameData/ReStock/Plugins/Restock.dll differ diff --git a/Source/Restock/ModuleRestockLaunchClamp.cs b/Source/Restock/ModuleRestockLaunchClamp.cs new file mode 100644 index 00000000..9cfbc009 --- /dev/null +++ b/Source/Restock/ModuleRestockLaunchClamp.cs @@ -0,0 +1,165 @@ +using System; +using System.Collections.Generic; +using System.Reflection; +using UnityEngine; + +namespace Restock +{ + public class ModuleRestockLaunchClamp : LaunchClamp + { + [KSPField] public string trf_towerGirder_name = ""; + [KSPField] public string trf_towerYoke_name = ""; + + [KSPField] public Transform towerPivot; + [KSPField] public Transform towerYoke; + [KSPField] public Transform towerAnchor; + [KSPField] public Transform towerGirder; + [KSPField] public Transform towerStretch; + + [KSPField] private Mesh _girderSegmentMesh; + [KSPField] private Mesh _girderMesh; + + private List _girderVerts; + private List _girderUVs; + private List _girderNormals; + private List _girderTangents; + private List _girderColors; + private List _girderTris; + private int _girderSegments; + + private float _girderHeight = -1f; + + private bool _girderHasTangents = false; + private bool _girderHasColors = false; + private int _girderVertCount; + private int _girderTriCount; + + private bool _flightUpdate = false; + + public override void OnLoad(ConfigNode node) + { + base.OnLoad(node); + + if (HighLogic.LoadedSceneIsEditor || HighLogic.LoadedSceneIsFlight) return; + + + } + + public override void OnStart(StartState state) + { + + Debug.Log("OnStart Called"); + towerPivot = base.part.FindModelTransform(trf_towerPivot_name); + towerYoke = base.part.FindModelTransform(trf_towerYoke_name); + towerAnchor = base.part.FindModelTransform(trf_anchor_name); + towerGirder = base.part.FindModelTransform(trf_towerGirder_name); + towerStretch = base.part.FindModelTransform(trf_towerStretch_name); + + _girderMesh = towerGirder.GetComponent().mesh; + _girderSegmentMesh = Instantiate(_girderMesh); + + _girderHeight = Vector3.Distance(towerStretch.position, towerAnchor.position); + + _girderVertCount = _girderSegmentMesh.vertexCount; + _girderTriCount = _girderSegmentMesh.triangles.Length; + + _girderVerts = new List(_girderSegmentMesh.vertices); + _girderUVs = new List(_girderSegmentMesh.uv); + _girderNormals = new List(_girderSegmentMesh.normals); + if (_girderSegmentMesh.tangents.Length > 0) + { + _girderHasTangents = true; + _girderTangents = new List(_girderSegmentMesh.tangents); + } + if (_girderSegmentMesh.colors32.Length > 0) + { + _girderHasColors = true; + _girderColors = new List(_girderSegmentMesh.colors32); + } + _girderTris = new List(_girderSegmentMesh.triangles); + _girderSegments = 1; + + base.OnStart(state); + + UpdateClamp(); + } + + public void LateUpdate() + { + if (!HighLogic.LoadedSceneIsEditor) + { + if (_flightUpdate) return; + else _flightUpdate = true; + } + + UpdateClamp(); + } + + public void UpdateClamp() + { + var vec1 = Vector3.down; + var vec2 = towerAnchor.localPosition - towerYoke.localPosition ; + towerYoke.localRotation = Quaternion.FromToRotation(vec1, vec2); + + var height = Vector3.Distance(towerStretch.position, towerAnchor.position); + var segments = Mathf.CeilToInt(height / _girderHeight); + if (segments != _girderSegments) + { + UpdateGirder(segments); + } + + towerAnchor.position = towerStretch.position - (towerStretch.up * height); + } + + private void UpdateGirder(int length) + { + if (length > _girderSegments) + { + for (int i = _girderSegments; i < length; i++) + { + var offset = Vector3.down * _girderHeight * i; + var indexOffset = _girderVertCount * i; + for (int v = 0; v < _girderVertCount; v++) + { + _girderVerts.Add(_girderSegmentMesh.vertices[v] + offset); + } + _girderNormals.AddRange(_girderSegmentMesh.normals); + _girderUVs.AddRange(_girderSegmentMesh.uv); + if (_girderHasTangents) _girderTangents.AddRange(_girderSegmentMesh.tangents); + if (_girderHasColors) _girderColors.AddRange(_girderSegmentMesh.colors32); + + for (int t = 0; t < _girderTriCount; t++) + { + _girderTris.Add(_girderSegmentMesh.triangles[t] + indexOffset); + } + } + } + else + { + var startIndex = length * _girderVertCount; + var count = (_girderSegments - length) * _girderVertCount; + Debug.Log("removing verts"); + _girderVerts.RemoveRange(startIndex, count); + _girderNormals.RemoveRange(startIndex, count); + _girderUVs.RemoveRange(startIndex, count); + if (_girderHasTangents) _girderTangents.RemoveRange(startIndex, count); + if (_girderHasColors) _girderColors.RemoveRange(startIndex, count); + + Debug.Log("removing tris"); + _girderTris.RemoveRange(length * _girderTriCount, (_girderSegments - length) * _girderTriCount); + } + + _girderMesh.Clear(); + + _girderMesh.SetVertices(_girderVerts); + _girderMesh.SetNormals(_girderNormals); + _girderMesh.SetUVs(0, _girderUVs); + if (_girderHasTangents) _girderMesh.SetTangents(_girderTangents); + if (_girderHasColors) _girderMesh.SetColors(_girderColors); + _girderMesh.SetTriangles(_girderTris, 0); + + _girderMesh.RecalculateBounds(); + _girderSegments = length; + } + } +} \ No newline at end of file diff --git a/Source/Restock/Restock.csproj b/Source/Restock/Restock.csproj index 658d5fa5..ac272e40 100644 --- a/Source/Restock/Restock.csproj +++ b/Source/Restock/Restock.csproj @@ -47,6 +47,7 @@ + @@ -61,4 +62,4 @@ sh -e -c "cp -v '$(TargetPath)' '$(SolutionDir)/../Distribution/Restock/GameData/ReStock/Plugins'" - + \ No newline at end of file