Skip to content

Commit

Permalink
Move all event manager instances to static Events class
Browse files Browse the repository at this point in the history
  • Loading branch information
valkyrienyanko committed Mar 11, 2023
1 parent 41bc05d commit 3e0dc05
Show file tree
Hide file tree
Showing 15 changed files with 55 additions and 56 deletions.
2 changes: 1 addition & 1 deletion Scripts/Entities/Level/Coin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ private void _on_Area_area_entered(Area2D area)
{
if (area.GetParent() is Player)
{
GameManager.Events.Notify(Event.OnCoinPickup);
Events.Generic.Notify(EventGeneric.OnCoinPickup);
GameManager.PlayerManager.AddCoins();
GameManager.LevelUI.SetLabelCoins(GameManager.PlayerManager.Coins);
QueueFree();
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Entities/Level/LevelScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override void _Ready()
}
}

GameManager.Events.AddListener(Event.OnGameClientLeft, (args) =>
Events.Generic.AddListener(EventGeneric.OnGameClientLeft, (args) =>
{
var id = (byte)args[0];
OtherPlayers[id].QueueFree();
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Entities/Player/Commands/PlayerCommandDash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public override void Start()

if (DashDir != Vector2.Zero)
{
GameManager.EventsPlayer.Notify(EventPlayer.OnDash);
Events.Player.Notify(EventPlayer.OnDash);
Entity.GravityEnabled = false;
DashCount++;
DashReady = false;
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Entities/Player/Commands/PlayerCommandDeath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public override void Start()

Entity.HaltLogic = true;

GameManager.EventsPlayer.Notify(EventPlayer.OnDied);
Events.Player.Notify(EventPlayer.OnDied);

Entity.LevelScene.Camera.StopFollowingPlayer();
Entity.AnimatedSprite.Stop();
Expand Down
4 changes: 2 additions & 2 deletions Scripts/Entities/Player/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public override void UpdatePhysics()
HoldingJumpKey = true;
JumpForceLossCounter = 0;

GameManager.EventsPlayer.Notify(EventPlayer.OnJump);
Events.Player.Notify(EventPlayer.OnJump);

JumpCount++;
//Velocity = new Vector2(Velocity.X, 0); // reset velocity before jump (is this really needed?)
Expand Down Expand Up @@ -168,7 +168,7 @@ public override void UpdatePhysicsAir()

public void OnWallJump(object _, EventArgs _2)
{
GameManager.EventsPlayer.Notify(EventPlayer.OnJump);
Events.Player.Notify(EventPlayer.OnJump);

// Lock movement
PreventMovement = true;
Expand Down
6 changes: 1 addition & 5 deletions Scripts/Managers/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ public class GameManager
public static UIMenu Menu { get; private set; }
public static PlayerManager PlayerManager { get; private set; }

// notifications
public static EventManager<Event> Events { get; private set; } = new();
public static EventManager<EventPlayer> EventsPlayer { get; private set; } = new();

private static Node NodeMap { get; set; }

public GameManager(Linker linker)
Expand Down Expand Up @@ -75,7 +71,7 @@ public static void LoadMap()
Map = (Map)Prefabs.Map.Instantiate();

NodeMap.CallDeferred("add_child", Map); // need to wait for the engine because we are dealing with areas with is physics related
GameManager.Events.Notify(Event.OnMapLoaded);
Events.Generic.Notify(EventGeneric.OnMapLoaded);
}

public static void DestroyMap() => NodeMap.QueueFreeChildren();
Expand Down
4 changes: 2 additions & 2 deletions Scripts/Netcode/Client/GameClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected override void Receive(PacketReader reader)

protected override void Connect(ref Event netEvent)
{
GodotCommands.Enqueue(GodotOpcode.NetEvent, Sankari.Event.OnGameClientConnected);
GodotCommands.Enqueue(GodotOpcode.NetEvent, Sankari.EventGeneric.OnGameClientConnected);
Log("Client connected");
}

Expand All @@ -62,7 +62,7 @@ protected override void Leave(ref Event netEvent)

protected override void Stopped()
{
GodotCommands.Enqueue(GodotOpcode.NetEvent, Sankari.Event.OnGameClientStopped);
GodotCommands.Enqueue(GodotOpcode.NetEvent, Sankari.EventGeneric.OnGameClientStopped);
Log("Client stopped");
}

Expand Down
6 changes: 3 additions & 3 deletions Scripts/Netcode/Packets/SPacketGameInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ public override async Task Handle()
private void HandlePlayerJoinLeave()
{
if (Joining)
GameManager.Events.Notify(Event.OnGameClientJoined, Id, Username);
Events.Generic.Notify(EventGeneric.OnGameClientJoined, Id, Username);
else
GameManager.Events.Notify(Event.OnGameClientLeft, Id);
Events.Generic.Notify(EventGeneric.OnGameClientLeft, Id);
}

private void HandlePlayersOnServer()
{
GameManager.Events.Notify(Event.OnReceivePlayersFromServer, Usernames);
Events.Generic.Notify(EventGeneric.OnReceivePlayersFromServer, Usernames);
}

private async Task HandleStartLevel()
Expand Down
14 changes: 5 additions & 9 deletions Scripts/Static/Audio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,30 @@ public static void Init(GAudioStreamPlayer musicPlayer, Node sfxPlayers)
LoadSoundEffects();
LoadSoundTracks();

var eventsPlayer = GameManager.EventsPlayer;

// Player
eventsPlayer.AddListener(EventPlayer.OnJump, (args) =>
Events.Player.AddListener(EventPlayer.OnJump, (args) =>
{
PlaySFX("player_jump", 80);
});

eventsPlayer.AddListener(EventPlayer.OnDied, (args) =>
Events.Player.AddListener(EventPlayer.OnDied, (args) =>
{
StopMusic();
PlaySFX("game_over_1");
});

eventsPlayer.AddListener(EventPlayer.OnDash, (args) =>
Events.Player.AddListener(EventPlayer.OnDash, (args) =>
{
PlaySFX("dash");
});

// Game
var events = GameManager.Events;

events.AddListener(Event.OnCoinPickup, (args) =>
Events.Generic.AddListener(EventGeneric.OnCoinPickup, (args) =>
{
PlaySFX("coin_pickup_1", 30);
});

events.AddListener(Event.OnMapLoaded, (args) =>
Events.Generic.AddListener(EventGeneric.OnMapLoaded, (args) =>
{
//PlayMusic("map_grassy");
});
Expand Down
24 changes: 0 additions & 24 deletions Scripts/Static/EventManager.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
namespace Sankari;

public enum Event
{
OnKeyboardInput,
OnMouseButtonInput,
OnMouseMotionInput,
OnJoypadButtonInput,
OnSceneChanged,
OnGameClientStopped,
OnGameClientConnected,
OnLevelLoaded,
OnGameClientLeft,
OnGameClientJoined,
OnReceivePlayersFromServer,
OnCoinPickup,
OnMapLoaded
}

public enum EventPlayer
{
OnJump,
OnDied,
OnDash
}

/// <summary>
/// This class was created to attempt to simplify the process of creating C# for gamedev.
///
Expand Down
31 changes: 31 additions & 0 deletions Scripts/Static/Events.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Sankari;

public static class Events
{
public static EventManager<EventGeneric> Generic { get; private set; } = new();
public static EventManager<EventPlayer> Player { get; private set; } = new();
}

public enum EventGeneric
{
OnKeyboardInput,
OnMouseButtonInput,
OnMouseMotionInput,
OnJoypadButtonInput,
OnSceneChanged,
OnGameClientStopped,
OnGameClientConnected,
OnLevelLoaded,
OnGameClientLeft,
OnGameClientJoined,
OnReceivePlayersFromServer,
OnCoinPickup,
OnMapLoaded
}

public enum EventPlayer
{
OnJump,
OnDied,
OnDash
}
2 changes: 1 addition & 1 deletion Scripts/Static/GodotCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static async Task Update()
packetReader.Dispose();
break;
case GodotOpcode.NetEvent:
GameManager.Events.Notify((Event)cmd.Data);
Events.Generic.Notify((EventGeneric)cmd.Data);
break;
case GodotOpcode.SpawnPopupMessage:
var dataMessage = (GodotCmdPopupMessage)cmd.Data;
Expand Down
2 changes: 1 addition & 1 deletion Scripts/Static/LevelManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private static void LoadALevel()
public static async Task CompleteLevel(string levelName)
{
// the level scene is no longer active, clean it up
GameManager.Events.RemoveAllListenersForEventType(Event.OnGameClientLeft);
Events.Generic.RemoveAllListenersForEventType(EventGeneric.OnGameClientLeft);
GameManager.LevelScene = null;

await GameManager.Transition.AlphaToBlackAndBack();
Expand Down
4 changes: 2 additions & 2 deletions Scripts/Static/Net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static void Init()

var mapScript = GameManager.UIMapMenu;

GameManager.Events.AddListener(Event.OnGameClientStopped, (args) =>
Events.Generic.AddListener(EventGeneric.OnGameClientStopped, (args) =>
{
Client.ExecuteCode((client) => client.TryingToConnect = false);
mapScript.BtnJoin.Disabled = false;
Expand All @@ -39,7 +39,7 @@ public static void Init()
GameManager.UIPlayerList.RemoveAllPlayers();
});

GameManager.Events.AddListener(Event.OnGameClientConnected, (args) =>
Events.Generic.AddListener(EventGeneric.OnGameClientConnected, (args) =>
{
Client.Send(ClientPacketOpcode.GameInfo, new CPacketGameInfo
{
Expand Down
6 changes: 3 additions & 3 deletions Scripts/UI/UIPlayerList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ public override void _Ready()

public void SetupListeners()
{
GameManager.Events.AddListener(Event.OnGameClientLeft, (args) =>
Events.Generic.AddListener(EventGeneric.OnGameClientLeft, (args) =>
{
RemovePlayer((byte)args[0]);
});

GameManager.Events.AddListener(Event.OnGameClientJoined, (args) =>
Events.Generic.AddListener(EventGeneric.OnGameClientJoined, (args) =>
{
AddPlayer((byte)args[0], (string)args[1]);
});

GameManager.Events.AddListener(Event.OnReceivePlayersFromServer, (args) =>
Events.Generic.AddListener(EventGeneric.OnReceivePlayersFromServer, (args) =>
{
var usernames = (Dictionary<byte, string>)args[0];

Expand Down

0 comments on commit 3e0dc05

Please sign in to comment.