进入房间相关代码流程实现

This commit is contained in:
ALIENJACK\alien 2024-11-08 11:31:12 +08:00
parent 46a62ca9f7
commit 814cc5ed5e
9 changed files with 79 additions and 28 deletions

View File

@ -1,5 +1,3 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace AxibugEmuOnline.Client
@ -17,4 +15,13 @@ namespace AxibugEmuOnline.Client
void SetupScheme();
void StartGame(RomFile romFile);
}
public static class IEnumCoreTool
{
public static bool IsNull(this IEmuCore core)
{
if (core == null) return true;
return core.Equals(null);
}
}
}

View File

@ -1,17 +1,47 @@
using AxibugEmuOnline.Client.ClientCore;
using AxibugEmuOnline.Client.Event;
using MyNes.Core;
using System;
using UnityEngine;
namespace AxibugEmuOnline.Client.Manager
{
public class AppEmu
{
private GameObject m_emuInstance;
/// <summary>
/// unity的c#实现有bug,以接口类型保存的monobehaviour引用,!=和==运算符没有调用到monobehaviour重写过的运算符
/// 但是Equals方法可以,所以,这个接口判断为空请使用Equals
/// </summary>
private IEmuCore m_emuCore;
public AppEmu()
{
Eventer.Instance.RegisterEvent(EEvent.OnMineJoinRoom, OnSelfJoinRoom);
}
private void OnSelfJoinRoom()
{
//如果当前正在游戏中,就先结束游戏
if (!m_emuCore.IsNull()) StopGame();
var roomInfo = App.roomMgr.mineRoomMiniInfo;
roomInfo.FetchRomFileInRoomInfo(EnumPlatform.NES, (room, romFile) =>
{
if (!romFile.RomReady) //这个rom并没有下载,所以取消进入房间
{
App.roomMgr.SendLeavnRoom();
}
else
{
BeginGame(romFile);
}
});
}
public void BeginGame(RomFile romFile)
{
if (m_emuInstance != null) return;
if (!m_emuCore.Equals(null)) return;
switch (romFile.Platform)
{
@ -20,8 +50,6 @@ namespace AxibugEmuOnline.Client.Manager
break;
}
m_emuInstance = m_emuCore.gameObject;
m_emuCore.StartGame(romFile);
LaunchUI.Instance.HideMainMenu();
InGameUI.Instance.Show(romFile, m_emuCore);
@ -31,9 +59,9 @@ namespace AxibugEmuOnline.Client.Manager
public void StopGame()
{
if (m_emuInstance == null) return;
GameObject.Destroy(m_emuInstance);
m_emuInstance = null;
if (m_emuCore.IsNull()) return;
GameObject.Destroy(m_emuCore.gameObject);
m_emuCore = null;
InGameUI.Instance.Hide();
LaunchUI.Instance.ShowMainMenu();

View File

@ -48,7 +48,7 @@ namespace AxibugEmuOnline.Client
public int Page { get; private set; }
public string Hash => webData != null ? webData.hash : string.Empty;
public event Action OnDownloadOver;
public event Action<RomFile> OnDownloadOver;
public event Action OnInfoFilled;
public RomFile(EnumPlatform platform, int index, int insidePage)
@ -73,7 +73,7 @@ namespace AxibugEmuOnline.Client
File.WriteAllBytes(LocalFilePath, bytes);
hasLocalFile = true;
}
OnDownloadOver?.Invoke();
OnDownloadOver?.Invoke(this);
}));
}
@ -115,13 +115,16 @@ namespace AxibugEmuOnline.Client
downloadRequest = UnityWebRequest.Get($"{App.httpAPI.WebHost}/{webData.url}");
yield return downloadRequest.SendWebRequest();
if (downloadRequest.result != UnityWebRequest.Result.Success)
var request = downloadRequest;
downloadRequest = null;
if (request.result != UnityWebRequest.Result.Success)
{
callback(null);
}
else
{
callback(downloadRequest.downloadHandler.data);
callback(request.downloadHandler.data);
}
}

View File

