public DirectSpawnModifier(Actor self, DirectSpawnModifierInfo info) : base(info) { playerCiv = self.Owner.PlayerActor.Trait <PlayerCivilization>(); }
public IsTownhall(Actor self, IsTownhallInfo info) : base(info) { playerCiv = self.Owner.PlayerActor.Trait <PlayerCivilization>(); }
/// <summary> /// Memoize, if required, the information for the specified object and civilization which is initialized on the start script. /// </summary> /// <param name="type">The type of the object.</param> /// <param name="civilization">The civilization of the object.</param> private static ObjectInfo GetObjectInfo(RTSObjectType type, PlayerCivilization civilization) { // We have a pretty big problem here, which is that the costs of the object // are actually defined in the object initialization scripts and not in the prefab // For this reason, to get the cost, we need to actually initialize the object, // get the cost, and destroy it again. Since this is pretty costly, once we get it, // memoize the value to avoid recaculating it in future calls if (!memoizedObjectInfo.ContainsKey(new KeyValuePair<RTSObjectType, PlayerCivilization>(type, civilization))) { // Get object template. If not existing return empty ObjectInfo. var objectTemplate = GetObjectTemplate(type, civilization); if (objectTemplate == null) { memoizedObjectInfo[new KeyValuePair<RTSObjectType, PlayerCivilization>(type, civilization)] = new ObjectInfo(); return memoizedObjectInfo[new KeyValuePair<RTSObjectType, PlayerCivilization>(type, civilization)]; } // Initialize object instance and script. Otherwise return empty ObjectInfo. var objectInstance = GameObject.Instantiate(objectTemplate); try { var objectScript = objectInstance.GetComponent<RTSObject>(); if (objectScript == null) { memoizedObjectInfo[new KeyValuePair<RTSObjectType, PlayerCivilization>(type, civilization)] = new ObjectInfo(); return memoizedObjectInfo[new KeyValuePair<RTSObjectType, PlayerCivilization>(type, civilization)]; } // Get properties from initialized script memoizedObjectInfo[new KeyValuePair<RTSObjectType, PlayerCivilization>(type, civilization)] = new ObjectInfo { Cost = objectScript.cost, ObjectIconSprite = objectScript.objectIconSprite }; } finally { // Make sure we destroy the temporary GameObject we created GameObject.Destroy(objectInstance); } } return memoizedObjectInfo[new KeyValuePair<RTSObjectType, PlayerCivilization>(type, civilization)]; }
void Awake() { // Load player civilization from menu parameters // We have to do this in Awake() and not in Start() because some scripts // may depend on the player civilization on their initialization routines var menuGameParametersObject = GameObject.Find("MenuGameParameters"); var menuGameParameters = (menuGameParametersObject != null) ? menuGameParametersObject.GetComponent<MenuGameParameters>() : null; if (menuGameParameters != null) { civilization = human ? menuGameParameters.SelectedHumanCivilization : menuGameParameters.SelectedEnemyCivilization; Debug.LogFormat("The user has choosen the {0} civilization in the menu for the {1} player.", civilization, human ? "human" : "enemy"); } else { Debug.Log("Can't find the menu game parameters object (either it's not correctly " + "configured, or you launched the game directly from the campaign scene). Setting defaults."); } }
/// <summary> /// Get the default object resource prefab for the specified civilization. /// </summary> /// <param name="type">The type of object to load.</param> /// <param name="civilization">The civilization of the player.</param> /// <param name="useFallback">If resource is not available for given civilization, return an equivalent for another civilization.</param> /// <returns>A reference to the object resource prefab, or null if not available.</returns> public static GameObject GetObjectTemplate(RTSObjectType type, PlayerCivilization civilization, bool useFallback = false) { // Validate input arguments correctness if (!Enum.IsDefined(typeof(RTSObjectType), type)) throw new ArgumentOutOfRangeException("type"); if (!Enum.IsDefined(typeof(PlayerCivilization), civilization)) throw new ArgumentOutOfRangeException("civilization"); // Load list of prefabs for the given object if (!resourceNames.ContainsKey(type)) throw new NotSupportedException("The object type '" + type + "' doesn't have its prefabs defined. " + "Define them in " + typeof(RTSObjectFactory) + "."); var prefabsForObject = resourceNames[type]; // Try to load the resource for the exact civilization passed as an argument if (prefabsForObject.ContainsKey(civilization)) { var loadedResource = LoadAndCheckPrefab(prefabsForObject[civilization], true); if (loadedResource != null) return loadedResource; } if (useFallback) { // If the resource couldn't be loaded and useFallback was given, try to load the object // Iterate over the civilizations in order in order to get a consistent behaviour // (dictionary enumeration order is undefined) foreach (var fallbackCivilization in prefabsForObject.Keys.Where(k => k != civilization).OrderBy(k => k)) { var loadedResource = LoadAndCheckPrefab(prefabsForObject[fallbackCivilization], false); if (loadedResource != null) { Debug.LogWarning("Using model for civilization " + fallbackCivilization + " for object of type " + type + " because no model is available for civilization " + civilization + "."); HUDInfo.insertMessage("Object " + type + " not available for civilization " + civilization + "; Using fallback."); return loadedResource; } } throw new NotSupportedException("No prefab available for object type '" + type + " for any civilization. " + "Check the prefab definition in " + typeof(RTSObjectFactory) + "."); } return null; }
/// <summary> /// Get the preview icon sprite the specified object for the specified civilization. /// </summary> /// <param name="type">The type of the object.</param> /// <param name="civilization">The civilization of the object.</param> /// <returns>The preview icon sprite for the required object.</returns> public static Sprite GetObjectIconSprite(RTSObjectType type, PlayerCivilization civilization) { return GetObjectInfo(type, civilization).ObjectIconSprite; }
/// <summary> /// Get the cost of creating the specified object for the specified civilization. /// </summary> /// <param name="type">The type of the object to create.</param> /// <param name="civilization">The civilization of the object to create.</param> /// <returns>The cost in resources required to create the object</returns> public static int GetObjectCost(RTSObjectType type, PlayerCivilization civilization) { return GetObjectInfo(type, civilization).Cost; }
//private GameObject menu; void Start() { artificialIntelligence = GameObject.Find("EnemyPlayer1").GetComponent<Player>(); civilitzation = GameObject.Find("EnemyPlayer1").GetComponent<Player>().civilization; soldiers = new List<GameObject>(); archers = new List<GameObject>(); cavalry = new List<GameObject>(); townCenters = new List<GameObject>(); civils = new List<GameObject>(); Vector3 coords = new Vector3(453.51f, 0f, 435.28f); BuildTownCenter(coords,true); coords = new Vector3(453.51f-10f, 0f, 435.28f-10f); //GameObject civil = Instantiate(RTSObjectFactory.GetObjectTemplate(RTSObjectType.UnitCivil, civilitzation), coords, Quaternion.identity) as GameObject; //civil.GetComponent<CivilUnit>().owner = artificialIntelligence; //civils.Add(civil); CreateNewCivil(true); spawnPos = townCenters[0].transform.position; }
// Use this for initialization //GameObject prefab; void Start() { GameObject menu; artificialIntelligence = GameObject.Find("EnemyPlayer1").GetComponent<Player>(); civilitzation = GameObject.Find("EnemyPlayer1").GetComponent<Player>().civilization; }