2017-05-13 21:54:14 +02:00
2017-05-13 22:21:29 +02:00
using UnityEngine ;
2017-05-13 21:54:14 +02:00
namespace Cyber.Items {
/// <summary>
/// The <see cref="Drive"/> interface, which contains a grid of indices of items in the <see cref="Drive"/>. Use <see cref="GetItemAt(int, int)"/> to get items in the interface.
/// </summary>
public class DriveInterface {
/// <summary>
/// Width of the interface.
/// </summary>
2017-05-13 23:07:42 +02:00
public const int Width = 7 ;
2017-05-13 21:54:14 +02:00
/// <summary>
/// Minimun height of the interface.
/// </summary>
public const int MinHeight = 4 ;
2017-05-13 23:07:42 +02:00
private int [ , ] ItemGrid ;
2017-05-13 21:54:14 +02:00
private Drive Drive ;
/// <summary>
/// Creates a Drive interface for a <see cref="Drive"/>.
/// </summary>
/// <param name="drive"></param>
public DriveInterface ( Drive drive ) {
Drive = drive ;
2017-05-13 23:07:42 +02:00
ItemGrid = CreateEmptyGrid ( Width , 4 ) ;
2017-05-13 21:54:14 +02:00
}
/// <summary>
/// Returns the item at the specified coordinate on the interface. Returns null if invalid or empty coordinate.
/// </summary>
/// <param name="x">The x-coordinate</param>
/// <param name="y">The y-coordinate</param>
/// <returns>The item or null</returns>
public Item GetItemAt ( int x , int y ) {
2017-05-15 00:02:40 +02:00
if ( y < 0 | | x < 0 | | y > = GetHeight ( ) | | x > = GetWidth ( ) | |
ItemGrid [ y , x ] = = - 1 ) {
2017-05-13 21:54:14 +02:00
return null ;
} else {
return Drive . GetItem ( ItemGrid [ y , x ] ) ;
}
}
/// <summary>
/// Gets the Width of the interface, or simply <see cref="Width"/>.
/// </summary>
/// <returns></returns>
public int GetWidth ( ) {
return Width ;
}
/// <summary>
/// Gets the current height of the interface
/// </summary>
/// <returns></returns>
public int GetHeight ( ) {
2017-05-13 22:21:29 +02:00
return ItemGrid . GetLength ( 0 ) ;
2017-05-13 21:54:14 +02:00
}
/// <summary>
/// Updates the height of the interface, adding new rows or deleting old useless ones.
/// </summary>
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 ;
}
}
}
2017-05-13 23:07:42 +02:00
int [ , ] Temp = CreateEmptyGrid ( Width , RequiredHeight + 1 ) ;
2017-05-13 21:54:14 +02:00
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 ;
}
/// <summary>
/// Adds a new item to the grid. The idx in the parameter is the idx of the item in the drive.
/// </summary>
/// <param name="idx"></param>
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 ;
2017-05-13 23:07:42 +02:00
return ;
2017-05-13 21:54:14 +02:00
}
}
}
}
2017-05-13 23:07:42 +02:00
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 ;
}
2017-05-13 21:54:14 +02:00
}
}