@ -69,7 +69,6 @@ namespace AxibugEmuOnline.Client
}
}
public void Pause()
{
m_bPause = true;

View File

@ -18,7 +18,6 @@ namespace AxibugEmuOnline.Client
{
m_step = step;
switch (m_step)
{
//等待主机上报快照

View File

@ -2,6 +2,7 @@ using AxibugEmuOnline.Client.ClientCore;
using AxibugEmuOnline.Client.Event;
using AxibugEmuOnline.Client.UI;
using AxibugProtobuf;
using System;
using UnityEngine;
using UnityEngine.UI;
@ -23,6 +24,20 @@ namespace AxibugEmuOnline.Client
public int Index { get; set; }
public int roomID { get; private set; }
protected override void Awake()
{
base.Awake();
Eventer.Instance.RegisterEvent<int>(EEvent.OnRoomListSingleUpdate, OnRoomSignelUpdate);
}
private void OnRoomSignelUpdate(int roomID)
{
if (this.roomID != roomID) return;
if (App.roomMgr.GetRoomListMiniInfo(roomID, out var roomInfo))
UpdateUI(roomInfo);
}
public void SetData(object data)
{
@ -53,12 +68,12 @@ namespace AxibugEmuOnline.Client
SetBaseInfo(string.Empty, $"<b>{hostNick}</b>µÄ·¿¼ä - {cur}/{max}");
SetIcon(null);
roomInfo.FetchRomFileInRoomInfo(EnumPlatform.NES, (romFile) =>
roomInfo.FetchRomFileInRoomInfo(EnumPlatform.NES, (room, romFile) =>
{
m_romFile = romFile;
if (room.RoomID != roomID) return;
if (romFile.ID == roomInfo.GameRomID)
Txt.text = romFile.Alias;
m_romFile = romFile;
Txt.text = romFile.Alias;
UpdateRomInfoView();
App.CacheMgr.GetSpriteCache(romFile.ImageURL, OnGetRomImage);

View File

@ -12,7 +12,7 @@ namespace AxibugEmuOnline.Client
{
Eventer.Instance.RegisterEvent<int>(EEvent.OnRoomListAllUpdate, OnRoomListUpdateAll);
Eventer.Instance.RegisterEvent<int>(EEvent.OnRoomListSingleClose, OnRoomClosed);
Eventer.Instance.RegisterEvent<int>(EEvent.OnRoomListSingleUpdate, OnRoomSingleUpdate);
Eventer.Instance.RegisterEvent<int>(EEvent.OnRoomListSingleAdd, OnRoomSingleAdd);
base.Awake();
}
@ -20,7 +20,7 @@ namespace AxibugEmuOnline.Client
protected override void OnDestroy()
{
Eventer.Instance.UnregisterEvent<int>(EEvent.OnRoomListAllUpdate, OnRoomListUpdateAll);
Eventer.Instance.UnregisterEvent<int>(EEvent.OnRoomListSingleUpdate, OnRoomSingleUpdate);
Eventer.Instance.UnregisterEvent<int>(EEvent.OnRoomListSingleUpdate, OnRoomSingleAdd);
Eventer.Instance.UnregisterEvent<int>(EEvent.OnRoomListSingleClose, OnRoomClosed);
}
@ -38,7 +38,7 @@ namespace AxibugEmuOnline.Client
return res;
}
private void OnRoomSingleUpdate(int obj)
private void OnRoomSingleAdd(int obj)
{
if (m_entering)
{

View File

@ -20,7 +20,7 @@ namespace AxibugEmuOnline.Client
base.Awake();
RomGroupRoot.gameObject.SetActive(false);
RomGroupRoot.alpha = 0;
RomGroupRoot.alpha = 0;
}
public override void SetSelectState(bool selected)
@ -58,7 +58,7 @@ namespace AxibugEmuOnline.Client
thirdMenuGroup.itemGroup.Clear();
RefreshUI();
if (SubMenuItemGroup != null) SubMenuItemGroup.SetSelect(true);
return true;

View File

@ -38,11 +38,11 @@ namespace AxibugEmuOnline.Client
}
private static Dictionary<int, RomFile> s_RomFileCahcesInRoomInfo = new Dictionary<int, RomFile>();
public static void FetchRomFileInRoomInfo(this Protobuf_Room_MiniInfo roomInfo, EnumPlatform platform, Action<RomFile> callback)
public static void FetchRomFileInRoomInfo(this Protobuf_Room_MiniInfo roomInfo, EnumPlatform platform, Action<Protobuf_Room_MiniInfo, RomFile> callback)
{
if (s_RomFileCahcesInRoomInfo.TryGetValue(roomInfo.GameRomID, out RomFile romFile))
{
callback.Invoke(romFile);
callback.Invoke(roomInfo,romFile);
return;
}
switch (platform)
@ -52,9 +52,9 @@ namespace AxibugEmuOnline.Client
{
RomFile romFile = new RomFile(EnumPlatform.NES, 0, 0);
romFile.SetWebData(romWebData);
callback.Invoke(romFile);
s_RomFileCahcesInRoomInfo[roomInfo.GameRomID] = romFile;
callback.Invoke(roomInfo,romFile);
}));
break;
}