Skip to content

Commit a85329c

Browse files
committed
Added LIBaseShip for LIShipStatus and LILobbyBehavior to share
1 parent b26734d commit a85329c

39 files changed

Lines changed: 207 additions & 232 deletions

LevelImposter/Builders/BuildRouter.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public class BuildRouter(IElemBuilder[] buildStack)
1616
/// Time to warn the user (in ms) when an element is taking too long to load
1717
private const int WARN_MAX_BUILD_DURATION = 200;
1818

19-
private readonly Stopwatch _buildTimer = new();
19+
private static MapObjectDB MapObjectDB => LIBaseShip.Instance!.MapObjectDB;
2020

21-
public MapObjectDB MapObjectDB { get; } = new();
21+
private readonly Stopwatch _buildTimer = new();
2222

2323
/// <summary>
2424
/// Builds the provided LIElements into GameObjects under the specified parent transform.
@@ -27,6 +27,10 @@ public class BuildRouter(IElemBuilder[] buildStack)
2727
/// <param name="parentTransform">Parent transform for the built GameObjects</param>
2828
public void BuildMap(LIElement[] elements, Transform? parentTransform = null)
2929
{
30+
// Check for LIBaseShip instance
31+
if (LIBaseShip.Instance == null)
32+
throw new Exception("LIBaseShip instance not found!");
33+
3034
// Create GameObjects
3135
foreach (var element in elements)
3236
{

LevelImposter/Builders/Generic/CustomTextBuilder.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ public void OnBuild(LIElement elem, GameObject obj)
7777
if (customText == null || customText.Count <= 0)
7878
return;
7979

80-
// ShipStatus
81-
var shipStatus = LIShipStatus.GetInstance();
82-
8380
// Replace Custom Text
8481
foreach (var (textID, text) in customText)
8582
{
@@ -96,7 +93,7 @@ public void OnBuild(LIElement elem, GameObject obj)
9693
}
9794

9895
// Replace Text
99-
shipStatus.Renames.Add(stringName, text);
96+
LIBaseShip.Instance?.Renames.Add(stringName, text);
10097
LILogger.Debug($"Custom Text '{stringName}' >>> '{text}'");
10198
}
10299
}

LevelImposter/Builders/Sab/SabBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public void OnBuild(LIElement elem, GameObject obj)
7272

7373
// Rename Task
7474
if (!string.IsNullOrEmpty(elem.properties.description))
75-
LIShipStatus.GetInstanceOrNull()?.Renames.Add(task.TaskType, elem.properties.description);
75+
LIBaseShip.Instance?.Renames.Add(task.TaskType, elem.properties.description);
7676

7777
// Add To Quick Chat
7878
var taskName = TranslationController.Instance.GetTaskName(task.TaskType);

LevelImposter/Builders/Sab/SabConsoleBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void OnBuild(LIElement elem, GameObject obj)
4242
}
4343

4444
if (!string.IsNullOrEmpty(elem.properties.description))
45-
LIShipStatus.GetInstanceOrNull()?.Renames.Add(sabotageTask.TaskType, elem.properties.description);
45+
LIBaseShip.Instance?.Renames.Add(sabotageTask.TaskType, elem.properties.description);
4646

4747
// Console
4848
var console = obj.AddComponent<Console>();

LevelImposter/Builders/Sab/SabMixupBuilder.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ public void OnBuild(LIElement elem, GameObject obj)
6060

6161
// Rename Task
6262
if (!string.IsNullOrEmpty(elem.properties.description))
63-
LIShipStatus.GetInstanceOrNull()?.Renames
64-
.Add(StringNames.MushroomMixupSabotage, elem.properties.description);
63+
LIBaseShip.Instance?.Renames.Add(
64+
StringNames.MushroomMixupSabotage,
65+
elem.properties.description);
6566

6667
// Add Task
6768
shipStatus.SpecialTasks = MapUtils.AddToArr(shipStatus.SpecialTasks, task);

LevelImposter/Builders/Task/ShipTaskBuilder.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,23 @@ public void Build(LIElement elem, Console console)
6868
var systemType = RoomBuilder.GetParentOrDefault(elem);
6969

7070
// Rename
71-
var renameHandler = LIShipStatus.GetInstance().Renames;
71+
var renameHandler = LIBaseShip.Instance?.Renames;
7272
if (prefabTask != null && !string.IsNullOrEmpty(elem.properties.description))
7373
{
74-
renameHandler.Add(prefabTask.TaskType, elem.properties.description);
74+
renameHandler?.Add(prefabTask.TaskType, elem.properties.description);
7575

7676
// Rename Node Description
7777
if (isNode || isNodeSwitch)
78-
renameHandler.Add(StringNames.FixWeatherNode, elem.properties.description);
78+
renameHandler?.Add(StringNames.FixWeatherNode, elem.properties.description);
7979
}
8080

