Drag & Drop Inventory

Implemented the ability to drag items within and between inventories, including equipping them onto entity hardpoints
This commit is contained in:
Reinier Vens 2020-12-20 03:04:52 +00:00
commit e3f3bd5de5
21 changed files with 2029 additions and 1389 deletions

View file

@ -118,13 +118,24 @@ public class ActionGameManager : MonoBehaviour
var dockingBayData = ItemData.GetAll<DockingBayData>().First();
var dockingBay = ItemManager.CreateInstance(dockingBayData, 1, 1) as EquippableItem;
station.TryEquip(dockingBay);
var dockingCargo = station.DockingBays.First();
var thrusterData = ItemData.GetAll<GearData>().First(x => x.HardpointType == HardpointType.Thruster && x.Shape.Height == 1);
for (int i = 0; i < 8; i++)
{
dockingCargo.TryStore(ItemManager.CreateInstance(thrusterData, .5f, 1));
}
var reactorData = ItemData.GetAll<GearData>().First(x => x.HardpointType == HardpointType.Reactor && x.Shape.Height == 2);
dockingCargo.TryStore(ItemManager.CreateInstance(reactorData, .5f, 1));
var hullType = ItemData.GetAll<HullData>().First(x=>x.HullType==HullType.Ship);
var shipHull = ItemManager.CreateInstance(hullType, 0, 1) as EquippableItem;
var ship = new Ship(ItemManager, Zone, shipHull);
PlayerShips.Add(ship);
_currentShip = ship;
station.TryDock(ship);
Dock();
// MapButton.onClick.AddListener(() =>
// {
@ -137,6 +148,23 @@ public class ActionGameManager : MonoBehaviour
//InventoryPanel.Display(ItemManager, entity);
}
public void Dock()
{
foreach (var entity in Zone.Entities)
{
if (lengthsq(entity.Position - _currentShip.Position) <
Settings.GameplaySettings.DockingDistance * Settings.GameplaySettings.DockingDistance)
{
var bay = entity.TryDock(_currentShip);
if (bay != null)
{
Inventory.GetPanel.Display(bay);
Inventory.GetPanel.Display(_currentShip);
}
}
}
}
public void SaveZone() => File.WriteAllBytes(
Path.Combine(_filePath.FullName, $"{Zone.Data.Name}.zone"), MessagePackSerializer.Serialize(Zone.Pack()));

View file

@ -14,7 +14,7 @@ namespace UniRx.Triggers
void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
{
if (onPointerClick != null) onPointerClick.OnNext(eventData);
onPointerClick?.OnNext(eventData);
}
public IObservable<PointerEventData> OnPointerClickAsObservable()

View file

