100 lines
2.5 KiB
C#
100 lines
2.5 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
|
|
{
|
|
[Header(Constant.Header.Settings)]
|
|
[SerializeReference,SubclassSelector] private IReference expectedMap;
|
|
|
|
[Header(Constant.Header.Providers)]
|
|
[SerializeReference,SubclassSelector] private INetClient netClient;
|
|
[SerializeReference,SubclassSelector] private INetProvider netProvider;
|
|
[SerializeReference,SubclassSelector] private ISceneService sceneService;
|
|
#if STEAMWORKS_NET
|
|
[SerializeReference, SubclassSelector] private ISteamService steamService;
|
|
#endif
|
|
|
|
[SerializeField] private Optional<string> allowLoadOfflineMap;
|
|
public static IGameService Singleton { get; set; }
|
|
|
|
private readonly Optional<string> _currentMap = new();
|
|
|
|
private void Awake()
|
|
{
|
|
Singleton = this;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
netProvider.AddCommandListener<GameChangeMapCommand>(ChangeMap);
|
|
netClient.OnConnected += OnConnected;
|
|
}
|
|
|
|
private void OnConnected()
|
|
{
|
|
#if STEAMWORKS_NET
|
|
netClient.SendServerMessage($"{steamService.Name} is connected");
|
|
#endif
|
|
netClient.SendServerMessage($"{Environment.UserDomainName} is connected");
|
|
}
|
|
|
|
private void ChangeMap(GameChangeMapCommand obj)
|
|
{
|
|
sceneService.LoadSceneAsync(obj.MapName,default,LoadSceneMode.Single).Forget();
|
|
_currentMap.SetValueThenAllow(obj.MapName);
|
|
}
|
|
|
|
public string ExpectMap
|
|
{
|
|
get => expectedMap.Value;
|
|
set => expectedMap = new Reference(value);
|
|
}
|
|
|
|
public void Play()
|
|
{
|
|
if (allowLoadOfflineMap.Allow)
|
|
{
|
|
ChangeMap(new GameChangeMapCommand(){MapName = allowLoadOfflineMap.Value});
|
|
}
|
|
else
|
|
{
|
|
if (netClient.IsConnected)
|
|
{
|
|
netProvider.ServerCommand(new GameRequestMapCommand(){MapName = "Map1"});
|
|
}
|
|
else
|
|
{
|
|
ChangeMap(new GameChangeMapCommand(){MapName =expectedMap.Value});
|
|
}
|
|
}
|
|
}
|
|
public void Stop()
|
|
{
|
|
if (allowLoadOfflineMap.Allow)
|
|
{
|
|
sceneService.UnloadSceneAsync(allowLoadOfflineMap.Value,default).Forget();
|
|
}else if (_currentMap.Allow)
|
|
{
|
|
sceneService.UnloadSceneAsync(_currentMap.Value,default).Forget();
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|