Skip to content

Commit

Permalink
disconnecting state
Browse files Browse the repository at this point in the history
  • Loading branch information
shnok committed Dec 4, 2024
1 parent 48ba8c3 commit fb00613
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 297 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ public enum GameEvent
LOADING_COMPLETE,
PLAY_ALLOWED,
RESTART_ALLOWED,
EXIT_ALLOWED,
}
295 changes: 1 addition & 294 deletions l2-unity/Assets/Scripts/Game/Manager/StateMachine/GameManager.cs
Original file line number Diff line number Diff line change
@@ -1,297 +1,3 @@
// using System;
// using System.Collections;
// using System.Collections.Generic;
// using UnityEngine;
// using static CharSelectedPacket;
// using static ServerListPacket;

// public class GameManager : MonoBehaviour
// {
// // TODO: SWITCH TO EVENT HANDLER AND STATE MACHINE
// // TODO: SWITCH TO EVENT HANDLER AND STATE MACHINE
// // TODO: SWITCH TO EVENT HANDLER AND STATE MACHINE
// // TODO: SWITCH TO EVENT HANDLER AND STATE MACHINE
// // TODO: SWITCH TO EVENT HANDLER AND STATE MACHINE
// // TODO: SWITCH TO EVENT HANDLER AND STATE MACHINE
// // TODO: SWITCH TO EVENT HANDLER AND STATE MACHINE
// [SerializeField] private int _protocolVersion = 740;
// [SerializeField] private GameState _gameState = GameState.LOGIN_SCREEN;
// private bool _gameReady = false;
// [SerializeField] private bool _autoLogin = false;
// [SerializeField] private Camera _loadingCamera;
// [SerializeField] private bool _loading;

// public bool AutoLogin { get { return _autoLogin; } }

// public GameState GameState
// {
// get { return _gameState; }
// set
// {
// _gameState = value;
// Debug.Log($"Game state is now {_gameState}.");
// }
// }

// public bool GameReady { get { return _gameReady; } set { _gameReady = value; } }
// public int ProtocolVersion { get { return _protocolVersion; } }

// private static GameManager _instance;
// public static GameManager Instance { get { return _instance; } }

// void Awake()
// {
// if (_instance == null)
// {
// _instance = this;
// }
// else if (_instance != this)
// {
// Destroy(this);
// }

// GameObject camObject = GameObject.Find("LoadingCamera");
// if (camObject == null)
// {
// Debug.LogError("Can't find loading camera");
// return;
// }

// camObject.TryGetComponent(out _loadingCamera);

// StartLoading();
// }

// private void Start()
// {
// LoadTables();
// SceneLoader.Instance.LoadMenu();
// }

// private void LoadTables()
// {
// SkillSoundgrpTable.Instance.Initialize();
// SkillTable.Instance.Initialize();
// ItemTable.Instance.Initialize();
// ItemNameTable.Instance.Initialize();
// ItemStatDataTable.Instance.Initialize();
// ArmorgrpTable.Instance.Initialize();
// EtcItemgrpTable.Instance.Initialize();
// WeapongrpTable.Instance.Initialize();
// NpcgrpTable.Instance.Initialize();
// NpcNameTable.Instance.Initialize();
// ActionNameTable.Instance.Initialize();
// SysStringTable.Instance.Initialize();
// SkillNameTable.Instance.Initialize();
// SkillgrpTable.Instance.Initialize();
// LogongrpTable.Instance.Initialize();
// SystemMessageTable.Instance.Initialize();
// ChargrpTable.Instance.Initialize();

// // Caching
// ItemTable.Instance.CacheItems();
// SkillTable.Instance.CacheSkills();
// ModelTable.Instance.Initialize();
// SkillEffectTable.Instance.Initialize();
// ParticleEffectTable.Instance.Initialize();
// IconTable.Instance.Initialize();
// KeyImageTable.Instance.Initialize();

// // Memory cleanup
// ArmorgrpTable.Instance.ClearTable();
// EtcItemgrpTable.Instance.ClearTable();
// ItemNameTable.Instance.ClearTable();
// SkillgrpTable.Instance.ClearTable();
// SkillNameTable.Instance.ClearTable();
// }

// public void OnWorldSceneLoaded()
// {
// Debug.LogWarning("OnWorldSceneLoaded");
// GameObject.Destroy(L2LoginUI.Instance.gameObject);

// PlayerInfo playerInfo = GameClient.Instance.PlayerInfo;

// WorldClock.Instance.SynchronizeClock(playerInfo.CurrentGameTime);

// WorldSpawner.Instance.OnReceivePlayerInfo(playerInfo.Identity, playerInfo.Status, playerInfo.Stats, playerInfo.Appearance, playerInfo.EntityActionInfo);

// PlayerStateMachine.Instance.enabled = true;

// GameClient.Instance.ClientPacketHandler.SendLoadWorld();
// }

// public void OnPlayerInfoReceive()
// {
// // Add a small delay to avoid visual bugs
// if (_loading)
// {
// StartCoroutine(StopLoading());
// }
// }

// public void OnLoginServerConnected()
// {
// GameState = GameState.LOGIN_CONNECTED;
// }

// public void OnLoginServerAuthAllowed()
// {
// GameState = GameState.READING_LICENSE;

