In-game Inventory UI

Created InventoryPanel script which populates a grid with sprites representing the layout of an entity's hull
This commit is contained in:
Reinier Vens 2020-12-12 03:42:58 +00:00
commit 02e85cb0e2
44 changed files with 2230 additions and 191 deletions

View file

@ -44,9 +44,9 @@ public abstract class Entity
private bool _active;
private EquippedItem[,] _gearOccupancy;
private EquippedItem[,] _thermalOccupancy;
private HardpointData[,] _hardpoints;
public EquippedItem[,] GearOccupancy;
public EquippedItem[,] ThermalOccupancy;
public HardpointData[,] Hardpoints;
public ItemManager ItemManager { get; }
public int AssignedPopulation => PopulationAssignments.Sum(pa => pa.AssignedPopulation);
@ -77,20 +77,21 @@ public abstract class Entity
ItemManager = itemManager;
Zone = zone;
Hull = hull;
MapShip();
}
private void MapShip()
{
var hullData = ItemManager.GetData(Hull) as HullData;
_gearOccupancy = new EquippedItem[hullData.Shape.Width, hullData.Shape.Height];
_thermalOccupancy = new EquippedItem[hullData.Shape.Width, hullData.Shape.Height];
_hardpoints = new HardpointData[hullData.Shape.Width, hullData.Shape.Height];
GearOccupancy = new EquippedItem[hullData.Shape.Width, hullData.Shape.Height];
ThermalOccupancy = new EquippedItem[hullData.Shape.Width, hullData.Shape.Height];
Hardpoints = new HardpointData[hullData.Shape.Width, hullData.Shape.Height];
foreach (var hardpoint in hullData.Hardpoints)
{
foreach (var hardpointCoord in hardpoint.Shape.Coordinates)
{
var hullCoord = hardpoint.Position + hardpointCoord;
_hardpoints[hullCoord.x, hullCoord.y] = hardpoint;
Hardpoints[hullCoord.x, hullCoord.y] = hardpoint;
}
}
}
@ -193,7 +194,7 @@ public abstract class Entity
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;
(itemData.HardpointType == HardpointType.Thermal ? ThermalOccupancy : GearOccupancy)[itemCoord.x, itemCoord.y] = null;
}
return item.EquippableItem;
@ -211,12 +212,12 @@ public abstract class Entity
// If there is any gear already occupying that space, it won't fit
// Thermal items have their own layer and do not collide with gear
var itemCoord = hullCoord + itemData.Shape.Rotate(i, item.Rotation);
if (!hullData.InteriorCells[itemCoord] || (itemData.HardpointType == HardpointType.Thermal ? _thermalOccupancy : _gearOccupancy)[itemCoord.x, itemCoord.y] != null) return false;
if (!hullData.InteriorCells[itemCoord] || (itemData.HardpointType == HardpointType.Thermal ? ThermalOccupancy : GearOccupancy)[itemCoord.x, itemCoord.y] != null) return false;
}
}
else
{
var hardpoint = _hardpoints[hullCoord.x, hullCoord.y];
var hardpoint = Hardpoints[hullCoord.x, hullCoord.y];
// If there's no hardpoint there, it won't fit
if (hardpoint == null) return false;
@ -233,7 +234,7 @@ public abstract class Entity
// 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[itemCoord.x, itemCoord.y] != null) return false;
}
}
@ -320,7 +321,7 @@ public abstract class Entity
foreach (var i in itemData.Shape.Coordinates)
{
var occupiedCoord = hullCoord + itemData.Shape.Rotate(i, item.Rotation);
(itemData.HardpointType == HardpointType.Thermal ? _thermalOccupancy : _gearOccupancy)[occupiedCoord.x, occupiedCoord.y] = equippedItem;
(itemData.HardpointType == HardpointType.Thermal ? ThermalOccupancy : GearOccupancy)[occupiedCoord.x, occupiedCoord.y] = equippedItem;
}
Hydrate(equippedItem);
@ -334,7 +335,7 @@ public abstract class Entity
foreach (var i in itemData.Shape.Coordinates)
{
var occupiedCoord = hullCoord + i;
(itemData.HardpointType == HardpointType.Thermal ? _thermalOccupancy : _gearOccupancy)[occupiedCoord.x, occupiedCoord.y] = equippedItem;
(itemData.HardpointType == HardpointType.Thermal ? ThermalOccupancy : GearOccupancy)[occupiedCoord.x, occupiedCoord.y] = equippedItem;
}
Hydrate(equippedItem);

View file

@ -18,19 +18,17 @@ public enum LauncherCaliber
public enum HardpointType
{
Hull,
Tool,
Thermal,
Thruster,
WarpThruster,
Reactor,
Radiator,
Shield,
Cooler,
Sensors,
Tool,
Energy,
Ballistic,
Launcher,
Infrastructure,
ControlModule
}

View file

@ -90,15 +90,31 @@ public class Shape
private IEnumerable<int2> EnumerateShapeCoordinates()
{
for(int x = 0; x < Width; x++)
for (int y = 0; y < Height; y++)
{
for (int y = 0; y < Height; y++)
for(int x = 0; x < Width; x++)
{
if(Cells[x,y]) yield return int2(x, y);
}
}
}
private int2[] _cachedAllShapeCoordinates;
[IgnoreMember]
public int2[] AllCoordinates => _cachedAllShapeCoordinates ?? (_cachedAllShapeCoordinates = EnumerateAllShapeCoordinates().ToArray());
private IEnumerable<int2> EnumerateAllShapeCoordinates()
{
for (int y = 0; y < Height; y++)
{
for(int x = 0; x < Width; x++)
{
yield return int2(x, y);
}
}
}
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]; }
set
@ -306,7 +322,7 @@ public class HardpointData : ITintInspector
x => ColorMath.HsvToRgb(float3((float)(int)x/hardpointTypes.Length, 1, 1)));
}
return _tintColors[Type];
return _tintColors.ContainsKey(Type) ? _tintColors[Type] : _tintColors[HardpointType.Hull];
}
}
private static Dictionary<HardpointType, float3> _tintColors;

View file

@ -411,14 +411,13 @@ public class ItemManager
// return ship;
// }
public SimpleCommodity CreateInstance(Guid data, int count)
public SimpleCommodity CreateInstance(SimpleCommodityData item, int count)
{
var item = ItemData.Get<SimpleCommodityData>(data);
if (item != null)
{
var newItem = new SimpleCommodity
{
Data = data,
Data = item.ID,
Quantity = count
};
//ItemData.Add(newItem);
@ -429,16 +428,15 @@ public class ItemManager
return null;
}
public CraftedItemInstance CreateInstance(Guid data, float qualityMin, float qualityMax)
public CraftedItemInstance CreateInstance(CraftedItemData item, float qualityMin, float qualityMax)
{
var item = ItemData.Get<CraftedItemData>(data);
if (item == null)
{
_logger("Attempted to create crafted item instance using missing or incorrect item id!");
return null;
}
var blueprint = ItemData.GetAll<BlueprintData>().FirstOrDefault(b => b.Item == data);
var blueprint = ItemData.GetAll<BlueprintData>().FirstOrDefault(b => b.Item == item.ID);
if (blueprint == null)
{
_logger("Attempted to create crafted item instance which has no blueprint!");
@ -451,9 +449,9 @@ public class ItemManager
foreach (var ingredient in blueprint.Ingredients)
{
var itemData = ItemData.Get<ItemData>(ingredient.Key);
if (itemData is SimpleCommodityData)
if (itemData is SimpleCommodityData simpleIngredientData)
{
var itemInstance = CreateInstance(ingredient.Key, ingredient.Value);
var itemInstance = CreateInstance(simpleIngredientData, ingredient.Value);
if(itemInstance == null)
{
invalidIngredientFound = true;
@ -463,9 +461,10 @@ public class ItemManager
}
else
{
var craftedIngredientData = ItemData.Get<CraftedItemData>(ingredient.Key);
for (int i = 0; i < ingredient.Value; i++)
{
var itemInstance = CreateInstance(ingredient.Key, qualityMin, qualityMax);
var itemInstance = CreateInstance(craftedIngredientData, qualityMin, qualityMax);
if (itemInstance == null)
{
invalidIngredientFound = true;
@ -488,7 +487,7 @@ public class ItemManager
{
var newGear = new EquippableItem
{
Data = data,
Data = item.ID,
Ingredients = ingredients,
Quality = Random.NextFloat(qualityMin, qualityMax),
Blueprint = blueprint.ID,
@ -500,7 +499,7 @@ public class ItemManager
var newCommodity = new CompoundCommodity
{
Data = data,
Data = item.ID,
Ingredients = ingredients,
Quality = Random.NextFloat(qualityMin, qualityMax),
Blueprint = blueprint.ID,