private void MageEnergyDrainTalent(Card cardObject) { Player playerObject =…
- User
private void MageEnergyDrainTalent(Card cardObject) { Player playerObject = GetActorObject(cardObject.GetOwnerPlayer()); HeroTalent heroTalent = GetActorObject(playerObject.GetHeroTalent()); if (heroTalent.GetTalentType() == HeroTypes.Mage && heroTalent.GetTalentIndex() == 0 && heroTalent.GetTalentCount() == 0) { cardObject.AddEffect(CardKeyWordType.EnergyDrain); GameAttribute originGameHealth = cardObject.GetHealth(); originGameHealth += 2; // 卡牌的原始生命值增加 CardAttribute originCardAttribute = cardObject.GetOriginAttribute(); GameAttribute originAttribute = originCardAttribute.Health; originAttribute += 2; originCardAttribute.Health = originAttribute; CardAttribute metaCardAttribute = cardObject.GetMetaAttribute(); GameAttribute metaAttribute = metaCardAttribute.Health; metaAttribute += 2; metaCardAttribute.Health = metaAttribute;
GameAttribute originGameAttack = cardObject.GetAttack(); originGameAttack += 2; // 卡牌的原始攻击力值增加 GameAttribute originAttributeAttack = originCardAttribute.Attack; originAttributeAttack += 2; originCardAttribute.Attack = originAttributeAttack; GameAttribute metaAttributeAttack = metaCardAttribute.Attack; metaAttributeAttack += 2; metaCardAttribute.Attack = metaAttributeAttack; cardObject.SetMetaAttribute(metaCardAttribute); cardObject.SetOriginAttribute(originCardAttribute); cardObject.SetAttack(originGameAttack); cardObject.SetHealth(originGameHealth); return; } if (heroTalent.GetTalentType() == HeroTypes.Mage && heroTalent.GetTalentIndex() == 0 && heroTalent.GetTalentCount() > 0) { int count = heroTalent.GetTalentCount() - 1; heroTalent.SetTalentCount(count); } } 帮忙优化这段代码 - Assistant
当然,可以通过减少重复代码并优化逻辑结构来优化这段代码。以下是优化后的代码:
private void MageEnergyDrainTalent(Card cardObject) { Player playerObject = GetActorObject(cardObject.GetOwnerPlayer()); HeroTalent heroTalent = GetActorObject(playerObject.GetHeroTalent()); if (heroTalent.GetTalentType() == HeroTypes.Mage && heroTalent.GetTalentIndex() == 0) { if (heroTalent.GetTalentCount() == 0) { cardObject.AddEffect(CardKeyWordType.EnergyDrain); IncreaseCardAttributes(cardObject, 2, 2); } else { int count = heroTalent.GetTalentCount() - 1; heroTalent.SetTalentCount(count); } } } private void IncreaseCardAttributes(Card cardObject, int healthIncrease, int attackIncrease) { // Increase Health attributes GameAttribute originGameHealth = cardObject.GetHealth(); originGameHealth += healthIncrease; CardAttribute originCardAttribute = cardObject.GetOriginAttribute(); GameAttribute originAttribute = originCardAttribute.Health; originAttribute += healthIncrease; originCardAttribute.Health = originAttribute; CardAttribute metaCardAttribute = cardObject.GetMetaAttribute(); GameAttribute metaAttribute = metaCardAttribute.Health; metaAttribute += healthIncrease; metaCardAttribute.Health = metaAttribute; // Increase Attack attributes GameAttribute originGameAttack = cardObject.GetAttack(); originGameAttack += attackIncrease; GameAttribute originAttributeAttack = originCardAttribute.Attack; originAttributeAttack += attackIncrease; originCardAttribute.Attack = originAttributeAttack; GameAttribute metaAttributeAttack = metaCardAttribute.Attack; metaAttributeAttack += attackIncrease; metaCardAttribute.Attack = metaAttributeAttack; // Set the updated attributes back to the card object cardObject.SetMetaAttribute(metaCardAttribute); cardObject.SetOriginAttribute(originCardAttribute); cardObject.SetAttack(originGameAttack); cardObject.SetHealth(originGameHealth); }优化后的代码做了以下几点改进:
- 方法提取:将增加卡牌属性的代码提取到一个单独的方法
IncreaseCardAttributes中,减少了重复代码,增强了代码的可读性和可维护性。 - 逻辑结构优化:重新组织了条件判断,使其结构更加清晰。
通过这些改进,可以使代码更具有可读性、模块化和易于维护。
- 方法提取:将增加卡牌属性的代码提取到一个单独的方法
- User
unity怎么计算Collider 到gameobject中心的距离呢
- Assistant
在 Unity 中,要计算碰撞体(Collider)到游戏对象中心的距离,你可以使用以下方法。假设你有一个目标游戏对象并且它有一个 Collider,我们可以通过脚本来计算这个距离。
以下是一个示例脚本,这个脚本假设它附加到具有 Collider 和 Transform 的游戏对象上:
using UnityEngine; public class ColliderDistanceToCenter : MonoBehaviour { // Update is called once per frame void Update() { Collider collider = GetComponent<Collider>(); if (collider != null) { Vector3 objectCenter = transform.position; // 计算碰撞体与游戏对象中心的距离 // 获取碰撞体最近点(离中心点最近的点) Vector3 closestPoint = collider.ClosestPoint(objectCenter); // 计算距离 float distance = Vector3.Distance(closestPoint, objectCenter); // 打印距离 Debug.Log("距离碰撞体到游戏对象中心的距离: " + distance); } } }示例代码解释:
-
获取 Collider 和 Transform:首先需要获得 Collider 和 Transform 组件。Collider 用于获取碰撞体的信息,Transform 用于获取游戏对象的中心位置。
-
计算最近点:使用
collider.ClosestPoint方法来找到碰撞体上距离游戏对象中心最近的点。 -
计算距离:使用
Vector3.Distance方法计算这个最近点到游戏对象中心的距离。 -
输出结果:使用
Debug.Log输出结果。
将此脚本附加到具有 Collider 的游戏对象上,当游戏运行时,它会在每帧更新时计算并打印碰撞体到游戏对象中心的距离。
请注意,如果你的场景中可能会包含多个碰撞体,你需要调整脚本以处理这种情况。你也可以根据需要调整脚本,使其只在特定时刻(如触发某种事件时)计算距离,而不是每帧更新。
-