Clean Inventory Grids

Improved inventory grid layout system by creating a single spritesheet with all 256 possible combinations of cell adjacency, and modified the inventory script to index individual sprites using adjacency
This commit is contained in:
Reinier Vens 2020-12-12 20:43:09 +00:00
commit ac0361fd9b
40 changed files with 6736 additions and 1869 deletions

View file

@ -0,0 +1,41 @@
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using UnityEditorInternal;
using System.IO;
// https://forum.unity.com/threads/sprite-editor-automatic-slicing-by-script.320776/
// http://www.sarpersoher.com/a-custom-asset-importer-for-unity/
// public class SpritePostprocessor : AssetPostprocessor {
// public void OnPostprocessTexture(Texture2D texture) {
// TextureImporter importer = assetImporter as TextureImporter;
// if (importer.spriteImportMode != SpriteImportMode.Multiple) {
// return;
// }
//
// Debug.Log("OnPostprocessTexture generating sprites");
//
// int minimumSpriteSize = 8;
// int extrudeSize = 0;
//
// Rect[] rects = InternalSpriteUtility.GenerateGridSpriteRectangles(texture, Vector2.one, Vector2.one * 8, Vector2.one * 2, true);
// //InternalSpriteUtility.GenerateAutomaticSpriteRectangles(texture, minimumSpriteSize, extrudeSize);
// // List<Rect> rectsList = new List<Rect>(rects);
// // rectsList = SortRects(rectsList, texture.width);
//
// string filenameNoExtension = Path.GetFileNameWithoutExtension(assetPath);
// List<SpriteMetaData> metas = new List<SpriteMetaData>();
// int rectNum = 0;
//
// foreach (Rect rect in rects) {
// SpriteMetaData meta = new SpriteMetaData();
// meta.border = Vector4.one * 4;
// meta.rect = rect;
// meta.name = filenameNoExtension + "_" + rectNum++;
// metas.Add(meta);
// }
//
// importer.spritesheet = metas.ToArray();
// }
// }

View file

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

View file

@ -6,6 +6,7 @@ using Unity.Mathematics;
using UnityEngine;
using UnityEngine.UI;
using static Unity.Mathematics.math;
using int2 = Unity.Mathematics.int2;
public class InventoryPanel : MonoBehaviour
{
@ -15,6 +16,17 @@ public class InventoryPanel : MonoBehaviour
public Prototype NodePrototype;
private Dictionary<int2, GameObject> _nodeInstances;
private int2[] _offsets = new[]
{
int2(0, 1),
int2(1, 0),
int2(0, -1),
int2(-1, 0),
int2(1, 1),
int2(1, -1),
int2(-1, -1),
int2(-1, 1)
};
public void Display(ItemManager manager, Entity entity)
{
@ -35,43 +47,27 @@ public class InventoryPanel : MonoBehaviour
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)
{
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;
for(int i = 0; i < 8; i++)
if (HardpointMatch(_offsets[i]))
spriteIndex += 1 << i;
}
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;
for(int i = 0; i < 8; i++)
if (NoHardpointMatch(_offsets[i]))
spriteIndex += 1 << i;
}
if (spriteIndex == 15)