Skip to content

Commit

Permalink
#6: Update to fix only equipable weapon show up under weapon choices …
Browse files Browse the repository at this point in the history
…and bot will not stuck
  • Loading branch information
Danil Ko committed Mar 14, 2021
1 parent a11b3b9 commit 4e0a444
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 40 deletions.
4 changes: 0 additions & 4 deletions agents/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ public class Agent : KinematicBody2D

public int RightWeaponAction { set; get; }
public int LeftWeaponAction { set; get; }
public bool RightWeaponReloading { set; get; }
public bool LeftWeaponReloading { set; get; }

protected Godot.Collections.Array<Weapon> RightWeapons = new Godot.Collections.Array<Weapon>();
protected Godot.Collections.Array<Weapon> LeftWeapons = new Godot.Collections.Array<Weapon>();

Expand Down Expand Up @@ -498,7 +495,6 @@ public void DamageEffectTimerTimeout()
public void ReloadWeapon(Weapon.WeaponOrder weaponOrder)
{
((Weapon)GetWeapons(weaponOrder)[CurrentWeaponIndex[weaponOrder]]).StartReload();
RightWeaponReloading = false;
}

public void Heal(int amount)
Expand Down
73 changes: 45 additions & 28 deletions ai/AI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public void Control(float delta)
break;

case State.ENGAGE:
if (IsInstanceValid(_targetAgent))
if (_targetAgent != null && IsInstanceValid(_targetAgent))
{
_agent.RotateToward(_targetAgent.GlobalPosition, delta);

Expand All @@ -193,6 +193,10 @@ public void Control(float delta)
_agent.MoveToward(_agent.GlobalPosition.DirectionTo(_targetAgent.GlobalPosition), delta);
}
}
else
{
_checkTarget();
}
break;

case State.ADVANCE:
Expand Down Expand Up @@ -289,40 +293,53 @@ private void _onDetectionZoneBodyExited(Node body)
}
}

Godot.Collections.Array<String> removeTargetList = new Godot.Collections.Array<String>();

foreach (String targetAgentUnitName in _targetAgents.Keys)
// If target is not vaild, then will try to check for next target
if (_targetAgent == null || IsInstanceValid(_targetAgent))
{
Agent targetAgent = _gameWorld.GetTeamMapAIs()[(int)_targetAgents[targetAgentUnitName]].GetUnit(targetAgentUnitName);

if (targetAgent != null && IsInstanceValid(targetAgent))
{
_targetAgent = targetAgent;
}
else
{
// Remove this target from list as it is no longer valid
removeTargetList.Add(targetAgentUnitName);
}
// Compute target
_checkTarget();
}
}
}

private void _checkTarget()
{
_targetAgent = null;

Godot.Collections.Array<String> removeTargetList = new Godot.Collections.Array<String>();

foreach (String targetAgentUnitName in _targetAgents.Keys)
{
Agent targetAgent = _gameWorld.GetTeamMapAIs()[(int)_targetAgents[targetAgentUnitName]].GetUnit(targetAgentUnitName);

foreach (String targetAgentUnitName in removeTargetList)
if (targetAgent != null && IsInstanceValid(targetAgent))
{
_targetAgents.Remove(targetAgentUnitName);
SetState(State.ENGAGE);
_targetAgent = targetAgent;
}
else
{
// Remove this target from list as it is no longer valid
removeTargetList.Add(targetAgentUnitName);
}
}

// If no possible target, then set to ADVANCE
if (_targetAgent == null)
foreach (String targetAgentUnitName in removeTargetList)
{
_targetAgents.Remove(targetAgentUnitName);
}

// If no possible target, then set to ADVANCE
if (_targetAgent == null)
{
// Set advance if next target is available
if (_nextBasePosition != Vector2.Zero)
{
// Set advance if next target is available
if (_nextBasePosition != Vector2.Zero)
{
SetState(State.ADVANCE);
}
else
{
SetState(State.PATROL);
}
SetState(State.ADVANCE);
}
else
{
SetState(State.PATROL);
}
}
}
Expand Down
6 changes: 0 additions & 6 deletions inventory/Inventory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,6 @@ public void EquipItem(int itemIndex, Weapon.WeaponOrder weaponOrder, int weaponI
_usedIndex.Add(itemIndex);
_agent.EquipWeapon(_items[itemIndex].ReferencePackedScene, weaponOrder, weaponIndex);

if (weaponOrder == Weapon.WeaponOrder.Left && weaponIndex == 1)
{
GD.Print("EQUIP " + _agent.GetUnitName() + " " + weaponOrder + weaponIndex + (_agent.GetWeapons(weaponOrder)[weaponIndex] != null));

}

EmitSignal(nameof(WeaponChangeSignal), weaponOrder, weaponIndex);
EmitSignal(nameof(InventoryChangeSignal));
}
Expand Down
2 changes: 1 addition & 1 deletion ui/InventoryUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private void _populateWeaponChoices(Weapon.WeaponOrder weaponOrder, int weaponIn
int inventoryItemIndex = 0;
foreach (ItemResource itemResource in _inventory.GetItems())
{
if (itemResource != null)
if (itemResource != null && itemResource.CurrentItemType == ItemResource.ItemType.EQUIPABLE)
{
ItemPanel panel = (ItemPanel)_itemPanel.Duplicate();

Expand Down
2 changes: 1 addition & 1 deletion ui/TitleScreen.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ margin_left = 168.411
margin_top = 111.168
margin_right = 242.411
margin_bottom = 135.168
value = 5.0
value = 10.0
__meta__ = {
"_edit_use_anchors_": false
}
Expand Down

0 comments on commit 4e0a444

Please sign in to comment.