mirror of
http://47.107.252.169:3000/Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite.git
synced 2025-11-19 23:37:13 +08:00
Co-authored-by: m0_75251201 <m0_75251201@noreply.gitcode.com> Reviewed-on: http://47.107.252.169:3000/Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite/pulls/60
203 lines
6.7 KiB
C#
203 lines
6.7 KiB
C#
using Configs;
|
||
using Data;
|
||
using Managers;
|
||
using Map;
|
||
using UI;
|
||
using UnityEngine;
|
||
|
||
namespace Base
|
||
{
|
||
public enum GameEnding
|
||
{
|
||
// 区域肃清
|
||
AreaSecured,
|
||
// 干员失联 (也可以理解为任务失败,干员失踪)
|
||
AgentMissing,
|
||
// 据点被毁
|
||
BaseDestroyed
|
||
}
|
||
|
||
|
||
public class PlayGameSceneControl : MonoBehaviour, ITick
|
||
{
|
||
public Dimension dimensionPrefab;
|
||
|
||
public WaitStartUI waitStartUI;
|
||
|
||
public bool AllDimensionsLoaded => OutsideDimensionsLoaded && InsideDimensionsLoaded;
|
||
public bool OutsideDimensionsLoaded { get; private set; }
|
||
public bool InsideDimensionsLoaded { get; private set; }
|
||
|
||
public DimensionDef OutsideDef { get; private set; }
|
||
public DimensionDef InsideDef { get; private set; }
|
||
|
||
public Dimension OutsideDimension { get; private set; }
|
||
public Dimension InsideDimension { get; private set; }
|
||
|
||
public bool TaskCompleted { get; private set; }
|
||
|
||
|
||
private int totalThreat;
|
||
private int monstersKilled;
|
||
|
||
public SettlementUI settlementUI;
|
||
|
||
private string playerAffiliation;
|
||
|
||
public Entity.Entity playerEntity;
|
||
|
||
public BaseBuildingHPBarUI baseBuildingHPBarUI;
|
||
|
||
private void Start()
|
||
{
|
||
playerAffiliation = Configs.ConfigManager.Instance.GetValue<string>("playerAffiliation");
|
||
OutsideDef = Program.Instance.OutsidePlayDimension;
|
||
InsideDef = Program.Instance.CurrentCharacter.insideDimensionDef ??
|
||
DefineManager.Instance.FindDefine<DimensionDef>(
|
||
ConfigManager.Instance.GetValue<string>("InsideDimension"));
|
||
if (OutsideDef == null || InsideDef == null)
|
||
{
|
||
MessageManager.Instance.DisplayMessage("初始化失败!检查XML文件完整性", PromptDisplayCategory.ScreenCenterLargeText,
|
||
Color.brown);
|
||
Invoke(nameof(ReturnMainMenu), 3);
|
||
return;
|
||
}
|
||
|
||
OutsideDimension = Instantiate(dimensionPrefab);
|
||
InsideDimension = Instantiate(dimensionPrefab);
|
||
|
||
OutsideDimension.transform.localPosition = new Vector3(-0.5f, -0.5f, 1);
|
||
InsideDimension.transform.localPosition = new Vector3(-0.5f, 999.5f, 1);
|
||
|
||
_ = OutsideDimension.ApplyDimensionDef(OutsideDef);
|
||
_ = InsideDimension.ApplyDimensionDef(InsideDef);
|
||
|
||
OutsideDimension.OnDimensionLoaded += OutsideDimensionLoaded;
|
||
InsideDimension.OnDimensionLoaded += InsideDimensionLoaded;
|
||
|
||
OutsideDimension.name = "OutsideDimension";
|
||
InsideDimension.name = "InsideDimension";
|
||
|
||
|
||
Program.Instance.SetFocusedDimension(OutsideDimension.DimensionId);
|
||
|
||
|
||
EntityManager.Instance.OnCreateEntity += OnCreateEntity;
|
||
|
||
|
||
|
||
Clock.AddTick(this);
|
||
}
|
||
|
||
private void OnDestroy()
|
||
{
|
||
EntityManager.Instance.OnCreateEntity -= OnCreateEntity;
|
||
|
||
Clock.RemoveTick(this);
|
||
}
|
||
|
||
public void Tick()
|
||
{
|
||
if (!AllDimensionsLoaded)
|
||
return;
|
||
if (EventManager.Instance.HasStoryDisplay || TaskCompleted ||
|
||
EntityManager.Instance.ExistsHostile(OutsideDimension.DimensionId,
|
||
Program.Instance.FocusedEntity.entityPrefab))
|
||
return;
|
||
EndGame(GameEnding.AreaSecured);
|
||
}
|
||
|
||
public void EndGame(GameEnding gameEnding)
|
||
{
|
||
KeyValueArchiveManager.Instance.Set("LastGameTotalThreat", totalThreat);
|
||
KeyValueArchiveManager.Instance.Set("LastGameMonstersKilled", monstersKilled);
|
||
KeyValueArchiveManager.Instance.Set("LastGameEnding", gameEnding);
|
||
TaskCompleted = true;
|
||
UIInputControl.Instance.Show("SettlementUI");
|
||
}
|
||
|
||
|
||
private void StartGame()
|
||
{
|
||
if (!AllDimensionsLoaded) return;
|
||
waitStartUI.Hide();
|
||
Program.Instance.PutPlayer();
|
||
playerEntity = Program.Instance.FocusedEntity;
|
||
playerEntity.OnEntityDiedEvent += OnPlayerDied;
|
||
if (OutsideDef.story != null)
|
||
EventManager.Instance.PlayStory(OutsideDef.story.defName, OutsideDimension.DimensionId);
|
||
|
||
if (InsideDef.story != null)
|
||
EventManager.Instance.PlayStory(InsideDef.story.defName, InsideDimension.DimensionId);
|
||
|
||
Program.Instance.OnFocusedDimensionChanged += OnSwitchDimension;
|
||
baseBuildingHPBarUI.Init();
|
||
}
|
||
|
||
private void ReturnBase()
|
||
{
|
||
Program.Instance.ReturnBase();
|
||
}
|
||
private void ReturnMainMenu()
|
||
{
|
||
EscUI.ReturnMainMenu();
|
||
}
|
||
|
||
private void OutsideDimensionLoaded(Dimension dimension)
|
||
{
|
||
OutsideDimensionsLoaded = true;
|
||
StartGame();
|
||
}
|
||
|
||
private void InsideDimensionLoaded(Dimension dimension)
|
||
{
|
||
InsideDimensionsLoaded = true;
|
||
StartGame();
|
||
}
|
||
|
||
private void OnCreateEntity(Entity.Entity entity)
|
||
{
|
||
if (entity is not Entity.Monster monster)
|
||
return;
|
||
if (AffiliationManager.Instance.GetRelation(playerAffiliation,
|
||
entity.affiliation) == Relation.Hostile)
|
||
{
|
||
monster.OnEntityDiedEvent += OnMonsterKilled;
|
||
}
|
||
else if (entity.affiliation==Configs.ConfigManager.Instance.GetValue<string>("NanorobotsAffiliation"))
|
||
{
|
||
|
||
}
|
||
}
|
||
|
||
private void OnPlayerDied(Entity.Entity entity)
|
||
{
|
||
EndGame(GameEnding.AgentMissing);
|
||
}
|
||
|
||
private void OnMonsterKilled(Entity.Entity entity)
|
||
{
|
||
monstersKilled++;
|
||
var monster=entity as Entity.Monster;
|
||
if (monster && monster.entityDef is MonsterDef monsterDef)
|
||
{
|
||
totalThreat += monsterDef.threat;
|
||
}
|
||
}
|
||
|
||
private void OnSwitchDimension(Dimension dimension)
|
||
{
|
||
if (dimension == OutsideDimension)
|
||
{
|
||
Program.Instance.SetFocusedEntity(playerEntity);
|
||
}
|
||
else if (dimension == InsideDimension)
|
||
{
|
||
var nanorobots = EntityManager.Instance.FindEntitiesByFaction(dimension.DimensionId,
|
||
Configs.ConfigManager.Instance.GetValue<string>("NanorobotsAffiliation"));
|
||
if (nanorobots is { Length: > 0 })
|
||
Program.Instance.SetFocusedEntity(nanorobots[0].entity);
|
||
}
|
||
}
|
||
}
|
||
} |