top of page

エイリアンエイジ TownController.cs

  • msandfrey3
  • Aug 12, 2024
  • 3 min read
using System.Collections;
using UnityEngine;
using IndieWizards.Audio;
using IndieWizards.FX;
using IndieWizards.GameManagement;
using IndieWizards.ItemManagement;
using IndieWizards.Terrain;
using IndieWizards.UI;
using IndieWizards.CameraControls;
{
    public class TownController : MonoBehaviour, IHighlight, ITile
    {
        public enum TownState
        {
            Inactive,
            Active,
            Complete,
            Dead
        }
        [Header("UI")]
        [SerializeField]
        private TownHUDController townHUDController;
        [SerializeField]
        public TownTabController townTabController;
        [SerializeField]
        [Tooltip("Number of seconds left on the town that triggers the warning in the UI")]
        private float warningStartTime = 30.0f;
        [Header("Age Control")]
        [SerializeField]
        private TownProgressionManager townProgressionManager;
        [SerializeField]
        private GameObject tileChild;
        [Header("Particle effects")]
        [SerializeField]
        private ParticleSystem groundPulseFXLoop;
        [SerializeField]
        private GameObject tileHighlight;
        [SerializeField]
        private ParticleSystem progressFX;
        [Header("Debug Purposes")]
        [SerializeField]
        private AgeSettings ageSettings;
        private Age age;
        private float townTimer = 10;
        private bool isFinalAgeComplete = false;
        public bool IsFinalAgeComplete { get { return isFinalAgeComplete; } set { isFinalAgeComplete = value; } }
        private TownResourceTracker townResourceTracker;
        private RocketLaunch rocketLaunch;
        private CinemaCamControls cam;
        public TileType TileType { get => TileType.Town; }
        public GameObject GameObject { get => gameObject; }
        private bool isFreePlay = false;
        private bool triggeredWarning = false;
        private bool isTownActive = false;
        public bool IsTownActive { get => isTownActive; set => isTownActive = value; }
        private TownState currentTownState = TownState.Inactive;
        public TownState CurrentTownState { get => currentTownState; set => currentTownState = value; }
        private GameSessionManager gameSessionManager;
        private void Awake()
        {
            rocketLaunch = GetComponent<RocketLaunch>();
            cam = FindObjectOfType<CinemaCamControls>();
            triggeredWarning = false;
            if (townTabController != null)
            {
                townTabController.EnableTownTab();
            }
        }
        private void Start()
        {
            gameSessionManager = GameSessionManager.Instance;
            isFreePlay = ApplicationConfig.Instance.CurrentLevel.IsFreePlay;
            if (townTabController != null)
            {
                townTabController.RegisterTownController(this);
            }
        }
        // Update is called once per frame
        private void Update()
        {
            if (gameSessionManager.IsGamePlayPaused)
            {
                return;
            }
            if (!isTownActive)
            {
                return;
            }
            if (!isFinalAgeComplete && !isFreePlay)
            {
                //tick timer
                if (townTimer > 0)
                {
                    townTimer -= Time.deltaTime;
                    townHUDController.SetTimerUI(townTimer, ageSettings.ageTimer);
                    townTabController.UpdateTimeRemaining(townTimer);
                    if (warningStartTime >= townTimer && !triggeredWarning)
                    {
                        triggeredWarning = true;
                        TownWarning();
                    }
                }
                else if (townTimer <= 0 && isTownActive)
                {
                    isTownActive = false;
                    KillTown();
                }
            }
        }
        private void TownWarning()
        {
            townTabController.StartWarning();
        }
        public ItemType GetRequiredItemType()
        {
            return townResourceTracker.RequiredResourceType;
        }
        public void HandleTownResourceInteraction(GameObject itemObject)//take in type as well
        {
            Item item = itemObject.GetComponent<Item>();
            try
            {
                townResourceTracker.AddResources(item);
                townTabController.UpdateResourceCount(townResourceTracker.CurrentResources);
                //so should only do below code if successfully added
                progressFX.Play();
                PrintResourceAgeUI();
                if (townResourceTracker.TownHasEnoughResources())
                {
                    MoveToNewAge();
                }
            }
            catch (System.InvalidOperationException e)
            {
                Debug.LogError("Unable to add item to town: " + e);
            }
            Destroy(itemObject);
        }
        // IMPORTANT: This is ONLY public b/c it's used in the DebugKeys.cs
        //possibly have in a different place
        public void MoveToNewAge()
        {
            //load the next age
            AgeSettings newAge = townProgressionManager.GetNextAge();
            //----------------------
            // if null we are on the final stage and there is no next stage
            //***do something special if we are on space age***
            // but for now just return and stop the timer
            //----------------------
            if (newAge == null)
            {
                FinalAgeCompleteSequence();
                return;
            }
            SetupAge(newAge);
            PrintResourceAgeUI();
        }
        public void PanCameraToTown(CamMovementCall movementCall)
        {
            cam.StartCameraPanning(transform.position, movementCall, CameraPanComplete);
        }
        private void CameraPanComplete(PanningEvent panningEvent)
        {
            panningEvent.FinishPanningEvent();
        }
        private void ChangeTownPrefab(GameObject newAgeTownPrefab)
        {
            //play animation
            //kill the old town
            if (tileChild)
            {
                Destroy(tileChild);
                RemoveHighlight();
            }
            //bring in the new town
            tileChild = Instantiate(newAgeTownPrefab, transform.position, Quaternion.identity, transform);
            tileChild.GetComponent<TownMouseOver>().SetHUDController(townHUDController);
        }
        public void RunDestroyTownSequence(PanningEvent panningEvent)
        {
            isTownActive = false;
            //time is up, shut it down
            AudioManager.Instance.PlayTownCollapseAudioEvent(this.gameObject);
            townProgressionManager.TownDies();
            townTabController.HandleTownUpgradeFailure();
            RemoveHighlight();
            //play animation for dying town
            if (tileChild)
            {
                Destroy(tileChild);
            }
            GameObject crater = townProgressionManager.GetCraterPrefab();
            tileChild = Instantiate(crater, transform.position, Quaternion.identity, transform);
            HideUI();
            this.enabled = false;
            StartCoroutine(panningEvent.FinishPanningEventAsync(1.0f));
        }
        /// <summary>
        /// Public for debugging only. Do not call from game code. 
        /// </summary>        
        public void KillTown()
        {
            isTownActive = false;
            currentTownState = TownState.Dead;
            cam.StartCameraPanning(transform.position, CamMovementCall.TownDies, RunDestroyTownSequence);
        }
        private void FinalAgeCompleteSequence()
        {
            //ensure that this is set to stop update logic
            isFinalAgeComplete = true;
            currentTownState = TownState.Complete;
            townTabController.HandleTownProgressComplete();
            townHUDController.HandleTownProgressComplete();
            //anything else that should be stopped, deleted, etc.
            // TODO: Turn this one when it's wired up correctly
            //Launch the Rocket
            //rocketLaunch.Launch();
        }
        public void SetupAge(AgeSettings ageSettings)//take in the age
        {
            isTownActive = true;
            currentTownState = TownState.Active;
            GameObject newTownPrefab = townProgressionManager.GetTownPrefab();
            ChangeTownPrefab(newTownPrefab);
            townResourceTracker = new TownResourceTracker(ageSettings);
            //take in an age and set all vals from it
            this.ageSettings = ageSettings;
            age = ageSettings.name;
            townTimer = ageSettings.ageTimer;
            PrintResourceAgeUI();
            townHUDController.SetResourceIcon(townResourceTracker.RequiredResourceType);
            townTabController.SetTownAge(ageSettings);
            triggeredWarning = false;
        }
        private void HideUI()
        {
            townHUDController.gameObject.SetActive(false);
        }
        private void PrintResourceAgeUI()
        {
            townHUDController.SetAgeUI(age);
            townHUDController.SetResourceUI(townResourceTracker);
        }
        public void OnCollisionWithItem(GameObject itemObject)
        {
            HandleTownResourceInteraction(itemObject);
        }
        public void ApplyHighlight()
        {
            if (groundPulseFXLoop != null)
            {
                groundPulseFXLoop.Play();
            }
            if (tileHighlight != null)
            {
                tileHighlight.SetActive(true);
            }
        }
        public void RemoveHighlight()
        {
            if (groundPulseFXLoop != null)
            {
                groundPulseFXLoop.Stop();
                groundPulseFXLoop.Clear();
            }
            if (tileHighlight != null)
            {
                tileHighlight.SetActive(false);
            }
        }
        public Vector3 GetCenter()
        {
            return transform.position;
        }
    }
}

Recent Posts

See All

Comments


© 2024 by Matthew Sandfrey. Proudly created with Wix.com

bottom of page