// L2LoginUI.Instance.ShowLicenseWindow();
// }

// public void OnLoginServerPlayOk()
// {
// GameState = GameState.READY_TO_CONNECT;
// }

// public void OnConnectingToGameServer()
// {
// GameState = GameState.CONNECTING_TO_GAMESERVER;
// }

// public void OnReceivedServerList(byte lastServer, List<ServerData> serverData, Dictionary<int, int> charsOnServers)
// {
// GameState = GameState.SERVER_LIST;

// L2LoginUI.Instance.ShowServerSelectWindow();

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

// public void OnAuthAllowed()
// {
// GameState = GameState.CHAR_SELECT;

// SwitchToCharSelect();

// L2LoginUI.Instance.ShowCharSelectWindow();
// }

// public void OnCharacterSelect()
// {
// GameState = GameState.IN_GAME;

// StartLoading();
// SceneLoader.Instance.LoadGame();
// }

// public void OnCreateUser()
// {
// GameState = GameState.CHAR_CREATION;

// LoginCameraManager.Instance.SwitchCamera("Login");

// L2LoginUI.Instance.ShowCharCreationWindow();
// }

// public void OnWorldLoading()
// {
// MusicManager.Instance.Clear();
// StartLoading();
// }

// public void OnRelogin()
// {
// GameState = GameState.LOGIN_SCREEN;

// LoginCameraManager.Instance.SwitchCamera("Login");

// L2LoginUI.Instance.ShowLoginWindow();
// }

// public void OnDisconnect()
// {
// if (GameState > GameState.CHAR_CREATION)
// {
// MusicManager.Instance.Clear();
// SceneLoader.Instance.LoadMenu();
// }
// else if (GameState > GameState.LOGIN_SCREEN && !GameClient.Instance.IsConnected && !LoginClient.Instance.IsConnected)
// {
// OnRelogin();
// }
// }

// public void OnGameserverSelected()
// {
// Debug.Log("Gameserver selected, connecting...");

// //GameClient.Instance.Connect();
// }

// public void OnStartingGame()
// {
// Debug.Log("On Starting game");
// }

// public void OnGameLaunched()
// {
// Debug.Log("On game launched");

// if (GameState == GameState.RESTARTING)
// {
// GameState = GameState.CHAR_SELECT;
// L2LoginUI.Instance.ShowCharSelectWindow();
// }

// StopLoading();
// PawnCreator.Instance.SpawnAllPawns();
// }

// internal void OnCharSelectAllowed()
// {
// if (GameState > GameState.IN_GAME)
// {
// MusicManager.Instance.Clear();
// SceneLoader.Instance.LoadMenu();
// StartLoading();
// }
// }

// public void OnLoginCamerasInitialized()
// {
// if (GameState == GameState.CHAR_SELECT)
// {
// SwitchToCharSelect();
// }
// else
// {
// LoginCameraManager.Instance.SwitchCamera("Login");
// }
// }

// public void SwitchToCharSelect()
// {
// CharSelectWindow.Instance.SetCharacterList(CharacterSelector.Instance.Characters);

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

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

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

// public void OnCharCreateOk()
// {
// SwitchToCharSelect();
// }

// public void OnLoginUILoaded()
// {
// if (GameState == GameState.RESTARTING)
// {
// LoginWindow.Instance.HideWindow();
// }
// else
// {
// LoginWindow.Instance.ShowLogo();
// }
// }

using System;
using System.Collections;
using UnityEngine;
Expand Down Expand Up @@ -455,6 +161,7 @@ private void InitializeState()
GameState.ENTERING_WORLD => new EnteringWorldState(this),
GameState.IN_GAME => new InGameState(this),
GameState.RESTARTING => new RestartingState(this),
GameState.DISONNECTING => new DisconnectingState(this),
_ => throw new ArgumentException("Invalid state")
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ public enum GameState : byte
CHAR_SELECT,
CHAR_CREATION,
IN_GAME,
RESTARTING
RESTARTING,
DISONNECTING
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using UnityEngine;

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

public override void Enter(object arg0)
{
MusicManager.Instance.Clear();
_stateMachine.StartLoading();

SceneLoader.Instance.LoadMenu();
}

public override void Update()
{

}

public override void HandleEvent(GameEvent evt, object arg0)
{
switch (evt)
{
case GameEvent.UI_LOADED:
case GameEvent.WORLD_LOADED:
base.HandleEvent(evt, arg0);
break;
case GameEvent.LOADING_COMPLETE:
_stateMachine.StopLoading();
L2LoginUI.Instance.ShowLoginWindow();
_stateMachine.ChangeState(GameState.LOGIN_SCREEN);
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 @@ -25,6 +25,7 @@ public override void HandleEvent(GameEvent evt, object arg0)
_stateMachine.StopLoading();
break;
case GameEvent.GAME_DISCONNECTED:
_stateMachine.ChangeState(GameState.DISONNECTING);
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 @@ -85,10 +85,11 @@ public void Disconnect()
return;
}

Debug.Log("Disconnect");

ClientCleanup();

Debug.Log("Disconnect");

EventProcessor.Instance.QueueEvent(() => _client.OnDisconnect());
}

Expand Down

0 comments on commit fb00613

Please sign in to comment.