Skip to content

Commit

Permalink
new offset; autocancel on glued kaboom problems
Browse files Browse the repository at this point in the history
  • Loading branch information
yalov committed Sep 14, 2021
1 parent 1838a17 commit 8d9c22f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
37 changes: 19 additions & 18 deletions Source/Welding.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
// Based on the https://github.com/UmbraSpaceIndustries/Konstruction/tree/master/Source/Konstruction/Konstruction/Welding
// GPLV3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using System.Reflection;
using PreFlightTests;
using TestScripts;

namespace Kaboom
{
Expand All @@ -23,19 +16,20 @@ public Welding(Vessel vessel, Part part)
this.part = part;
}

public void MergeParts(bool compress)
public bool MergeParts(bool compress)
{
if (vessel.rootPart == part)
{
ScreenMessages.PostScreenMessage("You cannot weld the root part!");
return;
return false;
}

var wData = LoadWeldingData();
if (wData == null)
return;
return false;

PerformWeld(wData, compress);
bool sucess = PerformWeld(wData, compress);
return sucess;
}

private WeldingData LoadWeldingData(bool silent = false)
Expand Down Expand Up @@ -82,15 +76,20 @@ private WeldingData LoadWeldingData(bool silent = false)

private Vector3 GetOffset(WeldingData wData)
{
var nodeA = WeldingNodeUtilities.GetLinkingNode(wData.LinkedPartA, wData.KaboomGluedPart);
var nodeB = WeldingNodeUtilities.GetLinkingNode(wData.LinkedPartB, wData.KaboomGluedPart);
//var nodeA = WeldingNodeUtilities.GetLinkingNode(wData.LinkedPartA, wData.KaboomGluedPart);
//var nodeB = WeldingNodeUtilities.GetLinkingNode(wData.LinkedPartB, wData.KaboomGluedPart);

// offset in wrong direction //Vector3 offset = nodeA.position - nodeB.position;
// nulref //Vector3 offset2 = nodeA.nodeTransform.localPosition - nodeB.nodeTransform.localPosition;

Vector3 offset = nodeA.position - nodeB.position;
Debug.Log("offset: " + offset);
return offset;
// works for a stack of simmetrical parts
Vector3 offset3 = wData.LinkedPartA.transform.localPosition - wData.LinkedPartB.transform.localPosition;
offset3.Normalize();
offset3 *= WeldingNodeUtilities.GetPartThickness(wData.KaboomGluedPart);
return offset3;
}

private void PerformWeld(WeldingData wData, bool compress)
private bool PerformWeld(WeldingData wData, bool compress)
{
var nodeA = WeldingNodeUtilities.GetLinkingNode(wData.LinkedPartA, wData.KaboomGluedPart);
var nodeB = WeldingNodeUtilities.GetLinkingNode(wData.LinkedPartB, wData.KaboomGluedPart);
Expand Down Expand Up @@ -131,7 +130,9 @@ private void PerformWeld(WeldingData wData, bool compress)

wData.LinkedPartB.attachJoint = newJoint;

SoftExplode(wData.KaboomGluedPart);
//SoftExplode(wData.KaboomGluedPart);
wData.KaboomGluedPart.explode();
return true;
}

private static void SoftExplode(Part thisPart)
Expand Down
32 changes: 18 additions & 14 deletions source/Kaboom.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using KSP.Localization;
using KSP.Localization;

namespace Kaboom
{
Expand All @@ -13,7 +8,7 @@ namespace Kaboom
public class ModuleKaboom : PartModule
{
[KSPField(isPersistant = true, guiActiveEditor = true, guiActive = true, guiName = "Kaboom delay",
groupDisplayName = "<color=red><b>Switch Safety Cover</b></color>", groupName = "Kaboom", groupStartCollapsed = true,
groupDisplayName = "<color=red><b>Kaboom Safety Cover</b></color>", groupName = "Kaboom", groupStartCollapsed = true,

This comment has been minimized.

Copy link
@zer0Kerbal

zer0Kerbal Sep 14, 2021

Owner

thank you @yalov, the wording was always awkward - this is better. 🪂

guiUnits = " Seconds"),
UI_FloatRange(minValue = 0f, maxValue = 30f, stepIncrement = 1f)]

Expand All @@ -32,7 +27,8 @@ public class ModuleKaboom : PartModule
public string gluedText = Localizer.Format("#autoLOC_6001071"); /*Disabled*/


[KSPEvent(guiName = "Toggle Superglue", guiActive = true, guiActiveEditor = true, groupName = "Kaboom", active = true)]
[KSPEvent(guiName = "Toggle Superglue", groupName = "Kaboom",
guiActive = true, guiActiveEditor = true, active = true, guiActiveUncommand = true)]

This comment has been minimized.

Copy link
@zer0Kerbal

zer0Kerbal Sep 14, 2021

Owner

guiActiveUncommand what does this do? am unfamiliar with it - is it new? @yalov

public void GluedEvent()
{
isGlued = !isGlued;
Expand All @@ -46,13 +42,15 @@ public void GluedEvent()
}
}

[KSPEvent(guiActive = true, guiActiveUnfocused = true, unfocusedRange = 5f, guiName = "Kaboom!", groupName = "Kaboom", active = true)]
[KSPEvent(guiName = "Kaboom!", groupName = "Kaboom",
guiActive = true, guiActiveUnfocused = true, unfocusedRange = 5f, active = true, guiActiveUncommand = true)]
public void KaboomEvent()
{
KaboomIt();
}

[KSPEvent(guiActive = true, guiActiveUnfocused = true, unfocusedRange = 5f, guiName = "Cancel Kaboom!", groupName = "Kaboom", active = false)]
[KSPEvent(guiName = "Cancel Kaboom!", groupName = "Kaboom",
guiActive = true, guiActiveUnfocused = true, unfocusedRange = 5f, active = false, guiActiveUncommand = true)]
public void CancelKaboomEvent()
{
CancelKaboomIt();
Expand All @@ -69,7 +67,9 @@ private void KaboomIt()

if (delay == 0)
{
Proceed();
bool success = Proceed();
if (!success)
CancelKaboomIt();
}
else
{
Expand All @@ -79,16 +79,18 @@ private void KaboomIt()
}
}

private void Proceed()
private bool Proceed()
{
if (isGlued)
{
var k = new Welding(vessel, part);
k.MergeParts(true);
bool success = k.MergeParts(true);
return success;
}
else
{
part.explode();
return true;
}
}

Expand All @@ -108,7 +110,9 @@ public override void OnUpdate()
if (Planetarium.GetUniversalTime() >= kaboomTime)
{
timerActive = false;
Proceed();
bool success = Proceed();
if (!success)
CancelKaboomIt();
}
}
//base.OnUpdate();
Expand Down

0 comments on commit 8d9c22f

Please sign in to comment.