Skip to content

Commit

Permalink
login states
Browse files Browse the repository at this point in the history
  • Loading branch information
shnok committed Dec 3, 2024
1 parent 9963fd0 commit f56a81e
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ public enum GameEvent
{
CONNECT_ALLOWED,
AUTH_ALLOWED,
DISCONNECTED,
SERVER_LIST_RECEIVED,
LOGIN_DISCONNECTED,
GAME_DISCONNECTED,
CHAR_CREATED,
CHAR_SELECTED,
CHAR_LOADED,
RETURN,
LOADING_STARTED,
LOADING_COMPLETE
LOADING_COMPLETE,
PLAY_ALLOWED
}
Original file line number Diff line number Diff line change
Expand Up @@ -443,13 +443,19 @@ private void InitializeState()
GameState.LOGIN_SCREEN => new LoginState(this),
GameState.LOGIN_CONNECTED => new LoginConnectedState(this),
GameState.LOGIN_AUTHED => new LoginAuthedState(this),
GameState.CHAR_SELECT => new CharSelectState(this),
_ => throw new ArgumentException("Invalid state")
};
}

public void NotifyEvent(GameEvent evt)
{
NotifyEvent(evt, null);
}

public void NotifyEvent(GameEvent evt, object arg0)
{
if (_enableLogs) Debug.Log("[GameStateMachine][EVENT] " + evt);
_stateInstance?.HandleEvent(evt);
_stateInstance?.HandleEvent(evt, arg0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using UnityEngine;

public class CharSelectState : GameStateBase
{
public CharSelectState(GameManager stateMachine) : base(stateMachine) { }

public override void Enter(object arg0)
{
L2LoginUI.Instance.ShowCharSelectWindow();

CharSelectWindow.Instance.SetCharacterList(CharacterSelector.Instance.Characters);

CharacterSelector.Instance.ApplyCharacterList();
CharacterSelector.Instance.SelectDefaultCharacter();

CharSelectWindow.Instance.SelectSlot(CharacterSelector.Instance.SelectedSlot);

LoginCameraManager.Instance.SwitchCamera("CharSelect");
}

public override void Update()
{

}

public override void HandleEvent(GameEvent evt, object arg0)
{
switch (evt)
{
case GameEvent.GAME_DISCONNECTED:
L2LoginUI.Instance.ShowLoginWindow();
_stateMachine.ChangeState(GameState.LOGIN_SCREEN);
break;
case GameEvent.AUTH_ALLOWED:
break;
default:
Debug.LogWarning($"[GameStateMachine] Unhandled event {evt} for state {_stateMachine.State}");
break;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public GameStateBase(GameManager stateMachine)
public virtual void Enter(object arg0) { }
public virtual void Exit() { }
public virtual void Update() { }
public virtual void HandleEvent(GameEvent evt) { }
public virtual void HandleEvent(GameEvent evt, object arg0) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,33 @@ public class LoginAuthedState : GameStateBase
{
public LoginAuthedState(GameManager stateMachine) : base(stateMachine) { }

public override void Enter(object arg0)
{
L2LoginUI.Instance.ShowLicenseWindow();
}

public override void Update()
{

}

public override void HandleEvent(GameEvent evt)
public override void HandleEvent(GameEvent evt, object arg0)
{
switch (evt)
{
case GameEvent.DISCONNECTED:
_stateMachine.ChangeState(GameState.LOGIN_AUTHED);
case GameEvent.LOGIN_DISCONNECTED:
L2LoginUI.Instance.ShowLoginWindow();
_stateMachine.ChangeState(GameState.LOGIN_SCREEN);
break;
case GameEvent.SERVER_LIST_RECEIVED:
L2LoginUI.Instance.ShowServerSelectWindow();
ServerSelectWindow.Instance.UpdateServerList((ServerListPacket)arg0);
break;
case GameEvent.PLAY_ALLOWED:
GameClient.Instance.Connect();
break;
case GameEvent.AUTH_ALLOWED:
_stateMachine.ChangeState(GameState.CHAR_SELECT);
break;
default:
Debug.LogWarning($"[GameStateMachine] Unhandled event {evt} for state {_stateMachine.State}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ public override void Update()

}

public override void HandleEvent(GameEvent evt)
public override void HandleEvent(GameEvent evt, object arg0)
{
switch (evt)
{
case GameEvent.AUTH_ALLOWED:
_stateMachine.ChangeState(GameState.LOGIN_AUTHED);
break;
case GameEvent.DISCONNECTED:
case GameEvent.LOGIN_DISCONNECTED:
_stateMachine.ChangeState(GameState.LOGIN_SCREEN);
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public override void Update()

}

public override void HandleEvent(GameEvent evt)
public override void HandleEvent(GameEvent evt, object arg0)
{
switch (evt)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override void Update()

}

public override void HandleEvent(GameEvent evt)
public override void HandleEvent(GameEvent evt, object arg0)
{
switch (evt)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private void OnServerListReceived(byte[] data)
ServerListPacket packet = new ServerListPacket(data);

EventProcessor.Instance.QueueEvent(
() => LoginClient.Instance.OnServerListReceived(packet.LastServer, packet.ServersData, packet.CharsOnServers));
() => LoginClient.Instance.OnServerListReceived(packet));
}

private void OnPlayFail(byte[] data)
Expand Down
1 change: 0 additions & 1 deletion l2-unity/Assets/Scripts/Networking/DefaultClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ public void Disconnect()
public virtual void OnDisconnect()
{
_connected = false;
GameManager.Instance.NotifyEvent(GameEvent.DISCONNECTED);
}

#if UNITY_EDITOR
Expand Down
3 changes: 3 additions & 0 deletions l2-unity/Assets/Scripts/Networking/GameClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public override void OnAuthAllowed()
Debug.Log("Authed to GameServer.");

// GameManager.Instance.OnAuthAllowed();
GameManager.Instance.NotifyEvent(GameEvent.AUTH_ALLOWED);
}

public void OnCharSelectAllowed()
Expand All @@ -98,6 +99,8 @@ public override void OnDisconnect()
{
base.OnDisconnect();
Debug.Log("Disconnected from GameServer.");
GameManager.Instance.NotifyEvent(GameEvent.GAME_DISCONNECTED);

}

public void OnCharCreateOk()
Expand Down
12 changes: 11 additions & 1 deletion l2-unity/Assets/Scripts/Networking/LoginClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,18 @@ public void OnPlayOk()
// {
// GameClient.Instance.Connect();
// }

GameManager.Instance.NotifyEvent(GameEvent.PLAY_ALLOWED);
}

public void OnServerListReceived(byte lastServer, List<ServerData> serverData, Dictionary<int, int> charsOnServers)
public void OnServerListReceived(ServerListPacket packet)
{
GameManager.Instance.NotifyEvent(GameEvent.SERVER_LIST_RECEIVED, packet);
// / GameState = GameState.SERVER_LIST;

// L2LoginUI.Instance.ShowServerSelectWindow();

// ServerSelectWindow.Instance.UpdateServerList(lastServer, serverData, charsOnServers);
// GameManager.Instance.OnReceivedServerList(lastServer, serverData, charsOnServers);
}

Expand All @@ -144,5 +152,7 @@ public override void OnDisconnect()
base.OnDisconnect();

Debug.Log("Disconnected from LoginServer.");

GameManager.Instance.NotifyEvent(GameEvent.LOGIN_DISCONNECTED);
}
}
6 changes: 5 additions & 1 deletion l2-unity/Assets/Scripts/UI/Login/ServerSelectWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,12 @@ protected override IEnumerator BuildWindow(VisualElement root)
yield return new WaitForEndOfFrame();
}

public void UpdateServerList(int lastServer, List<ServerData> serverData, Dictionary<int, int> charsOnServers)
public void UpdateServerList(ServerListPacket packet)
{
int lastServer = packet.LastServer;
List<ServerData> serverData = packet.ServersData;
Dictionary<int, int> charsOnServers = packet.CharsOnServers;

ResetWindow();

_serverData = serverData;
Expand Down

0 comments on commit f56a81e

Please sign in to comment.