246 lines
9.2 KiB
C#
246 lines
9.2 KiB
C#
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using GameCult.Aetheria.State.Unity;
|
|
using UnityEngine;
|
|
using UniRx;
|
|
using UniRx.Triggers;
|
|
using Unity.Mathematics;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.UI;
|
|
|
|
public class InventoryMenu : MonoBehaviour
|
|
{
|
|
public InventoryPanel[] InventoryPanels;
|
|
public PropertiesPanel PropertiesPanel;
|
|
public ActionGameManager GameManager;
|
|
public RectTransform DragParent;
|
|
public ConfirmationDialog Dialog;
|
|
// public ClickCatcher Background;
|
|
|
|
private int _currentPanel = -1;
|
|
|
|
private int2 _selectedPosition;
|
|
private InventoryPanel _selectedPanel;
|
|
private ItemInstance _selectedItem;
|
|
private int2[] _selectedCells;
|
|
// private List<IDisposable> _backgroundSubscriptions;
|
|
|
|
// private ItemInstance _dragItem;
|
|
// private Transform[] _dragCells;
|
|
// private Vector2[] _dragOffsets;
|
|
// private int2 _dragCellOffset;
|
|
// private ItemRotation _originalRotation;
|
|
// //private Shape _previousFakeOccupancy;
|
|
// private Shape _originalOccupancy;
|
|
// private EquippedItem _originalEquippedItem;
|
|
// private InventoryPanel _originalPanel;
|
|
//
|
|
// private InventoryPanel _dragTargetPanel;
|
|
// private int2 _dragTargetPosition;
|
|
// private int2 _lastDragPosition;
|
|
// private bool _dragTargetValid;
|
|
// private bool _destroyItem;
|
|
|
|
private void OnEnable()
|
|
{
|
|
// Background.gameObject.SetActive(true);
|
|
|
|
// Background.OnEnter.Subscribe(enter =>
|
|
// {
|
|
// _destroyItem = true;
|
|
// });
|
|
// BackgroundExited.OnPointerExitAsObservable().Subscribe(enter =>
|
|
// {
|
|
// _destroyItem = false;
|
|
// });
|
|
var cargo = GameManager.DockingBay ?? GameManager.CurrentEntity.CargoBays.FirstOrDefault();
|
|
if (cargo!=null)
|
|
InventoryPanels[0].Display(cargo);
|
|
else InventoryPanels[0].Clear();
|
|
if(GameManager.CurrentEntity != null)
|
|
InventoryPanels[1].Display(GameManager.CurrentEntity);
|
|
else InventoryPanels[1].Clear();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
// Background.gameObject.SetActive(false);
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
PropertiesPanel.GameManager = GameManager;
|
|
foreach (var panel in InventoryPanels)
|
|
{
|
|
panel.OnClickAsObservable().Subscribe(e =>
|
|
{
|
|
if (e.data is InventoryCargoEventData cargoEvent)
|
|
{
|
|
var item = cargoEvent.CargoBay.Occupancy[cargoEvent.Position.x, cargoEvent.Position.y];
|
|
if(item!=null)
|
|
{
|
|
if (e.clickCount == 2)
|
|
{
|
|
var otherPanel = panel == InventoryPanels[0] ? InventoryPanels[1] : InventoryPanels[0];
|
|
if (CommitCargoItemTransfer(cargoEvent.CargoBay, otherPanel, item))
|
|
{
|
|
// TODO: SFX: Equip
|
|
panel.RefreshCells();
|
|
otherPanel.RefreshCells();
|
|
}
|
|
// else
|
|
// TODO: SFX: Fail
|
|
}
|
|
else
|
|
{
|
|
ClearSelectedCellHighlight();
|
|
_selectedPanel = panel;
|
|
_selectedPosition = cargoEvent.Position;
|
|
PropertiesPanel.Inspect(item);
|
|
_selectedPanel = panel;
|
|
_selectedPosition = cargoEvent.CargoBay.Cargo[item];
|
|
_selectedItem = item;
|
|
_selectedCells = GetSelectedCells(item, _selectedPosition);
|
|
ApplySelectedCellHighlight();
|
|
// TODO: SFX: Success
|
|
}
|
|
}
|
|
}
|
|
else if (e.data is InventoryEntityEventData entityEvent)
|
|
{
|
|
var item = entityEvent.Entity.GearOccupancy[entityEvent.Position.x, entityEvent.Position.y];
|
|
if (item != null)
|
|
{
|
|
if (e.clickCount == 2)
|
|
{
|
|
var otherPanel = panel == InventoryPanels[0] ? InventoryPanels[1] : InventoryPanels[0];
|
|
if (CommitEquippedItemTransfer(entityEvent.Entity, item, otherPanel))
|
|
{
|
|
// TODO: SFX: Unequip
|
|
panel.RefreshCells();
|
|
otherPanel.RefreshCells();
|
|
// else
|
|
// TODO: SFX: Fail
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ClearSelectedCellHighlight();
|
|
|
|
PropertiesPanel.Inspect(item);
|
|
_selectedPanel = panel;
|
|
_selectedPosition = item.Position;
|
|
_selectedItem = item.EquippableItem;
|
|
_selectedCells = GetSelectedCells(item.EquippableItem, _selectedPosition);
|
|
ApplySelectedCellHighlight();
|
|
// TODO: SFX: Success
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
panel.OnBackgroundClick.Subscribe(data =>
|
|
{
|
|
if (GameManager.CurrentEntity == null) return; // Only show ship settings if there's a ship, duh!
|
|
|
|
PropertiesPanel.Clear();
|
|
PropertiesPanel.AddField("Shutdown Threshold",
|
|
() => GameManager.CurrentEntity.Settings.ShutdownPerformance,
|
|
f => GameManager.CommitEntityShutdownPerformance(GameManager.CurrentEntity, f),
|
|
0,
|
|
1);
|
|
});
|
|
}
|
|
}
|
|
|
|
private bool CommitCargoItemTransfer(EquippedCargoBay origin, InventoryPanel destination, ItemInstance item)
|
|
{
|
|
if (destination.DisplayedEntity != null && item is EquippableItem equippableItem)
|
|
return GameManager.CommitCargoItemEquip(origin, destination.DisplayedEntity, equippableItem);
|
|
|
|
if (destination.DisplayedCargo != null)
|
|
return GameManager.CommitCargoItemTransfer(origin, destination.DisplayedCargo, item);
|
|
|
|
return false;
|
|
}
|
|
|
|
private bool CommitEquippedItemTransfer(Entity origin, EquippedItem item, InventoryPanel destination)
|
|
{
|
|
if (destination.DisplayedCargo != null)
|
|
return GameManager.CommitEquippedItemStore(origin, item, destination.DisplayedCargo);
|
|
|
|
return false;
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
// if (Keyboard.current.qKey.wasPressedThisFrame && _dragItem != null)
|
|
// {
|
|
// _dragItem.Rotation = (ItemRotation) (((int) _dragItem.Rotation + 1) % 4);
|
|
// }
|
|
// if (Keyboard.current.eKey.wasPressedThisFrame && _dragItem != null)
|
|
// {
|
|
// _dragItem.Rotation = (ItemRotation) (((int) _dragItem.Rotation + 3) % 4);
|
|
// }
|
|
}
|
|
|
|
private void ClearSelectedCellHighlight()
|
|
{
|
|
if (_selectedPanel == null || _selectedCells == null) return;
|
|
|
|
foreach (var v in _selectedCells)
|
|
{
|
|
if (_selectedPanel.CellInstances.ContainsKey(v))
|
|
_selectedPanel.CellInstances[v].Icon.color = _selectedPanel.GetColor(v);
|
|
}
|
|
}
|
|
|
|
private void ApplySelectedCellHighlight()
|
|
{
|
|
if (_selectedPanel == null || _selectedCells == null) return;
|
|
|
|
foreach (var v in _selectedCells)
|
|
{
|
|
if (_selectedPanel.CellInstances.ContainsKey(v))
|
|
_selectedPanel.CellInstances[v].Icon.color = _selectedPanel.GetColor(v, true);
|
|
}
|
|
}
|
|
|
|
private int2[] GetSelectedCells(ItemInstance item, int2 position)
|
|
{
|
|
var typedItem = FindTypedInventoryItem(item);
|
|
if (typedItem != null && typedItem.ShapeCells.Count > 0)
|
|
{
|
|
return typedItem.ShapeCells
|
|
.Select(cell => RotateTypedShapeCell(cell, typedItem, item.Rotation) + position)
|
|
.ToArray();
|
|
}
|
|
|
|
return Array.Empty<int2>();
|
|
}
|
|
|
|
private static AetheriaRuntimeCatalogItem FindTypedInventoryItem(ItemInstance item)
|
|
{
|
|
return ActionGameManager.RuntimeCatalog?.FindItem(item?.ItemKey ?? "");
|
|
}
|
|
|
|
private static int2 RotateTypedShapeCell(
|
|
AetheriaRuntimeShapeCell cell,
|
|
AetheriaRuntimeCatalogItem item,
|
|
ItemRotation rotation)
|
|
{
|
|
return rotation switch
|
|
{
|
|
ItemRotation.Clockwise => new int2(cell.Y, item.ShapeWidth - 1 - cell.X),
|
|
ItemRotation.Reversed => new int2(item.ShapeWidth - 1 - cell.X, item.ShapeHeight - 1 - cell.Y),
|
|
ItemRotation.CounterClockwise => new int2(item.ShapeHeight - 1 - cell.Y, cell.X),
|
|
_ => new int2(cell.X, cell.Y)
|
|
};
|
|
}
|
|
}
|