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 = 7;
///
/// Minimun height of the interface.
///
public const int MinHeight = 4;
private int[,] ItemGrid;
private Drive Drive;
///
/// Creates a Drive interface for a .
///
///
public DriveInterface(Drive drive) {
Drive = drive;
ItemGrid = CreateEmptyGrid(Width, 4);
}
///
/// 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] == -1) {
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 = CreateEmptyGrid(Width, RequiredHeight + 1);
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;
return;
}
}
}
}
private int[,] CreateEmptyGrid(int width, int height) {
int[,] Grid = new int[height, width];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
Grid[y, x] = -1;
}
}
return Grid;
}
}
}