Dropping and picking up

This commit is contained in:
Marco 2025-03-28 19:47:10 +01:00
commit bcd007fa1e
14 changed files with 294 additions and 62 deletions

View file

@ -34,8 +34,6 @@ public partial class EnemyDropsProvider : Node2D
private void DropItem(LootItem item)
{
if (!string.IsNullOrWhiteSpace(item.DropScenePath))
{
GD.Print($"Dropped item: {item.ItemName}");

View file

@ -7,11 +7,13 @@ using Godot.Collections;
namespace Cirno.Scripts.Components.FSM.Enemy;
public partial class EnemyStorageModule : Node2D
public partial class EnemyStorageModule : Node2D, IFSMStorage
{
[Export]
public EnemyFSMProxy Root { get; private set; }
public Node2D RootAsNode => Root;
public EnemyResource EnemyData => Root.EnemyResource;
public Vector2 MovementDirection { get; set; }

View file

@ -0,0 +1,11 @@
using System.Collections.Generic;
using Cirno.Scripts.Resources.Loot;
using Godot;
namespace Cirno.Scripts.Components.FSM;
public interface IFSMStorage
{
public IEnumerable<LootDrop> LootDrops { get; }
public Node2D RootAsNode { get; }
}

View file

@ -0,0 +1,66 @@
using System.Linq;
using Cirno.Scripts.Interactables;
using Godot;
namespace Cirno.Scripts.Components.FSM.Player;
public partial class AutoPickupModule : PlayerArea2DModule
{
private bool _enabled = false;
public bool Enabled
{
get => _enabled;
set
{
if (_enabled == value) return;
_enabled = value;
// if (_enabled)
// {
// EmitSignal(SignalName.InteractionStarted);
// }
}
}
public override void Init(IStateMachine<PlayerState, CharacterBody2D> machine)
{
base.Init(machine);
}
public override void EnterState(PlayerState state)
{
Enabled = true;
this.AreaEntered += _on_area_entered;
}
public override void ExitState(PlayerState state)
{
Enabled = false;
this.AreaEntered -= _on_area_entered;
}
public override void Process(double delta)
{
}
public override void PhysicsProcess(double delta)
{
}
private void _on_area_entered(Area2D area)
{
if (!Enabled) return;
if (area is ItemPickup { AutoPickup: true } itemPickup)
{
//Check if items are not maxed to avoid a looping autopickup situation
var canAdd = itemPickup.LootTable.Aggregate(false, (current, item) => current || InventoryManager.Instance.CanAddItem(item.ItemKey));
if (canAdd)
{
itemPickup.Collect();
}
}
}
}

View file

@ -0,0 +1 @@
uid://c8f4tbcjcfsu1

View file

@ -0,0 +1,75 @@
using Cirno.Scripts.Components.Actors;
using Cirno.Scripts.Interactables;
using Cirno.Scripts.Resources;
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Components.FSM.Player;
public partial class DeathItemDropper : Node2D
{
[Export] public PlayerStorageModule Storage { get; private set; }
[Export] public ActorResourceProvider HealthProvider { get; private set; }
[Export] public Array<LootItem> ItemsToDrop { get; private set; } = [];
[Export(PropertyHint.None, "suffix:%")] public float PercentageToDrop { get; private set; } = 50f;
[Export(PropertyHint.None, "suffix:%")] public float PercentageToDelete { get; private set; } = 50f;
private readonly StringName _dropPhysicsWrapperScene = "uid://d3hds3dbosfcm";
[Export] public float DropRadius { get; private set; } = 8f;
[Export] public float DropSpeed { get; set; } = 40f;
private PackedScene _loadedDropScene;
public override void _Ready()
{
HealthProvider.ResourceDepleted += HealthProviderOnResourceDepleted;
_loadedDropScene = GD.Load<PackedScene>(_dropPhysicsWrapperScene);
}
public override void _ExitTree()
{
HealthProvider.ResourceDepleted -= HealthProviderOnResourceDepleted;
}
private void HealthProviderOnResourceDepleted()
{
foreach (var item in ItemsToDrop)
{
if (InventoryManager.Instance.TryGetItem(item.ItemKey, out var itm))
{
var amountToDrop = Mathf.RoundToInt(itm.Count * PercentageToDrop / 100f);
var removedAmount = InventoryManager.Instance.RemoveItem(item.ItemKey, amountToDrop);
var toDelete = Mathf.RoundToInt(removedAmount * PercentageToDelete / 100f);
DropItem(item, removedAmount - toDelete);
}
}
}
private void DropItem(LootItem item, int amountToDrop)
{
if (!string.IsNullOrWhiteSpace(item.DropScenePath))
{
for (int i = 0; i < amountToDrop; i++)
{
GD.Print($"Dropped item: {item.ItemName}");
var dropInstance = Storage.Root.CreateSibling<ItemDrop>(_loadedDropScene);
dropInstance.ItemToDrop = item;
dropInstance.StartingSpeed = DropSpeed;
}
}
else
{
GD.Print($"Skipping Item with missing path: {item.ItemName}");
}
}
}

View file

@ -0,0 +1 @@
uid://bcpn47j3lr1tg

View file

@ -4,5 +4,9 @@ namespace Cirno.Scripts.Components.FSM.Player;
public partial class PlayerStorageModule : Node2D
{
[Export]
public PlayerFSMProxy Root { get; private set; }
public Node2D RootAsNode => Root;
public Vector2 FacingDirection { get; set; }
}