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

@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Cinemachine;
using MessagePack;
using UniRx;
@ -19,6 +20,7 @@ public class ActionGameManager : MonoBehaviour
public CinemachineVirtualCamera TopDownCamera;
public CinemachineVirtualCamera FollowCamera;
public GameObject GameplayUI;
public InventoryPanel InventoryPanel;
//public PlayerInput Input;
private DirectoryInfo _filePath;
@ -27,23 +29,23 @@ public class ActionGameManager : MonoBehaviour
private AetheriaInput _input;
private int _zoomLevelIndex;
public DatabaseCache Cache { get; private set; }
public ItemManager Context { get; private set; }
public DatabaseCache ItemData { get; private set; }
public ItemManager ItemManager { get; private set; }
public Zone Zone { get; private set; }
void Start()
{
ConsoleController.MessageReceiver = this;
_filePath = new DirectoryInfo(Application.dataPath).Parent.CreateSubdirectory("GameData");
Cache = new DatabaseCache();
Cache.Load(_filePath.FullName);
Context = new ItemManager(Cache, Settings.GameplaySettings, Debug.Log);
ItemData = new DatabaseCache();
ItemData.Load(_filePath.FullName);
ItemManager = new ItemManager(ItemData, Settings.GameplaySettings, Debug.Log);
var zoneFile = Path.Combine(_filePath.FullName, "Home.zone");
// If the game has already been run, there will be a Home file containing a ZonePack; if not, generate one
var zonePack = File.Exists(zoneFile) ?
MessagePackSerializer.Deserialize<ZonePack>(File.ReadAllBytes(zoneFile)) :
MessagePackSerializer.Deserialize<ZonePack>(File.ReadAllBytes(zoneFile)):
ZoneGenerator.GenerateZone(
settings: Settings.ZoneSettings,
// mapLayers: Context.MapLayers,
@ -91,6 +93,11 @@ public class ActionGameManager : MonoBehaviour
Zone.Data.Name = args[0];
SaveZone();
});
var hullType = ItemData.GetAll<HullData>().First(x=>x.HullType==HullType.Ship);
var hull = ItemManager.CreateInstance(hullType, 0, 1) as EquippableItem;
var entity = new Ship(ItemManager, Zone, hull);
InventoryPanel.Display(ItemManager, entity);
}
public void SaveZone() => File.WriteAllBytes(
@ -108,7 +115,7 @@ public class ActionGameManager : MonoBehaviour
if(!_editMode)
{
_time += Time.deltaTime;
Context.Time = Time.time;
ItemManager.Time = Time.time;
}
Zone.Update(_time, Time.deltaTime);
SectorRenderer.Time = _time;

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,

View file

@ -0,0 +1,91 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.UI;
using static Unity.Mathematics.math;
public class InventoryPanel : MonoBehaviour
{
public TextMeshProUGUI Title;
public GridLayoutGroup Grid;
public Sprite[] NodeTextures;
public Prototype NodePrototype;
private Dictionary<int2, GameObject> _nodeInstances;
public void Display(ItemManager manager, Entity entity)
{
var hullData = manager.GetData(entity.Hull) as HullData;
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];
if (hardpoint != null)
{
if (hullData.Shape[v + int2(0, 1)] &&
entity.Hardpoints[v.x, v.y + 1] == hardpoint &&
entity.GearOccupancy[v.x, v.y + 1] == item)
spriteIndex += 1;
if (hullData.Shape[v + int2(1, 0)] &&
entity.Hardpoints[v.x + 1, v.y] == hardpoint &&
entity.GearOccupancy[v.x + 1, v.y] == item)
spriteIndex += 2;
if (hullData.Shape[v + int2(0, -1)] &&
entity.Hardpoints[v.x, v.y - 1] == hardpoint &&
entity.GearOccupancy[v.x, v.y - 1] == item)
spriteIndex += 4;
if (hullData.Shape[v + int2(-1, 0)] &&
entity.Hardpoints[v.x - 1, v.y] == hardpoint &&
entity.GearOccupancy[v.x - 1, v.y] == item)
spriteIndex += 8;
}
else
{
if (hullData.Shape[v + int2(0, 1)] && (
!interior && !hullData.InteriorCells[v + int2(0, 1)] && entity.Hardpoints[v.x, v.y + 1] == null ||
interior && item != null && entity.GearOccupancy[v.x, v.y + 1] == item))
spriteIndex += 1;
if (hullData.Shape[v + int2(1, 0)] && (
!interior && !hullData.InteriorCells[v + int2(1, 0)] && entity.Hardpoints[v.x + 1, v.y] == null ||
interior && item != null && entity.GearOccupancy[v.x + 1, v.y] == item))
spriteIndex += 2;
if (hullData.Shape[v + int2(0, -1)] && (
!interior && !hullData.InteriorCells[v + int2(0, -1)] && entity.Hardpoints[v.x, v.y - 1] == null ||
interior && item != null && entity.GearOccupancy[v.x, v.y - 1] == item))
spriteIndex += 4;
if (hullData.Shape[v + int2(-1, 0)] && (
!interior && !hullData.InteriorCells[v + int2(-1, 0)] && entity.Hardpoints[v.x - 1, v.y] == null ||
interior && item != null && entity.GearOccupancy[v.x - 1, v.y] == item))
spriteIndex += 8;
}
if (spriteIndex == 15)
{
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 != null ? hardpoint.TintColor.ToColor() : interior ? Color.white * .5f : Color.white;
return node;
});
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 78aa0b8ec7594a7438f13757228e3500
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: