Frame/Assets/Scripts/Dinosaurs/DinosaursGameManager.cs

289 lines
10 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using Game.Boss;
using Game.Pathfinding;
using Game.Player;
using Game.RayCast;
using Game.Room;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Game.Dinosaurs
{
public class DinosaursGameManager : IDinosaursGameManager
{
// private bool isInit;
private bool isPlaying;
private bool isInBalance;
private float currentTime = 0f;
private float bossRefreshTime = 20f;
private GameSceneMainUI sceneMainUI;
private BFSManager _bfsManager;
private MouseInputManager _mouseInputManager;
private BossManager _bossManager;
private PlayerManager _playerManager;
private RoomManager _roomManager;
private RobotManager _robotManager;
public IBFSManager bfsManager => this._bfsManager;
public IMouseInputManager mouseInputManager => this._mouseInputManager;
public IBossManager bossManager => this._bossManager;
public IPlayerManager playerManager => this._playerManager;
public IRoomManager roomManager => this._roomManager;
public IRobotManager robotManager => this._robotManager;
public IPlayer currentPlayer => this._playerManager.currentPlayer;
public IRoom room => this._roomManager.currentRoom;
public DinosaursGameManager()
{
this._bfsManager = new BFSManager();
this._mouseInputManager = new MouseInputManager();
this._bossManager = new BossManager(this.bfsManager);
this._playerManager = new PlayerManager(this.bfsManager);
this._roomManager = new RoomManager();
this._robotManager = new RobotManager(this.bfsManager, this.roomManager);
}
public void Init()
{
this._bfsManager.Init();
this._mouseInputManager.Init();
this._bossManager.Init();
this._playerManager.Init();
this._roomManager.Init();
this._robotManager.Init();
}
public void StartGame()
{
this.isInBalance = false;
this.isPlaying = true;
this.currentTime = 0;
//
this.LoadGameSceneAsync().Forget();
EventManager.Instance.Subscribe(InputObjectFinishEventArgs.EventId, InputObjectFinishEvent);
sceneMainUI = Game.uiManager.GetUI<GameSceneMainUI>(UIType.GameSceneMainUI);
}
#region StartGame
async UniTask LoadGameSceneAsync()
{
await Game.resourceManager.LoadSceneAsync(AssetConstPath.Assets_GameRes_Scene_Game, LoadSceneMode.Additive);
EventManager.Instance.FireNow(this, new LoadingGameSceneFinishEventArgs(true));
var player = this._playerManager.CreatePlayer(Game.currentPlayerData.name, Game.currentPlayerData.jinBei);
this._playerManager.SetCurrentPlayer(player);
EventManager.Instance.FireNow(this, new InitCurrentPlayerDataEventArgs(player));
player.self.transform.position = new Vector3(-9.6f, -10.3f);
Game.uiManager.ShowUI(UIType.GameSceneMainUI);
await this.CreateRoomAsync();
}
async UniTask CreateRoomAsync()
{
var room = this._roomManager.CreateRoom(RoomType., AssetConstPath.Assets_GameRes_Prefabs_Room_);
this._roomManager.CreateRoom(RoomType., AssetConstPath.Assets_GameRes_Prefabs_Room_);
this._roomManager.CreateRoom(RoomType., AssetConstPath.Assets_GameRes_Prefabs_Room_);
this._roomManager.CreateRoom(RoomType., AssetConstPath.Assets_GameRes_Prefabs_Room_);
this._roomManager.CreateRoom(RoomType., AssetConstPath.Assets_GameRes_Prefabs_Room_);
this._roomManager.CreateRoom(RoomType., AssetConstPath.Assets_GameRes_Prefabs_Room_);
this._roomManager.CreateRoom(RoomType., AssetConstPath.Assets_GameRes_Prefabs_Room_);
this._roomManager.CreateRoom(RoomType., AssetConstPath.Assets_GameRes_Prefabs_Room_);
this._roomManager.CreateRoom(RoomType.殿, AssetConstPath.Assets_GameRes_Prefabs_Room_殿);
var player = this._playerManager.currentPlayer;
await player.WaitMoveRoomAsync(room, default);
this.CreateOtherPlayer();
// Game.procedureManager.ChangeProcedure(ProcedureType.GameSceneLogicProcedure);
}
void CreateOtherPlayer()
{
}
#endregion
public void UpdateGame(float time)
{
this._bfsManager.Update(time);
this._mouseInputManager.Update(time);
this._bossManager.Update(time);
this._playerManager.Update(time);
this._roomManager.Update(time);
this._robotManager.Update(time);
if (this.isInBalance || !isPlaying) return;
WaitTimeGoNext();
}
void WaitTimeGoNext()
{
this.currentTime += Time.deltaTime;
string content = $"({(int)(this.bossRefreshTime - currentTime)}) 恐龙即将出现!";
sceneMainUI.UpdateMessage(content);
if (this.currentTime >= this.bossRefreshTime)
{
this.WaitBalance().Forget();
return;
// Game.procedureManager.ChangeProcedure(ProcedureType.GameSceneKillPlayerProcedure);
}
}
async UniTask WaitBalance()
{
this.isInBalance = true;
string content = $"恐龙出没!";
sceneMainUI.UpdateMessage(content);
var roomData = this._roomManager.GetAllRandomRoom();
await this._bossManager.MoveToKillPlayerAsync(roomData.killRoom, default);
this._bossManager.DeleteBoss();
float jinBei = 0;
foreach (var room in roomData.killRoom)
{
jinBei += room.roomData.jinBeiCount;
}
Debug.Log($"杀掉了随机房间玩家,金贝数量总和为:{jinBei}");
List<IPlayer> players = new List<IPlayer>();
foreach (var room in roomData.survivorRoom)
{
foreach (var roomPlayer in room.players)
{
Debug.Log($"幸存者:{roomPlayer.playerName}");
players.Add(roomPlayer);
}
}
if (players.Count > 0)
jinBei /= players.Count;
Debug.Log($"胜利总金贝: is {jinBei}");
EventManager.Instance.FireNow(this, new JinBeiSettlementEventArgs(players, jinBei));
var resultUI = Game.uiManager.GetUI<GameSceneResultUI>(UIType.GameSceneResultUI);
if (players.Contains(this._playerManager.currentPlayer))
resultUI.SetResult($"恭喜你躲避成功,胜利总金贝:{jinBei},你得到的金贝:{jinBei}", true);
else
resultUI.SetResult($"躲避失败,胜利总金贝:{jinBei}你得到的金贝0", false);
//TODO:
UniTask.Create(async () => { await this._roomManager.QuitAllRoomAsync(default); });
this.currentTime = 0;
this.isInBalance = false;
await this.WaitShowResultAsync();
}
async UniTask WaitShowResultAsync()
{
var showUI = Game.uiManager.ShowUI(UIType.GameSceneResultUI);
var resultUI = showUI as GameSceneResultUI;
await resultUI.WaitShowAndCloseResultAsync(default);
Game.uiManager.CloseLast();
//
this._robotManager.EndOfTurnAuto();
}
public void PauseGame()
{
this.isPlaying = false;
this._bfsManager.Pause();
this._mouseInputManager.Pause();
this._bossManager.Pause();
this._playerManager.Pause();
this._roomManager.Pause();
this._robotManager.Pause();
}
public void ResumeGame()
{
this.isPlaying = true;
this._bfsManager.Resume();
this._mouseInputManager.Resume();
this._bossManager.Resume();
this._playerManager.Resume();
this._roomManager.Resume();
this._robotManager.Resume();
}
public void StopGame()
{
this._mouseInputManager.Dispose();
this._bossManager.Dispose();
this._playerManager.Dispose();
this._roomManager.Dispose();
this._robotManager.Dispose();
}
public void Dispose()
{
EventManager.Instance.Unsubscribe(InputObjectFinishEventArgs.EventId, InputObjectFinishEvent);
this._bfsManager.Dispose();
this._mouseInputManager.Dispose();
this._bossManager.Dispose();
this._playerManager.Dispose();
this._roomManager.Dispose();
this._robotManager.Dispose();
this._bfsManager = null;
this._mouseInputManager = null;
this._bossManager = null;
this._playerManager = null;
this._roomManager = null;
this._robotManager = null;
}
private void InputObjectFinishEvent(object sender, GameEventArgs e)
{
if (this._playerManager.currentPlayer.isMoving)
{
Debug.Log("Player is moveing");
return;
}
if (this.isInBalance)
{
Debug.Log("in balanceing !");
return;
}
if (!this.isPlaying)
{
Debug.Log("the game is paused !");
return;
}
var args = e as InputObjectFinishEventArgs;
var inputData = args.data as MouseInputData;
var roomInfo = inputData.go.GetComponent<RoomInfo>();
this._roomManager.SetCurrentRoom(roomInfo.roomType);
UniTask.Create(async () => { await this._roomManager.JoinRoomAsync(roomInfo.roomType, this._playerManager.currentPlayer, default); });
}
}
}