8181
// Rename Node Room
8282
if (isNode)
8383
{
8484
var controlType = WeatherSwitchGame.ControlNames[console.ConsoleId];
85-
var roomName = renameHandler.Get(systemType);
85+
var roomName = renameHandler?.Get(systemType);
8686
if (roomName != null)
87-
renameHandler.Add(controlType, roomName);
87+
renameHandler?.Add(controlType, roomName);
8888
}
8989

9090
// Built List

LevelImposter/Builders/Util/DisplayBuilder.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ public void OnBuild(LIElement elem, GameObject obj)
1717
if (elem.type != "util-display")
1818
return;
1919

20-
// ShipStatus
21-
var shipStatus = LIShipStatus.GetInstance().ShipStatus;
22-
2320
// Prefab
2421
var minigamePrefab = AssetDB.GetObject("util-cams")?.GetComponent<SystemConsole>().MinigamePrefab
2522
.Cast<PlanetSurveillanceMinigame>();
@@ -32,7 +29,7 @@ public void OnBuild(LIElement elem, GameObject obj)
3229
// Camera
3330
var cameraObject = new GameObject("DisplayCamera");
3431
cameraObject.layer = (int)Layer.UI;
35-
cameraObject.transform.parent = shipStatus.transform;
32+
cameraObject.transform.parent = LIBaseShip.Instance?.transform;
3633
cameraObject.transform.position = new Vector3(
3734
(elem.properties.camXOffset ?? 0) + obj.transform.position.x,
3835
(elem.properties.camYOffset ?? 0) + obj.transform.position.y,

LevelImposter/Builders/Util/RoomBuilder.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ public void OnBuild(LIElement elem, GameObject obj)
4242
LILogger.Warn($"{shipRoom.name} is missing a collider");
4343

4444
// Rename Room Name
45-
// TODO: Apply this to lobbies as well
46-
LIShipStatus.GetInstanceOrNull()?.Renames.Add(systemType, obj.name);
45+
LIBaseShip.Instance?.Renames.Add(systemType, obj.name);
4746

4847
// Add to DB
4948
RoomDB.Add(new RoomData
@@ -60,8 +59,7 @@ public void OnBuild(LIElement elem, GameObject obj)
6059
public void OnPostBuild()
6160
{
6261
// Add Default Room Name
63-
// TODO: Apply this to lobbies as well
64-
LIShipStatus.GetInstanceOrNull()?.Renames.Add((SystemTypes)0, "Default Room");
62+
LIBaseShip.Instance?.Renames.Add((SystemTypes)0, "Default Room");
6563
}
6664

6765
/// <summary>

LevelImposter/Builders/Util/TeleLinkBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void OnBuild(LIElement elem, GameObject obj)
1919
if (targetID == null)
2020
return;
2121

22-
var targetTeleporterGameObject = LIShipStatus.MapObjectDB.GetObject((Guid)targetID);
22+
var targetTeleporterGameObject = LIBaseShip.Instance?.MapObjectDB.GetObject((Guid)targetID);
2323
var targetTeleporter = targetTeleporterGameObject?.GetComponent<LITeleporter>();
2424

2525
// Get Teleporter
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using Il2CppInterop.Runtime.Attributes;
3+
using LevelImposter.Lobby;
4+
using LevelImposter.Trigger;
5+
using UnityEngine;
6+
7+
namespace LevelImposter.Core;
8+
9+
/// <summary>
10+
/// Represents the base class for all custom ship implementations in LevelImposter.
11+
/// Extended by <see cref="LIShipStatus"/> and <see cref="LILobbyBehaviour"/>.
12+
/// </summary>
13+
public class LIBaseShip(IntPtr intPtr) : MonoBehaviour(intPtr)
14+
{
15+
// Singleton instance
16+
public static LIBaseShip? Instance { get; private set; }
17+
18+
// Cached shader property IDs
19+
private static readonly int Mask = Shader.PropertyToID("_Mask");
20+
21+
// Subsystems
22+
[HideFromIl2Cpp] public LIMap? CurrentMap { get; private set; }
23+
[HideFromIl2Cpp] public TriggerSystem TriggerSystem { get; } = new();
24+
[HideFromIl2Cpp] public RenameHandler Renames { get; } = new();
25+
[HideFromIl2Cpp] public MapObjectDB MapObjectDB { get; } = new();
26+
27+
protected virtual void Awake()
28+
{
29+
Instance = this;
30+
}
31+
32+
protected virtual void Start()
33+
{
34+
// Apply a fixed shadow quad mask for LevelImposter maps
35+
DestroyableSingleton<HudManager>.Instance.ShadowQuad.material.SetInt(Mask, 7);
36+
}
37+
38+
protected virtual void OnDestroy()
39+
{
40+
// It's possible both lobby and game ship exist at the same time during scene transitions,
41+
// so we only clear the instance if it's this one
42+
if (Instance == this)
43+
Instance = null;
44+
}
45+
46+
public virtual void SetMap(LIMap? map)
47+
{
48+
CurrentMap = map;
49+
}
50+
}

0 commit comments

Comments
 (0)