using Microsoft.AspNetCore.Components; using System.Text.Json; using ValblazeProject.Factories; using ValblazeProject.Models; using ValblazeProject.Pages; namespace ValblazeProject.Components { public partial class InventoryItem { [Parameter] public int Index { get; set; } [Parameter] public Item Item { get; set; } [Parameter] public bool NoDrop { get; set; } [CascadingParameter] public Inventaire Parent { get; set; } /// /// method call when a drag enter a slot and send an action /// internal void OnDragEnter() { if (NoDrop) { return; } Parent.Actions.Add(new InventoryAction { Action = "Drag Enter", Item = this.Item, Index = this.Index }); } /// /// method call when a drag leave a slot and send an action /// internal void OnDragLeave() { if (NoDrop) { return; } Parent.Actions.Add(new InventoryAction { Action = "Drag Leave", Item = this.Item, Index = this.Index }); } /// /// method that manage a drop and send an action /// internal void OnDrop() { if (NoDrop == true || Parent.CurrentDragItem == null) { return; } if (this.Item == null) { this.Item = ItemFactory.Create(Parent.CurrentDragItem); } else if (this.Item.Id == Parent.CurrentDragItem.Id) { if (this.Item.StackSize > this.Item.Num) { ItemFactory.Add1(this.Item, this.Item); } } Parent.Jitems[this.Index] = this.Item; string fileName = "Inventory.json"; string jsonString = JsonSerializer.Serialize(Parent.Jitems); File.WriteAllText(fileName, jsonString); Parent.Actions.Add(new InventoryAction { Action = "Drop", Item = this.Item, Index = this.Index }); } /// /// method call when darg start and send an action /// private void OnDragStart() { Parent.CurrentDragItem = this.Item; Parent.Actions.Add(new InventoryAction { Action = "Drag Start", Item = this.Item, Index = this.Index }); } /// /// method call when drag end and send an action specialy when outside the inventory /// private void OnDragEnd() { if (Parent.Actions.Last().Action == "Drag Leave") { this.Item = null; } Parent.Actions.Add(new InventoryAction { Action = "Delete", Item = this.Item, Index = this.Index }); Parent.Jitems[this.Index] = null; string fileName = "Inventory.json"; string jsonString = JsonSerializer.Serialize(Parent.Jitems); File.WriteAllText(fileName, jsonString); } } }