top of page

エイリアンエイジ TownProgressionManager.cs

  • msandfrey3
  • Aug 12, 2024
  • 1 min read
using System;
using System.Collections.Generic;
using UnityEngine;
using IndieWizards.WorldManagement;
{
    public class TownProgressionManager : MonoBehaviour
    {
        [Serializable]
        private struct AgeToTownPrefabs
        {
            public Age age;
            public GameObject townTilePrefab;
            public GameObject craterPrefab;
        }
        [Header("Prefab Setup")]
        [SerializeField]
        [Tooltip("Each of the town tile prefabs.")]
        private List<AgeToTownPrefabs> ageToTownPrefabs = new List<AgeToTownPrefabs>();
        private Dictionary<Age, GameObject> townTilePrefabMap = new Dictionary<Age, GameObject>();
        //option 1 is just create a secondary one for craters. option 2 is expand the first one to be <Age, Struct>
        private Dictionary<Age, GameObject> craterTilePrefabMap = new Dictionary<Age, GameObject>();
        private int currentAgeIndex = 0;
        private AgeSettings currentAgeSettings;
        public AgeSettings CurrentAgeSettings { get { return currentAgeSettings; } }
        private TownController townController;
        private WorldProgressionManager worldProgressionManager;
        private void Awake()
        {
            townController = this.GetComponent<TownController>();
            worldProgressionManager = FindObjectOfType<WorldProgressionManager>();
            //dictionaries arent serialized so we gotta do by hand
            foreach (AgeToTownPrefabs typePrefabPair in ageToTownPrefabs)
            {
                townTilePrefabMap[typePrefabPair.age] = typePrefabPair.townTilePrefab;
                craterTilePrefabMap[typePrefabPair.age] = typePrefabPair.craterPrefab;
            }
        }
        private void Start()
        {
            //this will set up the town to be at whatever age the world is at for new towns
            currentAgeIndex = worldProgressionManager.CurrentWorldAge;
            currentAgeSettings = worldProgressionManager.AgeSettingsList[currentAgeIndex];
            //worldProgressionManager.AddLivingTown(townController);
            townController.SetupAge(currentAgeSettings);
        }
        public AgeSettings GetNextAge()
        {
            currentAgeIndex++;
            //when there is no next age the age would not be valid
            if(!worldProgressionManager.IsAgeValid(currentAgeIndex))
            {
                //if the town just finished the final age check if win
                townController.IsFinalAgeComplete = true;
                townController.CurrentTownState = TownController.TownState.Complete;
                worldProgressionManager.CheckGameState();
                return null; 
            }
            currentAgeSettings = worldProgressionManager.OnReachNextAge(currentAgeIndex);
            return currentAgeSettings;
        }
        public GameObject GetTownPrefab()
        {
            return townTilePrefabMap[currentAgeSettings.name];
        }
        public GameObject GetCraterPrefab()
        {
            return craterTilePrefabMap[currentAgeSettings.name];
        }
        public void TownDies()
        {
            worldProgressionManager.RemoveLivingTown(townController);
        }
    }
}

Recent Posts

See All

Comments


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

bottom of page