You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.1 KiB

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; }
/// <summary>
/// method call when a drag enter a slot and send an action
/// </summary>
internal void OnDragEnter()
{
if (NoDrop)
{
return;
}
Parent.Actions.Add(new InventoryAction { Action = "Drag Enter", Item = this.Item, Index = this.Index });
}
/// <summary>
/// method call when a drag leave a slot and send an action
/// </summary>
internal void OnDragLeave()
{
if (NoDrop)
{
return;
}
Parent.Actions.Add(new InventoryAction { Action = "Drag Leave", Item = this.Item, Index = this.Index });
}
/// <summary>
/// method that manage a drop and send an action
/// </summary>
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 });
}
/// <summary>
/// method call when darg start and send an action
/// </summary>
private void OnDragStart()
{
Parent.CurrentDragItem = this.Item;
Parent.Actions.Add(new InventoryAction { Action = "Drag Start", Item = this.Item, Index = this.Index });
}
/// <summary>
/// method call when drag end and send an action specialy when outside the inventory
/// </summary>
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);
}
}
}