/// <summary> /// 目标刚体的表现逻辑:如扣血以及坦克被弹开 /// other Entity have two type: TargetableObject or Bullet /// </summary> /// <param name="entity">Targetable entity component, such as Tank entity</param> /// <param name="other">entity component, such as bullet entity</param> public static void PerformCollision(TargetableObject entity, Entity other, object userData) { if (entity == null || other == null) { return; } /* if other entity is targetable entity */ TargetableObject target = other as TargetableObject; if (target != null) { ImpactData entityImpactData = entity.GetImpactData(); ImpactData targetImpactData = target.GetImpactData(); if (GetRelation(entityImpactData.Camp, targetImpactData.Camp) == RelationType.Friendly) { return; } float entityDamageHP = CalcDamageHP(targetImpactData.Attack, entityImpactData.Defense); float targetDamageHP = CalcDamageHP(entityImpactData.Attack, targetImpactData.Defense); float delta = Mathf.Min(entityImpactData.HP - entityDamageHP, targetImpactData.HP - targetDamageHP); if (delta > 0) { entityDamageHP += delta; targetDamageHP += delta; } entity.ApplyDamage(target, entityDamageHP); target.ApplyDamage(entity, targetDamageHP); return; } /* if other entity is only a entity */ Bullet bullet = other as Bullet; BulletData bulletData = userData as BulletData; if (bullet != null) { ImpactData entityImpactData = entity.GetImpactData(); // get tank impact data ImpactData bulletImpactData = bullet.GetImpactData(); // get bullet impact data // Block the damage of the same camp 自己发射的子弹,屏蔽其伤害 //if (GetRelation(entityImpactData.Camp, bulletImpactData.Camp) == RelationType.Friendly) { // return; //} int entityDamageHP = CalcDamageHP(bulletImpactData.Attack, entityImpactData.Defense); // the bullet of attack value, and the tank of defense value. float bulletDamageHP = GetRelativeDistance(entity, bullet, bulletData) * entityDamageHP; //Debug.Log("entityDamageHP : " + bulletDamageHP); entity.ApplyDamage(bullet, bulletDamageHP); //GameEntry.Entity.HideEntity(bullet); return; } }
/// <summary> /// get the two entity relative distance /// </summary> public static float GetRelativeDistance(Entity fromEntity, Entity toEntity, object userData) { // get the two entity distance, example: the shell to the Tank Distance. float explosionDistance = GetDistance(fromEntity, toEntity); BulletData bulletData = userData as BulletData; if (bulletData != null) { return((bulletData.ExplosionRadius - explosionDistance) / bulletData.ExplosionRadius); } return(-1); }
// 对象池中的Bullt对象,只要被复用时,都会重新执行OnShow方法。 protected override void OnShow(object userData) { base.OnShow(userData); //Debug.Log(System.Reflection.MethodBase.GetCurrentMethod().Name); m_BulletData = userData as BulletData; if (m_BulletData == null) { Log.Error("Bullet data is invalid."); return; } // 每次复用Bullet对象时刷新炮弹的发射速率,而炮弹发射的position和rotation已经在show bullet的时候,进行初始化了。 GetComponent <Rigidbody>().velocity = m_BulletData.Velocity; }
public static void ShowBullet(this EntityComponent entityCompoennt, BulletData data) { entityCompoennt.ShowEntity(typeof(Bullet), "Bullet", Constant.AssetPriority.BulletAsset, data); }