public override void OnAttachTo(EntityLogicBase parentEntity, Transform parentTransform, object userData) { base.OnAttachTo(parentEntity, parentTransform, userData); RuntimeEntity.Name = Utility.Text.Format("Weapon of {0}", parentEntity.RuntimeEntity.Name); RuntimeEntity.LocalPosition = Vector3.zero; }
protected override void OnDead(EntityLogicBase attacker) { base.OnDead(attacker); //显示特效 GameEntry.Entity.ShowEffect(new EffectData(GameEntry.Entity.GenerateSerialId(), m_AircraftData.DeadEffectId) { Position = RuntimeEntity.Position }); GameEntry.Sound.PlaySound(m_AircraftData.DeadSoundId); //播放死亡声音 }
//触发检测 public override void OnTriggerEnter(HotEntity other) { if (other == null) { return; } EntityLogicBase otherTarget = other.HotLogicInstance as EntityLogicBase; if (otherTarget != null && other.Id >= RuntimeEntity.Id) { // 碰撞事件由 Id 小的一方处理,避免重复处理 return; } AIUtility.PerformCollision(this, otherTarget); }
//应用伤害 public void ApplyDamage(EntityLogicBase attacker, int damageHP) { float fromHPRatio = m_TargetableObjectData.HPRatio; m_TargetableObjectData.HP -= damageHP; float toHPRatio = m_TargetableObjectData.HPRatio; //显示血条动画 if (fromHPRatio > toHPRatio) { HotfixEntry.HPBar.ShowHPBar(RuntimeEntity, fromHPRatio, toHPRatio); } if (m_TargetableObjectData.HP <= 0) { OnDead(attacker); } }
//解除实体 public override void OnDetached(EntityLogicBase childEntity, object userData) { base.OnDetached(childEntity, userData); if (childEntity.GetType() == typeof(Thruster)) //推进器 { m_Thruster = null; return; } if (childEntity.GetType() == typeof(Weapon)) //武器 { m_ListWeapon.Remove(childEntity as Weapon); return; } if (childEntity.GetType() == typeof(Armor)) //装甲 { m_ListArmor.Remove(childEntity as Armor); return; } }
//附加实体的回调 public override void OnAttached(EntityLogicBase childEntity, Transform parentTransform, object userData) { base.OnAttached(childEntity, parentTransform, userData); if (childEntity.GetType() == typeof(Thruster)) //附加推进器 { m_Thruster = childEntity as Thruster; return; } if (childEntity.GetType() == typeof(Weapon)) //附加武器 { m_ListWeapon.Add(childEntity as Weapon); return; } if (childEntity.GetType() == typeof(Armor)) //附加装甲 { m_ListArmor.Add(childEntity as Armor); return; } }
//附加实体 public static void AttachEntity(this EntityComponent entityComponent, EntityLogicBase entity, int ownerId, string parentTransformPath = null, object userData = null) { entityComponent.AttachEntity(entity.RuntimeEntity.Entity, ownerId, parentTransformPath, userData); }
//隐藏逻辑实体 public static void HideEntity(this EntityComponent entityComponent, EntityLogicBase entity) { entityComponent.HideEntity(entity.RuntimeEntity.Entity); }
//执行碰撞 public static void PerformCollision(TargetableObject entity, EntityLogicBase other) { if (entity == null || other == null) { return; } //如果是可命中的实体对象 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; } //计算双方的伤害血量 int entityDamageHP = CalculateDamageHP(targetImpactData.Attack, entityImpactData.Defense); int targetDamageHP = CalculateDamageHP(entityImpactData.Attack, targetImpactData.Defense); //这里增加伤害值,为了让其中一方收到伤害直接死亡 int delta = Mathf.Min(entityImpactData.HP - entityDamageHP, targetImpactData.HP - targetDamageHP); if (delta > 0) { entityDamageHP += delta; targetDamageHP += delta; } //应用伤害 entity.ApplyDamage(target, entityDamageHP); target.ApplyDamage(entity, targetDamageHP); //记得回收 ReferencePool.Release(entityImpactData); ReferencePool.Release(targetImpactData); return; } //子弹碰撞 Bullet bullet = other as Bullet; if (bullet != null) { ImpactData bulletImpactData = bullet.GetImpactData(); ImpactData entityImpactData = entity.GetImpactData(); if (GetRelation(entityImpactData.Camp, bulletImpactData.Camp) == RelationType.Friendly) { return; } int entityDamageHP = CalculateDamageHP(bulletImpactData.Attack, entityImpactData.Defense); //伤害值 entity.ApplyDamage(bullet, entityDamageHP); //执行伤害 GameEntry.Entity.HideEntity(bullet); //隐藏子弹 //记得回收 ReferencePool.Release(bulletImpactData); ReferencePool.Release(entityImpactData); return; } }
/// <summary> /// 死亡 /// </summary> /// <param name="attacker">攻击者</param> protected virtual void OnDead(EntityLogicBase attacker) { GameEntry.Entity.HideEntity(RuntimeEntity.Entity); }
/// <summary> /// 实体解除子实体 /// </summary> /// <param name="childEntity">被解除的父实体</param> /// <param name="userData">用户自定义数据</param> public virtual void OnDetachFrom(EntityLogicBase parentEntity, object userData) { }
/// <summary> /// 实体附加子实体 /// </summary> /// <param name="parentEntity">被附加的父实体</param> /// <param name="parentTransform">被附加父实体的位置</param> /// <param name="userData">用户自定义数据</param> public virtual void OnAttachTo(EntityLogicBase parentEntity, Transform parentTransform, object userData) { }
/// <summary> /// 实体解除子实体 /// </summary> /// <param name="childEntity">解除的子实体</param> /// <param name="userData">用户自定义数据</param> public virtual void OnDetached(EntityLogicBase childEntity, object userData) { }
/// <summary> /// 实体附加子实体 /// </summary> /// <param name="childEntity">附加的子实体</param> /// <param name="parentTransform">被附加的父实体</param> /// <param name="userData">用户自定义数据</param> public virtual void OnAttached(EntityLogicBase childEntity, Transform parentTransform, object userData) { }