public bool TryToAddBlock(BlockStack block) { Debug.Log("trying to add block " + block.blockName + " with count " + block.count + " and block id " + block.block); // look for a stack for (int i = 0; i < blocks.Length; i++) { if (!IsEmptyBlockStack(blocks[i]) && blocks[i].block == block.block) { if (blocks[i].TryToAddToStack(block)) { return(true); } } } // no stack, look for empty slot for (int i = 0; i < blocks.Length; i++) { if (IsEmptyBlockStack(blocks[i])) { blocks[i] = block.Copy(); return(true); } } // no empty spots or stacks we can put it in, return false return(false); }
public bool CanAddBlock(BlockStack block) { // look for a stack for (int i = 0; i < blocks.Length; i++) { if (!IsEmptyBlockStack(blocks[i]) && blocks[i].block == block.block) { if (blocks[i].CanAddToStack(block)) { return(true); } } } // no stack, look for empty slot for (int i = 0; i < blocks.Length; i++) { if (IsEmptyBlockStack(blocks[i])) { return(true); } } // no empty spots or stacks we can put it in, return false return(false); }
public bool CanAddToStack(BlockStack block) { if (maxDurability != 0 || block.maxDurability != 0) { return(false); } return(block.block == this.block && World.stackableSize.ContainsKey(this.block) && World.stackableSize[this.block] >= count + block.count); }
public Inventory(int capacity) { this.capacity = capacity; blocks = new BlockStack[capacity]; for (int i = 0; i < blocks.Length; i++) { blocks[i] = new BlockStack(BlockValue.Air, 0); } }
// only adds and returns true if we can add the entire stack (this will break the code in blocks player when you left click to combine stack with finished product from crafting if this behavior is changed) public bool TryToAddToStack(BlockStack block) { if (CanAddToStack(block)) { this.count += block.count; return(true); } else { return(false); } }
public Recipe(BlockValue[] recipe, BlockStack result, bool unordered = true, float timeToFinish = 0) { this.unorderedRecipe = recipe; this.unordered = unordered; // if not unordered, then it's just a single row if (!unordered) { this.recipe = new BlockValue[recipe.Length, 1]; for (int i = 0; i < recipe.Length; i++) { this.recipe[i, 0] = recipe[i]; } } this.result = result; }
private void Update() { if (inventory != null && menuManager.CurrentMenu != MenuManager.MenuStatus.BlockInventory && menuManager.CurrentMenu != MenuManager.MenuStatus.Inventory && !menuManager.paused && customBlockOwnerPosition != LVector3.Invalid && customBlockOwner != null) { if (customBlockOwner.ReturnsItemsWhenDeselected() && playerUsing != null) { for (int i = 0; i < inventory.blocks.Length; i++) { if (inventory.blocks[i].Block != BlockValue.Air && inventory.blocks[i].count > 0) { if (playerUsing.inventory.CanAddBlock(inventory.blocks[i])) { if (playerUsing.inventory.TryToAddBlock(inventory.blocks[i])) { } else { World.mainWorld.CreateBlockEntity(inventory.blocks[i], (new LVector3(0, 1, 0) + customBlockOwnerPosition).BlockCentertoUnityVector3()); } inventory.blocks[i] = new BlockStack(BlockValue.Air, 0); } } } playerUsing = null; customBlockOwner = null; customBlockOwnerPosition = LVector3.Invalid; } } if (inventory == null || !displaying || playerUsing == null) { hasInventory = false; } if (!displaying || playerUsing == null) { for (int i = 0; i < blockItems.Count; i++) { GameObject.Destroy(blockItems[i].gameObject); } blockItems.Clear(); return; } if (!hasInventory && inventory != null && displaying && playerUsing != null) { hasInventory = true; CallInventoryModifiedCallbacks(); } if (selection < 0) { selection = 0; } int actualMaxItems = inventory.capacity; if (maxItems != -1) { actualMaxItems = Mathf.Min(maxItems, actualMaxItems); } if (selection > actualMaxItems - 1) { selection = actualMaxItems - 1; } ShowInventory(numRows, maxItems); if (playerUsing.mouseLook.allowedToCapture) { if (HoldingSomethingWithMouse()) { ThrowStuff(); } return; } bool inventoryModified = false; // click when not capturing if (!HoldingSomethingWithMouse()) { if (Input.GetMouseButtonDown(0) && displaying) { for (int i = 0; i < blockItems.Count; i++) { if (i < inventory.blocks.Length) { //Debug.Log("trying " + i + " " + inventory.blocks[i] + " " + name); if (MouseIntersectsBlockEntity(blockItems[i]) && !IsEmptyBlockStack(inventory.blocks[i])) { //Debug.Log("got dat boi"); playerUsing.blocksHoldingWithMouse = inventory.blocks[i].Copy(); playerUsing.holdingWithMouseEntity = MakeNewBlockEntity(inFront: true); playerUsing.holdingWithMouseEntity.Stack = inventory.blocks[i]; inventory.blocks[i] = new BlockStack(BlockValue.Air, 0); inventoryModified = true; //playerUsing.mouseLook.allowedToCapture = false; break; } } else { int ind = i - inventory.blocks.Length; //Debug.Log("trying " + i + " " + inventory.blocks[i] + " " + name); if (MouseIntersectsBlockEntity(blockItems[i]) && !IsEmptyBlockStack(inventory.resultBlocks[ind])) { //Debug.Log("got dat boi"); playerUsing.blocksHoldingWithMouse = inventory.resultBlocks[ind].Copy(); playerUsing.holdingWithMouseEntity = MakeNewBlockEntity(inFront: true); playerUsing.holdingWithMouseEntity.Stack = inventory.resultBlocks[ind].Copy(); inventory.resultBlocks[ind] = new BlockStack(BlockValue.Air, 0); inventoryModified = true; //playerUsing.mouseLook.allowedToCapture = false; break; } } } } } else if (HoldingSomethingWithMouse() && playerUsing.holdingWithMouseEntity != null && displaying) { int prevCount = playerUsing.blocksHoldingWithMouse.count; //player.mouseLook.allowedToCapture = false; //Vector3 offset; //Ray ray = playerUsing.mainCamera.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0.1f)); playerUsing.holdingWithMouseEntity.transform.localPosition = new Vector3(Input.mousePosition.x - Screen.width / 2.0f, Input.mousePosition.y - Screen.height / 2.0f, -0.02f); if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)) { //bool foundCollision = false; for (int i = 0; i < blockItems.Count; i++) { if (i < inventory.blocks.Length) { if (MouseIntersectsBlockEntity(blockItems[i])) { //foundCollision = true; bool letGo = false; if (IsEmptyBlockStack(inventory.blocks[i])) { // right click, drop one if (Input.GetMouseButtonDown(1)) { inventory.blocks[i] = playerUsing.blocksHoldingWithMouse.Copy(); inventory.blocks[i].count = 1; inventoryModified = true; if (playerUsing.blocksHoldingWithMouse.count == 1) { letGo = true; } else { playerUsing.blocksHoldingWithMouse.count -= 1; } } // left click, put all else if (Input.GetMouseButtonDown(0)) { inventory.blocks[i] = playerUsing.blocksHoldingWithMouse.Copy(); inventoryModified = true; letGo = true; } } else { if (inventory.blocks[i].block == playerUsing.blocksHoldingWithMouse.block) { int numMaxStack = 1; if (World.stackableSize.ContainsKey(playerUsing.blocksHoldingWithMouse.block)) { numMaxStack = World.stackableSize[playerUsing.blocksHoldingWithMouse.block]; } // right click, drop one if (Input.GetMouseButtonDown(1)) { // can fit some in, put 1 in if (inventory.blocks[i].count < numMaxStack) { // we are only holding one, put it all in if (playerUsing.blocksHoldingWithMouse.count == 1) { inventory.blocks[i].count += playerUsing.blocksHoldingWithMouse.count; inventoryModified = true; letGo = true; playerUsing.blocksHoldingWithMouse = new BlockStack(BlockValue.Air, 0); } // put 1 in and leave rest of stack in hand else { inventory.blocks[i].count += 1; inventoryModified = true; playerUsing.blocksHoldingWithMouse.count -= 1; if (playerUsing.blocksHoldingWithMouse.count <= 0) { Debug.LogError("we should have count > 0 when not putting all (right click and we have more than 1 put only put 1 in) into block, got count " + playerUsing.blocksHoldingWithMouse.count + " instead"); } } } } // left click, place all sorta else if (Input.GetMouseButtonDown(0)) { // can fit some in if (inventory.blocks[i].count < numMaxStack) { // can fit all in if (inventory.blocks[i].count + playerUsing.blocksHoldingWithMouse.count <= numMaxStack) { inventory.blocks[i].count += playerUsing.blocksHoldingWithMouse.count; inventoryModified = true; letGo = true; playerUsing.blocksHoldingWithMouse = new BlockStack(BlockValue.Air, 0); } // can only fit some in else { int numCanFitIn = numMaxStack - inventory.blocks[i].count; inventory.blocks[i].count += numCanFitIn; inventoryModified = true; playerUsing.blocksHoldingWithMouse.count -= numCanFitIn; if (playerUsing.blocksHoldingWithMouse.count <= 0) { Debug.LogError("we should have count > 0 when not putting all into block, got count " + playerUsing.blocksHoldingWithMouse.count + " instead"); } } } // can't fit any more in, swap else { inventory.blocks[i].count = playerUsing.blocksHoldingWithMouse.count; inventoryModified = true; playerUsing.blocksHoldingWithMouse.count = numMaxStack; } } } // different things, swap else if (Input.GetMouseButtonDown(0)) { BlockStack tmp = inventory.blocks[i].Copy(); inventory.blocks[i] = playerUsing.blocksHoldingWithMouse.Copy(); inventoryModified = true; playerUsing.blocksHoldingWithMouse = tmp; playerUsing.holdingWithMouseEntity.Stack = tmp; } } if (letGo) { playerUsing.blocksHoldingWithMouse = new BlockStack(BlockValue.Air, 0); GameObject.Destroy(playerUsing.holdingWithMouseEntity.gameObject); playerUsing.holdingWithMouseEntity = null; //mouseLook.allowedToCapture = true; } } } // result blocks else { int ind = i - inventory.blocks.Length; //Debug.Log("trying " + i + " " + inventory.blocks[i] + " " + name); if (MouseIntersectsBlockEntity(blockItems[i]) && !IsEmptyBlockStack(inventory.resultBlocks[ind])) { // only work if we add the whole stack to it if (playerUsing.blocksHoldingWithMouse.TryToAddToStack(inventory.resultBlocks[ind])) { inventory.resultBlocks[ind] = new BlockStack(BlockValue.Air, 0); inventoryModified = true; //playerUsing.mouseLook.allowedToCapture = false; break; } } } } // didn't click on anything, threw on ground instead //if (!foundCollision) //{ // ThrowStuff(); //} } if (!IsEmptyBlockStack(playerUsing.blocksHoldingWithMouse) && prevCount != playerUsing.blocksHoldingWithMouse.count) { playerUsing.holdingWithMouseEntity.blockStack.count = playerUsing.blocksHoldingWithMouse.count; } } if (HoldingSomethingWithMouse() && displaying && playerUsing.holdingWithMouseEntity != null) { //player.mouseLook.allowedToCapture = false; //Vector3 offset; //Ray ray = playerUsing.mainCamera.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0.1f)); playerUsing.holdingWithMouseEntity.transform.localPosition = new Vector3(Input.mousePosition.x - Screen.width / 2.0f, Input.mousePosition.y - Screen.height / 2.0f, -0.02f); } if (inventoryModified) { CallInventoryModifiedCallbacks(); } }
public void ShowInventoryItem(int index, Vector2 offset, float rowP, float columnP, BlockStack itemStack, bool resultBlocks) { if (customBlockOwner != null) { if (resultBlocks) { offset += customBlockOwner.OutputSlotOffset(index); } else { offset += customBlockOwner.InventorySlotOffset(index); } } // 0 to 1 -> -0.5 to 0.5 BlockEntity displayItem = blockItems[index]; float xPos = inventoryWidth * columnP + offset.x; float yPos = inventoryHeight * rowP + offset.y; xPos -= actualInventoryWidth / 2.0f; yPos -= actualInventoryHeight / 2.0f; if (IsEmptyBlockStack(itemStack)) { displayItem.blockId = -1; displayItem.blockStack = null; } else { displayItem.blockId = itemStack.block; displayItem.blockStack = itemStack; } if (!IsEmptyBlockStack(itemStack) && itemStack.count > 1) { displayItem.GetComponentInChildren <UnityEngine.UI.Text>().text = itemStack.count + ""; } else { displayItem.GetComponentInChildren <UnityEngine.UI.Text>().text = ""; } if (index == selection) { displayItem.selected = true; } else { displayItem.selected = false; } displayItem.transform.localPosition = Vector3.right * xPos + Vector3.up * yPos; }
bool IsEmptyBlockStack(BlockStack blockStack) { return(blockStack == null || (blockStack.Block == BlockValue.Air && blockStack.count == 0)); }
// Update is called once per frame void Update() { if (movingEntity == null) { movingEntity = GetComponent <MovingEntity>(); } if (playerPulling != null) { transform.position += movingEntity.GetVel() * Time.deltaTime + movingEntity.desiredMove * Time.deltaTime; //movingEntity.SetVel(movingEntity.GetVel() * 0.99f); } timeSinceSpawned += Time.deltaTime; if (blockName != "") { try { blockId = BlockUtils.StringToBlockId(blockName); if (blockStack != null) { blockStack.block = blockId; } blockName = ""; } catch { } } if (movingEntity != null && movingEntity.IsTouchingGround()) { movingEntity.desiredMove *= 0.9f; } if (playerThrowing != null) { movingEntity.speed = 5.0f; if (movingEntity.IsTouchingGround()) { //movingEntity.desiredMove *= 0.9f; } if (Time.time - timeThrown > 3.3f) { playerThrowing = null; } } if (blockStack == null) { blockStack = new BlockStack(blockId, 1); } if (blockStack != null) { if (blockStack.count <= 0) { blockStack = null; } else { blockId = blockStack.block; if (transform.GetComponentInChildren <UnityEngine.UI.Text>() != null) { if (blockStack.count > 1) { transform.GetComponentInChildren <UnityEngine.UI.Text>().text = blockStack.count + ""; } else { transform.GetComponentInChildren <UnityEngine.UI.Text>().text = ""; } } } } if (playerPulling != null || !pullable) { if (movingEntity != null) { movingEntity.enabled = false; } } if (playerPulling == null && pullable) { if (movingEntity != null) { movingEntity.enabled = true; } } if (playerPulling == null && movingEntity != null && movingEntity.enabled) { if (movingEntity.IsTouchingGround() && informStuffBelow.Do()) { RaycastResults blockStandingOn = movingEntity.BlockStandingOn(); //Debug.Log("checking below entity, hit block " + BlockValue.IdToBlockName(blockStandingOn.hitBlock.Block)); Blocks.BlockOrItem customBlock; if (World.mainWorld.customBlocks.ContainsKey(blockStandingOn.hitBlock.Block, out customBlock)) { //Debug.Log("calling block stack above on it"); using (BlockData hitBlockData = World.mainWorld.GetBlockData(blockStandingOn.hitBlock)) { BlockStack myStack = Stack; BlockStack resStack; customBlock.BlockStackAbove(hitBlockData, myStack, transform.position, out resStack); if (resStack == null || resStack.count <= 0 || resStack.block == BlockValue.Air) { Destroy(transform.gameObject); } else { Stack = resStack; } } } } } if (GetComponent <UnityEngine.UI.Outline>() != null) { GetComponent <UnityEngine.UI.Outline>().enabled = selected; } if (GetComponentInChildren <DurabilityBar>() != null) { DurabilityBar bar = GetComponentInChildren <DurabilityBar>(); bar.enabled = blockStack != null && blockStack.maxDurability != 0; if (bar.enabled) { bar.durability = blockStack.durability / (float)blockStack.maxDurability; } } UpdateTexture(); if (pullable) { transform.rotation *= Quaternion.Euler(0, 12.0f * Time.deltaTime, 0); } }
public void ChangeSize(int newInventorySize, Vector3 throwLeftoversPosition) { if (newInventorySize <= 0) { Debug.LogWarning("inventory size should not be set to less than or equal to zero, tried to set it to " + newInventorySize + " ignoring"); return; } else { // inventory grew, copy the contents over if (capacity < newInventorySize) { BlockStack[] newBlocks = new BlockStack[newInventorySize]; for (int i = 0; i < blocks.Length; i++) { newBlocks[i] = blocks[i]; } // fill remaining with empty slots (need to do this cause unity chooses weird default values otherwise) for (int i = blocks.Length; i < newBlocks.Length; i++) { newBlocks[i] = new BlockStack(BlockValue.Air, 0); } blocks = newBlocks; } // inventory shrunk, copy the contents over and throw any stuff that we can't fit else if (capacity > newInventorySize) { BlockStack[] newBlocks = new BlockStack[newInventorySize]; for (int i = 0; i < newBlocks.Length; i++) { newBlocks[i] = blocks[i]; } // try to squeeze stuff in any open spots for (int i = newBlocks.Length; i < blocks.Length; i++) { if (!IsEmptyBlockStack(blocks[i])) { bool foundPlaceForThisItem = false; // go through new inventory and look for open spots for (int j = 0; j < newBlocks.Length; j++) { if (IsEmptyBlockStack(newBlocks[j])) { newBlocks[j] = blocks[i]; foundPlaceForThisItem = true; break; } } if (!foundPlaceForThisItem) { blocks[i].ThrowMe(throwLeftoversPosition); } } } blocks = newBlocks; } // inventory stayed the same size, do nothing } this.capacity = newInventorySize; }
public bool InventoryMatchesRecipe(Inventory inventory, int nRows, int maxBlocks, bool useResources) { if (maxBlocks == -1) { maxBlocks = inventory.capacity; } if (maxBlocks > inventory.capacity) { maxBlocks = inventory.capacity; } if (maxBlocks % nRows != 0) { Debug.LogError("maxBlocks size of " + maxBlocks + " is not a multiple of given nRows=" + nRows); return(false); } if (unordered) { int numMatchesNeeded = unorderedRecipe.Length; int numMatches = 0; int[] matches = new int[unorderedRecipe.Length]; for (int i = 0; i < matches.Length; i++) { matches[i] = -1; } for (int i = 0; i < maxBlocks; i++) { if (!IsEmptyBlockStack(inventory.blocks[i])) { bool foundMatch = false; for (int j = 0; j < unorderedRecipe.Length; j++) { if (matches[j] == -1 && unorderedRecipe[j] == inventory.blocks[i].Block) { matches[j] = i; foundMatch = true; numMatches += 1; break; } } if (!foundMatch) { return(false); } } } if (numMatches == numMatchesNeeded) { if (useResources) { for (int i = 0; i < matches.Length; i++) { inventory.blocks[matches[i]].count -= 1; if (inventory.blocks[matches[i]].count <= 0) { inventory.blocks[matches[i]] = new BlockStack(BlockValue.Air, 0); } } } return(true); } return(false); } else { int nColumns = maxBlocks / nRows; int numMatchesNeeded = recipe.GetLength(0) * recipe.GetLength(1); for (int topLeftX = 0; topLeftX < nColumns; topLeftX++) { for (int topLeftY = 0; topLeftY < nRows; topLeftY++) { int numMatches = 0; for (int i = 0; i < recipe.GetLength(1); i++) { for (int j = 0; j < recipe.GetLength(0); j++) { int x = topLeftX + i; int y = topLeftY + j; if (x < nColumns && y < nRows) { int index = x + y * nColumns; BlockStack cur = inventory.blocks[index]; BlockValue recipeCur = recipe[recipe.GetLength(0) - j - 1, i]; if (IsEmptyBlockStack(cur)) { if (recipeCur == BlockValue.Air) { //Debug.Log(x + " " + y + "inventory empty here and recipe is too, recipeCur=" + World.BlockToString((int)recipeCur)); numMatches += 1; } else { //Debug.Log(x + " " + y + "inventory empty here and recipe is not, recipeCur=" + World.BlockToString((int)recipeCur)); } } else if (!IsEmptyBlockStack(cur)) { if (cur.block == recipeCur) { //Debug.Log(x + " " + y + "same: inventory is " + World.BlockToString(cur.block) + " and recipe is " + World.BlockToString((int)recipeCur)); numMatches += 1; } else { //Debug.Log(x + " " + y + "different: inventory is " + World.BlockToString(cur.block) + " and recipe is " + World.BlockToString((int)recipeCur)); } } } } } //Debug.Log("got num matches " + numMatches + " with top left x=" + topLeftX + " and topLeftY=" + topLeftY); if (numMatches == numMatchesNeeded) { bool hasExtraStuff = false; for (int x = 0; x < topLeftX; x++) { for (int y = 0; y < nRows; y++) { int index = x + y * nColumns; if (!IsEmptyBlockStack(inventory.blocks[index])) { hasExtraStuff = true; break; } } if (hasExtraStuff) { break; } } for (int x = topLeftX; x < nColumns; x++) { for (int y = 0; y < topLeftY; y++) { int index = x + y * nColumns; if (!IsEmptyBlockStack(inventory.blocks[index])) { hasExtraStuff = true; break; } } if (hasExtraStuff) { break; } } if (hasExtraStuff) { //Debug.Log("matches recipe but has extra stuff"); return(false); } else { //Debug.Log("matches recipe completely"); if (useResources) { for (int i = 0; i < recipe.GetLength(1); i++) { for (int j = 0; j < recipe.GetLength(0); j++) { int x = topLeftX + i; int y = topLeftY + j; int index = x + y * nColumns; if (!IsEmptyBlockStack(inventory.blocks[index])) { inventory.blocks[index].count -= 1; if (inventory.blocks[index].count <= 0) { inventory.blocks[index] = new BlockStack(BlockValue.Air, 0); } } } } } return(true); } } else if (numMatches > numMatchesNeeded) { Debug.LogWarning("numMatches = " + numMatches + " but maximum num matches is " + numMatchesNeeded + " this is invalid how u do this"); } } } return(false); } }
public Recipe(BlockValue[,] recipe, BlockStack result, float timeToFinish = 0) { this.recipe = recipe; this.unordered = false; this.result = result; }