Skip to content

Commit 25dba22

Browse files
committed
The Prawn Self Defense Module is now working.
Started working on custom triggerboxes for custom story goal assignments with"callback"/action expressions
1 parent 5380b49 commit 25dba22

File tree

6 files changed

+199
-0
lines changed

6 files changed

+199
-0
lines changed

AlterraWeaponry/AlterraWeaponry.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,13 @@
9696
</Reference>
9797
</ItemGroup>
9898
<ItemGroup>
99+
<Compile Include="behaviours\CustomTriggerEventAssigner.cs" />
100+
<Compile Include="behaviours\ZapFunctionalityBehaviour.cs" />
99101
<Compile Include="items\PrawnSelfDefenseModule.cs" />
100102
<Compile Include="patches\ExosuitTorpedoArm_OpenTorpedoStorageExternal_Patch.cs" />
101103
<Compile Include="patches\SeamothTorpedo_OnEnergyDepleted_Patch.cs" />
104+
<Compile Include="patches\Vehicle_OnUpgradeModuleChange_Patch.cs" />
105+
<Compile Include="patches\Vehicle_OnUpgradeModuleUse_Patch.cs" />
102106
<Compile Include="utils\ExplosiveTorpedoInitializer.cs" />
103107
<Compile Include="behaviours\TorpedoExplosionBehaviour.cs" />
104108
<Compile Include="Global.cs" />
-12 Bytes
Binary file not shown.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace VELD.AlterraWeaponry.behaviours;
9+
10+
public class CustomTriggerStoryGoalAssigner : MonoBehaviour
11+
{
12+
public StoryGoal storyGoal { get; private set; }
13+
public string id { get; private set; }
14+
15+
public Action action { get; private set; }
16+
17+
public CustomTriggerStoryGoalAssigner(StoryGoal storyGoal, string id, Action action = null)
18+
{
19+
this.storyGoal = storyGoal;
20+
this.id = id;
21+
this.action = action;
22+
}
23+
24+
private void Start()
25+
{
26+
bool flag = this.GetComponentInParent<BoxCollider>();
27+
if(!flag)
28+
{
29+
throw new Exception($"The CustomTriggerEventAssigner monobehaviour of parent CustomEventTrigger has no Collider.");
30+
}
31+
32+
bool flag2 = this.storyGoal != null;
33+
if(!flag2)
34+
{
35+
throw new Exception($"The StoryGoal of the CustomTriggerEventAssigner is undefined.");
36+
}
37+
}
38+
39+
private void OnTriggerEnter(Collision collision)
40+
{
41+
Main.logger.LogInfo("Custom collider triggered.");
42+
bool flag = this.storyGoal != null;
43+
if(!flag)
44+
{
45+
Main.logger.LogWarning($"TriggerBox {this.id} has no StoryGoal to play.");
46+
return;
47+
}
48+
49+
Main.logger.LogInfo($"Trying to play storyGoal {this.storyGoal.key}");
50+
this.storyGoal.Trigger();
51+
Main.logger.LogInfo($"Has finished playing storyGoal {this.storyGoal.key}");
52+
53+
bool flag2 = this.action != null;
54+
if(flag2) {
55+
Main.logger.LogInfo("Trigger Callback is trying to invoke Action...");
56+
this.action.Invoke();
57+
Main.logger.LogInfo("Trigger Callback has successfully executed Action");
58+
}
59+
}
60+
61+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using UWE;
8+
9+
namespace VELD.AlterraWeaponry.behaviours;
10+
11+
internal class ZapFunctionalityBehaviour : MonoBehaviour // Thanks to ECM and PrimeSonic 👌
12+
{
13+
private static GameObject seamothElectricalDefensePrefab = null;
14+
15+
public static GameObject ElectricalDefensePrefab => seamothElectricalDefensePrefab;
16+
17+
private const float EnergyCostPerZap = 5;
18+
private const float ZapPower = 6f;
19+
private const float BaseCharge = 2f;
20+
private const float BaseRadius = 1f;
21+
22+
public const float ZapCooldown = 10f;
23+
public static float timeNextZap = 0;
24+
private static float DamageMultiplier => 1f;
25+
private static float DirectZapDamage = (BaseRadius + ZapPower * BaseCharge) * DamageMultiplier * 0.5f;
26+
// Calculations and initial values based off ElectricalDefense component
27+
28+
public static bool AbleToZap(Vehicle vehicle)
29+
{
30+
vehicle.energyInterface.GetValues(out float charge, out float capacity);
31+
if (GameModeManager.GetOption<bool>(GameOption.TechnologyRequiresPower) && charge < EnergyCostPerZap)
32+
return false;
33+
34+
return true;
35+
}
36+
37+
public static IEnumerator UpdateDefensePrefab()
38+
{
39+
if (seamothElectricalDefensePrefab) yield break;
40+
41+
var task = CraftData.GetPrefabForTechTypeAsync(TechType.SeaTruck);
42+
yield return task;
43+
var prefab = task.GetResult();
44+
45+
seamothElectricalDefensePrefab = prefab?.GetComponent<SeaTruckUpgrades>().electricalDefensePrefab;
46+
}
47+
48+
public bool Zap(Vehicle vehicle)
49+
{
50+
CoroutineHost.StartCoroutine(UpdateDefensePrefab());
51+
if (Time.time < timeNextZap)
52+
return true;
53+
54+
if (!AbleToZap(vehicle))
55+
return false;
56+
57+
ZapRadius(vehicle);
58+
59+
timeNextZap = Time.time + ZapCooldown;
60+
return true;
61+
}
62+
63+
private static void ZapRadius(Vehicle vehicle)
64+
{
65+
if (vehicle == null)
66+
return;
67+
68+
GameObject gameObject = Utils.SpawnZeroedAt(ElectricalDefensePrefab, vehicle.transform, false);
69+
ElectricalDefense defenseComponent = gameObject.GetComponent<ElectricalDefense>();
70+
defenseComponent.charge = ZapPower;
71+
defenseComponent.chargeScalar = ZapPower;
72+
defenseComponent.radius *= ZapPower;
73+
defenseComponent.chargeRadius *= ZapPower;
74+
75+
if (GameModeManager.GetOption<bool>(GameOption.TechnologyRequiresPower))
76+
vehicle.energyInterface.ConsumeEnergy(EnergyCostPerZap);
77+
}
78+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace VELD.AlterraWeaponry.patches;
8+
9+
[HarmonyPatch(typeof(Vehicle))]
10+
internal class Vehicle_OnUpgradeModuleChange_Patch
11+
{
12+
[HarmonyPostfix]
13+
[HarmonyPatch(nameof(Vehicle.OnUpgradeModuleChange))]
14+
public static void OnUpgradeModuleChange(TechType techType, bool added, Vehicle __instance)
15+
{
16+
if(techType == PrawnSelfDefenseModule.techType)
17+
{
18+
if(added)
19+
{
20+
Main.logger.LogInfo("Adding component ZapFunctionality to Vehicle.");
21+
__instance.gameObject.AddComponent<ZapFunctionalityBehaviour>();
22+
Main.logger.LogInfo("Added successfully ZapFunctionality to Vehicle.");
23+
}
24+
else
25+
{
26+
__instance.TryGetComponent<ZapFunctionalityBehaviour>(out ZapFunctionalityBehaviour defenseMono);
27+
if (defenseMono != null)
28+
UnityEngine.Object.Destroy(defenseMono);
29+
}
30+
}
31+
}
32+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace VELD.AlterraWeaponry.patches;
8+
9+
[HarmonyPatch(typeof(Vehicle))]
10+
public class Vehicle_OnUpgradeModuleUse_Patch
11+
{
12+
[HarmonyPostfix]
13+
[HarmonyPatch(nameof(Vehicle.OnUpgradeModuleUse))]
14+
public static void OnUpgradeModuleUse(TechType techType, int slotID, Vehicle __instance)
15+
{
16+
if(techType == PrawnSelfDefenseModule.techType)
17+
{
18+
Main.logger.LogInfo($"OnUpgradeModuleUse input received on slot {slotID}");
19+
if (!__instance.TryGetComponent(out ZapFunctionalityBehaviour defenseMono))
20+
return;
21+
defenseMono.Zap(__instance);
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)