using System; using System.Collections; using System.Collections.Generic; using System.Linq; using BITFALL.Entities.Inventory; using BITKit; using BITKit.Entities.Player; using UnityEngine; namespace BITFALL.Trading { public class WorldTrader : MonoBehaviour,ITrader { public static int GlobalCredit { get => _GlobalCredit; set => _GlobalCredit = Mathf.Clamp(value, 0, 5000); } private static int _GlobalCredit; public int Credit { get => GlobalCredit; set => GlobalCredit = value; } public IBasicItem[] Items=> inventory.ToArray(); [SerializeReference,SubclassSelector] private IPlayerService _playerService; [SerializeField] private ScriptableItem[] items; private readonly List inventory = new(); private void Start() { foreach (var x in items) { inventory.Add(x.Clone().As()); } } public void Trade(IBasicItem[] priceItems, IBasicItem[] tradeItems) { if (!_playerService.LocalPlayer) return; if(_playerService.LocalPlayer.TryGetComponent(out var playerInventory) is false) return; var check = priceItems.Sum(x => x.Value); var price = tradeItems.Sum(x => x.Value); GlobalCredit -= price; GlobalCredit += check - price; foreach (var x in priceItems) { playerInventory.Remove(x); } foreach (var x in tradeItems) { inventory.Remove(x); playerInventory.Add(x.Clone().As()) ; } foreach (var x in priceItems) { inventory.Add(x); } } public bool AllowTrade(IBasicItem[] priceItems, IBasicItem[] tradeItems) { var money = priceItems.Sum(x => x.Value); var price = tradeItems.Sum(x => x.Value); return money >= price; } } }