using DunGen; using ScarletMansion.GamePatch.Managers; using ScarletMansion.GamePatch.Props; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Unity.Netcode; using UnityEngine; namespace ScarletMansion.GamePatch.Components.TreasureRoom { public class TreasureRoomRadioEvent : TreasureRoom { [Header("References")] public Transform radioParentTransform; public ScarletRadio radio; private int selectedAudioIndex; private int selectedVolume; public override bool CanOpen() { var ready = GetDoorStatusValue() == 3; if (ready) { Plugin.logger.LogDebug($"Opening cause radio all setup"); return ready; } return false; } public override int GetDoorStatusValue() { if (radio == null) return 3; var count = 0; if (radio.playing) count++; if (radio.audioIndex == selectedAudioIndex) count++; if (radio.volume == selectedVolume) count++; return count; } private IEnumerable GetChildren(Transform parent){ foreach(Transform child in parent){ yield return child; } } protected override void OnTreasureRoomSetup(bool hasTreasureRoom) { var randomStream = DunGenPatch.Patch.GetRandomStream(); var randomProps = GetChildren(radioParentTransform).Where(t => t.gameObject.activeSelf).ToArray(); Utility.Shuffle(randomStream, randomProps); var spawnedRadioProp = false; foreach(var parentTransform in randomProps) { var prop = parentTransform.GetComponentInChildren(); if (hasTreasureRoom) { if (!spawnedRadioProp) { prop.ProcessIndex(randomStream, 0); spawnedRadioProp = true; } else { prop.ProcessSkipCount(randomStream, 1); } } else { prop.Process(randomStream, null); } // so the children get the process prop cycling as well DunGenPatch.Patch.generatorInstance.ProcessProps(null, prop.transform.GetChild(0).gameObject); } // hack so i can set up the SpawnSycnedObject myself if (hasTreasureRoom) { radioParentTransform.gameObject.SetActive(false); } } protected override void LockTreasureDoor(List doorways) { base.LockTreasureDoor(doorways); radioParentTransform.gameObject.SetActive(true); if (StartOfRound.Instance.IsHost) { Plugin.logger.LogDebug("HOST: Creating scarlet radio"); var mapPropsContainer = GetMapPropsContainer(); var ssoList = radioParentTransform.GetComponentsInChildren(); foreach(var sso in ssoList) { var ssoGameobject = GameObject.Instantiate(sso.spawnPrefab, sso.transform.position, sso.transform.rotation, mapPropsContainer.transform); var networkObject = ssoGameobject.GetComponent(); Plugin.logger.LogDebug($"Spawned {ssoGameobject.name}"); var r = ssoGameobject.GetComponentInChildren(); if (r != null) { radio = r; radio.treasureRoomEvent = this; } networkObject.Spawn(true); } if (radio == null) { Plugin.logger.LogError($"Somehow didn't spawn a scarlet radio. Not really expected but the world won't explode"); return; } selectedAudioIndex = UnityEngine.Random.Range(0, radio.audioClips.Length); selectedVolume = UnityEngine.Random.Range(1, 11); CreateDoorStatuses(doorways); UpdateTreasureDoorStatus(); Plugin.logger.LogDebug($"HOST: Radio combination is song index {selectedAudioIndex} and volume {selectedVolume}"); } } } }