using UnityEngine;
namespace Cyber.Items {
///
/// The interface, which contains a grid of indices of items in the . Use to get items in the interface.
///
public class DriveInterface {
///
/// Width of the interface.
///
public const int Width = 8;
///
/// Minimun height of the interface.
///
public const int MinHeight = 4;
private int[,] ItemGrid = new int[4, Width];
private Drive Drive;
///
/// Creates a Drive interface for a .
///
///
public DriveInterface(Drive drive) {
Drive = drive;
}
///
/// Returns the item at the specified coordinate on the interface. Returns null if invalid or empty coordinate.
///
/// The x-coordinate
/// The y-coordinate
/// The item or null
public Item GetItemAt(int x, int y) {
if (ItemGrid[y, x] == 0) {
return null;
} else {
return Drive.GetItem(ItemGrid[y, x]);
}
}
///
/// Gets the Width of the interface, or simply .
///
///
public int GetWidth() {
return Width;
}
///
/// Gets the current height of the interface
///
///
public int GetHeight() {
return ItemGrid.GetLength(0);
}
///
/// Updates the height of the interface, adding new rows or deleting old useless ones.
///
public void UpdateHeight() {
int RequiredHeight = MinHeight;
for (int y = MinHeight; y < GetHeight(); y++) {
for (int x = 0; x < Width; x++) {
if (GetItemAt(x, y) == null || (x == Width - 1 && y == GetHeight() - 1)) {
RequiredHeight = y;
}
}
}
int[,] Temp = new int[RequiredHeight + 1, Width];
for (int y = 0; y < RequiredHeight - 1; y++) {
for (int x = 0; x < Width; x++) {
if (GetItemAt(x, y) != null) {
Temp[y, x] = Drive.GetItems().IndexOf(GetItemAt(x, y));
}
}
}
ItemGrid = Temp;
}
///
/// Adds a new item to the grid. The idx in the parameter is the idx of the item in the drive.
///
///
public void AddNewItem(int idx) {
UpdateHeight();
for (int y = 0; y < GetHeight(); y++) {
for (int x = 0; x < Width; x++) {
if (GetItemAt(x, y) == null) {
ItemGrid[y, x] = idx;
}
}
}
}
}
}