Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix kabooming by kerbal #12

Merged
merged 3 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 25 additions & 41 deletions source/Kaboom.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using KSP.Localization;
using UnityEngine;

namespace Kaboom
{
Expand All @@ -7,24 +8,18 @@ namespace Kaboom
/// </summary>
public class ModuleKaboom : PartModule
{
[KSPField(isPersistant = true, guiActiveEditor = true, guiActive = true, guiName = "Kaboom delay",
groupName = "Kaboom", groupStartCollapsed = true,
guiUnits = " Seconds"),
[KSPField(isPersistant = true,
guiName = "Kaboom delay", groupName = "Kaboom", groupStartCollapsed = true, guiUnits = " Seconds",
guiActive = true, guiActiveUnfocused = true, unfocusedRange = 10f, guiActiveEditor = true),
UI_FloatRange(minValue = 0f, maxValue = 30f, stepIncrement = 1f)]

public float delay = 0;

[KSPField(isPersistant = true)]
public bool timerActive = false;

[KSPField(isPersistant = true)]
public double kaboomTime;

[KSPField(isPersistant = true)]
public bool isGlued = false;

[KSPEvent(groupName = "Kaboom",
guiActive = true, guiActiveEditor = true, active = true, guiActiveUncommand = true)]
guiActive = true, guiActiveUnfocused = true, unfocusedRange = 10f, guiActiveEditor = true,
active = true, guiActiveUncommand = true)]
public void GluedEvent()
{
isGlued = !isGlued;
Expand All @@ -40,14 +35,16 @@ private void GUITextUpdate()
}

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

[KSPEvent(guiName = "Cancel Kaboom!", groupName = "Kaboom",
guiActive = true, guiActiveUnfocused = true, unfocusedRange = 5f, active = false, guiActiveUncommand = true)]
guiActive = true, guiActiveUnfocused = true, unfocusedRange = 10f,
active = false, guiActiveUncommand = true)]
public void CancelKaboomEvent()
{
CancelKaboomIt();
Expand All @@ -66,66 +63,53 @@ public override void OnStart(StartState state)
Fields["delay"].group.displayName = "Kaboom Safety Cover";

GUITextUpdate();

}

private void KaboomIt()
{
Events["CancelKaboomEvent"].active = true;
Events["KaboomEvent"].active = false;
part.force_activate();

if (delay == 0)
{
bool success = Proceed();
if (!success)
CancelKaboomIt();
Proceed();
}
else
{
float delay_scaled = delay;

if (TimeWarp.WarpMode == TimeWarp.Modes.HIGH)
delay_scaled /= TimeWarp.CurrentRate;

Invoke("Proceed", delay_scaled);
ScreenMessages.PostScreenMessage("Kaboom set for " + delay + " seconds.", 5.0f, ScreenMessageStyle.UPPER_CENTER);
kaboomTime = Planetarium.GetUniversalTime() + delay;
timerActive = true;
}
}

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

if (!success)
CancelKaboomIt();
}

private void CancelKaboomIt()
{
Events["CancelKaboomEvent"].active = false;
Events["KaboomEvent"].active = true;
ScreenMessages.PostScreenMessage("Kaboom canceled.", 5.0f, ScreenMessageStyle.UPPER_CENTER);
timerActive = false;
}

public override void OnUpdate()
{
base.OnUpdate();
if (timerActive)
{
if (Planetarium.GetUniversalTime() >= kaboomTime)
{
timerActive = false;
bool success = Proceed();
if (!success)
CancelKaboomIt();
}
}
//base.OnUpdate();
}
}
}
4 changes: 4 additions & 0 deletions source/Repo-Kaboom112.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@
<DesignTime>True</DesignTime>
<DependentUpon>Version.tt</DependentUpon>
</Compile>
<Compile Include="Support\Settings.cs" />
<Compile Include="Welding\Welding.cs" />
<Compile Include="Welding\WeldingData.cs" />
<Compile Include="Welding\WeldingUtilities.cs" />
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Service References\" />
Expand Down