From 95515ddab2a2f0256480c390b86a9fbdbdb78655 Mon Sep 17 00:00:00 2001 From: Cold-Mint Date: Thu, 6 Jun 2024 21:05:51 +0800 Subject: [PATCH] =?UTF-8?q?When=20the=20selected=20item=20slot=20A=20is=20?= =?UTF-8?q?selected,=20pressing=20the=20shortcut=20key=20of=20item=20slot?= =?UTF-8?q?=20A=20again=20will=20no=20longer=20invoke=20the=20press=20even?= =?UTF-8?q?t=20of=20A.=20=E5=BD=93=E9=80=89=E4=B8=AD=E7=89=A9=E5=93=81?= =?UTF-8?q?=E6=A7=BDA=E6=97=B6=EF=BC=8C=E5=86=8D=E6=AC=A1=E6=8C=89?= =?UTF-8?q?=E4=B8=8B=E7=89=A9=E5=93=81=E6=A7=BDA=E7=9A=84=E5=BF=AB?= =?UTF-8?q?=E6=8D=B7=E9=94=AE=EF=BC=8C=E5=B0=B1=E4=B8=8D=E8=83=BD=E5=86=8D?= =?UTF-8?q?=E8=B0=83=E7=94=A8A=E7=9A=84=E6=8C=89=E4=B8=8B=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E4=BA=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/behaviorTree/BehaviorNode.cs | 5 -- scripts/behaviorTree/IBehaviorTree.cs | 4 ++ scripts/behaviorTree/ai/AiAttackNode.cs | 70 ++++++++++--------- scripts/behaviorTree/ai/AiPatrolNode.cs | 1 + .../behaviorTree/framework/SequenceNode.cs | 2 +- scripts/camp/Camp.cs | 5 +- scripts/camp/CampManager.cs | 2 +- scripts/character/AiCharacter.cs | 11 +-- scripts/character/CharacterTemplate.cs | 18 ++--- scripts/inventory/HotBar.cs | 12 ++-- 10 files changed, 67 insertions(+), 63 deletions(-) diff --git a/scripts/behaviorTree/BehaviorNode.cs b/scripts/behaviorTree/BehaviorNode.cs index 03b09a7..f2f8c58 100644 --- a/scripts/behaviorTree/BehaviorNode.cs +++ b/scripts/behaviorTree/BehaviorNode.cs @@ -15,11 +15,6 @@ public partial class BehaviorNode : Node2D InvokeBehaviorTreeNode(true, delta); } - // public override void _Process(double delta) - // { - // InvokeBehaviorTreeNode(false, delta); - // } - /// /// InvokeBehaviorTreeNode /// 调用行为树节点 diff --git a/scripts/behaviorTree/IBehaviorTree.cs b/scripts/behaviorTree/IBehaviorTree.cs index e25a945..074fcb6 100644 --- a/scripts/behaviorTree/IBehaviorTree.cs +++ b/scripts/behaviorTree/IBehaviorTree.cs @@ -1,5 +1,9 @@ namespace ColdMint.scripts.behaviorTree; +/// +/// IBehavior Tree +/// 行为树 +/// public interface IBehaviorTree { string? Id { get; } diff --git a/scripts/behaviorTree/ai/AiAttackNode.cs b/scripts/behaviorTree/ai/AiAttackNode.cs index aa145b7..c95111b 100644 --- a/scripts/behaviorTree/ai/AiAttackNode.cs +++ b/scripts/behaviorTree/ai/AiAttackNode.cs @@ -3,6 +3,10 @@ using ColdMint.scripts.character; namespace ColdMint.scripts.behaviorTree.ai; +/// +/// AI attack node +/// AI的攻击节点 +/// public class AiAttackNode : BehaviorTreeNodeTemplate { public AiCharacter? Character { get; set; } @@ -29,47 +33,49 @@ public class AiAttackNode : BehaviorTreeNodeTemplate var selfCamp = CampManager.GetCamp(Character.CampId); foreach (var node in nodesInTheAttackRange) { - if (node is CharacterTemplate characterTemplate) + if (node is not CharacterTemplate characterTemplate) { - if (node == Character) - { - continue; - } + continue; + } + + if (node == Character) + { + continue; + } - var characterCamp = CampManager.GetCamp(characterTemplate.CampId); - var canCause = CampManager.CanCauseHarm(selfCamp, characterCamp); - if (!canCause) - { - continue; - } + var characterCamp = CampManager.GetCamp(characterTemplate.CampId); + var canCause = CampManager.CanCauseHarm(selfCamp, characterCamp); + if (!canCause) + { + continue; + } - if (selfCamp == null || characterCamp == null) - { - continue; - } + if (selfCamp == null || characterCamp == null) + { + continue; + } + + if (selfCamp.Id == characterCamp.Id) + { + //If it is the same side, do not attack, if allowed friend damage, this code will prevent the AI from actively attacking the player. + //如果是同一阵营,不攻击,如果允许友伤,这段代码会阻止AI主动攻击玩家。 + continue; + } - if (selfCamp.Id == characterCamp.Id) - { - //如果是同一阵营,不攻击 - continue; - } - - var distance = characterTemplate.GlobalPosition - Character.GlobalPosition; - var distanceLength = distance.Length(); - if (distanceLength < closestDistance) - { - closestDistance = distanceLength; - closestEnemy = characterTemplate; - } + var distance = characterTemplate.GlobalPosition.DistanceTo(Character.GlobalPosition); + if (distance < closestDistance) + { + closestDistance = distance; + closestEnemy = characterTemplate; } } if (closestEnemy != null && Character.AttackObstacleDetection != null) { - //There are the closest enemies - //有距离最近的敌人 - var distance = closestEnemy.GlobalPosition - Character.GlobalPosition; - Character.AttackObstacleDetection.TargetPosition = distance; + //With the nearest enemy and no obstacles + //有距离最近的敌人,且没有障碍物 + var distanceVector2 = closestEnemy.GlobalPosition - Character.GlobalPosition; + Character.AttackObstacleDetection.TargetPosition = distanceVector2; if (Character.AttackObstacleDetection.GetCollider() == null) { Character.StopMoving(); diff --git a/scripts/behaviorTree/ai/AiPatrolNode.cs b/scripts/behaviorTree/ai/AiPatrolNode.cs index cf0e9a5..586abdd 100644 --- a/scripts/behaviorTree/ai/AiPatrolNode.cs +++ b/scripts/behaviorTree/ai/AiPatrolNode.cs @@ -4,6 +4,7 @@ using ColdMint.scripts.character; namespace ColdMint.scripts.behaviorTree.ai; /// +/// AI patrol node /// AI巡逻节点 /// public class AiPatrolNode : SelectorNode diff --git a/scripts/behaviorTree/framework/SequenceNode.cs b/scripts/behaviorTree/framework/SequenceNode.cs index ba670cf..77904a8 100644 --- a/scripts/behaviorTree/framework/SequenceNode.cs +++ b/scripts/behaviorTree/framework/SequenceNode.cs @@ -14,7 +14,7 @@ public class SequenceNode : BehaviorTreeNodeTemplate /// Check whether all child nodes are executed in sequence /// 所有子节点是否按顺序执行完毕 /// - bool _complete = true; + private bool _complete = true; public override int Execute(bool isPhysicsProcess, double delta) { diff --git a/scripts/camp/Camp.cs b/scripts/camp/Camp.cs index 5e21b74..841e707 100644 --- a/scripts/camp/Camp.cs +++ b/scripts/camp/Camp.cs @@ -8,12 +8,11 @@ namespace ColdMint.scripts.camp; /// public class Camp { - private readonly string _id; private readonly List _friendlyCampIdList; public Camp(string id) { - _id = id; + Id = id; _friendlyCampIdList = new List(); } @@ -21,7 +20,7 @@ public class Camp /// Get camp ID /// 获取阵营ID /// - public string Id => _id; + public string Id { get; } /// /// Get camp name diff --git a/scripts/camp/CampManager.cs b/scripts/camp/CampManager.cs index 37a1479..6014320 100644 --- a/scripts/camp/CampManager.cs +++ b/scripts/camp/CampManager.cs @@ -6,7 +6,7 @@ namespace ColdMint.scripts.camp; /// Camp manager /// 阵营管理器 /// -public class CampManager +public static class CampManager { private static readonly Dictionary Camps = new Dictionary(); diff --git a/scripts/character/AiCharacter.cs b/scripts/character/AiCharacter.cs index 6c0765a..d40e5be 100644 --- a/scripts/character/AiCharacter.cs +++ b/scripts/character/AiCharacter.cs @@ -9,7 +9,7 @@ namespace ColdMint.scripts.character; /// The role played by computers /// 由电脑扮演的角色 /// -public partial class AiCharacter : CharacterTemplate +public sealed partial class AiCharacter : CharacterTemplate { /// /// How fast the character moves @@ -67,10 +67,11 @@ public partial class AiCharacter : CharacterTemplate if (_attackArea != null) { + //If true, the zone will detect objects or areas entering and leaving the zone. //如果为true,该区域将检测进出该区域的物体或区域。 _attackArea.Monitoring = true; - //Other regions cannot detect our pick region - //其他区域不能检测到我们的拾取区域 + //Other areas can't detect our attack zone + //其他区域不能检测到我们的攻击区域 _attackArea.Monitorable = false; _attackArea.BodyEntered += EnterTheAttackArea; _attackArea.BodyExited += ExitTheAttackArea; @@ -83,12 +84,12 @@ public partial class AiCharacter : CharacterTemplate // _behaviorNode.Root = patrolBehaviorTree.Root; } - protected virtual void EnterTheAttackArea(Node node) + private void EnterTheAttackArea(Node node) { _nodesInTheAttackRange?.Add(node); } - protected virtual void ExitTheAttackArea(Node node) + private void ExitTheAttackArea(Node node) { _nodesInTheAttackRange?.Remove(node); } diff --git a/scripts/character/CharacterTemplate.cs b/scripts/character/CharacterTemplate.cs index a9910a7..019b65a 100644 --- a/scripts/character/CharacterTemplate.cs +++ b/scripts/character/CharacterTemplate.cs @@ -6,7 +6,6 @@ using ColdMint.scripts.damage; using ColdMint.scripts.debug; using ColdMint.scripts.health; using ColdMint.scripts.inventory; -using ColdMint.scripts.map.events; using ColdMint.scripts.utils; using ColdMint.scripts.weapon; using Godot; @@ -259,16 +258,18 @@ public partial class CharacterTemplate : CharacterBody2D /// public bool PickItem(Node2D? pickAbleItem) { - if (pickAbleItem == null) + //Empty reference checking is implicitly performed here. + //此处隐式的执行了空引用检查。 + if (pickAbleItem is not IItem item) { return false; } - + if (_itemContainer == null) { return false; } - + //Get the currently selected node //拿到当前选择的节点 var itemSlotNode = _itemContainer.GetSelectItemSlotNode(); @@ -277,11 +278,6 @@ public partial class CharacterTemplate : CharacterBody2D return false; } - if (pickAbleItem is not IItem item) - { - return false; - } - //First check if we can pick up the item. //先检查我们能否拾起此物品。 var canPick = _itemContainer.CanAddItem(item); @@ -368,7 +364,7 @@ public partial class CharacterTemplate : CharacterBody2D /// Update the role's health bar /// 更新角色的健康条 /// - private void UpDataHealthBar(DamageTemplate damageTemplate) + private void UpDataHealthBar() { if (_healthBar == null) { @@ -441,7 +437,7 @@ public partial class CharacterTemplate : CharacterBody2D return true; } - UpDataHealthBar(damageTemplate); + UpDataHealthBar(); return true; } diff --git a/scripts/inventory/HotBar.cs b/scripts/inventory/HotBar.cs index af07b7f..6dff720 100644 --- a/scripts/inventory/HotBar.cs +++ b/scripts/inventory/HotBar.cs @@ -79,7 +79,7 @@ public partial class HotBar : HBoxContainer, IItemContainer { _selectIndex = count - 1; } - + SelectItemSlot(oldSelectIndex, _selectIndex); } @@ -223,6 +223,11 @@ public partial class HotBar : HBoxContainer, IItemContainer /// private void SelectItemSlot(int oldSelectIndex, int newSelectIndex) { + if (oldSelectIndex == newSelectIndex) + { + return; + } + if (_itemSlotNodes == null) { return; @@ -286,10 +291,7 @@ public partial class HotBar : HBoxContainer, IItemContainer { return false; } - else - { - return itemSlotNode.SetItem(item); - } + return itemSlotNode.SetItem(item); } public ItemSlotNode? GetSelectItemSlotNode()