//a method that spawns an effect object considering a couple of options public EffectObj SpawnEffectObj(EffectObj prefab, Vector3 spawnPosition, Quaternion spawnRotation = default, Transform parent = null, bool enableLifeTime = true, bool autoLifeTime = true, float lifeTime = 0.0f, bool UI = false) { if (prefab == null) { return(null); } //get the attack effect (either create it or get one tht is inactive): EffectObj newEffect = GetFreeEffectObj(prefab); //make the object child of the assigned parent transform newEffect.transform.SetParent(parent, true); if (UI) { newEffect.GetComponent <RectTransform>().localPosition = spawnPosition; newEffect.GetComponent <RectTransform>().localRotation = spawnRotation; } else { //set the effect's position and rotation newEffect.transform.position = spawnPosition; newEffect.transform.rotation = spawnRotation; } newEffect.ReloadTimer(enableLifeTime, autoLifeTime, lifeTime); //reload lifetime newEffect.Enable(); return(newEffect); }
public float cycleDuration = 0.2f; //every time this time in seconds passes, the effect object is hidden or activated. void Start() { effectObj = GetComponent <EffectObj>(); if (isActive == true) { InvokeRepeating("Flash", 0.0f, cycleDuration); } }
public void AddFreeEffectObj(EffectObj instance) { Queue <EffectObj> currentQueue = new Queue <EffectObj>(); if (effectObjs.TryGetValue(instance.GetCode(), out currentQueue) == false) //if the queue for this effect object type is not found { effectObjs.Add(instance.GetCode(), currentQueue); //add it } currentQueue.Enqueue(instance); //add the effect object to the right queue }
//a method that destroys a faction entity locally public virtual void DestroyFactionEntityLocal(bool upgrade) { gameMgr.SelectionMgr.Selected.Remove(factionEntity); //deselect the faction entity if it was selected //faction entity death: isDead = true; CurrHealth = 0; //remove the minimap icon: factionEntity.GetSelection().DisableMinimapIcon(); //If this is no upgrade if (upgrade == false) { factionEntity.Disable(true); CustomEvents.OnFactionEntityDead(factionEntity); //call the custom events if (destructionEffect != null) //do not show desctruction effect if it's not even assigned { //get the destruction effect from the pool EffectObj newDestructionEffect = gameMgr.EffectPool.SpawnEffectObj(destructionEffect, transform.position, Quaternion.identity); //destruction sound effect if (destructionAudio != null) { //Check if the destruction effect object has an audio source: if (newDestructionEffect.GetComponent <AudioSource>() != null) { AudioManager.Play(newDestructionEffect.GetComponent <AudioSource>(), destructionAudio, false); //play the destruction audio } else { Debug.LogError("A destruction audio clip has been assigned but the destruction effect object doesn't have an audio source!"); } } } } //Destroy the faction entity's object: if (destroyObject == true) //only if object destruction is allowed { Destroy(gameObject, !upgrade ? destroyObjectTime : 0.0f); IsDestroyed = true; } }
//this method searches for a hidden effect object with a certain code so that it can be used again. public EffectObj GetFreeEffectObj(EffectObj prefab) { Assert.IsTrue(prefab != null, "[Effect Object Pool] invalid effect object prefab."); Queue <EffectObj> currentQueue; if (effectObjs.TryGetValue(prefab.GetCode(), out currentQueue) == false) //if the queue for this effect object type is not found { currentQueue = new Queue <EffectObj>(); effectObjs.Add(prefab.GetCode(), currentQueue); //add it } if (currentQueue.Count == 0) //if the queue is empty then we need to create a new effect object of this types { //create new effect object, init it and add it to the queue EffectObj newEffect = Instantiate(prefab.gameObject, Vector3.zero, Quaternion.identity).GetComponent <EffectObj>(); newEffect.Init(gameMgr); currentQueue.Enqueue(newEffect); } return(currentQueue.Dequeue()); //return the first inactive effect object in this queue }
//Spawn attack effect object and damage effect objects here: public void SpawnEffectObj(EffectObj Prefab, GameObject Target, Vector3 SpawnPostion, float LifeTime, bool AutoLifeTime, bool ChildObj) { //First check if the effect object is valid if (Prefab != null) { //if the effect object has an auto life time then assign it: if (AutoLifeTime == true) { LifeTime = Prefab.LifeTime; } //get the effect type (unit or building): EffectObjPool.EffectObjTypes EffectType; if (Target.GetComponent <Unit>()) { EffectType = EffectObjPool.EffectObjTypes.UnitDamageEffect; } else { EffectType = EffectObjPool.EffectObjTypes.BuildingDamageEffect; } //get the attack effect (either create it or get one tht is inactive): GameObject Effect = EffectObjPool.Instance.GetEffectObj(EffectType, Prefab); //settings: Effect.SetActive(true); //set the position and rotation of the damage object: Effect.transform.position = SpawnPostion; Effect.transform.rotation = Prefab.gameObject.transform.rotation; //if we need the effect object to be a child obj of the affected object: if (ChildObj == true) { //set the attack target as the parent of the damage effect Effect.transform.SetParent(Target.transform, true); } //default life time: Effect.GetComponent <EffectObj>().Timer = LifeTime; } }
protected void ToggleSourceTargetEffect(bool enable) { if (enable) { if (sourceEffect != null) { currSourceEffect = gameMgr.EffectPool.SpawnEffectObj( sourceEffect, transform.position, sourceEffect.transform.rotation, transform, false); //spawn the source effect on the source unit and don't enable the life timer } if (targetEffect != null) { currTargetEffect = gameMgr.EffectPool.SpawnEffectObj( targetEffect, target.transform.position, targetEffect.transform.rotation, target.transform, false); //spawn the target effect on the target and don't enable the life timer } } else { if (currSourceEffect != null) //if the source unit effect was assigned and it's still valid { currSourceEffect.Disable(); //stop it currSourceEffect = null; } if (currTargetEffect != null) //if a target effect was assigned and it's still valid { currTargetEffect.Disable(); //stop it currTargetEffect = null; } } }
//This method searches for a hidden effect object with a certain code so that it can be used again. public GameObject GetEffectObj(EffectObjTypes Type, EffectObj Prefab) { //Determine which list of objects the script is going to search depending on the given type: List <GameObject> SearchList = new List <GameObject>(); switch (Type) { case EffectObjTypes.AttackObjEffect: SearchList = AttackObjEffects; break; case EffectObjTypes.BuildingDamageEffect: SearchList = BuildingDamageEffects; break; case EffectObjTypes.UnitDamageEffect: SearchList = UnitDamageEffects; break; case EffectObjTypes.AttackObj: SearchList = AttackObjs; break; default: Debug.LogError("Effect object type not found!"); break; } GameObject Result = null; //Loop through all the spawned objects in the target list: if (SearchList.Count > 0) { int i = 0; string Code = Prefab.Code; //save the prefab's code here while (Result == null && i < SearchList.Count) { if (SearchList[i] != null) { //If the current object's code mathes the one we're looking for: if (SearchList[i].gameObject.GetComponent <EffectObj>().Code == Code) { //We can re-use non active objects, so we'll check for that as well: if (SearchList[i].gameObject.activeInHierarchy == false) { //This matches all what we're looking for so make it the result; Result = SearchList[i]; } } i++; } else { SearchList.RemoveAt(i); //if there's nothing here, remove the empty list field } } } //if we still haven't found the free effect object we're looking for: //Create one: if (Result == null) { Result = Instantiate(Prefab.gameObject, Vector3.zero, Quaternion.identity); //add it to the list: SearchList.Add(Result); } //return the result: return(Result); }
private EffectObj effectObjComp; //the effect obj component attached to the same gameobject public void Init(GameManager gameMgr) { this.gameMgr = gameMgr; effectObjComp = GetComponent <EffectObj>(); speed *= this.gameMgr.GetSpeedModifier(); //apply the speed modifier }
//Handle area damage attacks here: public void LaunchAreaDamage(Vector3 AreaCenter, Attack.AttackRangesVars[] AttackRanges, int SourceFactionID, EffectObj AttackEffect, float AttackEffectTime, List <string> AttackCategories, Attack.DoTVars DoT) { if (AttackRanges.Length > 0) //if the area ranges have been defined { //try to get all objects in the chosen range (biggest range must always be the last one) Collider[] ObjsInRange = Physics.OverlapSphere(AreaCenter, AttackRanges[AttackRanges.Length - 1].Range); int i = 0; //go through all of them while (i < ObjsInRange.Length) { //if we find a selection obj if (ObjsInRange[i].gameObject.GetComponent <SelectionObj>()) { //it can be one for a unit or a building Unit Unit = ObjsInRange[i].gameObject.GetComponent <SelectionObj>().MainObj.GetComponent <Unit>(); Building Building = ObjsInRange[i].gameObject.GetComponent <SelectionObj>().MainObj.GetComponent <Building>(); //when AttackCategories is empty, it means attak can effect all types //if it's a unit if (Unit) { //and it's in the same faction as the source if (Unit.FactionID == SourceFactionID || (AttackCategories.Count > 0 && !AttackCategories.Contains(Unit.Category))) { Unit = null; //cancel } } //if it's a building if (Building) { //and it's in the same faction as the source if (Building.FactionID == SourceFactionID || (AttackCategories.Count > 0 && !AttackCategories.Contains(Building.Category))) { Building = null; //cancel } } //if we have a unit or a building if (Unit != null || Building != null) { //check the distance of the object to the area center float Distance = Vector3.Distance(ObjsInRange[i].transform.position, AreaCenter); int j = 0; bool Found = false; //go through all ranges while (j < AttackRanges.Length && Found == false) { //if we find a suitable range if (Distance < AttackRanges[j].Range) { //apply damage: Found = true; if (Unit) { if (DoT.Enabled) { //DoT settings: Unit.DoT = DoT; Unit.DoT.Damage = AttackRanges[j].UnitDamage; //also there are still no effect objects for DoT, make their own? } else { Unit.AddHealth(AttackRanges[j].UnitDamage, this.gameObject); //Spawning the damage effect object: SpawnEffectObj(Unit.DamageEffect, ObjsInRange[i].gameObject, 0.0f, true); //currently there's attack effect objects for units only: SpawnEffectObj(AttackEffect, ObjsInRange[i].gameObject, AttackEffectTime, false); } } else if (Building) { //if DoT is enabled, stop here because it's not yet setup for buildings: if (DoT.Enabled) { return; } Building.AddHealth(AttackRanges[j].BuildingDamage, this.gameObject); //Spawning the damage effect object: SpawnEffectObj(Building.DamageEffect, ObjsInRange[i].gameObject, 0.0f, true); } } j++; } } } i++; } } }
private EffectObj terrainAttackTargetEffect = null; //when assigned, visible when the player commands units to do a terrain attack public void SpawnTargetEffect(bool movement, Vector3 position) { EffectObj nextEffect = movement ? movementTargetEffect : terrainAttackTargetEffect; gameMgr.EffectPool.SpawnEffectObj(nextEffect, position); }
//a method that upgrades a faction entity instance locally public void UpgradeInstance(FactionEntity instance, FactionEntity target, int factionID, EffectObj upgradeEffect) { switch (instance.Type) { case EntityTypes.building: //create upgraded instance of the building gameMgr.BuildingMgr.CreatePlacedInstance( (Building)target, instance.transform.position, target.transform.rotation.eulerAngles.y, ((Building)instance).CurrentCenter, factionID, true, ((Building)instance).FactionCapital); CustomEvents.OnBuildingInstanceUpgraded((Building)instance); //trigger custom event break; case EntityTypes.unit: //create upgraded instance of the unit gameMgr.UnitMgr.CreateUnit( (Unit)target, instance.transform.position, instance.transform.rotation, instance.transform.position, factionID, null); CustomEvents.OnUnitInstanceUpgraded((Unit)instance); //trigger custom event break; } //if there's a valid upgrade effect assigned: if (upgradeEffect != null) { //show the upgrade effect for the player: gameMgr.EffectPool.SpawnEffectObj(upgradeEffect, instance.transform.position, upgradeEffect.transform.rotation); } instance.EntityHealthComp.DestroyFactionEntity(true); //destroy the instance }
//a method that upgrades a faction entity instance locally public void UpgradeInstance(FactionEntity instance, FactionEntity target, int factionID, EffectObj upgradeEffect) { switch (instance.Type) { case EntityTypes.building: Building currBuilding = instance as Building; Unit[] currBuilders = currBuilding.WorkerMgr.GetAll(); //get the current builders of this building if there are any foreach (Unit unit in currBuilders) //and make them stop building the instance of the building since it will be destroyed. { unit.BuilderComp.Stop(); } //create upgraded instance of the building Building upgradedBuilding = gameMgr.BuildingMgr.CreatePlacedInstanceLocal( (Building)target, instance.transform.position, target.transform.rotation.eulerAngles.y, ((Building)instance).CurrentCenter, factionID, currBuilding.IsBuilt, //depends on the current state of the instance to destroy ((Building)instance).FactionCapital); foreach (Unit unit in currBuilders) { unit.BuilderComp.SetTarget(upgradedBuilding); } CustomEvents.OnBuildingInstanceUpgraded((Building)instance); //trigger custom event break; case EntityTypes.unit: //create upgraded instance of the unit gameMgr.UnitMgr.CreateUnit( (Unit)target, instance.transform.position, instance.transform.rotation, instance.transform.position, factionID, null); CustomEvents.OnUnitInstanceUpgraded((Unit)instance); //trigger custom event break; } //if there's a valid upgrade effect assigned: if (upgradeEffect != null) { //show the upgrade effect for the player: gameMgr.EffectPool.SpawnEffectObj(upgradeEffect, instance.transform.position, upgradeEffect.transform.rotation); } instance.EntityHealthComp.DestroyFactionEntity(true); //destroy the instance }