@ -292,7 +292,7 @@ public class SectorRenderer : MonoBehaviour
var planetInstance = _zone.PlanetInstances[planet.Key];
var p = _zone.GetOrbitPosition(planetInstance.BodyData.Orbit);
planet.Value.transform.position = new Vector3(p.x, 0, p.y);
planet.Value.Body.transform.position = new Vector3(0, _zone.GetHeight(p) + planetInstance.BodyRadius.Value * 2, 0);
planet.Value.Body.transform.localPosition = new Vector3(0, _zone.GetHeight(p) + planetInstance.BodyRadius.Value * 2, 0);
if(planet.Value is GasGiantObject gasGiantObject)
{
gasGiantObject.GravityWaves.material.SetFloat("_Phase", Time * Settings.PlanetSettings.WaveSpeed.Evaluate(planetInstance.BodyData.Mass.Value));

View file

@ -191,12 +191,12 @@ public abstract class Entity
Equipment.Remove(item);
var hullData = ItemManager.GetData(Hull) as HullData;
var itemData = ItemManager.GetData(item.EquippableItem);
foreach (var i in itemData.Shape.Coordinates)
{
var itemCoord = item.Position + itemData.Shape.Rotate(i, item.EquippableItem.Rotation);
(itemData.HardpointType == HardpointType.Thermal ? ThermalOccupancy : GearOccupancy)[itemCoord.x, itemCoord.y] = null;
}
var itemLayer = (itemData.HardpointType == HardpointType.Thermal ? ThermalOccupancy : GearOccupancy);
foreach (var i in hullData.Shape.Coordinates)
if (itemLayer[i.x, i.y] == item)
itemLayer[i.x, i.y] = null;
return item.EquippableItem;
}
@ -204,6 +204,9 @@ public abstract class Entity
// Check whether the given item will fit when its origin is placed at the given coordinate
private bool ItemFits(EquippableItemData itemData, HullData hullData, EquippableItem item, int2 hullCoord)
{
// If the given coordinate isn't even in the ship it obviously won't fit
if (!hullData.Shape[hullCoord]) return false;
// Items without specific hardpoints on the ship can be freely rotated and placed anywhere
if (itemData.HardpointType == HardpointType.Tool || itemData.HardpointType == HardpointType.Thermal)
{
@ -226,16 +229,21 @@ public abstract class Entity
// If the hardpoint type doesn't match the item, it won't fit
if (hardpoint.Type != itemData.HardpointType) return false;
// Items placed in hardpoints are automatically aligned to hardpoint rotation
item.Rotation = hardpoint.Rotation;
// Inset the shapes of both item and hardpoint
var itemShapeInset = hullData.Shape.Inset(itemData.Shape, hullCoord, item.Rotation);
var hardpointShapeInset = hullData.Shape.Inset(hardpoint.Shape, hardpoint.Position);
// Check every cell of the item's shape
foreach (var i in itemData.Shape.Coordinates)
foreach (var i in itemShapeInset.Coordinates)
{
// If the hardpoint does not have a matching cell, it wont fit
if (!hardpoint.Shape[i]) return false;
if (!hardpointShapeInset[i]) return false;
// If there is any gear already occupying that space, it won't fit
// Items placed in hardpoints are automatically aligned to hardpoint rotation
var itemCoord = hardpoint.Position + itemData.Shape.Rotate(i, hardpoint.Rotation);
if (GearOccupancy[itemCoord.x, itemCoord.y] != null) return false;
if (GearOccupancy[i.x, i.y] != null) return false;
}
}
@ -343,7 +351,7 @@ public abstract class Entity
foreach (var i in itemData.Shape.Coordinates)
{
var occupiedCoord = hullCoord + i;
var occupiedCoord = hullCoord + itemData.Shape.Rotate(i, item.Rotation);
(itemData.HardpointType == HardpointType.Thermal ? ThermalOccupancy : GearOccupancy)[occupiedCoord.x, occupiedCoord.y] = equippedItem;
}
@ -589,7 +597,7 @@ public class EquippedCargoBay : EquippedItem
}
// Check whether the given item will fit when its origin is placed at the given coordinate
private bool ItemFits(ItemInstance item, int2 cargoCoord)
public bool ItemFits(ItemInstance item, int2 cargoCoord)
{
var itemData = _itemManager.GetData(item);
// Check every cell of the item's shape
@ -597,7 +605,7 @@ public class EquippedCargoBay : EquippedItem
{
// If there is an item already occupying that space, it won't fit
var itemCargoCoord = cargoCoord + itemData.Shape.Rotate(i, item.Rotation);
if (Occupancy[itemCargoCoord.x, itemCargoCoord.y] != null) return false;
if (!Data.InteriorShape[itemCargoCoord] || (Occupancy[itemCargoCoord.x, itemCargoCoord.y] != null && Occupancy[itemCargoCoord.x, itemCargoCoord.y] != item)) return false;
}
return true;
@ -641,6 +649,15 @@ public class EquippedCargoBay : EquippedItem
return false;
}
public bool TryStore(ItemInstance item, int2 cargoCoord)
{
if (item is SimpleCommodity simpleCommodity)
return TryStore(simpleCommodity, cargoCoord);
if (item is CraftedItemInstance craftedItem)
return TryStore(craftedItem, cargoCoord);
return false;
}
// Attempts to store all of the given item anywhere in the inventory
// Will attempt to fill existing item stacks first
// Returns true only when ALL of the items are successfully stored
@ -751,8 +768,9 @@ public class EquippedCargoBay : EquippedItem
var itemData = _itemManager.GetData(item);
if(quantity >= item.Quantity)
{
foreach (var pos in itemData.Shape.Coordinates.Select(p => Cargo[item] + itemData.Shape.Rotate(p, item.Rotation)))
Occupancy[pos.x, pos.y] = null;
foreach(var v in Data.InteriorShape.Coordinates)
if (Occupancy[v.x, v.y] == item)
Occupancy[v.x, v.y] = null;
Cargo.Remove(item);
ItemsOfType[item.Data].Remove(item);
@ -779,8 +797,9 @@ public class EquippedCargoBay : EquippedItem
return;
}
var itemData = _itemManager.GetData(item);
foreach (var pos in itemData.Shape.Coordinates.Select(p => Cargo[item] + itemData.Shape.Rotate(p, item.Rotation)))
Occupancy[pos.x, pos.y] = null;
foreach(var v in Data.InteriorShape.Coordinates)
if (Occupancy[v.x, v.y] == item)
Occupancy[v.x, v.y] = null;
Cargo.Remove(item);
ItemsOfType[item.Data].Remove(item);
@ -790,6 +809,14 @@ public class EquippedCargoBay : EquippedItem
Mass -= _itemManager.GetMass(item);
ThermalMass -= _itemManager.GetThermalMass(item);
}
public void Remove(ItemInstance item)
{
if (item is SimpleCommodity simpleCommodity)
Remove(simpleCommodity, simpleCommodity.Quantity);
if (item is CraftedItemInstance craftedItem)
Remove(craftedItem);
}
public bool TryTransferItem(EquippedCargoBay target, SimpleCommodity item, int quantity)
{

View file

@ -116,9 +116,10 @@ public class Shape
}
public bool this[int2 pos] {
get { return pos.x >= 0 && pos.y >= 0 && pos.x < Cells.GetLength(0) && pos.y < Cells.GetLength(1) && Cells[pos.x, pos.y]; }
get { return pos.x >= 0 && pos.y >= 0 && pos.x < Width && pos.y < Height && Cells[pos.x, pos.y]; }
set
{
if (pos.x < 0 || pos.y < 0 || pos.x >= Width || pos.y >= Height) return;
_dirty = true;
Cells[pos.x, pos.y] = value;
}
@ -135,6 +136,30 @@ public class Shape
);
return shape;
}
public Shape Inset(Shape inset, int2 insetPosition, ItemRotation rotation = ItemRotation.None)
{
var shape = new Shape(Width, Height);
foreach (var v in inset.Coordinates)
{
var insetCoord = inset.Rotate(v, rotation) + insetPosition;
shape[insetCoord] = true;
}
return shape;
}
public Shape Expand()
{
var shape = new Shape(Width, Height);
foreach (var shapeCoord in AllCoordinates)
shape[shapeCoord] = (
this[shapeCoord + int2(-1,-1)] || this[shapeCoord + int2(0,-1)] || this[shapeCoord + int2(1,-1)] ||
this[shapeCoord + int2(-1,0)] || this[shapeCoord] || this[shapeCoord + int2(1,0)] ||
this[shapeCoord + int2(-1,1)] || this[shapeCoord + int2(0,1)] || this[shapeCoord + int2(1,1)]
);
return shape;
}
}
[MessagePackObject,
@ -336,9 +361,9 @@ public class HardpointData : ITintInspector
{
if (_tintColors == null)
{
var hardpointTypes = ((HardpointType[]) Enum.GetValues(typeof(HardpointType)));
var hardpointTypes = (HardpointType[]) Enum.GetValues(typeof(HardpointType));
_tintColors = hardpointTypes.ToDictionary(x => x,
x => ColorMath.HsvToRgb(float3((float)(int)x/hardpointTypes.Length, 1, 1)));
x => ColorMath.HsvToRgb(float3(frac((float)(int)x/hardpointTypes.Length + .25f), 1, 1)));
}
return _tintColors.ContainsKey(type) ? _tintColors[type] : _tintColors[HardpointType.Hull];

View file

@ -26,6 +26,7 @@ public class ItemManager
private double _time;
private float _deltaTime;
private Dictionary<Guid, Zone> _zones = new Dictionary<Guid, Zone>();
// private Guid _forceLoadZone;
// public GlobalData GlobalData => _globalData ?? (_globalData = ItemData.GetAll<GlobalData>().FirstOrDefault());
@ -48,6 +49,7 @@ public class ItemManager
public ItemManager(DatabaseCache itemData, GameplaySettings settings, Action<string> logger)
{
ItemData = itemData;
GameplaySettings = settings;
_logger = logger;
}

View file

@ -3,9 +3,12 @@ using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UniRx;
using UniRx.Triggers;
using UnityEngine;
using UnityEngine.UI;
using Unity.Mathematics;
using UnityEngine.EventSystems;
using static Unity.Mathematics.math;
using int2 = Unity.Mathematics.int2;
@ -18,10 +21,25 @@ public class InventoryPanel : MonoBehaviour
public GridLayoutGroup Grid;
public Sprite[] NodeTextures;
public Prototype NodePrototype;
Subject<InventoryEventData> _onBeginDrag;
Subject<InventoryEventData> _onDrag;
Subject<InventoryEventData> _onEndDrag;
Subject<InventoryEventData> _onClick;
Subject<InventoryEventData> _onPointerEnter;
Subject<InventoryEventData> _onPointerExit;
private Dictionary<int2, GameObject> _nodeInstances = new Dictionary<int2, GameObject>();
private int2[] _offsets = new[]
{
private Entity _displayedEntity;
private EquippedCargoBay _displayedCargo;
public EquippedItem FakeEquipment;
public ItemInstance FakeItem;
public Shape FakeOccupancy;
public Shape IgnoreOccupancy;
public List<GameObject> EmptyCells = new List<GameObject>();
public Dictionary<int2, InventoryCell> CellInstances = new Dictionary<int2, InventoryCell>();
private int2[] _offsets = {
int2(0, 1),
int2(1, 0),
int2(0, -1),
@ -44,138 +62,308 @@ public class InventoryPanel : MonoBehaviour
ContextMenu.AddDropdown(ship.Name,
ship.CargoBays
.Select<EquippedCargoBay, (string text, Action action, bool enabled)>((bay, index) =>
($"Bay {index}", () => Display(GameManager.ItemManager, bay), true))
.Prepend(("Equipment", () => Display(GameManager.ItemManager, ship), true)));
($"Bay {index}", () => Display(bay), true))
.Prepend(("Equipment", () => Display(ship), true)));
}
else ContextMenu.AddOption(ship.Name, () => Display(GameManager.ItemManager, ship));
else ContextMenu.AddOption(ship.Name, () => Display(ship));
}
foreach (var bay in GameManager.AvailableCargoBays())
{
ContextMenu.AddOption(bay.Name, () => Display(GameManager.ItemManager, bay));
ContextMenu.AddOption(bay.Name, () => Display(bay));
}
ContextMenu.Show();
});
}
public void Display(ItemManager manager, Entity entity)
public void Display(Entity entity)
{
foreach(var node in _nodeInstances.Values)
Destroy(node);
_nodeInstances.Clear();
foreach(var empty in EmptyCells)
Destroy(empty);
EmptyCells.Clear();
foreach(var node in CellInstances.Values)
node.GetComponent<Prototype>().ReturnToPool();
CellInstances.Clear();
_displayedEntity = entity;
_displayedCargo = null;
Title.text = entity.Name;
var hullData = manager.GetData(entity.Hull) as HullData;
var hullData = GameManager.ItemManager.GetData(entity.Hull) as HullData;
//FakeOccupancy = new Shape(hullData.Shape.Width, hullData.Shape.Height);
//IgnoreOccupancy = new Shape(hullData.Shape.Width, hullData.Shape.Height);
Grid.constraintCount = hullData.Shape.Width;
_nodeInstances = hullData.Shape.AllCoordinates
.ToDictionary(v => v,
v =>
{
if (!hullData.Shape[v])
{
var empty = new GameObject("Empty Node", typeof(RectTransform));
empty.transform.SetParent(Grid.transform);
return empty;
}
var spriteIndex = 0;
var interior = hullData.InteriorCells[v];
var item = entity.GearOccupancy[v.x, v.y];
var hardpoint = entity.Hardpoints[v.x, v.y];
bool HardpointMatch(int2 offset) => !(hullData.Shape[v + offset] &&
entity.Hardpoints[v.x + offset.x, v.y + offset.y] == hardpoint &&
entity.GearOccupancy[v.x + offset.x, v.y + offset.y] == item);
bool NoHardpointMatch(int2 offset) => !(hullData.Shape[v + offset] && (
!interior && !hullData.InteriorCells[v + offset] && entity.Hardpoints[v.x + offset.x, v.y + offset.y] == null ||
interior && item != null && entity.GearOccupancy[v.x + offset.x, v.y + offset.y] == item));
if (hardpoint != null)
{
for(int i = 0; i < 8; i++)
if (HardpointMatch(_offsets[i]))
spriteIndex += 1 << i;
}
else
{
for(int i = 0; i < 8; i++)
if (NoHardpointMatch(_offsets[i]))
spriteIndex += 1 << i;
}
if (item != null)
spriteIndex += 1 << 8;
if (spriteIndex == 0)
{
var empty = new GameObject("Empty Node", typeof(RectTransform));
empty.transform.SetParent(Grid.transform);
return empty;
}
var node = NodePrototype.Instantiate<Prototype>().gameObject;
var image = node.GetComponentInChildren<Image>();
image.sprite = NodeTextures[spriteIndex];
image.color = hardpoint?.TintColor.ToColor() ?? (interior ? Color.white * .5f : Color.white);
return node;
});
foreach (var v in hullData.Shape.AllCoordinates)
{
if (!hullData.Shape[v])
{
var empty = new GameObject("Empty Node", typeof(RectTransform));
empty.transform.SetParent(Grid.transform);
EmptyCells.Add(empty);
}
else
{
var cell = NodePrototype.Instantiate<InventoryCell>();
cell.PointerClickTrigger.OnPointerClickAsObservable()
.Subscribe(data => _onClick?.OnNext(new InventoryEntityEventData(data, v, entity)));
cell.BeginDragTrigger.OnBeginDragAsObservable()
.Subscribe(data => _onBeginDrag?.OnNext(new InventoryEntityEventData(data, v, entity)));
cell.DragTrigger.OnDragAsObservable()
.Subscribe(data => _onDrag?.OnNext(new InventoryEntityEventData(data, v, entity)));
cell.EndDragTrigger.OnEndDragAsObservable()
.Subscribe(data => _onEndDrag?.OnNext(new InventoryEntityEventData(data, v, entity)));
cell.PointerEnterTrigger.OnPointerEnterAsObservable()
.Subscribe(data => _onPointerEnter?.OnNext(new InventoryEntityEventData(data, v, entity)));
cell.PointerExitTrigger.OnPointerExitAsObservable()
.Subscribe(data => _onPointerExit?.OnNext(new InventoryEntityEventData(data, v, entity)));
CellInstances.Add(v, cell);
}
}
RefreshCells(CellInstances.Keys);
}
public void Display(ItemManager manager, EquippedCargoBay cargo)
public void Display(EquippedCargoBay cargo)
{
foreach(var node in _nodeInstances.Values)
Destroy(node);
_nodeInstances.Clear();
foreach(var empty in EmptyCells)
Destroy(empty);
EmptyCells.Clear();
foreach(var node in CellInstances.Values)
node.GetComponent<Prototype>().ReturnToPool();
CellInstances.Clear();
_displayedEntity = null;
_displayedCargo = cargo;
Title.text = cargo.Name;
// FakeOccupancy = new Shape(cargo.Data.InteriorShape.Width, cargo.Data.InteriorShape.Height);
// IgnoreOccupancy = new Shape(cargo.Data.InteriorShape.Width, cargo.Data.InteriorShape.Height);
Grid.constraintCount = cargo.Data.InteriorShape.Width;
_nodeInstances = cargo.Data.InteriorShape.AllCoordinates
.ToDictionary(v => v,
v =>
{
if (!cargo.Data.InteriorShape[v])
{
var empty = new GameObject("Empty Node", typeof(RectTransform));
empty.transform.SetParent(Grid.transform);
return empty;
}
var spriteIndex = 0;
var item = cargo.Occupancy[v.x, v.y];
bool ItemMatch(int2 offset) =>
!cargo.Data.InteriorShape[v + offset] ||
item == null ||
cargo.Occupancy[v.x + offset.x, v.y + offset.y] != item;
for(int i = 0; i < 8; i++)
if (ItemMatch(_offsets[i]))
spriteIndex += 1 << i;
if (item != null)
spriteIndex += 1 << 8;
if (spriteIndex == 0)
{
var empty = new GameObject("Empty Node", typeof(RectTransform));
empty.transform.SetParent(Grid.transform);
return empty;
}
var node = NodePrototype.Instantiate<Prototype>().gameObject;
var image = node.GetComponentInChildren<Image>();
image.sprite = NodeTextures[spriteIndex];
if (item == null)
image.color = Color.white * .5f;
else if (manager.GetData(item) is EquippableItemData equippable)
image.color = HardpointData.GetColor(equippable.HardpointType).ToColor();
else
image.color = Color.white;
return node;
});
foreach (var v in cargo.Data.InteriorShape.AllCoordinates)
{
if (!cargo.Data.InteriorShape[v])
{
var empty = new GameObject("Empty Node", typeof(RectTransform));
empty.transform.SetParent(Grid.transform);
EmptyCells.Add(empty);
}
else
{
var cell = NodePrototype.Instantiate<InventoryCell>();
cell.PointerClickTrigger.OnPointerClickAsObservable()
.Subscribe(data => _onClick?.OnNext(new InventoryCargoEventData(data, v, cargo)));
cell.BeginDragTrigger.OnBeginDragAsObservable()
.Subscribe(data => _onBeginDrag?.OnNext(new InventoryCargoEventData(data, v, cargo)));
cell.DragTrigger.OnDragAsObservable()
.Subscribe(data => _onDrag?.OnNext(new InventoryCargoEventData(data, v, cargo)));
cell.EndDragTrigger.OnEndDragAsObservable()
.Subscribe(data => _onEndDrag?.OnNext(new InventoryCargoEventData(data, v, cargo)));
cell.PointerEnterTrigger.OnPointerEnterAsObservable()
.Subscribe(data => _onPointerEnter?.OnNext(new InventoryCargoEventData(data, v, cargo)));
cell.PointerExitTrigger.OnPointerExitAsObservable()
.Subscribe(data => _onPointerExit?.OnNext(new InventoryCargoEventData(data, v, cargo)));
CellInstances.Add(v, cell);
}
}
RefreshCells();
}
public void RefreshCells()
{
RefreshCells(CellInstances.Keys);
}
public void RefreshCells(IEnumerable<int2> cells)
{
if (_displayedEntity != null)
{
foreach (var v in cells)
{
if(!CellInstances.ContainsKey(v)) continue;
var hullData = GameManager.ItemManager.GetData(_displayedEntity.Hull) as HullData;
var spriteIndex = 0;
var interior = hullData.InteriorCells[v];
var item = FakeOccupancy?[v]??false ? FakeEquipment : IgnoreOccupancy?[v]??false ? null : _displayedEntity.GearOccupancy[v.x, v.y];
var hardpoint = _displayedEntity.Hardpoints[v.x, v.y];
bool HardpointMatch(int2 offset)
{
var v2 = v + offset;
return !(
hullData.Shape[v2] &&
_displayedEntity.Hardpoints[v2.x, v2.y] == hardpoint &&
(FakeOccupancy?[v2]??false ? FakeEquipment : IgnoreOccupancy?[v2]??false ? null : _displayedEntity.GearOccupancy[v2.x, v2.y]) == item
);
}
bool NoHardpointMatch(int2 offset)
{
var v2 = v + offset;
return !(
hullData.Shape[v2] && (
!interior && !hullData.InteriorCells[v2] && _displayedEntity.Hardpoints[v2.x, v2.y] == null ||
interior && item != null &&
(FakeOccupancy?[v2]??false ? FakeEquipment : IgnoreOccupancy?[v2]??false ? null : _displayedEntity.GearOccupancy[v2.x, v2.y]) == item
)
);
}
if (hardpoint != null)
{
for(int i = 0; i < 8; i++)
if (HardpointMatch(_offsets[i]))
spriteIndex += 1 << i;
}
else
{
for(int i = 0; i < 8; i++)
if (NoHardpointMatch(_offsets[i]))
spriteIndex += 1 << i;
}
if (item != null)
spriteIndex += 1 << 8;
var image = CellInstances[v].GetComponentInChildren<Image>();
image.sprite = NodeTextures[spriteIndex];
image.color = GetColor(v);
}
}
else if(_displayedCargo != null)
{
foreach (var v in cells)
{
if(!CellInstances.ContainsKey(v)) continue;
var spriteIndex = 0;
var item = FakeOccupancy?[v]??false ? FakeItem : IgnoreOccupancy?[v]??false ? null : _displayedCargo.Occupancy[v.x, v.y];
bool ItemMatch(int2 offset)
{
var v2 = v + offset;
return !_displayedCargo.Data.InteriorShape[v2] || item == null ||
(FakeOccupancy?[v2]??false ? FakeItem : IgnoreOccupancy?[v2]??false ? null : _displayedCargo.Occupancy[v2.x, v2.y]) != item;
}
for(int i = 0; i < 8; i++)
if (ItemMatch(_offsets[i]))
spriteIndex += 1 << i;
if (item != null)
spriteIndex += 1 << 8;
var image = CellInstances[v].GetComponentInChildren<Image>();
image.sprite = NodeTextures[spriteIndex];
image.color = GetColor(v);
}
}
}
public Color GetColor(int2 position, bool highlight = false)
{
if (_displayedEntity != null)
{
var hullData = GameManager.ItemManager.GetData(_displayedEntity.Hull) as HullData;
var interior = hullData.InteriorCells[position];
var item = FakeOccupancy?[position]??false ? FakeEquipment : IgnoreOccupancy?[position]??false ? null : _displayedEntity.GearOccupancy[position.x, position.y];
var hardpoint = _displayedEntity.Hardpoints[position.x, position.y];
if (hardpoint == null)
{
if(!interior)
return Color.white;
if (item == null)
{
return Color.white * .25f;
}
if(highlight)
return Color.white;
return Color.white * .5f;
}
var tint = hardpoint.TintColor;
if (!highlight || item == null)
tint *= .7071f;
return tint.ToColor();
}
else
{
var item = FakeOccupancy?[position] ?? false ? FakeItem : IgnoreOccupancy?[position] ?? false ? null : _displayedCargo.Occupancy[position.x, position.y];
if (item == null)
return Color.white * .25f;
var c = float3(1);
if (GameManager.ItemManager.GetData(item) is EquippableItemData equippable)
c = HardpointData.GetColor(equippable.HardpointType);
if(!highlight)
c *= .7071f;
return c.ToColor();
}
}
public void DropItem(ItemInstance item, int2 position)
{
if (_displayedEntity != null && item is EquippableItem equippableItem)
{
_displayedEntity.TryEquip(equippableItem, position);
}
else
{
_displayedCargo?.TryStore(item, position);
}
}
public UniRx.IObservable<InventoryEventData> OnClickAsObservable() => _onClick ?? (_onClick = new Subject<InventoryEventData>());
public UniRx.IObservable<InventoryEventData> OnBeginDragAsObservable() => _onBeginDrag ?? (_onBeginDrag = new Subject<InventoryEventData>());
public UniRx.IObservable<InventoryEventData> OnDragAsObservable() => _onDrag ?? (_onDrag = new Subject<InventoryEventData>());
public UniRx.IObservable<InventoryEventData> OnEndDragAsObservable() => _onEndDrag ?? (_onEndDrag = new Subject<InventoryEventData>());
public UniRx.IObservable<InventoryEventData> OnPointerEnterAsObservable() => _onPointerEnter ?? (_onPointerEnter = new Subject<InventoryEventData>());
public UniRx.IObservable<InventoryEventData> OnPointerExitAsObservable() => _onPointerExit ?? (_onPointerExit = new Subject<InventoryEventData>());
}
public abstract class InventoryEventData
{
protected InventoryEventData(PointerEventData pointerEventData, int2 position)
{
PointerEventData = pointerEventData;
Position = position;
}
public PointerEventData PointerEventData { get; }
public int2 Position { get; }
}
public class InventoryEntityEventData : InventoryEventData
{
public InventoryEntityEventData(PointerEventData pointerEventData, int2 position, Entity entity) :
base(pointerEventData, position)
{
Entity = entity;
}
public Entity Entity { get; }
}
public class InventoryCargoEventData : InventoryEventData
{
public InventoryCargoEventData(PointerEventData pointerEventData, int2 position, EquippedCargoBay cargoBay) :
base(pointerEventData, position)
{
CargoBay = cargoBay;
}
public EquippedCargoBay CargoBay { get; }
}

