Add weapon acquisition and swapping systems with data handling
This commit is contained in:
50
GameCore/Combat/WeaponSwapSystem.cs
Normal file
50
GameCore/Combat/WeaponSwapSystem.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using GameCore.ECS;
|
||||
using GameCore.ECS.Interfaces;
|
||||
using GameCore.Events;
|
||||
using GameCore.Input;
|
||||
using GameCore.Player;
|
||||
|
||||
namespace GameCore.Combat;
|
||||
|
||||
public class WeaponSwapSystem : ISystem
|
||||
{
|
||||
public void Update(World world, float deltaTime)
|
||||
{
|
||||
var entities = world.GetEntitiesWith<InputStateComponent>();
|
||||
foreach (var entity in entities)
|
||||
{
|
||||
var input = world.GetComponent<InputStateComponent>(entity);
|
||||
var equipment = world.GetComponent<EquipmentComponent>(entity);
|
||||
|
||||
if (input == null || equipment == null || equipment.EquippableWeaponItemIds.Count <= 1)
|
||||
continue;
|
||||
|
||||
var direction = SwapDirection.None;
|
||||
if (input.IsSwapWeaponNext) direction = SwapDirection.Next;
|
||||
if (input.IsSwapWeaponPrevious) direction = SwapDirection.Previous;
|
||||
|
||||
if (direction != SwapDirection.None)
|
||||
{
|
||||
var newIndex = equipment.CurrentWeaponIndex + (int)direction;
|
||||
var weaponCount = equipment.EquippableWeaponItemIds.Count;
|
||||
|
||||
if (newIndex >= weaponCount) newIndex = 0;
|
||||
if (newIndex < 0) newIndex = weaponCount - 1;
|
||||
|
||||
if (newIndex != equipment.CurrentWeaponIndex)
|
||||
{
|
||||
equipment.CurrentWeaponIndex = newIndex;
|
||||
var newWeaponId = equipment.EquippableWeaponItemIds[newIndex];
|
||||
world.PublishEvent(new EquipWeaponEvent(entity, newWeaponId));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private enum SwapDirection
|
||||
{
|
||||
None = 0,
|
||||
Next = 1,
|
||||
Previous = -1
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user