SDM_LethalCompany_Mod/ScarletMansion/ScarletMansion/GamePatch/Managers/ScarletGenericManager.cs

161 lines
4.5 KiB
C#

using DunGen;
using DunGenPlus.Managers;
using ScarletMansion.GamePatch.Components;
using ScarletMansion.GamePatch.Items;
using ScarletMansion.Lights;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace ScarletMansion.GamePatch.Managers {
public class ScarletGenericManager : MonoBehaviour, IDungeonCompleteReceiver {
public static ScarletGenericManager Instance { get; private set; }
public List<Transform> roomsOfInterest;
public List<ScarletBedroom> bedrooms;
public List<ScarletPainting> paintings;
public List<ScarletDoor> doors;
public List<ScarletLight> lights;
public ScarletBookPath[] bookPaths;
public List<IScarletSelfDestroy> selfDestroyTargets;
private int lastHour;
void Awake(){
Instance = this;
roomsOfInterest = new List<Transform>();
bedrooms = new List<ScarletBedroom>();
paintings = new List<ScarletPainting>();
doors = new List<ScarletDoor>();
lights = new List<ScarletLight>();
}
void Update(){
if (!StartOfRound.Instance.IsServer) return;
if (selfDestroyTargets == null) return;
var time = Utility.GetTime();
var hours = time.hours;
var hourChanged = lastHour != hours;
if (!hourChanged) return;
Plugin.logger.LogDebug($"New hour: {hours}");
for (var i = 0; i < selfDestroyTargets.Count; i++){
var t = selfDestroyTargets[i];
var index = i;
if (!t.IsDestroyed && UnityEngine.Random.value < t.Chance) {
t.PreDestroy();
StartCoroutine(DelayDestroy(t, index, UnityEngine.Random.Range(0f, 60f)));
}
}
lastHour = hours;
}
private IEnumerator DelayDestroy(IScarletSelfDestroy target, int index, float delay){
Plugin.logger.LogDebug($"{target} destroying itself in {delay} sec");
yield return new WaitForSeconds(delay);
ScarletNetworkManager.Instance.CallSelfDestroyTargetClientRpc(index);
}
public void AddBedroom(ScarletBedroom b){
bedrooms.Add(b);
}
public void AddPainting(ScarletPainting painting){
paintings.Add(painting);
}
public void AddRoomOfInterest(Transform t) {
roomsOfInterest.Add(t);
}
public void AddDoor(ScarletDoor d){
// I want to get only doors that are revelant
foreach(var t in roomsOfInterest){
var dist = Vector3.SqrMagnitude(d.transform.position - t.position);
if (dist < 24f * 24f){
doors.Add(d);
return;
}
}
}
public ScarletBedroom GetBedroomWithPainting(Vector3 basePosition){
ScarletBedroom target = null;
var dist = 1f;
foreach(var b in bedrooms){
var newdist = Vector3.SqrMagnitude(basePosition - b.paintingSpawnTransform.position);
if (newdist < dist){
target = b;
dist = newdist;
}
}
if (target == null){
Plugin.logger.LogError($"There is no close bedroom painting spawn at {basePosition}");
return null;
}
//Plugin.logger.LogInfo($"Closest bedroom painting spawn {dist} away");
return target;
}
public ScarletDoor GetScarletDoor(Vector3 basePosition){
ScarletDoor target = null;
var dist = 1f;
foreach(var b in doors){
var newdist = Vector3.SqrMagnitude(basePosition - b.transform.position);
if (newdist < dist){
target = b;
dist = newdist;
}
}
if (target == null){
Plugin.logger.LogError($"There is no close door spawn at {basePosition}");
return null;
}
//Plugin.logger.LogInfo($"Closest door spawn {dist} away");
return target;
}
public void AddLight(ScarletLight light){
lights.Add(light);
}
public void OnDungeonComplete(Dungeon dungeon) {
selfDestroyTargets = dungeon.GetComponentsInChildren<IScarletSelfDestroy>().ToList();
bookPaths = dungeon.GetComponentsInChildren<ScarletBookPath>();
if (StartOfRound.Instance.IsServer){
DoorwayManager.onMainEntranceTeleportSpawnedEvent.AddTemporaryEvent("BookPaths", () => SetupBookPaths());
}
}
private void SetupBookPaths(){
StartCoroutine(SetupBookPathsDelay(5f));
}
private IEnumerator SetupBookPathsDelay(float delay){
yield return new WaitForSecondsRealtime(delay);
ScarletNetworkManager.Instance.SetupBookPathsClientRpc();
}
}
}