/// <summary> /// Adds an item given an extractor/inserter. Calculates and places in the right slot. /// </summary> /// <param name="item"></param> /// <param name="inserter"></param> /// <returns></returns> public GenericItem AddItem(GenericItem item, Extractor inserter) { //Side the inserter is using InsertSide insert_side = inserter.OUTPUT_SIDE; InsertDir insert_dir = GetInsertDir(inserter.GetComponent <GridController>()); BeltSide side = GetBeltSide(insert_dir, insert_side); BeltPos pos = GetBeltPos(insert_dir, insert_side); //If it isn't valid, return immediately. if (!isValidInsert(insert_dir, insert_side)) { return(null); } else { //Get the relevant list. List <ItemController> LIST = GetSideList(side); //Get the relevant index. int index = GetIndex(side, pos); //Update the right index. LIST[(int)pos] = item.SpawnObject(ITEM_POSITIONS[index].position, ITEM_POSITIONS[index].rotation, ITEM_POSITIONS[index]); return(LIST[(int)pos].INFO); } }
/// <summary> /// Attempts to insert an item given an extractor/inserter. /// Returns the item if successful, else null. /// </summary> /// <param name="item"></param> /// <param name="inserter"></param> /// <returns></returns> public bool AttemptExtractItem(GenericItem item, Extractor inserter) { //Side the inserter is using InsertSide insert_side = inserter.OUTPUT_SIDE; InsertDir insert_dir = GetInsertDir(inserter.GetComponent <GridController>()); BeltSide side = GetBeltSide(insert_dir, insert_side); BeltPos pos = GetBeltPos(insert_dir, insert_side); return(isItemAccessible(side, pos, item)); }
/// <summary> /// Returns the index in the item positions list for the given slot. /// </summary> /// <param name="side"></param> /// <param name="pos"></param> /// <returns></returns> public int GetIndex(BeltSide side, BeltPos pos) { if (side == BeltSide.Left) { return((int)pos); } else { return((int)pos + 3); } }
/// <summary> /// Returns true or false depending on if the slot that is meant to be used on insert based on direction is empty. /// Inserters output either to their left or right half of the square. As such: /// Left insert direction inserters outputting to the left output to the front of the belt. /// Left insert direction inserters outputting to the right output to the back of the belt. /// </summary> /// <param name="insertDir"></param> /// <returns></returns> public bool isValidInsert(InsertDir insertDir, InsertSide insertSide) { BeltSide side = GetBeltSide(insertDir, insertSide); BeltPos pos = GetBeltPos(insertDir, insertSide); ItemController EC = GetItem(side, pos); if (EC == null) { return(true); } else { return(false); } }
/// <summary> /// Checks if the given item is in the given side and position. /// </summary> /// <param name="side"></param> /// <param name="pos"></param> /// <param name="item"></param> /// <returns></returns> public bool isItemAccessible(BeltSide side, BeltPos pos, GenericItem item) { ItemController SLOT_ITEM = GetItem(side, pos); if (SLOT_ITEM == null) { return(false); } else if (SLOT_ITEM.INFO == item) { return(true); } else { return(false); } }
/// <summary> /// Returns the entitycontroller in the given position on the given side. /// </summary> /// <param name="side"></param> /// <param name="pos"></param> /// <returns></returns> public ItemController GetItem(BeltSide side, BeltPos pos) { //Get the right side of the belt. List <ItemController> SIDE_LIST = GetSideList(side); //Return the entity in a given position from the side list. switch (pos) { case BeltPos.Back: return(SIDE_LIST[(int)BeltPos.Back]); case BeltPos.Middle: return(SIDE_LIST[(int)BeltPos.Middle]); case BeltPos.Front: return(SIDE_LIST[(int)BeltPos.Front]); default: throw new System.Exception("Error, invalid belt position."); } }
/// <summary> /// Extracts item with given inserter. /// Only decrements this inventory, does not add to the inserter's inventory. /// Returns the item if successful, else null; /// </summary> /// <param name="item"></param> /// <param name="inserter"></param> /// <returns></returns> public GenericItem ExtractItem(GenericItem item, Extractor inserter) { //Side the inserter is using InsertSide insert_side = inserter.OUTPUT_SIDE; InsertDir insert_dir = GetInsertDir(inserter.GetComponent <GridController>()); BeltSide side = GetBeltSide(insert_dir, insert_side); BeltPos pos = GetBeltPos(insert_dir, insert_side); //Get the relevant list. List <ItemController> LIST = GetSideList(side); //Get the relevant index. int index = GetIndex(side, pos); //Save the item to be returned GenericItem item_ret = LIST[(int)pos].INFO; //Update side list. LIST[(int)pos].DespawnObject(); LIST[(int)pos] = null; return(item_ret); }