Skip to content

Commit

Permalink
Merge pull request #7 from cvusmo/dev
Browse files Browse the repository at this point in the history
0.1.3 release
  • Loading branch information
blacksheepcosmo authored Jul 27, 2023
2 parents 4b7f98e + 94418d6 commit 8c0dc2f
Show file tree
Hide file tree
Showing 8 changed files with 341 additions and 51 deletions.
2 changes: 1 addition & 1 deletion FFT/swinfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "FFT",
"description": "Fancy Fuel Tanks",
"source": "https://github.com/cvusmo/FFT",
"version": "0.1.2",
"version": "0.1.3",
"version_check": "https://raw.githubusercontent.com/cvusmo/FFT/Master/FFT/swinfo.json",
"dependencies": [
{
Expand Down
2 changes: 2 additions & 0 deletions FFTProject/FFT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
<ItemGroup>
<Reference Include="Assembly-CSharp">
<HintPath>..\..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program 2\KSP2_x64_Data\Managed\Assembly-CSharp.dll</HintPath>
<Publicize>true</Publicize>
<Private>false</Private>
</Reference>
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
Expand Down
93 changes: 87 additions & 6 deletions FFTProject/FFTPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,46 @@
using BepInEx;
using UnityEngine;
using UnityEngine.Sprites;
using HarmonyLib;
using BepInEx.Logging;
using FFT.Modules;
using KSP.Game;
using KSP.Messages.PropertyWatchers;
using KSP.Sim.impl;
using SpaceWarp;
using SpaceWarp.API.Mods;
using UnityEngine;

namespace FFT
{

[BepInPlugin(MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
[BepInDependency(SpaceWarpPlugin.ModGuid, SpaceWarpPlugin.ModVer)]
public class FFTPlugin : BaseSpaceWarpPlugin
{
public const string ModGuid = MyPluginInfo.PLUGIN_GUID;
public const string ModName = MyPluginInfo.PLUGIN_NAME;
public const string ModVer = MyPluginInfo.PLUGIN_VERSION;

internal GameInstance _gameInstance;
internal VesselComponent _vesselComponent;
private IsActiveVessel _isActiveVessel;
internal GameState? _state;
public FuelTankDefinitions fuelTankDefintions;
public GameObject CV401;
internal Module_TriggerVFX Module_TriggerVFX { get; private set; }
public static FFTPlugin Instance { get; set; }
internal new static ManualLogSource Logger { get; set; }
public static string Path { get; private set; }

public void GetFuelTanks()
{
CV401 = fuelTankDefintions.GetFuelTank("CV401");
}
void Awake()
{
fuelTankDefintions = FindObjectOfType<FuelTankDefinitions>();
GetFuelTanks();
if (CV401 == null)
{
Logger.LogInfo("CV401 not found in FuelTanks.");
}
}
public override void OnPreInitialized()
{
FFTPlugin.Path = this.PluginFolderPath;
Expand All @@ -26,8 +49,66 @@ public override void OnInitialized()
{
base.OnInitialized();

Instance = this;
Logger = base.Logger;
Logger.LogInfo("Loaded");

_gameInstance = GameManager.Instance.Game;
_isActiveVessel = new IsActiveVessel();
_vesselComponent = new VesselComponent();

Logger.LogInfo("gameInstance" + _gameInstance);
Logger.LogInfo("_isActiveVessel" + _isActiveVessel);
Logger.LogInfo("_vesselComponent" + _vesselComponent);
}
public override void OnPostInitialized() => base.OnPostInitialized();

public void Update()
{
_state = BaseSpaceWarpPlugin.Game?.GlobalGameState?.GetState();

if (_state == GameState.Launchpad || _state == GameState.FlightView || _state == GameState.Runway)
{
if (CV401 == null)
{
Logger.LogInfo("CV401 not found in FuelTanks.");
return;
}

GameObject CoolingVFX = CV401.transform.Find("CoolingVFX")?.gameObject;

if (CoolingVFX != null)
{
if (CoolingVFX.GetComponent<Module_TriggerVFX>() == null)
{
Module_TriggerVFX = CoolingVFX.AddComponent<Module_TriggerVFX>();
Logger.LogInfo("Module_TriggerVFX added to CoolingVFX");
}
}
else
{
Logger.LogInfo("CoolingVFX not found");
}

if (Module_TriggerVFX != null && _isActiveVessel.GetValueBool())
{
Logger.LogInfo("Module_TriggerVFX IsActive = True");
}
}
else
{
if (Module_TriggerVFX != null)
{
Logger.LogInfo("Module_TriggerVFX IsActive = False");
}
}
}

public GameState? GetGameState()
{
Logger.LogInfo("_state" + _state);
return _state;
}

public override void OnPostInitialized() => base.OnPostInitialized();
}
}
17 changes: 17 additions & 0 deletions FFTProject/Modules/Data_TriggerVFX.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using FFT.Modules;
using KSP.Sim;
using KSP.Sim.Definitions;
using System;
using UnityEngine;

namespace FFT.Modules
{
[Serializable]
public class Data_TriggerVFX : ModuleData
{
public override Type ModuleType => typeof(Module_TriggerVFX);

[KSPState]
public AnimationCurve VFXOpacityCurve;
}
}
26 changes: 26 additions & 0 deletions FFTProject/Modules/Definitions/FuelTankDefinitions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using UnityEngine;

public class FuelTankDefinitions : MonoBehaviour
{
[SerializeField]
private List<GameObject> fuelTankDefintions;

private Dictionary<string, GameObject> fuelTanksDict = new Dictionary<string, GameObject>();
private void Awake()
{
foreach (var tank in fuelTankDefintions)
{
fuelTanksDict[tank.name] = tank;
}
}
public GameObject GetFuelTank(string tankName)
{
if (fuelTanksDict.TryGetValue(tankName, out var tank))
{
return tank;
}

return null;
}
}
197 changes: 197 additions & 0 deletions FFTProject/Modules/Module_TriggerVFX.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
using KSP.Animation;
using KSP.Game;
using KSP.Messages.PropertyWatchers;
using KSP.Sim.Definitions;
using KSP.Sim.impl;
using KSP.UI;
using UnityEngine;
using VFX;

namespace FFT.Modules
{
public class Module_TriggerVFX : PartBehaviourModule
{
public override Type PartComponentModuleType => typeof(PartComponentModule_TriggerVFX);

[SerializeField]
public Data_TriggerVFX _dataTriggerVFX;
[SerializeField]
public GameObject CoolingVFX;
[SerializeField]

public TriggerVFXFromAnimation _triggerVFX;
public DynamicGravityForVFX _gravityForVFX;
public Animator animator;
public ParticleSystem _particleSystem;
public IsActiveVessel _isActiveVessel;
public VesselComponent _vesselComponent;
public bool _wasActive;
private float _fuelLevel;
public GameState _gameState { get; private set; }
public bool IsActive => _isActiveVessel.GetValueBool();
public override void OnInitialize()
{
base.OnInitialize();

if (PartBackingMode == PartBackingModes.Flight)
{
if (CoolingVFX)
{
Awake();
}
}
}
public void Awake()
{
FFTPlugin.Logger.LogInfo("Attached to GameObject: " + gameObject.name);

if (CoolingVFX != null)
{
_particleSystem = CoolingVFX.GetComponentInChildren<ParticleSystem>();
if (_particleSystem != null)
{
FFTPlugin.Logger.LogInfo("Successfully retrieved ParticleSystem.");
}
else
{
FFTPlugin.Logger.LogError("Could not find ParticleSystem on CoolingVFX.");
}
}
else
{
FFTPlugin.Logger.LogError("CoolingVFX GameObject is not assigned.");
}

animator = GetComponentInParent<Animator>();

_isActiveVessel = new IsActiveVessel();
_vesselComponent = new VesselComponent();

FFTPlugin.Logger.LogInfo("Module_TriggerVFX has started.");
}
public override void AddDataModules()
{
base.AddDataModules();
this._dataTriggerVFX ??= new Data_TriggerVFX();
this.DataModules.TryAddUnique<Data_TriggerVFX>(this._dataTriggerVFX, out this._dataTriggerVFX);
}
public override void OnModuleFixedUpdate(float fixedDeltaTime)
{
base.OnModuleFixedUpdate(fixedDeltaTime);
double fillRatioSum = 0;
int count = 0;

foreach (var container in part.Model.Containers)
{
foreach (var resourceID in container)
{
FFTPlugin.Logger.LogInfo("ResourceID Before: " + resourceID);
FFTPlugin.Logger.LogInfo("opacity Before: " + count);
count++;
fillRatioSum += container.GetResourceFillRatio(resourceID);
FFTPlugin.Logger.LogInfo("ResourceID After: " + resourceID);
FFTPlugin.Logger.LogInfo("opacity After: " + count);
}
}

double fillRatioAverage = fillRatioSum / count;

float opacity = _dataTriggerVFX.VFXOpacityCurve.Evaluate((float)fillRatioAverage);

_fuelLevel = opacity;
FFTPlugin.Logger.LogInfo("opacity: " + opacity);

animator.SetFloat("FuelLevel", _fuelLevel);
FFTPlugin.Logger.LogInfo("Fuel level: " + _fuelLevel);

if (_fuelLevel < 0)
{
FFTPlugin.Logger.LogError("Out of Fuel. Fuel level: " + _fuelLevel);
}

if (IsActive)
{
if (!FuelLevelExceedsThreshold())
{
StopVFX();
}
else if (!animator.GetCurrentAnimatorStateInfo(0).IsName("CoolingVFX_LOOP"))
{
StartVFX();
FFTPlugin.Logger.LogInfo("_triggerVFX: " + _triggerVFX);
}
}
else
{
StopVFX();
}
}
private void StartVFX()
{
FFTPlugin.Logger.LogInfo("StartVFX FuelLevel: " + _fuelLevel);
EnableEmission();
StartParticleSystem();
_triggerVFX.enabled = true;
_gravityForVFX.enabled = true;
FFTPlugin.Logger.LogInfo("StartParticleSystem & FL: " + _fuelLevel);
}
private void StopVFX()
{
FFTPlugin.Logger.LogInfo("StopVFX FuelLevel: " + _fuelLevel);
StopParticleSystem();
FFTPlugin.Logger.LogInfo("StopParticleSystem FuelLevel: " + _fuelLevel);
}
public void EnableEmission()
{
if (_particleSystem != null)
{
var emission = _particleSystem.emission;
emission.enabled = true;
}
else
{
FFTPlugin.Logger.LogError("_particleSystem is null in EnableEmission");
}
}
public void DisableEmission()
{
if (_particleSystem != null)
{
var emission = _particleSystem.emission;
emission.enabled = false;
}
else
{
FFTPlugin.Logger.LogError("_particleSystem is null in DisableEmission");
}
}
public void StartParticleSystem()
{
if (_particleSystem != null)
{
EnableEmission();
_particleSystem.Play();
}
else
{
FFTPlugin.Logger.LogError("_particleSystem is null in _particleSystem.Play");
}
}
public void StopParticleSystem()
{
if (_particleSystem != null)
{
DisableEmission();
_particleSystem.Stop();
}
else
{
FFTPlugin.Logger.LogError("_particleSystem is null in _particleSystem.Stop");
}
}
public bool FuelLevelExceedsThreshold()
{
return _fuelLevel > 0.8f;
}
}
}
11 changes: 11 additions & 0 deletions FFTProject/Modules/PartComponentModule_TriggerVFX.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using FFT.Modules;
using KSP.Sim.impl;
using System;

namespace FFT.Modules
{
public class PartComponentModule_TriggerVFX : PartComponentModule
{
public override Type PartBehaviourModuleType => typeof(Module_TriggerVFX);
}
}
Loading

0 comments on commit 8c0dc2f

Please sign in to comment.