Add weapon acquisition and swapping systems with data handling
This commit is contained in:
6
GameCore/Combat/Interfaces/IWeaponDataService.cs
Normal file
6
GameCore/Combat/Interfaces/IWeaponDataService.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace GameCore.Combat.Interfaces;
|
||||
|
||||
public interface IWeaponDataService
|
||||
{
|
||||
bool TryGetWeaponData(string weaponId, out WeaponData weaponData);
|
||||
}
|
||||
39
GameCore/Combat/WeaponAcquisitionSystem.cs
Normal file
39
GameCore/Combat/WeaponAcquisitionSystem.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using GameCore.ECS;
|
||||
using GameCore.ECS.Interfaces;
|
||||
using GameCore.Events;
|
||||
using GameCore.Player;
|
||||
|
||||
namespace GameCore.Combat;
|
||||
|
||||
public class WeaponAcquisitionSystem : ISystem
|
||||
{
|
||||
private const string WeaponItemPrefix = "weapon_";
|
||||
private readonly World _world;
|
||||
|
||||
public WeaponAcquisitionSystem(World world)
|
||||
{
|
||||
_world = world;
|
||||
_world.Subscribe<AddItemToInventoryEvent>(OnItemAdded);
|
||||
}
|
||||
|
||||
public void Update(World world, float deltaTime)
|
||||
{
|
||||
}
|
||||
|
||||
private void OnItemAdded(AddItemToInventoryEvent e)
|
||||
{
|
||||
if (!e.Item.ItemId.StartsWith(WeaponItemPrefix)) return;
|
||||
|
||||
var equipment = _world.GetComponent<EquipmentComponent>(e.Target);
|
||||
if (equipment == null) return;
|
||||
|
||||
if (!equipment.EquippableWeaponItemIds.Contains(e.Item.ItemId))
|
||||
equipment.EquippableWeaponItemIds.Add(e.Item.ItemId);
|
||||
|
||||
if (equipment.CurrentWeaponIndex == -1)
|
||||
{
|
||||
equipment.CurrentWeaponIndex = equipment.EquippableWeaponItemIds.Count - 1;
|
||||
_world.PublishEvent(new EquipWeaponEvent(e.Target, e.Item.ItemId));
|
||||
}
|
||||
}
|
||||
}
|
||||
15
GameCore/Combat/WeaponData.cs
Normal file
15
GameCore/Combat/WeaponData.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using GameCore.Combat.Interfaces;
|
||||
|
||||
namespace GameCore.Combat;
|
||||
|
||||
public class WeaponData(
|
||||
float fireRate,
|
||||
List<ICostEffect> fireCosts,
|
||||
List<IEffect> onFireEffects,
|
||||
List<IEffect> onHitEffects)
|
||||
{
|
||||
public float FireRate { get; set; } = fireRate;
|
||||
public List<ICostEffect> FireCosts { get; set; } = fireCosts;
|
||||
public List<IEffect> OnFireEffects { get; set; } = onFireEffects;
|
||||
public List<IEffect> OnHitEffects { get; set; } = onHitEffects;
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,7 @@ public class WeaponSystem : ISystem
|
||||
if (!cost.CanAfford(context))
|
||||
{
|
||||
canFire = false;
|
||||
//TODO: Publish an event or notify the player they can't fire
|
||||
world.PublishEvent(new WeaponFireFailedEvent());
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user