//for apply damage directly, not in used public void ApplyDamage(float damage) { if (immunityCounter > 0) { return; } //show the overlay (for UI) Vector3 offsetPos = new Vector3(0, Random.value + 0.5f, 0); new TextOverlay(thisT.position + offsetPos, damage.ToString("f0")); //if the unit is player, fire event to inform UI if (thisObj.layer == TDS.GetLayerPlayer()) { TDS.PlayerDamaged(damage); } //register the stagger hpRegenStaggerCounter = hpRegenStagger; hitPoint -= damage; //damage the hitpoint if (hitPoint <= 0) { Destroyed(); } if (hitPoint > 0 && uAnimation != null) { uAnimation.Hit(); } }
//when any collider trigger the collider of this collectible void OnTriggerEnter(Collider col) { //only carry on if the trigger object is player if (col.gameObject.layer != TDS.GetLayerPlayer()) { return; } //check the effect type and apply them accordingly if (type == _CollectType.Self) //apply effect to player unit { ApplyEffectSelf(col.gameObject.GetComponent <UnitPlayer>()); } else if (type == _CollectType.AOEHostile) //apply effect to all surrounding hostile { ApplyEffectAOE(col); } else if (type == _CollectType.AllHostile) //apply effect to all hostile { ApplyEffectAll(); } else if (type == _CollectType.Ability) { AbilityManager.TriggerAbility(abilityID); } GameControl.ColletibleCollected(this); //play the sound and show spawn the trigger effect object at current position AudioManager.PlaySound(triggerSFX); TriggeredEffect(transform.position + new Vector3(0, 0.1f, 0)); //Destroy(gameObject); ObjectPoolManager.Unspawn(gameObject); }
//function call when the unit instance gain/lost hitpoint from external source public virtual float GainHitPoint(float value) { float limit = GetFullHitPoint() - hitPoint; hitPoint += Mathf.Min(value, limit); //if the unit lost hitpoint instead of gaining it if (value < 0) { hpRegenStaggerCounter = hpRegenStagger; //if this is the player, call the damage event (inform UI) if (thisObj.layer == TDS.GetLayerPlayer()) { TDS.PlayerDamaged(value); } } //if hitpoint reach 0, destroy the unit if (hitPoint <= 0) { Destroyed(); } return(limit); }
// Use this for initialization public override void Awake() { base.Awake(); thisObj.layer = TDS.GetLayerPlayer(); isPlayer = true; SetDestroyCallback(this.PlayerDestroyCallback); if (enableAllWeapons) { weaponList = new List <Weapon>(Weapon_DB.Load()); } if (weaponMountPoint == null) { weaponMountPoint = thisT; } if (enableTurretRotate) { if (turretObj != null) { turretObjParent = turretObj.parent; } else { turretObj = thisT; //faceTravelDirection=false; } } hitPointBase = hitPointFull; energyBase = energyFull; InitSpawnImmunity(); progress = thisObj.GetComponent <PlayerProgression>(); if (progress != null) { progress.SetPlayer(this); } perk = thisObj.GetComponent <PlayerPerk>(); if (perk != null) { perk.SetPlayer(this); } Load(); }
//when the unit enter some trigger void OnTriggerEnter(Collider col) { if (col.gameObject.layer != TDS.GetLayerPlayer()) { return; } OnPlayerContact(); if (destroyUponPlayerContact) { ClearUnit(); //if destroyUponPlayerContact is enabled, clear the unit } }
//when the unit collide with something, for collision with player void OnCollisionEnter(Collision collision) { if (collision.gameObject.layer != TDS.GetLayerPlayer()) { return; } OnPlayerContact(); if (destroyUponPlayerContact) { ClearUnit(); //if destroyUponPlayerContact is enabled, clear the unit } }
//function call to scan for all active unit in game //not in used atm public static void ScanForUnit() { allUnitList = new List <Unit>(); Unit[] list = GameObject.FindObjectsOfType(typeof(UnitAI)) as Unit[]; for (int i = 0; i < list.Length; i++) { if (list[i].thisObj.layer == TDS.GetLayerPlayer()) { continue; } allUnitList.Add(list[i]); } }
//for explosion public static void ApplyExplosionForce(Vector3 impactPoint, AttackStats aStats, bool ignorePlayer = false) { float explosionForce = aStats.explosionForce * 10; float explosionRadius = explosionForce > 0 ? aStats.explosionRadius : 0; if (explosionRadius > 0) { LayerMask mask = ignorePlayer ? ~(1 << TDS.GetLayerPlayer()) : ~0; Collider[] cols = Physics.OverlapSphere(impactPoint, explosionRadius, mask); for (int i = 0; i < cols.Length; i++) { Rigidbody rd = cols[i].gameObject.GetComponent <Rigidbody>(); if (rd != null) { rd.AddExplosionForce(explosionForce, impactPoint, explosionRadius, 0, ForceMode.Impulse); } } } }
//applies the effect of an attack public void ApplyAttack(AttackInstance attInstance) { if (immunityCounter > 0) { return; } //if unit is invincible, do nothing if (IsInvincible()) { Debug.Log("Immuned"); Vector3 osPos = new Vector3(0, Random.value + 0.5f, 0); new TextOverlay(thisT.position + osPos, "Immuned"); return; } AttackStats aStats = attInstance.aStats; //check if the attack is an aoe and modify the damage value is dimishingAOE is enabled float damage = Random.Range(aStats.damageMin, aStats.damageMax); if (attInstance.isAOE && aStats.diminishingAOE) { damage *= Mathf.Clamp(1 - attInstance.aoeDistance / aStats.aoeRadius, 0, 1); } //check for cirtical and modify the damage based on critical multiplier bool critical = Random.value < aStats.critChance; if (critical) { damage *= aStats.critMultiplier; } //modify the damage based on damage and armor type damage *= DamageTable.GetModifier(armorType, aStats.damageType); //if damage is valid, modify the hit point if (damage > 0) { //show the overlay (for UI) Vector3 offsetPos = new Vector3(0, Random.value + 0.5f, 0); if (!critical) { new TextOverlay(thisT.position + offsetPos, damage.ToString("f0")); } else { new TextOverlay(thisT.position + offsetPos, damage.ToString("f0"), new Color(1f, 0.9f, 0.9f, 1f), 1.5f); } //if the unit is player, fire event to inform UI if (thisObj.layer == TDS.GetLayerPlayer()) { TDS.PlayerDamaged(damage); } //register the stagger hpRegenStaggerCounter = hpRegenStagger; hitPoint -= damage; //damage the hitpoint if (hitPoint <= 0) { Destroyed(attInstance.GetSrcPlayer()); } if (hitPoint > 0 && uAnimation != null) { uAnimation.Hit(); } } //apply effect if there's any //if(aStats.effect!=null) ApplyEffect(aStats.effect); if (hitPoint > 0 && aStats.effectIdx >= 0) { ApplyEffect(Effect_DB.CloneItem(aStats.effectIdx)); } }