125 lines
3.3 KiB
C#
125 lines
3.3 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using BITKit;
|
|
using BITKit.Game;
|
|
using BITKit.SceneManagement;
|
|
#if STEAMWORKS_NET
|
|
using BITKit.Steamwork;
|
|
#endif
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
namespace BITFALL.Game{
|
|
[Serializable]
|
|
public sealed class GameServiceSingleton:GameServiceImplement
|
|
{
|
|
protected override IGameService service => GameService.Singleton;
|
|
}
|
|
public sealed class GameService : StateBasedMonoBehaviour<IGameState>,IGameService
|
|
{
|
|
public static IGameService Singleton { get; set; }
|
|
|
|
[Header(Constant.Header.Settings)]
|
|
[SerializeReference,SubclassSelector] private IReference expectedMap;
|
|
|
|
[Header(Constant.Header.Providers)]
|
|
[SerializeReference,SubclassSelector] private INetClient netClient;
|
|
[SerializeReference,SubclassSelector] private INetServer netServer;
|
|
[SerializeReference,SubclassSelector] private ISceneService sceneService;
|
|
#if STEAMWORKS_NET
|
|
[SerializeReference, SubclassSelector] private ISteamService steamService;
|
|
#endif
|
|
|
|
[SerializeField] private Optional<string> allowLoadOfflineMap;
|
|
|
|
private readonly Optional<string> _currentMap = new();
|
|
private INetProvider clientNetProvider => netClient.Source as INetProvider;
|
|
private INetProvider serverNetProvider => netServer.Source as INetProvider;
|
|
|
|
private void Awake()
|
|
{
|
|
Singleton = this;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
Initialize();
|
|
TransitionState<GameInMenuState>();
|
|
|
|
clientNetProvider.AddCommandListener<GameChangeMapCommand>(ChangeMap);
|
|
|
|
netServer.OnClientConnected += OnClientConnected;
|
|
|
|
sceneService.OnSceneLoaded += OnSceneLoaded;
|
|
destroyCancellationToken.Register(() =>
|
|
{
|
|
netServer.OnClientConnected -= OnClientConnected;
|
|
sceneService.OnSceneLoaded -= OnSceneLoaded;
|
|
|
|
//clientNetProvider.RemoveCommandListener<GameChangeMapCommand>(ChangeMap);
|
|
});
|
|
|
|
if(PlayerPrefs.HasKey(BITConstant.Environment.sv_map))
|
|
{
|
|
expectedMap = new Reference(PlayerPrefs.GetString(BITConstant.Environment.sv_map));
|
|
}
|
|
}
|
|
|
|
private void OnSceneLoaded(string obj)
|
|
{
|
|
TransitionState<GameInPlayState>();
|
|
}
|
|
|
|
private void OnClientConnected(int obj)
|
|
{
|
|
serverNetProvider.ClientCommand(obj,new GameChangeMapCommand(){MapName = _currentMap.Value});
|
|
}
|
|
private void ChangeMap(GameChangeMapCommand obj)
|
|
{
|
|
TransitionState<GameLoadingState>();
|
|
sceneService.LoadSceneAsync(obj.MapName,default,LoadSceneMode.Single).Forget();
|
|
_currentMap.SetValueThenAllow(obj.MapName);
|
|
}
|
|
|
|
public string ExpectMap
|
|
{
|
|
get => expectedMap.Value;
|
|
set
|
|
{
|
|
expectedMap = new Reference(value);
|
|
PlayerPrefs.SetString(BITConstant.Environment.sv_map,value);
|
|
PlayerPrefs.Save();
|
|
}
|
|
}
|
|
public void Play()
|
|
{
|
|
if (allowLoadOfflineMap.Allow)
|
|
{
|
|
ChangeMap(new GameChangeMapCommand(){MapName = allowLoadOfflineMap.Value});
|
|
}
|
|
else
|
|
{
|
|
ChangeMap(new GameChangeMapCommand() { MapName = expectedMap.Value });
|
|
}
|
|
}
|
|
public void Stop()
|
|
{
|
|
TransitionState<GameInMenuState>();
|
|
if (allowLoadOfflineMap.Allow)
|
|
{
|
|
sceneService.UnloadSceneAsync(allowLoadOfflineMap.Value,default).Forget();
|
|
}else if (_currentMap.Allow)
|
|
{
|
|
sceneService.UnloadSceneAsync(_currentMap.Value,default).Forget();
|
|
}
|
|
if(netClient.IsConnected)
|
|
netClient.Disconnect();
|
|
if(netServer.IsRunningServer)
|
|
netServer.StopServer();
|
|
}
|
|
}
|
|
|
|
}
|