using System.Collections.Generic; using UnityEngine; public class BuildingInteractor : InteractorBase, DayNightCycle.IDaytimeSensitive { public enum InteractionState { None, Harvest, Upgrade } public static bool displayAllBuildPreviews; public BuildSlot targetBuilding; public Hp buildingHP; public CoinSpawner coinSpawner; public GameObject harvestCue; public GameObject harvestDeniedCue; public GameObject upgradeCue; public CostDisplay costDisplay; public Material previewMaterial; private List incomeModifiers = new List(); private InteractionState currentState; private bool knockedOutTonight; private bool harvestedToday; private bool focussed; private Mesh upgradePreviewMesh; private bool interactionComplete; private bool interactionStarted; private bool isWaitingForChoice; private PlayerInteraction bufferedPlayer; public bool buildingIsCurrentlyBusyAndCantBeUpgraded; public List IncomeModifiers => incomeModifiers; public bool KnockedOutTonight => knockedOutTonight; public bool canBeHarvested { get { if (targetBuilding.State == BuildSlot.BuildingState.Built && !harvestedToday && targetBuilding.GoldIncome > 0) { return !knockedOutTonight; } return false; } } public bool harvestIsDenied { get { if (targetBuilding.State == BuildSlot.BuildingState.Built && targetBuilding.GoldIncome > 0) { return knockedOutTonight; } return false; } } public int GoldIncome => targetBuilding.GoldIncome; public bool HarvestCueVisible => harvestCue.activeSelf; public bool UpgradeCueVisible => upgradeCue.activeSelf; public override bool CanBeInteractedWith => currentState != InteractionState.None; public bool IsWaitingForChoice => isWaitingForChoice; public Mesh UpgradePreviewMesh { get { if (targetBuilding.State == BuildSlot.BuildingState.Built) { if (!targetBuilding.CanBeUpgraded) { return null; } BuildSlot.Upgrade upgrade = targetBuilding.Upgrades[targetBuilding.Level]; int num = Mathf.RoundToInt(Time.time * 2f); return upgrade.upgradeBranches[num % upgrade.upgradeBranches.Count].replacementMesh; } return targetBuilding.MainMesh.mesh; } } public void MarkAsHarvested() { harvestedToday = true; } public override string ReturnTooltip() { return targetBuilding.ReturnTooltip(); } private void Start() { targetBuilding.buildingInteractor = this; if ((bool)buildingHP) { buildingHP.OnKillOrKnockout.AddListener(OnKnockOut); } targetBuilding.OnUpgrade.AddListener(OnTargetUpgrade); targetBuilding.OnUpgradeCancel.AddListener(OnTargetUpgradeCanceled); DayNightCycle.Instance.RegisterDaytimeSensitiveObject(this); UpdateInteractionState(); if ((bool)TagManager.instance) { TagManager.instance.playerBuildingInteractors.Add(this); } } public override void InteractionBegin(PlayerInteraction player) { if (!isWaitingForChoice) { interactionStarted = true; interactionComplete = false; bufferedPlayer = player; if (currentState == InteractionState.Upgrade) { ThronefallAudioManager.Oneshot(ThronefallAudioManager.AudioOneShot.CoinslotInteractionStart); } } } public override void InteractionHold(PlayerInteraction player) { if (currentState != InteractionState.Upgrade || interactionComplete || !interactionStarted || isWaitingForChoice) { return; } if (costDisplay.CompletelyFilled) { isWaitingForChoice = targetBuilding.NextUpgradeIsChoice; targetBuilding.TryToBuildOrUpgradeAndPay(player); if (!isWaitingForChoice) { BuildComplete(); } else { costDisplay.gameObject.SetActive(value: false); } } else if (player.Balance > 0) { if (costDisplay.FillUp()) { player.SpendCoins(1); } } else { costDisplay.Deny(); } } public override void InteractionEnd(PlayerInteraction player) { if (!isWaitingForChoice && interactionStarted) { interactionStarted = false; if (currentState == InteractionState.Upgrade) { costDisplay.CancelFill(player); ThronefallAudioManager.Instance.MakeSureCoinFillSoundIsNotPlayingAnymore(); } } } private void BuildComplete() { harvestedToday = true; interactionComplete = true; costDisplay.OnCompletion(); UpdateInteractionState(); } private void CancelBuild() { costDisplay.CancelFill(bufferedPlayer); } private void OnTargetUpgrade() { if (isWaitingForChoice) { isWaitingForChoice = false; costDisplay.gameObject.SetActive(value: true); interactionStarted = false; BuildComplete(); } } private void OnTargetUpgradeCanceled() { if (isWaitingForChoice) { isWaitingForChoice = false; costDisplay.gameObject.SetActive(value: true); interactionStarted = false; CancelBuild(); } } public void OnDusk() { knockedOutTonight = false; UpdateInteractionState(forceState: true); } public void OnDawn_BeforeSunrise() { harvestedToday = false; } public void OnDawn_AfterSunrise() { PlayerInteraction component = TagManager.instance.Players[0].GetComponent(); Harvest(component); foreach (IncomeModifyer incomeModifier in incomeModifiers) { incomeModifier.OnDawn(); } UpdateInteractionState(); } public void UpdateInteractionState(bool forceState = false, InteractionState forcedState = InteractionState.None) { if (isWaitingForChoice) { return; } base.gameObject.SetActive(value: true); if (canBeHarvested) { currentState = InteractionState.Harvest; } else if (targetBuilding.CanBeUpgraded) { currentState = InteractionState.Upgrade; } else { currentState = InteractionState.None; } if (forceState) { currentState = forcedState; } if (harvestIsDenied) { harvestDeniedCue.SetActive(value: true); } else { harvestDeniedCue.SetActive(value: false); } if (buildingIsCurrentlyBusyAndCantBeUpgraded) { currentState = InteractionState.None; } switch (currentState) { case InteractionState.None: harvestCue.SetActive(value: false); upgradeCue.SetActive(value: false); DisableCostDisplay(); break; case InteractionState.Harvest: harvestCue.SetActive(value: true); upgradeCue.SetActive(targetBuilding.CanBeUpgraded); DisableCostDisplay(); break; case InteractionState.Upgrade: harvestCue.SetActive(value: false); if (focussed) { ActivateCostDisplay(); upgradeCue.SetActive(value: false); break; } DisableCostDisplay(); if (targetBuilding.State == BuildSlot.BuildingState.Built) { upgradeCue.SetActive(value: true); } else { upgradeCue.SetActive(value: false); } break; } } public override void Focus(PlayerInteraction player) { focussed = true; UpdateInteractionState(); if (currentState == InteractionState.Harvest) { Harvest(player); } } public void Harvest(PlayerInteraction player) { if (canBeHarvested) { coinSpawner.TriggerCoinSpawn(targetBuilding.GoldIncome, player); harvestedToday = true; UpdateInteractionState(); } } public override void Unfocus(PlayerInteraction player) { focussed = false; if (!costDisplay.CompletelyEmpty) { costDisplay.CancelFill(player); } UpdateInteractionState(); ThronefallAudioManager.Instance.MakeSureCoinFillSoundIsNotPlayingAnymore(); } private void ActivateCostDisplay() { costDisplay.UpdateDisplay(targetBuilding.NextUpgradeOrBuildCost); } private void DisableCostDisplay() { costDisplay.Hide(); } public void OnKnockOut() { knockedOutTonight = true; } private void Update() { if ((focussed || displayAllBuildPreviews) && currentState == InteractionState.Upgrade) { if (!targetBuilding.ActivatorUpgradesThis) { PreviewSelf(); } else { targetBuilding.ActivatorBuilding.buildingInteractor.PreviewSelf(); } } } public void PreviewSelf() { MeshFilter mainMesh = targetBuilding.MainMesh; if ((bool)UpgradePreviewMesh) { Graphics.DrawMesh(UpgradePreviewMesh, targetBuilding.MainMesh.transform.localToWorldMatrix, previewMaterial, 0); } else if (!mainMesh.gameObject.activeInHierarchy) { Graphics.DrawMesh(mainMesh.mesh, mainMesh.transform.localToWorldMatrix, previewMaterial, 0); } int upgradeBranch = Mathf.RoundToInt(Time.time * 2f); List gameObjectsThatWillUnlockWhenUpgraded = targetBuilding.GetGameObjectsThatWillUnlockWhenUpgraded(upgradeBranch); for (int i = 0; i < gameObjectsThatWillUnlockWhenUpgraded.Count; i++) { if (gameObjectsThatWillUnlockWhenUpgraded[i].transform.parent == null) { PreviewGameObject(gameObjectsThatWillUnlockWhenUpgraded[i]); } else if (gameObjectsThatWillUnlockWhenUpgraded[i].transform.parent.gameObject.activeSelf || gameObjectsThatWillUnlockWhenUpgraded.Contains(gameObjectsThatWillUnlockWhenUpgraded[i].transform.parent.gameObject)) { PreviewGameObject(gameObjectsThatWillUnlockWhenUpgraded[i]); } } for (int j = 0; j < targetBuilding.BuiltSlotsThatRelyOnThisBuilding.Count; j++) { BuildSlot buildSlot = targetBuilding.BuiltSlotsThatRelyOnThisBuilding[j]; if (buildSlot.ActivatorUpgradesThis && buildSlot.ActivatorBuilding == targetBuilding && (bool)buildSlot.buildingInteractor) { buildSlot.buildingInteractor.PreviewSelf(); } } List blueprintPreviewsThatWillUnlockWhenUpgraded = targetBuilding.GetBlueprintPreviewsThatWillUnlockWhenUpgraded(); for (int k = 0; k < blueprintPreviewsThatWillUnlockWhenUpgraded.Count; k++) { mainMesh = blueprintPreviewsThatWillUnlockWhenUpgraded[k]; Graphics.DrawMesh(mainMesh.mesh, mainMesh.transform.localToWorldMatrix, previewMaterial, 0); } } private void PreviewGameObject(GameObject _go) { MeshFilter[] componentsInChildren = _go.GetComponentsInChildren(); foreach (MeshFilter meshFilter in componentsInChildren) { Graphics.DrawMesh(meshFilter.mesh, meshFilter.transform.localToWorldMatrix, previewMaterial, 0); } } private void OnDestroy() { if ((bool)TagManager.instance) { TagManager.instance.playerBuildingInteractors.Remove(this); } } }