Skip to content

Commit

Permalink
Refactor HazardousOcean into HazardousBody
Browse files Browse the repository at this point in the history
- Not bound to an ocean anymore
- Can be control using an altitude, longitude and latitude multiplier
- Can have a different interval (so it is not neccessarily applied every
  frame)
StollD committed Aug 21, 2018
1 parent bc4d0c5 commit adf77eb
Showing 6 changed files with 248 additions and 0 deletions.
118 changes: 118 additions & 0 deletions src/Kopernicus.Components/HazardousBody.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/**
* Kopernicus Planetary System Modifier
* -------------------------------------------------------------
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* This library is intended to be used as a plugin for Kerbal Space Program
* which is copyright 2011-2017 Squad. Your usage of Kerbal Space Program
* itself is governed by the terms of its EULA, not the license above.
*
* https://kerbalspaceprogram.com
*/

using System;
using System.Collections.Generic;
using UnityEngine;

namespace Kopernicus
{
namespace Components
{
/// <summary>
/// Regional heating for planet surfaces
/// </summary>
public class HazardousBody : MonoBehaviour
{
/// <summary>
/// The average heat rate on the surface of the body. Gets multiplied by latitude, longitude and altitude curves
/// </summary>
public Double HeatRate;

/// <summary>
/// How many seconds should pass between applying the heat rate to the ship.
/// </summary>
public Single HeatInterval = 0.05f;

/// <summary>
/// Controls the heat rate at a certain latitude
/// </summary>
public FloatCurve LatitudeCurve;

/// <summary>
/// Controls the heat rate at a certain longitude
/// </summary>
public FloatCurve LongitudeCurve;

/// <summary>
/// Controls the heat rate at a certain altitude
/// </summary>
public FloatCurve AltitudeCurve;

private CelestialBody _body;

/// <summary>
/// Get the body
/// </summary>
void Start()
{
_body = GetComponent<CelestialBody>();

StartCoroutine(Worker());
}

void OnLevelWasLoaded(Int32 level)
{
StartCoroutine(Worker());
}

/// <summary>
/// Update the heat
/// </summary>
/// <returns></returns>
private IEnumerator<WaitForSeconds> Worker()
{
while (true)
{
if (!FlightGlobals.ready)
{
yield return new WaitForSeconds(HeatInterval);
continue;
}

// Get all vessels
List<Vessel> vessels = FlightGlobals.Vessels.FindAll(v => v.mainBody == _body);

// Loop through them
foreach (Vessel vessel in vessels)
{
Double altitude =
AltitudeCurve.Evaluate((Single)Vector3d.Distance(vessel.transform.position, _body.transform.position));
Double latitude = LatitudeCurve.Evaluate((Single)vessel.latitude);
Double longitude = LongitudeCurve.Evaluate((Single)vessel.longitude);

Double heat = altitude * latitude * longitude * HeatRate;
foreach (Part part in vessel.Parts)
part.temperature += heat;
}

// Wait
yield return new WaitForSeconds(HeatInterval);
}
// ReSharper disable once IteratorNeverReturns
}
}
}
}
2 changes: 2 additions & 0 deletions src/Kopernicus.Components/HazardousOcean.cs
Original file line number Diff line number Diff line change
@@ -33,6 +33,8 @@ namespace Components
{
/// <summary>
/// Class to create a dangerous ocean that destroys ships
///
/// Obsolete. Nothing to see here. Look at the shiny new things in HazardousBody instead.
/// </summary>
public class HazardousOcean : MonoBehaviour
{
1 change: 1 addition & 0 deletions src/Kopernicus.Components/Kopernicus.Components.csproj
Original file line number Diff line number Diff line change
@@ -57,6 +57,7 @@
<ItemGroup>
<Compile Include="Events.cs" />
<Compile Include="GLTools.cs" />
<Compile Include="HazardousBody.cs" />
<Compile Include="HazardousOcean.cs" />
<Compile Include="KopernicusBuoyancy.cs" />
<Compile Include="KopernicusOrbitRendererData.cs" />
3 changes: 3 additions & 0 deletions src/Kopernicus/Configuration/Body.cs
Original file line number Diff line number Diff line change
@@ -190,6 +190,9 @@ public NumericParser<Int32> contractWeight
// Wrapper around Particle class for editing/loading
[ParserTargetCollection("Particles", AllowMerge = true)]
public List<ParticleLoader> particles { get; set; }

[ParserTarget("HazardousBody")]
public HazardousBodyLoader hazardousBody { get; set; }

// Wrapper around the settings for the SpaceCenter
[ParserTarget("SpaceCenter", AllowMerge = true)]
123 changes: 123 additions & 0 deletions src/Kopernicus/Configuration/HazardousBodyLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**
* Kopernicus Planetary System Modifier
* -------------------------------------------------------------
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* This library is intended to be used as a plugin for Kerbal Space Program
* which is copyright 2011-2017 Squad. Your usage of Kerbal Space Program
* itself is governed by the terms of its EULA, not the license above.
*
* https://kerbalspaceprogram.com
*/

using Kopernicus.Components;
using System;
using Kopernicus.UI;

namespace Kopernicus
{
namespace Configuration
{
[RequireConfigType(ConfigType.Node)]
public class HazardousBodyLoader : BaseLoader, ITypeParser<HazardousBody>
{
// Set-up our parental objects
public HazardousBody Value { get; set; }

// The average heat on the body
[ParserTarget("heat")]
[KittopiaDescription("The average heat on the body.")]
public NumericParser<Double> heat
{
get { return Value.HeatRate; }
set { Value.HeatRate = value; }
}

// How much time passes between applying the heat to a vessel
[ParserTarget("interval")]
[KittopiaDescription("How much time passes between applying the heat to a vessel.")]
public NumericParser<Single> interval
{
get { return Value.HeatInterval; }
set { Value.HeatInterval = value; }
}

// Controls the how much of the average heat gets applied at a certain altitude
[ParserTarget("AltitudeCurve")]
[KittopiaDescription("Controls the how much of the average heat gets applied at a certain altitude.")]
public FloatCurveParser altitudeCurve
{
get { return Value.AltitudeCurve; }
set { Value.AltitudeCurve = value; }
}

// Controls the how much of the average heat gets applied at a certain latitude
[ParserTarget("LatitudeCurve")]
[KittopiaDescription("Controls the how much of the average heat gets applied at a certain latitude.")]
public FloatCurveParser latitudeCurve
{
get { return Value.LatitudeCurve; }
set { Value.LatitudeCurve = value; }
}

// Controls the how much of the average heat gets applied at a certain longitude
[ParserTarget("LongitudeCurve")]
[KittopiaDescription("Controls the how much of the average heat gets applied at a certain longitude.")]
public FloatCurveParser longitudeCurve
{
get { return Value.LongitudeCurve; }
set { Value.LongitudeCurve = value; }
}

[KittopiaDestructor]
public void Destroy()
{
UnityEngine.Object.Destroy(Value);
}

/// <summary>
/// Creates a new Particle Loader from the Injector context.
/// </summary>
public HazardousBodyLoader()
{
// Is this the parser context?
if (!Injector.IsInPrefab)
{
throw new InvalidOperationException("Must be executed in Injector context.");
}

// Store values
Value = generatedBody.celestialBody.gameObject.AddOrGetComponent<HazardousBody>();
}

/// <summary>
/// Creates a new Particle Loader on a spawned CelestialBody.
/// </summary>
[KittopiaConstructor(KittopiaConstructor.Parameter.CelestialBody)]
public HazardousBodyLoader(CelestialBody body)
{
// Is this a spawned body?
if (body?.scaledBody == null || Injector.IsInPrefab)
{
throw new InvalidOperationException("The body must be already spawned by the PSystemManager.");
}

// Store values
Value = body.gameObject.AddOrGetComponent<HazardousBody>();
}
}
}
}
1 change: 1 addition & 0 deletions src/Kopernicus/Kopernicus.csproj
Original file line number Diff line number Diff line change
@@ -58,6 +58,7 @@
<Compile Include="Configuration\DebugLoader.cs" />
<Compile Include="Configuration\Enumerations\KopernicusNoiseQuality.cs" />
<Compile Include="Configuration\Enumerations\KopernicusNoiseType.cs" />
<Compile Include="Configuration\HazardousBodyLoader.cs" />
<Compile Include="Configuration\MaterialLoader\AerialTransCutoutLoader.cs" />
<Compile Include="Configuration\MaterialLoader\AlphaTestDiffuseLoader.cs" />
<Compile Include="Configuration\MaterialLoader\DiffuseWrapLoader.cs" />

0 comments on commit adf77eb

Please sign in to comment.