Skip to content

Instantly share code, notes, and snippets.

@FairlySadPanda
Created October 14, 2024 23:27
Show Gist options
  • Save FairlySadPanda/1a5b6f94b9b23c442d5c4247904fa4e0 to your computer and use it in GitHub Desktop.
Save FairlySadPanda/1a5b6f94b9b23c442d5c4247904fa4e0 to your computer and use it in GitHub Desktop.
Example of registration and use of event bus pattern in UdonSharp for VRChat :)
using UnityEngine;
using VRCBilliardsCE.Packages.com.vrcbilliards.vrcbce.Runtime.ScriptsV2.Core;
using VRCBilliardsCE.Packages.com.vrcbilliards.vrcbce.Runtime.ScriptsV2.Core.Infrastructure;
namespace VRCBilliardsCE.Packages.com.vrcbilliards.vrcbce.Runtime.ScriptsV2.Menu.Domain
{
public enum State
{
// The main menu is closed.
Closed,
// The main menu is locked and cannot be used.
Locked,
// The main menu is in the main menu.
InMainMenu,
// The main menu is hidden due to being in game.
InGame,
// The main menu is in tutorial mode.
InTutorial
}
public class MainMenu : LocalBehaviour
{
public BaseMenuImpl menu;
public DependencyManager dependency;
private State currentState = State.Closed;
private EventBus bus;
public void Start()
{
bus = dependency.Bus;
bus.Register(Core.Domain.Event.MenuOpened, this, nameof(OnMenuOpened));
bus.Register(Core.Domain.Event.MenuClosed, this, nameof(OnMenuClosed));
bus.Register(Core.Domain.Event.PlayerRegistrationChanged, this, nameof(OnPlayerRegistrationChanged));
bus.Register(Core.Domain.Event.GameOptionsChanged, this, nameof(OnGameSettingsChanged));
bus.Register(Core.Domain.Event.GameStarted, this, nameof(OnGameStarted));
bus.Register(Core.Domain.Event.GameOverCancelled, this, nameof(OnGameEnded));
bus.Register(Core.Domain.Event.GameOverWon, this, nameof(OnGameEnded));
bus.Register(Core.Domain.Event.MenuLocked, this, nameof(OnMenuLocked));
bus.Register(Core.Domain.Event.MenuUnlocked, this, nameof(OnMenuUnlocked));
}
public void OpenMenu()
{
if (currentState != State.Closed)
{
Debug.Log("was not closed; discarding");
return;
}
Debug.Log("raising menuopened");
bus.Raise(Core.Domain.Event.MenuOpened);
}
public void CloseMenu()
{
if (currentState != State.InMainMenu)
{
Debug.Log("was not open; discarding");
return;
}
Debug.Log("raising menuclosed");
bus.Raise(Core.Domain.Event.MenuClosed);
}
public void StartGame()
{
if (currentState != State.InMainMenu)
{
return;
}
bus.Raise(Core.Domain.Event.MenuStartedGame);
}
public void JoinGame()
{
if (currentState != State.InMainMenu)
{
return;
}
}
public void LeaveGame()
{
if (currentState != State.InMainMenu)
{
return;
}
bus.Raise(Core.Domain.Event.PlayerRegistrationChanged);
}
public void OnMenuOpened()
{
if (currentState != State.Closed)
{
return;
}
currentState = State.InMainMenu;
menu.OpenMenu();
}
public void OnMenuClosed()
{
if (currentState != State.InMainMenu)
{
return;
}
currentState = State.Closed;
menu.CloseMenu();
}
public void OnMenuLocked()
{
if (currentState == State.Locked)
{
return;
}
currentState = State.Locked;
menu.LockMenu();
}
public void OnMenuUnlocked()
{
if (currentState != State.Locked)
{
return;
}
currentState = State.Closed;
menu.UnlockMenu();
}
public void OnGameStarted() {}
public void OnGameEnded() {}
public void OnPlayerRegistrationChanged() {}
public void OnGameSettingsChanged() {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment