public IEnumerator ScanForTargetRoutine() { yield return(null); if (subClass == _UnitSubClass.Tower) { if (unitT.targetMode == _TargetMode.Hybrid) { LayerMask mask1 = 1 << LayerManager.LayerCreep(); LayerMask mask2 = 1 << LayerManager.LayerCreepF(); maskTarget = mask1 | mask2; } else if (unitT.targetMode == _TargetMode.Air) { maskTarget = 1 << LayerManager.LayerCreepF(); } else if (unitT.targetMode == _TargetMode.Ground) { maskTarget = 1 << LayerManager.LayerCreep(); } } while (true) { ScanForTarget(); yield return(new WaitForSeconds(0.1f)); } }
public override void Awake() { SetSubClass(this); base.Awake(); if (unitT.targetMode == _TargetMode.Air) { maskTarget = 1 << LayerManager.LayerCreepF(); } else if (unitT.targetMode == _TargetMode.Ground) { maskTarget = 1 << LayerManager.LayerCreep(); } else { LayerMask mask1 = 1 << LayerManager.LayerCreep(); LayerMask mask2 = 1 << LayerManager.LayerCreepF(); maskTarget = mask1 | mask2; } if (stats.Count == 0) { stats.Add(new UnitStat()); } }
IEnumerator AOETowerRoutine() { if (targetMode == _TargetMode.Hybrid) { LayerMask mask1 = 1 << LayerManager.LayerCreep(); LayerMask mask2 = 1 << LayerManager.LayerCreepF(); maskTarget = mask1 | mask2; } else if (targetMode == _TargetMode.Air) { maskTarget = 1 << LayerManager.LayerCreepF(); } else if (targetMode == _TargetMode.Ground) { maskTarget = 1 << LayerManager.LayerCreep(); } while (true) { yield return(new WaitForSeconds(GetCooldown())); while (stunned || IsInConstruction()) { yield return(null); } Transform soPrefab = GetShootObjectT(); if (soPrefab != null) { Instantiate(soPrefab, thisT.position, thisT.rotation); } Collider[] cols = Physics.OverlapSphere(thisT.position, GetRange(), maskTarget); if (cols.Length > 0) { for (int i = 0; i < cols.Length; i++) { Unit unit = cols[i].transform.GetComponent <Unit>(); if (unit == null && !unit.dead) { continue; } AttackInstance attInstance = new AttackInstance(); attInstance.srcUnit = this; attInstance.tgtUnit = unit; attInstance.Process(); unit.ApplyEffect(attInstance); } } } }
//Call by inherited class UnitCreep, caching inherited UnitCreep instance to this instance public void SetSubClass(UnitCreep unit) { unitC = unit; subClass = _UnitSubClass.Creep; if (!unitC.flying) { gameObject.layer = LayerManager.LayerCreep(); } else { gameObject.layer = LayerManager.LayerCreepF(); } }
public IEnumerator ScanForTargetRoutine() { if (subClass == _UnitSubClass.Tower) { if (unitT.targetMode == _TargetMode.Hybrid) { LayerMask mask1 = 1 << LayerManager.LayerCreep(); LayerMask mask2 = 1 << LayerManager.LayerCreepF(); maskTarget = mask1 | mask2; } else if (unitT.targetMode == _TargetMode.Air) { maskTarget = 1 << LayerManager.LayerCreepF(); } else if (unitT.targetMode == _TargetMode.Ground) { maskTarget = 1 << LayerManager.LayerCreep(); } } else if (subClass == _UnitSubClass.Creep) { maskTarget = 1 << LayerManager.LayerTower(); } //initiate scan direction //if(directionalTargeting){ // if(IsCreep()) dirScanRot=thisT.rotation; // else dirScanRot=thisT.rotation*Quaternion.Euler(0f, dirScanAngle, 0f); //} while (true) { ScanForTarget(); yield return(new WaitForSeconds(0.1f)); if (GameControl.ResetTargetAfterShoot()) { while (turretOnCooldown) { yield return(null); } } } //yield return null; }
void FPSHit(Unit hitUnit, Vector3 hitPoint) { if (attInstance.srcWeapon.GetAOERange() > 0) { LayerMask mask1 = 1 << LayerManager.LayerCreep(); LayerMask mask2 = 1 << LayerManager.LayerCreepF(); LayerMask mask = mask1 | mask2; Collider[] cols = Physics.OverlapSphere(hitPoint, attInstance.srcWeapon.GetAOERange(), mask); if (cols.Length > 0) { List <Unit> tgtList = new List <Unit>(); for (int i = 0; i < cols.Length; i++) { Unit unit = cols[i].gameObject.GetComponent <Unit>(); if (!unit.dead) { tgtList.Add(unit); } } if (tgtList.Count > 0) { for (int i = 0; i < tgtList.Count; i++) { AttackInstance attInst = new AttackInstance(); attInst.srcWeapon = attInstance.srcWeapon; attInst.tgtUnit = tgtList[i]; tgtList[i].ApplyEffect(attInst); } } } } else { if (hitUnit != null && hitUnit.IsCreep()) { attInstance.tgtUnit = hitUnit; hitUnit.ApplyEffect(attInstance); } } }
IEnumerator MineRoutine() { LayerMask maskTarget = 1 << LayerManager.LayerCreep(); while (true) { if (!dead && !IsInConstruction()) { Collider[] cols = Physics.OverlapSphere(thisT.position, GetRange(), maskTarget); if (cols.Length > 0) { Collider[] colls = Physics.OverlapSphere(thisT.position, GetAOERadius(), maskTarget); for (int i = 0; i < colls.Length; i++) { Unit unit = colls[i].transform.GetComponent <Unit>(); if (unit == null && !unit.dead) { continue; } AttackInstance attInstance = new AttackInstance(); attInstance.srcUnit = this; attInstance.tgtUnit = unit; attInstance.Process(); unit.ApplyEffect(attInstance); } Transform soPrefab = GetShootObjectT(); if (soPrefab != null) { Instantiate(soPrefab, thisT.position, thisT.rotation); } Dead(); } } yield return(new WaitForSeconds(0.1f)); } }
//apply the ability effect, damage, stun, buff and so on IEnumerator ApplyAbilityEffect(Ability ab, Vector3 pos, Unit tgtUnit = null) { yield return(new WaitForSeconds(ab.effectDelay)); LayerMask mask1 = 1 << LayerManager.LayerTower(); LayerMask mask2 = 1 << LayerManager.LayerCreep(); LayerMask mask3 = 1 << LayerManager.LayerCreepF(); LayerMask mask = mask1 | mask2 | mask3; List <Unit> creepList = new List <Unit>(); List <Unit> towerList = new List <Unit>(); if (tgtUnit == null) { float radius = ab.requireTargetSelection ? ab.GetAOERadius() : Mathf.Infinity; Collider[] cols = Physics.OverlapSphere(pos, radius, mask); if (cols.Length > 0) { for (int i = 0; i < cols.Length; i++) { Unit unit = cols[i].gameObject.GetComponent <Unit>(); if (unit.unitC != null) { creepList.Add(unit.unitC); } if (unit.unitT != null) { towerList.Add(unit.unitT); } } } } else { creepList.Add(tgtUnit); towerList.Add(tgtUnit); } AbilityEffect eff = ab.GetActiveEffect(); for (int n = 0; n < creepList.Count; n++) { if (eff.damageMax > 0) { creepList[n].ApplyDamage(Random.Range(eff.damageMin, eff.damageMax)); } else if (eff.stunChance > 0 && eff.duration > 0) { if (Random.Range(0f, 1f) < eff.stunChance) { creepList[n].ApplyStun(eff.duration); } } else if (eff.slow.IsValid()) { creepList[n].ApplySlow(eff.slow); } else if (eff.dot.GetTotalDamage() > 0) { creepList[n].ApplyDot(eff.dot); } } for (int n = 0; n < towerList.Count; n++) { if (eff.duration > 0) { if (eff.damageBuff > 0) { towerList[n].ABBuffDamage(eff.damageBuff, eff.duration); } else if (eff.rangeBuff > 0) { towerList[n].ABBuffRange(eff.rangeBuff, eff.duration); } else if (eff.cooldownBuff > 0) { towerList[n].ABBuffCooldown(eff.cooldownBuff, eff.duration); } } else if (eff.HPGainMax > 0) { towerList[n].RestoreHP(Random.Range(eff.HPGainMin, eff.HPGainMax)); } } }
public IEnumerator SupportRoutine() { supportRoutineRunning = true; LayerMask maskTarget = 0; if (subClass == _UnitSubClass.Tower) { maskTarget = 1 << LayerManager.LayerTower(); } else if (subClass == _UnitSubClass.Creep) { LayerMask mask1 = 1 << LayerManager.LayerCreep(); LayerMask mask2 = 1 << LayerManager.LayerCreepF(); maskTarget = mask1 | mask2; } while (true) { yield return(new WaitForSeconds(0.1f)); if (!dead) { List <Unit> tgtList = new List <Unit>(); Collider[] cols = Physics.OverlapSphere(thisT.position, GetRange(), maskTarget); if (cols.Length > 0) { for (int i = 0; i < cols.Length; i++) { Unit unit = cols[i].gameObject.GetComponent <Unit>(); if (!unit.dead) { tgtList.Add(unit); } } } for (int i = 0; i < buffedUnit.Count; i++) { Unit unit = buffedUnit[i]; if (unit == null || unit.dead) { buffedUnit.RemoveAt(i); i -= 1; } else if (!tgtList.Contains(unit)) { unit.UnBuff(GetBuff()); buffedUnit.RemoveAt(i); i -= 1; } } for (int i = 0; i < tgtList.Count; i++) { Unit unit = tgtList[i]; if (!buffedUnit.Contains(unit)) { unit.Buff(GetBuff()); buffedUnit.Add(unit); } } } } }
IEnumerator AOETowerRoutine() { if (targetMode == _TargetMode.Hybrid) { LayerMask mask1 = 1 << LayerManager.LayerCreep(); LayerMask mask2 = 1 << LayerManager.LayerCreepF(); maskTarget = mask1 | mask2; } else if (targetMode == _TargetMode.Air) { maskTarget = 1 << LayerManager.LayerCreepF(); } else if (targetMode == _TargetMode.Ground) { maskTarget = 1 << LayerManager.LayerCreep(); } UnitTower electricitySource; while (true) { yield return(new WaitForSeconds(GetCooldown())); while (stunned || IsInConstruction()) { yield return(null); } // disable shooting while there is no electricity electricitySource = getElectricitySource(GetElectricityNeedForShoot()); while (electricitySource == null) { // Paint deactivated towers black for (int i = 0; i < myRenderers.Length; i++) { myRenderers[i].material.color = new Color(0.2f, 0.2f, 0.2f); } electricitySource = getElectricitySource(GetElectricityNeedForShoot()); yield return(null); } // Restore original color for (int i = 0; i < myRenderers.Length; i++) { myRenderers[i].material.color = myRenderersColors[i]; } Transform soPrefab = GetShootObjectT(); if (soPrefab != null) { Instantiate(soPrefab, thisT.position, thisT.rotation); } Collider[] cols = Physics.OverlapSphere(thisT.position, GetRange(), maskTarget); if (electricitySource != null && !electricitySource.dead && cols.Length > 0) { // target will shoot so take that energy electricitySource.electricityCurrentlyStored -= GetElectricityNeedForShoot(); for (int i = 0; i < cols.Length; i++) { Unit unit = cols[i].transform.GetComponent <Unit>(); if (unit == null && !unit.dead) { continue; } AttackInstance attInstance = new AttackInstance(); attInstance.srcUnit = this; attInstance.tgtUnit = unit; attInstance.Process(); unit.ApplyEffect(attInstance); } } } }
//called in every frame, execute if there's an ability is selected and pending target selection //use only mouse input atm. void SelectAbilityTarget() { if (selectedAbilityID < 0) { return; } //only cast on terrain and platform LayerMask mask = 1 << LayerManager.LayerPlatform(); int terrainLayer = LayerManager.LayerTerrain(); if (terrainLayer >= 0) { mask |= 1 << terrainLayer; } Ability ability = abilityList[selectedAbilityID]; if (ability.singleUnitTargeting) { if (ability.targetType == Ability._TargetType.Hybrid) { mask |= 1 << LayerManager.LayerTower(); mask |= 1 << LayerManager.LayerCreep(); } else if (ability.targetType == Ability._TargetType.Friendly) { mask |= 1 << LayerManager.LayerTower(); } else if (ability.targetType == Ability._TargetType.Hostile) { mask |= 1 << LayerManager.LayerCreep(); } } #if UNITY_IPHONE || UNITY_ANDROID || UNITY_WP8 Unit targetUnit = null; if (Input.touchCount >= 1) { Camera mainCam = Camera.main; if (mainCam != null) { Ray ray = mainCam.ScreenPointToRay(Input.touches[0].position); RaycastHit hit; if (Physics.Raycast(ray, out hit, Mathf.Infinity, mask)) { currentIndicator.position = hit.point + new Vector3(0, 0.1f, 0); validTarget = true; if (ability.singleUnitTargeting) { targetUnit = hit.transform.GetComponent <Unit>(); if (targetUnit != null) { currentIndicator.position = targetUnit.thisT.position; } else { validTarget = false; } } } else { validTarget = false; } } } else { if (validTarget) { ActivateAbility(ability, currentIndicator.position, targetUnit); } else { GameControl.DisplayMessage("Invalid target for ability"); } ClearSelectedAbility(); } #else Camera mainCam = Camera.main; if (mainCam != null) { Ray ray = mainCam.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, Mathf.Infinity, mask)) { currentIndicator.position = hit.point + new Vector3(0, 0.1f, 0); Unit targetUnit = null; validTarget = true; if (ability.singleUnitTargeting) { targetUnit = hit.transform.GetComponent <Unit>(); if (targetUnit != null) { currentIndicator.position = targetUnit.thisT.position; } else { validTarget = false; } } if (Input.GetMouseButtonDown(0)) { if (validTarget) { ActivateAbility(ability, currentIndicator.position, targetUnit); ClearSelectedAbility(); } else { GameControl.DisplayMessage("Invalid target for ability"); } } } } if (Input.GetMouseButtonDown(1)) { ClearSelectedAbility(); } #endif }