protected bool _processSingleBullet(BulletComponent bullet) { EnemyComponent damagedEnemy = bullet.mDamagedEnemy; /// skip the bullet because it didn't hit any enemy if (damagedEnemy == null) { return(false); } HealthBarView healthBarView = damagedEnemy.GetComponentInChildren <HealthBarView>(); float newHealth = Mathf.Max(0.0f, damagedEnemy.mHealth - bullet.mDamage); healthBarView.CurrNormalizedHealth = newHealth / damagedEnemy.mConfigs.mHealth; /// compute normalized health's value damagedEnemy.mHealth = newHealth; /// destroy enemy's view if (damagedEnemy.mHealth < 1e-3f) { DestroyedComponent destroyedComponent = damagedEnemy.GetComponent <DestroyedComponent>(); destroyedComponent.mShouldBeDestroyed = true; /// kill the enemy //GameObject.Destroy(damagedEnemy.gameObject); EventBus.NotifyOnEnemyDestroyed(damagedEnemy.mConfigs.mReward); } /// TODO: return bullet to an object pool of bullets //GameObject.Destroy(bullet.gameObject); DestroyedComponent destroyedBullet = bullet.GetComponent <DestroyedComponent>(); destroyedBullet.mShouldBeDestroyed = true; return(true); }
protected override void OnUpdate() { foreach (var entityT in GetEntities <GroupTower>()) { foreach (var entityM in GetEntities <GroupMonster>()) { BulletComponent bullet = entityT.TowerComponent.Bullet; // If the monster is within the fire range of the tower // The tower rotate to look at the monster and open fire if (Vector3.Distance(entityT.TransformTower.position, entityM.TransformMonster.position) < entityT.TowerComponent.FireRange) { Quaternion targetRotation; // The tower look at the monster targetRotation = Quaternion.LookRotation(entityM.TransformMonster.position - entityT.TransformTower.position); entityT.TransformTower.rotation = Quaternion.Slerp(entityT.TransformTower.rotation, new Quaternion(0f, targetRotation.y, 0f, targetRotation.w), entityT.SpeedTower.Value); // The weapon look at the monster targetRotation = Quaternion.LookRotation(entityM.TransformMonster.position - entityT.TowerComponent.TowerWeapon.transform.position) * Quaternion.Euler(90f, 0, 0); entityT.TowerComponent.TowerWeapon.transform.rotation = Quaternion.Slerp(entityT.TowerComponent.TowerWeapon.transform.rotation, targetRotation, entityT.SpeedTower.Value); entityT.TowerComponent.TargetMonster = entityM.MonsterComponent; bullet.IsActive = true; break; } // If a bullet is shot and no monster is within range, reset the bullet after it arrive in the tower max fire range else if (bullet != null && Vector3.Distance(bullet.transform.position, bullet.GetComponent <PositionComponent>().Value) < 0.1f) { // Smooth animation and return the weapon to its initial state entityT.TransformTower.rotation = Quaternion.Slerp(entityT.TransformTower.rotation, entityT.RotationTower.Value, entityT.SpeedTower.Value); entityT.TowerComponent.TowerWeapon.transform.rotation = Quaternion.Slerp(entityT.TowerComponent.TowerWeapon.transform.rotation, new Quaternion(0.7071068f, 0f, 0f, 0.7071068f), entityT.SpeedTower.Value); bullet.IsActive = false; } } } }