Frame/Assets/Scripts/Dinosaurs/Room/Room.cs

107 lines
3.0 KiB
C#
Raw Normal View History

2024-04-06 11:59:18 +08:00
using System.Collections.Generic;
2024-04-10 16:18:32 +08:00
using System.Linq;
2024-04-08 18:20:55 +08:00
using System.Threading;
using Cysharp.Threading.Tasks;
2024-04-06 11:59:18 +08:00
using Game.Player;
using UnityEngine;
namespace Game.Room
2024-04-06 11:59:18 +08:00
{
public interface IRoom
{
string roomName { get; }
RoomData roomData { get; }
RoomInfo roomInfo { get; }
IReadOnlyList<IPlayer> players { get; }
RoomType roomType { get; }
2024-05-05 17:02:33 +08:00
string roomId { get; }
2024-04-06 11:59:18 +08:00
UniTask<bool> JoinAsync(IPlayer player, CancellationToken token);
UniTask<bool> Quit(IPlayer player);
2024-04-10 16:18:32 +08:00
List<IPlayer> QuitAndClearAll();
2024-04-10 16:18:32 +08:00
void Dispose();
2024-04-06 11:59:18 +08:00
2024-05-05 17:02:33 +08:00
bool RefreshMoney(float jinBei);
}
2024-04-06 11:59:18 +08:00
[System.Serializable]
public class Room : IRoom
{
private GameObject self;
private RoomInfo _roomInfo;
private RoomData _roomData;
private string _roomName;
2024-04-06 11:59:18 +08:00
public RoomType roomType => this._roomData.roomType;
2024-05-05 17:02:33 +08:00
public string roomId => ((int)this._roomData.roomType).ToString();
2024-04-09 13:22:52 +08:00
public string roomName => this._roomName;
2024-04-09 13:22:52 +08:00
public RoomData roomData => this._roomData;
public RoomInfo roomInfo => this._roomInfo;
2024-04-06 11:59:18 +08:00
public IReadOnlyList<IPlayer> players => this._roomData.players;
2024-04-06 11:59:18 +08:00
2024-05-05 17:02:33 +08:00
public Room(RoomType roomType, GameObject roomGo, float jinBei)
{
2024-05-05 17:02:33 +08:00
this._roomData = new RoomData(roomType, jinBei);
_roomName = roomType.ToString();
// view
this._roomInfo = roomGo.GetComponent<RoomInfo>();
this._roomInfo.SetRoom(this);
this.self = roomGo;
this._roomData.jinBeiCallback += this._roomInfo.UpdateNumber;
}
2024-04-10 16:18:32 +08:00
public void Dispose()
{
this._roomData.jinBeiCallback -= this._roomInfo.UpdateNumber;
this._roomData.Reset();
2024-04-10 16:18:32 +08:00
GameObject.Destroy(this.self);
this.self = null;
this._roomInfo = null;
this._roomData = null;
this._roomName = null;
}
2024-04-06 11:59:18 +08:00
public async UniTask<bool> JoinAsync(IPlayer player, CancellationToken token)
{
if (this.players.Contains(player))
return false;
2024-04-11 14:44:39 +08:00
// Debug.Log($"{player.playerName} join {roomType} !!");
bool res = await player.WaitMoveRoomAsync(this, token);
this._roomData.AddPlayer(player);
return true;
}
2024-04-06 11:59:18 +08:00
public async UniTask<bool> Quit(IPlayer player)
{
if (!this.players.Contains(player))
return false;
// Debug.Log($"{player.playerName} quit {roomType} !!");
2024-04-10 16:18:32 +08:00
2024-05-05 17:02:33 +08:00
this._roomData.RemovePlayer(player);
2024-04-11 14:44:39 +08:00
await UniTask.Yield();
return true;
}
2024-04-06 11:59:18 +08:00
public List<IPlayer> QuitAndClearAll()
2024-04-10 16:18:32 +08:00
{
var list = new List<IPlayer>(_roomData.players);
this._roomData.Reset();
return list;
}
2024-04-06 11:59:18 +08:00
2024-05-05 17:02:33 +08:00
public bool RefreshMoney(float jinBei)
2024-04-08 18:20:55 +08:00
{
2024-05-05 17:02:33 +08:00
this._roomData.RefreshMoney(jinBei);
return true;
}
2024-04-06 11:59:18 +08:00
}
}