Skip to content

Commit 46369bc

Browse files
committed
WIP: Improved lobby build process
1 parent 444e6c3 commit 46369bc

26 files changed

Lines changed: 299 additions & 114 deletions
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using LevelImposter.Core;
3+
using LevelImposter.Lobby;
4+
using UnityEngine;
5+
using UnityEngine.Events;
6+
7+
namespace LevelImposter.Builders.Lobby;
8+
9+
public class LobbyMapConsoleBuilder : IElemBuilder
10+
{
11+
private static Sprite? _defaultConsoleSprite;
12+
13+
public void OnBuild(LIElement elem, GameObject gameObject)
14+
{
15+
if (elem.type != "util-lobbymaps")
16+
return;
17+
18+
Build(gameObject);
19+
}
20+
21+
/// <summary>
22+
/// Builds a Lobby Map Console at the specified GameObject or creates a new one if null
23+
/// </summary>
24+
/// <param name="gameObject">The GameObject to build the console on, or null to create a new one</param>
25+
public static void Build(GameObject? gameObject = null)
26+
{
27+
if (gameObject == null)
28+
{
29+
gameObject = new GameObject("panel_LevelImposter");
30+
gameObject.transform.parent = LILobbyBehaviour.GetInstance().transform;
31+
gameObject.transform.localPosition = new Vector3(-1.41f, 1.84f, -9.998f); // <-- Default Position
32+
}
33+
34+
// Load Prefab
35+
var panelPrefab = LobbyDropshipPrefab.GetObjectFromPrefab("panel_Wardrobe");
36+
var prefabSpriteRenderer = panelPrefab.GetComponent<SpriteRenderer>();
37+
38+
// SpriteRenderer
39+
var spriteRenderer = gameObject.GetOrAddComponent<SpriteRenderer>();
40+
spriteRenderer.material = prefabSpriteRenderer.material;
41+
if (spriteRenderer.sprite == null)
42+
spriteRenderer.sprite = GetDefaultSprite();
43+
44+
// Console
45+
var console = gameObject.AddComponent<LobbyMapConsole>();
46+
47+
// Button
48+
var button = gameObject.AddComponent<ButtonBehavior>();
49+
button.OnMouseOver = new UnityEvent();
50+
button.OnMouseOut = new UnityEvent();
51+
button.OnClick.AddListener((Action)console.Use);
52+
53+
// Colliders
54+
MapUtils.CreateDefaultColliders(gameObject, panelPrefab);
55+
56+
// Layer
57+
gameObject.layer = (int)Layer.ShortObjects;
58+
}
59+
60+
/// <summary>
61+
/// Gets the default console sprite from assembly resources
62+
/// </summary>
63+
/// <returns>The default console sprite</returns>
64+
/// <exception cref="Exception">Thrown if the resource could not be found</exception>
65+
private static Sprite GetDefaultSprite()
66+
{
67+
if (_defaultConsoleSprite == null)
68+
_defaultConsoleSprite = MapUtils.LoadSpriteResource("LobbyConsole.png");
69+
if (_defaultConsoleSprite == null)
70+
throw new Exception("The \"LobbyConsole.png\" resource was not found in assembly");
71+
return _defaultConsoleSprite;
72+
}
73+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using LevelImposter.Core;
2+
using LevelImposter.Lobby;
3+
using UnityEngine;
4+
using UnityEngine.Events;
5+
6+
namespace LevelImposter.Builders.Lobby;
7+
8+
public class LobbyOptionsConsoleBuilder : IElemBuilder
9+
{
10+
public void OnBuild(LIElement elem, GameObject gameObject)
11+
{
12+
if (elem.type != "util-lobbyoptions")
13+
return;
14+
15+
// Load Prefab
16+
var prefab = LobbyDropshipPrefab.GetObjectFromPrefab("SmallBox/Panel");
17+
var prefabConsole = prefab.GetComponent<OptionsConsole>();
18+
var prefabButton = prefab.GetComponent<PassiveButton>();
19+
20+
// Build Console
21+
var console = gameObject.AddComponent<OptionsConsole>();
22+
console.CustomPosition = prefabConsole.CustomPosition;
23+
console.HostOnly = true;
24+
console.MenuPrefab = prefabConsole.MenuPrefab;
25+
console.Outline = MapUtils.CloneSprite(gameObject, prefab);
26+
console.CustomUseIcon = ImageNames.OptionsButton;
27+
28+
// Button
29+
var button = gameObject.AddComponent<PassiveButton>();
30+
button.ClickMask = prefabButton?.ClickMask;
31+
button.OnMouseOver = new UnityEvent();
32+
button.OnMouseOut = new UnityEvent();
33+
button.OnClick.AddListener((System.Action)console.Use);
34+
35+
// Colliders
36+
MapUtils.CreateDefaultColliders(gameObject, prefab);
37+
38+
}
39+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using LevelImposter.Core;
2+
using LevelImposter.Lobby;
3+
using UnityEngine;
4+
using UnityEngine.Events;
5+
6+
namespace LevelImposter.Builders.Lobby;
7+
8+
public class LobbyWardrobeConsoleBuilder : IElemBuilder
9+
{
10+
public void OnBuild(LIElement elem, GameObject gameObject)
11+
{
12+
if (elem.type != "util-lobbywardrobe")
13+
return;
14+
15+
// Load Prefab
16+
var prefab = LobbyDropshipPrefab.GetObjectFromPrefab("panel_Wardrobe/Console");
17+
var prefabConsole = prefab.GetComponent<OptionsConsole>();
18+
19+
// Build Console
20+
var console = gameObject.AddComponent<OptionsConsole>();
21+
console.CustomPosition = prefabConsole.CustomPosition;
22+
console.HostOnly = false;
23+
console.MenuPrefab = prefabConsole.MenuPrefab;
24+
console.Outline = MapUtils.CloneSprite(gameObject, prefab.transform.parent.gameObject);
25+
console.CustomUseIcon = ImageNames.WardrobeButton;
26+
27+
// Button
28+
var button = gameObject.AddComponent<ButtonBehavior>();
29+
button.OnMouseOver = new UnityEvent();
30+
button.OnMouseOut = new UnityEvent();
31+
button.OnClick.AddListener((System.Action)console.Use);
32+
33+
// Colliders
34+
MapUtils.CreateDefaultColliders(gameObject, prefab);
35+
36+
}
37+
}

LevelImposter/Shop/Util/GameConfiguration.cs renamed to LevelImposter/Core/GameConfiguration.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
using System;
21
using AmongUs.GameOptions;
3-
using LevelImposter.AssetLoader;
4-
using LevelImposter.Core;
5-
using LevelImposter.FileIO;
2+
using LevelImposter.Lobby;
3+
using LevelImposter.Shop;
64

7-
namespace LevelImposter.Shop;
5+
namespace LevelImposter.Core;
86

97
public static class GameConfiguration
108
{

LevelImposter/Core/Utils/MapUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ public static void PreloadAllMapSprites()
458458
/// <returns>obj's SpriteRenderer</returns>
459459
public static SpriteRenderer CloneSprite(GameObject obj, GameObject? prefab, bool isSpriteAnim = false)
460460
{
461-
var prefabRenderer = prefab?.GetComponentInChildren<SpriteRenderer>();
461+
var prefabRenderer = prefab?.GetComponentInChildren<SpriteRenderer>(true);
462462
if (!prefabRenderer)
463463
throw new Exception("Failed to get SpriteRenderer from prefab");
464464

LevelImposter/DB/AssetDB.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ namespace LevelImposter.DB;
1616
/// </summary>
1717
internal class AssetDB : MonoBehaviour
1818
{
19-
public const string LEVELIMPOSTER_MAP_NAME = "Random LI Map";
2019
private const string SUBMERGED_MAP_GUID = "Submerged";
2120
private readonly Stack<MapType> _loadedShips = new();
2221
private bool _isInit;

LevelImposter/LevelImposter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Reactor.Networking;
1111
using Reactor.Networking.Attributes;
1212
using Reactor.Utilities;
13-
using UnityEngine;
1413

1514
namespace LevelImposter;
1615

@@ -85,7 +84,7 @@ public override void Load()
8584
ClassInjector.RegisterTypeInIl2Cpp<LoadingBar>();
8685
ClassInjector.RegisterTypeInIl2Cpp<LILobbyBehaviour>();
8786
ClassInjector.RegisterTypeInIl2Cpp<LobbyVersionTag>();
88-
ClassInjector.RegisterTypeInIl2Cpp<LobbyConsole>(usableInterface);
87+
ClassInjector.RegisterTypeInIl2Cpp<LobbyMapConsole>(usableInterface);
8988

9089
// Reactor Version Patch
9190
ReactorCredits.Register("LevelImposter", DisplayVersion, false, ReactorCredits.AlwaysShow);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using UnityEngine;
2+
3+
namespace LevelImposter.Lobby;
4+
5+
/// <summary>
6+
/// Stores and provides access to the lobby dropship as a prefab.
7+
/// </summary>
8+
/// <note>
9+
/// This is seperate from AssetDB since the lobby is stored in the
10+
/// "OnlineGame" scene rather than its own AssetReference.
11+
/// In addition, LobbyBehaviour is not a ShipStatus which certainly doesn't help.
12+
/// </note>
13+
public static class LobbyDropshipPrefab
14+
{
15+
private static GameObject? _prefab;
16+
17+
/// <summary>
18+
/// Called when the lobby is loaded to
19+
/// retrieve and store the dropship prefab
20+
/// </summary>
21+
public static void OnLobbyLoad()
22+
{
23+
if (_prefab != null)
24+
return;
25+
26+
// Create a disabled container to hold the prefab instance
27+
var prefabContainer = new GameObject("LI_DropshipPrefabContainer");
28+
prefabContainer.SetActive(false);
29+
30+
// Instantiate Dropship prefab
31+
var dropship = LILobbyBehaviour.GetInstance();
32+
_prefab = Object.Instantiate(dropship.gameObject, prefabContainer.transform);
33+
}
34+
35+
/// <summary>
36+
/// Instantiates a new instance of the lobby dropship prefab
37+
/// </summary>
38+
/// <param name="transform">Optional parent transform</param>
39+
/// <returns>The instantiated GameObject</returns>
40+
/// <exception cref="System.Exception">If the prefab is not loaded</exception>
41+
public static GameObject Instantiate(Transform? transform = null)
42+
{
43+
if (_prefab == null)
44+
throw new System.Exception("Lobby Dropship Prefab not loaded yet!");
45+
46+
return Object.Instantiate(_prefab, transform);
47+
}
48+
49+
/// <summary>
50+
/// Gets a GameObject from the lobby dropship prefab
51+
/// by its transform path
52+
/// </summary>
53+
/// <param name="path">The transform path to the target object</param>
54+
/// <returns>The target GameObject</returns>
55+
/// <exception cref="System.Exception">If the prefab is not loaded or the path is not found</exception>
56+
public static GameObject GetObjectFromPrefab(string path)
57+
{
58+
// Check if prefab is loaded
59+
if (_prefab == null)
60+
throw new System.Exception("Lobby Dropship Prefab not loaded yet!");
61+
62+
// Find target object
63+
var targetTransform = _prefab.transform.Find(path);
64+
if (targetTransform == null)
65+
throw new System.Exception($"Path '{path}' not found in Lobby Dropship Prefab!");
66+
67+
// Return target GameObject
68+
return targetTransform.gameObject;
69+
}
70+
}

LevelImposter/Lobby/Builders/LobbyMapBuilder.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using Il2CppInterop.Runtime.InteropTypes.Arrays;
33
using LevelImposter.Builders;
4+
using LevelImposter.Builders.Lobby;
45
using LevelImposter.Core;
56
using LevelImposter.Shop;
67
using UnityEngine;
@@ -18,8 +19,12 @@ public static class LobbyMapBuilder
1819
new AmbientSoundBuilder(),
1920
new StepSoundBuilder(),
2021

22+
new LobbyOptionsConsoleBuilder(),
23+
new LobbyWardrobeConsoleBuilder(),
24+
new LobbyMapConsoleBuilder(),
25+
2126
new TriggerShakeBuilder(),
22-
new StarfieldBuilder()
27+
new StarfieldBuilder(),
2328
]);
2429

2530
/// <summary>
@@ -66,8 +71,10 @@ private static void ResetMap()
6671
private static void BuildMap(LIMap map)
6772
{
6873
LILogger.Info($"Building lobby map from {map}...");
74+
6975
var lobbyBehaviour = LILobbyBehaviour.GetInstance();
7076
LobbyBuildRouter.BuildMap(map.elements, lobbyBehaviour.transform);
77+
7178
LILogger.Info($"Built lobby map from {map}");
7279
}
7380

@@ -76,10 +83,15 @@ private static void BuildMap(LIMap map)
7683
/// </summary>
7784
private static void BuildDropship()
7885
{
79-
var lobbyBehaviour = LILobbyBehaviour.GetLobbyBehaviour();
86+
LILogger.Info("Rebuilding original lobby dropship...");
87+
88+
var lobbyBehaviour = LILobbyBehaviour.GetInstance();
89+
Object.Destroy(lobbyBehaviour.gameObject);
90+
91+
LobbyDropshipPrefab.Instantiate();
8092

81-
lobbyBehaviour.GetComponent<Collider2D>().enabled = true;
93+
// TODO: Replay spawn animation
8294

83-
throw new NotImplementedException();
95+
LILogger.Info("Rebuilt original lobby dropship");
8496
}
8597
}

LevelImposter/Lobby/Components/LILobbyBehaviour.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
using System;
2+
using LevelImposter.Builders.Lobby;
3+
using LevelImposter.Core;
4+
using LevelImposter.Shop;
25
using UnityEngine;
36

47
namespace LevelImposter.Lobby;
58

69
public class LILobbyBehaviour(IntPtr intPtr) : MonoBehaviour(intPtr)
710
{
811
private static LILobbyBehaviour? _instance;
12+
913
private LobbyBehaviour? _lobbyBehaviour;
1014

1115
public void Awake()
@@ -15,8 +19,13 @@ public void Awake()
1519

1620
LobbyBehaviour.Instance = _lobbyBehaviour; // <-- Jump start singleton assignment for GameState.IsInLobby
1721

18-
// Rebuild lobby map on startup
19-
LobbyMapBuilder.Rebuild();
22+
// Run initialization methods
23+
LobbyDropshipPrefab.OnLobbyLoad();
24+
LobbyMapConsoleBuilder.Build();
25+
26+
// Build lobby map on startup
27+
if (GameConfiguration.CurrentLobbyMap != null)
28+
LobbyMapBuilder.Rebuild();
2029
}
2130

2231
public void Start()

0 commit comments

Comments
 (0)