Skip to content
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
6 changes: 2 additions & 4 deletions LevelImposter/Builders/Util/DummyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ public void OnBuild(LIElement elem, GameObject obj)
if (elem.type != "util-dummy")
return;

// ShipStatus
var shipStatus = LIShipStatus.GetInstance().ShipStatus;

// Add Location
// Add location and save its index with the element id (see DummyPatch)
LIShipStatus.GetInstance().DummyIndex[elem.id] = shipStatus.DummyLocations.Length;
shipStatus.DummyLocations = MapUtils.AddToArr(shipStatus.DummyLocations, obj.transform);

// TODO: Customize each dummy location with name/outfit
}
}
4 changes: 4 additions & 0 deletions LevelImposter/Core/Components/LIShipStatus.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Il2CppInterop.Runtime.Attributes;
using LevelImposter.Shop;
using LevelImposter.Trigger;
Expand Down Expand Up @@ -30,6 +31,9 @@ public class LIShipStatus(IntPtr intPtr) : MonoBehaviour(intPtr)

public bool IsReady => !Builder.IsBuilding && !LoadingBar.IsVisible;

// Populated by DummyBuilder, used to customize dummies in DummyPatch
public Dictionary<Guid, int> DummyIndex { get; } = new();

public ShipStatus? ShipStatus { get; private set; }

public void Awake()
Expand Down
49 changes: 49 additions & 0 deletions LevelImposter/Core/Patches/Ship/DummyPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using HarmonyLib;
using LevelImposter.Shop;
using System.Linq;

namespace LevelImposter.Core;

/// <summary>
/// Update the names of all dummy PlayerControls to match
/// the names of their corresponding elements in the editor.
/// </summary>
[HarmonyPatch(typeof(DummyBehaviour), nameof(DummyBehaviour.Start))]
public static class DummyPatch
{
public static void Postfix(DummyBehaviour __instance)
{
// Only applies to dummies on LI freeplay games
if (!LIShipStatus.IsInstance())
return;
if (!GameState.IsInFreeplay)
return;
if(MapLoader.CurrentMap == null)
return;

// Since the game is in freeplay, removing the local player just leaves all the dummy PlayerControls
var dummyPlayers = PlayerControl.AllPlayerControls.ToArray().Where(p => p != PlayerControl.LocalPlayer).ToArray();

// Finds a dummy element that has a corresponding location index (see DummyBuilder)
// which matches this DummyBehaviour's PlayerControl
foreach (var elem in MapLoader.CurrentMap.elements)
{
if(elem.type != "util-dummy")
continue;
if (!LIShipStatus.GetInstance().DummyIndex.TryGetValue(elem.id, out int index))
continue;
if (__instance.myPlayer != dummyPlayers[index])
continue;

CustomizeDummy(__instance, elem);
}
}

/// <summary>
/// Applies any customizations to the dummy game object using its editor element.
/// </summary>
private static void CustomizeDummy(DummyBehaviour dummy, LIElement element)
{
dummy.myPlayer.SetName(element.name);
}
}