View file

@ -29,6 +29,7 @@ public class PropertiesPanel : MonoBehaviour
public PropertyButton PropertyButton;
public ButtonField ButtonField;
public IncrementField IncrementField;
public RectTransform Content;
[HideInInspector] public FlatFlatButton SelectedChild;
[HideInInspector] public ItemManager Context;
@ -82,7 +83,7 @@ public class PropertiesPanel : MonoBehaviour
public RectTransform AddSection(string name)
{
var section = Instantiate(Section, transform);
var section = Instantiate(Section, Content ?? transform);
section.GetComponentInChildren<TextMeshProUGUI>().text = name;
Properties.Add(section.gameObject);
OnPropertyAdded?.Invoke(section.gameObject);
@ -93,9 +94,9 @@ public class PropertiesPanel : MonoBehaviour
{
Property property;
if(read != null)
property = Instantiate(PropertyLabel, transform);
property = Instantiate(PropertyLabel, Content ?? transform);
else
property = Instantiate(Property, transform);
property = Instantiate(Property, Content ?? transform);
property.Label.text = name;
if (read != null)
@ -107,7 +108,7 @@ public class PropertiesPanel : MonoBehaviour
public PropertiesList AddList(string name) //, IEnumerable<(string, Func<string>)> elements)
{
var list = Instantiate(List, transform);
var list = Instantiate(List, Content ?? transform);
list.Context = Context;
list.Dropdown = Dropdown;
list.Title.text = name;
@ -127,7 +128,7 @@ public class PropertiesPanel : MonoBehaviour
public AttributeProperty AddPersonalityProperty(PersonalityAttribute attribute, Func<float> read)
{
var attributeInstance = Instantiate(Attribute, transform);
var attributeInstance = Instantiate(Attribute, Content ?? transform);
attributeInstance.Title.text = attribute.Name;
attributeInstance.HighLabel.text = attribute.HighName;
attributeInstance.LowLabel.text = attribute.LowName;
@ -139,7 +140,7 @@ public class PropertiesPanel : MonoBehaviour
public virtual PropertyButton AddButton(string name, Action onClick)
{
var button = Instantiate(PropertyButton, transform);
var button = Instantiate(PropertyButton, Content ?? transform);
button.Label.text = name;
button.Button.onClick.AddListener(() => onClick());
Properties.Add(button.gameObject);
@ -149,7 +150,7 @@ public class PropertiesPanel : MonoBehaviour
public void AddButton(string name, string label, Action onClick)
{
var button = Instantiate(ButtonField, transform);
var button = Instantiate(ButtonField, Content ?? transform);
button.Label.text = name;
button.ButtonLabel.text = label;
button.Button.onClick.AddListener(() => onClick());
@ -159,7 +160,7 @@ public class PropertiesPanel : MonoBehaviour
public void AddField(string name, Func<string> read, Action<string> write)
{
var field = Instantiate(InputField, transform);
var field = Instantiate(InputField, Content ?? transform);
field.Label.text = name;
field.Field.contentType = TMP_InputField.ContentType.Standard;
field.Field.onValueChanged.AddListener(val => write(val));
@ -184,7 +185,7 @@ public class PropertiesPanel : MonoBehaviour
public void AddField(string name, Func<float> read, Action<float> write)
{
var field = Instantiate(InputField, transform);
var field = Instantiate(InputField, Content ?? transform);
field.Label.text = name;
field.Field.contentType = TMP_InputField.ContentType.DecimalNumber;
field.Field.onValueChanged.AddListener(val => write(float.Parse(val)));
@ -204,7 +205,7 @@ public class PropertiesPanel : MonoBehaviour
public void AddField(string name, Func<int> read, Action<int> write)
{
var field = Instantiate(InputField, transform);
var field = Instantiate(InputField, Content ?? transform);
field.Label.text = name;
field.Field.contentType = TMP_InputField.ContentType.IntegerNumber;
field.Field.onValueChanged.AddListener(val => write(int.Parse(val)));
@ -224,7 +225,7 @@ public class PropertiesPanel : MonoBehaviour
public void AddIncrementField(string name, Func<int> read, Action<int> write, Func<int> min, Func<int> max)
{
var field = Instantiate(IncrementField, transform);
var field = Instantiate(IncrementField, Content ?? transform);
field.Label.text = name;
field.Increment.onClick.AddListener(() => write(read() + 1));
field.Decrement.onClick.AddListener(() => write(read() - 1));
@ -245,7 +246,7 @@ public class PropertiesPanel : MonoBehaviour
public void AddField(string name, Func<float> read, Action<float> write, float min, float max)
{
var field = Instantiate(RangedFloatField, transform);
var field = Instantiate(RangedFloatField, Content ?? transform);
field.Label.text = name;
field.Slider.wholeNumbers = false;
field.Slider.minValue = min;
@ -258,7 +259,7 @@ public class PropertiesPanel : MonoBehaviour
public void AddProgressField(string name, Func<float> read)
{
var field = Instantiate(ProgressField, transform);
var field = Instantiate(ProgressField, Content ?? transform);
field.Label.text = name;
field.Slider.wholeNumbers = false;
field.Slider.minValue = 0;
@ -271,7 +272,7 @@ public class PropertiesPanel : MonoBehaviour
public void AddField(string name, Func<int> read, Action<int> write, int min, int max)
{
var field = Instantiate(RangedFloatField, transform);
var field = Instantiate(RangedFloatField, Content ?? transform);
field.Label.text = name;
field.Slider.wholeNumbers = true;
field.Slider.minValue = min;
@ -284,7 +285,7 @@ public class PropertiesPanel : MonoBehaviour
public void AddField(string name, Func<bool> read, Action<bool> write)
{
var field = Instantiate(BoolField, transform);
var field = Instantiate(BoolField, Content ?? transform);
field.Label.text = name;
field.Toggle.onValueChanged.AddListener(val => write(val));
RefreshPropertyValues += () => field.Toggle.isOn = read();
@ -294,7 +295,7 @@ public class PropertiesPanel : MonoBehaviour
public void AddField(string name, Func<int> read, Action<int> write, string[] enumOptions)
{
var field = Instantiate(EnumField, transform);
var field = Instantiate(EnumField, Content ?? transform);
field.Label.text = name;
field.Dropdown.OnClick += data =>
{
@ -389,6 +390,56 @@ public class PropertiesPanel : MonoBehaviour
}
}
public void AddItemProperties(ItemInstance item)
{
var data = Context.ItemData.Get<ItemData>(item.Data);
AddProperty("Type", () => data.Name);
AddProperty(data.Description).Label.fontStyle = FontStyles.Normal;
// if (item is CraftedItemInstance craftedItemInstance)
// {
// var sourceEntity = Context.ItemData.Get<Entity>(craftedItemInstance.SourceEntity);
// if (sourceEntity != null)
// {
// var corporation = Context.ItemData.Get<Corporation>(sourceEntity.Corporation);
// AddProperty("Manufacturer", () => corporation.Name);
// }
// else
// {
// AddProperty("Manufacturer", () => "GameCult");
// }
// }
// if (item is SimpleCommodity simpleCommodity)
// AddProperty("Quantity", () => simpleCommodity.Quantity.ToString());
AddProperty("Mass", () => Context.GetMass(item).SignificantDigits(Context.GameplaySettings.SignificantDigits));
AddProperty("Thermal Mass", () => Context.GetThermalMass(item).SignificantDigits(Context.GameplaySettings.SignificantDigits));
if (item is EquippableItem gear)
{
var gearData = Context.GetData(gear);
AddProperty("Durability", () =>
$"{gear.Durability.SignificantDigits(Context.GameplaySettings.SignificantDigits)}/{gearData.Durability.SignificantDigits(Context.GameplaySettings.SignificantDigits)}");
foreach (var behavior in gearData.Behaviors)
{
var type = behavior.GetType();
if (type.GetCustomAttribute(typeof(RuntimeInspectable)) != null)
{
foreach (var field in type.GetFields().Where(f => f.GetCustomAttribute<RuntimeInspectable>() != null))
{
var fieldType = field.FieldType;
if (fieldType == typeof(float))
AddProperty(field.Name, () => $"{((float) field.GetValue(behavior)).SignificantDigits(Context.GameplaySettings.SignificantDigits)}");
else if (fieldType == typeof(int))
AddProperty(field.Name, () => $"{(int) field.GetValue(behavior)}");
else if (fieldType == typeof(PerformanceStat))
{
var stat = (PerformanceStat) field.GetValue(behavior);
AddProperty(field.Name, () => $"{Context.Evaluate(stat, gear).SignificantDigits(Context.GameplaySettings.SignificantDigits)}");
}
}
}
}
}
}
public void Inspect(Entity entity, EquippedItem item)
{
if (this is PropertiesList list)