2024-04-28 13:55:19 +00:00
|
|
|
|
using ColdMint.scripts.character;
|
2024-06-11 16:51:40 +00:00
|
|
|
|
|
2024-06-22 11:21:06 +00:00
|
|
|
|
using WeaponTemplate = ColdMint.scripts.weapon.WeaponTemplate;
|
2024-04-28 13:55:19 +00:00
|
|
|
|
|
|
|
|
|
namespace ColdMint.scripts.behaviorTree.ai;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <para>Deal with AI picking up items</para>
|
|
|
|
|
/// <para>处理AI拾起物品的行为</para>
|
|
|
|
|
/// </summary>
|
2024-05-08 10:22:04 +00:00
|
|
|
|
public class AiPickNode : BehaviorTreeNodeTemplate
|
2024-04-28 13:55:19 +00:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2024-05-08 10:22:04 +00:00
|
|
|
|
public AiCharacter? Character { get; set; }
|
2024-04-28 13:55:19 +00:00
|
|
|
|
|
|
|
|
|
public override int Execute(bool isPhysicsProcess, double delta)
|
|
|
|
|
{
|
|
|
|
|
if (Character == null)
|
|
|
|
|
{
|
|
|
|
|
return Config.BehaviorTreeResult.Failure;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Character.CurrentItem != null)
|
|
|
|
|
{
|
|
|
|
|
//If the character already has the item, we don't pick it up
|
|
|
|
|
//如果角色已经持有物品了,我们就不再拾取
|
|
|
|
|
return Config.BehaviorTreeResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Find the nearest item
|
|
|
|
|
//查找距离最近的物品
|
|
|
|
|
var childCount = Character.PickingRangeBodies.Length;
|
|
|
|
|
if (childCount == 0)
|
|
|
|
|
{
|
|
|
|
|
//We can't pick things up without them
|
|
|
|
|
//没有物品,我们不能捡起
|
|
|
|
|
return Config.BehaviorTreeResult.Failure;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//The closest weapon
|
|
|
|
|
//距离最近的武器
|
2024-05-08 10:22:04 +00:00
|
|
|
|
WeaponTemplate? closestWeapon = null;
|
2024-04-28 13:55:19 +00:00
|
|
|
|
var closestDistance = float.MaxValue;
|
|
|
|
|
foreach (var weaponTemplate in Character.GetCanPickedWeapon())
|
|
|
|
|
{
|
|
|
|
|
//If it's a weapon
|
|
|
|
|
//如果是武器
|
2024-06-04 14:23:06 +00:00
|
|
|
|
var distanceLength = weaponTemplate.GlobalPosition.DistanceTo(Character.GlobalPosition);
|
2024-04-28 13:55:19 +00:00
|
|
|
|
if (distanceLength < closestDistance)
|
|
|
|
|
{
|
|
|
|
|
closestDistance = distanceLength;
|
|
|
|
|
closestWeapon = weaponTemplate;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//绘制一条线,从AI到武器
|
|
|
|
|
// Draw a line from AI to weapon
|
|
|
|
|
if (closestWeapon != null)
|
|
|
|
|
{
|
|
|
|
|
//If we find the nearest weapon
|
|
|
|
|
//如果找到了最近的武器
|
|
|
|
|
Character.PickItem(closestWeapon);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Config.BehaviorTreeResult.Success;
|
|
|
|
|
}
|
|
|
|
|
}
|