Skip to content

Commit

Permalink
The Prawn Self Defense Module is now working.
Browse files Browse the repository at this point in the history
Started working on custom triggerboxes for custom story goal assignments with"callback"/action expressions
  • Loading branch information
VELD-Dev committed Apr 23, 2023
1 parent 5380b49 commit 25dba22
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 0 deletions.
4 changes: 4 additions & 0 deletions AlterraWeaponry/AlterraWeaponry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,13 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="behaviours\CustomTriggerEventAssigner.cs" />
<Compile Include="behaviours\ZapFunctionalityBehaviour.cs" />
<Compile Include="items\PrawnSelfDefenseModule.cs" />
<Compile Include="patches\ExosuitTorpedoArm_OpenTorpedoStorageExternal_Patch.cs" />
<Compile Include="patches\SeamothTorpedo_OnEnergyDepleted_Patch.cs" />
<Compile Include="patches\Vehicle_OnUpgradeModuleChange_Patch.cs" />
<Compile Include="patches\Vehicle_OnUpgradeModuleUse_Patch.cs" />
<Compile Include="utils\ExplosiveTorpedoInitializer.cs" />
<Compile Include="behaviours\TorpedoExplosionBehaviour.cs" />
<Compile Include="Global.cs" />
Expand Down
Binary file modified AlterraWeaponry/alterraweaponry.assets
Binary file not shown.
61 changes: 61 additions & 0 deletions AlterraWeaponry/behaviours/CustomTriggerEventAssigner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace VELD.AlterraWeaponry.behaviours;

public class CustomTriggerStoryGoalAssigner : MonoBehaviour
{
public StoryGoal storyGoal { get; private set; }
public string id { get; private set; }

public Action action { get; private set; }

public CustomTriggerStoryGoalAssigner(StoryGoal storyGoal, string id, Action action = null)
{
this.storyGoal = storyGoal;
this.id = id;
this.action = action;
}

private void Start()
{
bool flag = this.GetComponentInParent<BoxCollider>();
if(!flag)
{
throw new Exception($"The CustomTriggerEventAssigner monobehaviour of parent CustomEventTrigger has no Collider.");
}

bool flag2 = this.storyGoal != null;
if(!flag2)
{
throw new Exception($"The StoryGoal of the CustomTriggerEventAssigner is undefined.");
}
}

private void OnTriggerEnter(Collision collision)
{
Main.logger.LogInfo("Custom collider triggered.");
bool flag = this.storyGoal != null;
if(!flag)
{
Main.logger.LogWarning($"TriggerBox {this.id} has no StoryGoal to play.");
return;
}

Main.logger.LogInfo($"Trying to play storyGoal {this.storyGoal.key}");
this.storyGoal.Trigger();
Main.logger.LogInfo($"Has finished playing storyGoal {this.storyGoal.key}");

bool flag2 = this.action != null;
if(flag2) {
Main.logger.LogInfo("Trigger Callback is trying to invoke Action...");
this.action.Invoke();
Main.logger.LogInfo("Trigger Callback has successfully executed Action");
}
}

}
78 changes: 78 additions & 0 deletions AlterraWeaponry/behaviours/ZapFunctionalityBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UWE;

namespace VELD.AlterraWeaponry.behaviours;

internal class ZapFunctionalityBehaviour : MonoBehaviour // Thanks to ECM and PrimeSonic 👌
{
private static GameObject seamothElectricalDefensePrefab = null;

public static GameObject ElectricalDefensePrefab => seamothElectricalDefensePrefab;

private const float EnergyCostPerZap = 5;
private const float ZapPower = 6f;
private const float BaseCharge = 2f;
private const float BaseRadius = 1f;

public const float ZapCooldown = 10f;
public static float timeNextZap = 0;
private static float DamageMultiplier => 1f;
private static float DirectZapDamage = (BaseRadius + ZapPower * BaseCharge) * DamageMultiplier * 0.5f;
// Calculations and initial values based off ElectricalDefense component

public static bool AbleToZap(Vehicle vehicle)
{
vehicle.energyInterface.GetValues(out float charge, out float capacity);
if (GameModeManager.GetOption<bool>(GameOption.TechnologyRequiresPower) && charge < EnergyCostPerZap)
return false;

return true;
}

public static IEnumerator UpdateDefensePrefab()
{
if (seamothElectricalDefensePrefab) yield break;

var task = CraftData.GetPrefabForTechTypeAsync(TechType.SeaTruck);
yield return task;
var prefab = task.GetResult();

seamothElectricalDefensePrefab = prefab?.GetComponent<SeaTruckUpgrades>().electricalDefensePrefab;
}

public bool Zap(Vehicle vehicle)
{
CoroutineHost.StartCoroutine(UpdateDefensePrefab());
if (Time.time < timeNextZap)
return true;

if (!AbleToZap(vehicle))
return false;

ZapRadius(vehicle);

timeNextZap = Time.time + ZapCooldown;
return true;
}

private static void ZapRadius(Vehicle vehicle)
{
if (vehicle == null)
return;

GameObject gameObject = Utils.SpawnZeroedAt(ElectricalDefensePrefab, vehicle.transform, false);
ElectricalDefense defenseComponent = gameObject.GetComponent<ElectricalDefense>();
defenseComponent.charge = ZapPower;
defenseComponent.chargeScalar = ZapPower;
defenseComponent.radius *= ZapPower;
defenseComponent.chargeRadius *= ZapPower;

if (GameModeManager.GetOption<bool>(GameOption.TechnologyRequiresPower))
vehicle.energyInterface.ConsumeEnergy(EnergyCostPerZap);
}
}
32 changes: 32 additions & 0 deletions AlterraWeaponry/patches/Vehicle_OnUpgradeModuleChange_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VELD.AlterraWeaponry.patches;

[HarmonyPatch(typeof(Vehicle))]
internal class Vehicle_OnUpgradeModuleChange_Patch
{
[HarmonyPostfix]
[HarmonyPatch(nameof(Vehicle.OnUpgradeModuleChange))]
public static void OnUpgradeModuleChange(TechType techType, bool added, Vehicle __instance)
{
if(techType == PrawnSelfDefenseModule.techType)
{
if(added)
{
Main.logger.LogInfo("Adding component ZapFunctionality to Vehicle.");
__instance.gameObject.AddComponent<ZapFunctionalityBehaviour>();
Main.logger.LogInfo("Added successfully ZapFunctionality to Vehicle.");
}
else
{
__instance.TryGetComponent<ZapFunctionalityBehaviour>(out ZapFunctionalityBehaviour defenseMono);
if (defenseMono != null)
UnityEngine.Object.Destroy(defenseMono);
}
}
}
}
24 changes: 24 additions & 0 deletions AlterraWeaponry/patches/Vehicle_OnUpgradeModuleUse_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VELD.AlterraWeaponry.patches;

[HarmonyPatch(typeof(Vehicle))]
public class Vehicle_OnUpgradeModuleUse_Patch
{
[HarmonyPostfix]
[HarmonyPatch(nameof(Vehicle.OnUpgradeModuleUse))]
public static void OnUpgradeModuleUse(TechType techType, int slotID, Vehicle __instance)
{
if(techType == PrawnSelfDefenseModule.techType)
{
Main.logger.LogInfo($"OnUpgradeModuleUse input received on slot {slotID}");
if (!__instance.TryGetComponent(out ZapFunctionalityBehaviour defenseMono))
return;
defenseMono.Zap(__instance);
}
}
}

0 comments on commit 25dba22

Please sign in to comment.