public bool Load(Pos warehouseLocation, List <Product> itemsToLoad) { int newWeight = CurrentWeight; for (int i = 0; i < itemsToLoad.Count; i++) { newWeight += ItemWeights[i] * itemsToLoad[i].ProductQuantity; } if (newWeight > MaxWeight) { return(false); } else { bool found; for (int i = 0; i < itemsToLoad.Count; i++) { found = false; int left = 0, right = HeldItems.Count - 1, mid = 0; while (left <= right) { mid = left + ((right - left) / 2); if (HeldItems[mid].ProductNumb > itemsToLoad[i].ProductNumb) { right = mid - 1; } else if (HeldItems[mid].ProductNumb < itemsToLoad[i].ProductNumb) { left = mid + 1; } else { HeldItems[mid].ProductQuantity += itemsToLoad[i].ProductQuantity; found = true; break; } } //------------------------------------------------------------------- if (found == false) { HeldItems.Insert(mid, itemsToLoad[i]); } } return(true); } }
// Update is called once per frame void Update() { if (Dead) { return; } // First look for any prompts // Pickup Prompt if (PickupPromptOn) { // Confirm pickup if (Input.GetButtonDown("A_Button_" + PlayerNumber)) { grabbed = true; HeldItem = PickupPromptObject; SpeechBubble.SetActive(false); BubbleText.text = ""; PickupPromptOn = false; // Put into UI Inventory HeldItemImage.sprite = HeldItem.GetComponent <SpriteRenderer>().sprite; // Turn off the item HeldItem.SetActive(false); // Whatever sort group the item was before, just make it be the player level now HeldItem.GetComponent <SpriteRenderer>().sortingLayerName = "Player"; } // Deny pickup else if (Input.GetButtonDown("B_Button_" + PlayerNumber)) { SpeechBubble.SetActive(false); BubbleText.text = ""; PickupPromptOn = false; } return; } // Swap Prompt else if (SwapPromptOn) { // Confirm pickup if (Input.GetButtonDown("A_Button_" + PlayerNumber)) { // Swap the location of the held item with the swap item HeldItem.transform.position = SwapPromptObject.transform.position; HeldItem.SetActive(true); // Pickup the new item HeldItem = SwapPromptObject; SpeechBubble.SetActive(false); BubbleText.text = ""; SwapPromptOn = false; // Put into UI Inventory HeldItemImage.sprite = HeldItem.GetComponent <SpriteRenderer>().sprite; // Turn off the item HeldItem.SetActive(false); } // Deny pickup else if (Input.GetButtonDown("B_Button_" + PlayerNumber)) { SpeechBubble.SetActive(false); BubbleText.text = ""; SwapPromptOn = false; } return; } // Change character input if (Input.GetButtonDown("LB_Button_" + PlayerNumber)) { Invoke("ChangePlayerNumberDown", 0.2f); return; } else if (Input.GetButtonDown("RB_Button_" + PlayerNumber)) { Invoke("ChangePlayerNumberUp", 0.2f); return; } // Input for movement xInput = Input.GetAxis("Horizontal" + PlayerNumber); yInput = Input.GetAxis("Vertical" + PlayerNumber); // Handle the player moving horizontally if (xInput >= 0.5f) { // Moving Right IsWalking = true; // Toggle animation front.SetBool("Walk", IsWalking); side.SetBool("Walk", IsWalking); back.SetBool("Walk", IsWalking); if (Class == "Illusionist") { side.transform.localScale = new Vector3(Math.Abs(side.transform.localScale.x), side.transform.localScale.y, side.transform.localScale.z); } else { side.transform.localScale = new Vector3(Math.Abs(side.transform.localScale.x) * -1, side.transform.localScale.y, side.transform.localScale.z); } // Set the side to active side.gameObject.SetActive(true); // Turn of the others front.gameObject.SetActive(false); back.gameObject.SetActive(false); } else if (xInput <= -0.5f) { // Moving Left IsWalking = true; // Toggle animation front.SetBool("Walk", IsWalking); side.SetBool("Walk", IsWalking); back.SetBool("Walk", IsWalking); if (Class == "Illusionist") { side.transform.localScale = new Vector3(Math.Abs(side.transform.localScale.x) * -1, side.transform.localScale.y, side.transform.localScale.z); } else { side.transform.localScale = new Vector3(Math.Abs(side.transform.localScale.x), side.transform.localScale.y, side.transform.localScale.z); } // Set the side to active side.gameObject.SetActive(true); // Turn off the others front.gameObject.SetActive(false); back.gameObject.SetActive(false); } else if (yInput >= 0.5f) { // Moving Up IsWalking = true; // Toggle animation front.SetBool("Walk", IsWalking); side.SetBool("Walk", IsWalking); back.SetBool("Walk", IsWalking); // Set the back to active back.gameObject.SetActive(true); // Turn off the others front.gameObject.SetActive(false); side.gameObject.SetActive(false); } else if (yInput <= -0.5f) { // Moving Down IsWalking = true; // Toggle animation front.SetBool("Walk", IsWalking); side.SetBool("Walk", IsWalking); back.SetBool("Walk", IsWalking); // Set the front to active front.gameObject.SetActive(true); // Turn off the others back.gameObject.SetActive(false); side.gameObject.SetActive(false); } else { IsWalking = false; // Toggle animation front.SetBool("Walk", IsWalking); side.SetBool("Walk", IsWalking); back.SetBool("Walk", IsWalking); } var moveVector = new Vector3(xInput, yInput, 0) * SPEED * Time.deltaTime * 10; Body.MovePosition(new Vector2(transform.position.x + moveVector.x, transform.position.y + moveVector.y)); //Grabber code Vector3 dir = new Vector2(xInput, yInput); if (dir == Vector3.zero) { dir = previousGood; } else { previousGood = dir; } // B Button Pressed if (Input.GetButtonDown("B_Button_" + PlayerNumber)) { // Drop a held Item, handle UI images if (grabbed) { DropItem(); } } // X Button Pressed if (Input.GetButtonDown("X_Button_" + PlayerNumber)) { if (grabbed) { // Get the helditem's description HeldItems item = HeldItem.GetComponent <HeldItems>(); if (item != null) { BubbleText.text = item.ItemDescription; } else { BubbleText.text = "Looks like I'm holding " + HeldItem.name; } SpeechBubble.SetActive(true); Invoke("DeactivateSpeechBubble", 4f); // Special case for Rusty knife that hurts the player if (HeldItem.name == "Rusty Knife") { TakeDamage(); } } } // A Button Pressed if (Input.GetButtonDown("A_Button_" + PlayerNumber) || Input.GetKeyDown(KeyCode.Space)) { // try to interact if (Interacting) { ActivateSpeechBubble(); InteractingWith.TryToInteractWithThisObject(this.Class, ref this.HeldItem, ref this.BubbleText, ref this.grabbed); if (grabbed) { // Put into UI Inventory HeldItemImage.sprite = HeldItem.GetComponent <SpriteRenderer>().sprite; // Turn off the item HeldItem.SetActive(false); } else { // Take out of UI Inventory HeldItemImage.sprite = defaultItem; } return; } else if (StoveInteracting) { ActivateSpeechBubble(); StoveInteractingWith.TryToInteractWithThisObject(this.Class, ref this.HeldItem, ref this.BubbleText, ref this.grabbed); if (grabbed) { // Put into UI Inventory HeldItemImage.sprite = HeldItem.GetComponent <SpriteRenderer>().sprite; // Turn off the item HeldItem.SetActive(false); } else { // Take out of UI Inventory HeldItemImage.sprite = defaultItem; } return; } print("Throwing out raycast to look for objects to pickup"); //Raycast Zone of pickup after shifting the pickup orgin down Vector3 Adjustment = new Vector2(0, 3.3f); Collider2D[] collide = Physics2D.OverlapBoxAll(transform.position + Adjustment + (dir * 10), new Vector2(10, 10), 0); bool collidedWithAnItem = false; string collidedWithName = null; // Search each collision looking for an item to pickup foreach (Collider2D collision in collide) { // Make sure you can actually pick item up before moving it if (collision.tag == "Item" && collision.gameObject != HeldItem && collision.gameObject.GetComponent <HeldItems>().CanPickThisUp) { collidedWithAnItem = true; if (!grabbed) { // Prompt player to pick up item SpeechBubble.SetActive(true); BubbleText.text = "Do you want to pick this " + collision.name + " up?\n A : Pickup B : Ignore"; PickupPromptOn = true; PickupPromptObject = collision.gameObject; return; } else { // Prompt player to swap item SpeechBubble.SetActive(true); BubbleText.text = "Do you want to swap for the " + collision.name + "?\n A : Swap B : Ignore"; SwapPromptOn = true; SwapPromptObject = collision.gameObject; return; } } // Ignore players else if (collision.tag == "Player") { continue; } // Save the collided with name for later to put in speech bubble collidedWithName = collision.name; } // Show a speech bubble of whatever else was collided with if (!collidedWithAnItem && collidedWithName != null) { ActivateSpeechBubble(); BubbleText.text = "It's a " + collidedWithName; } } // Y Button Pressed if (Input.GetButtonDown("Y_Button_" + PlayerNumber)) { print(this.name + " just used their ability"); // Call the correct ability based on object name switch (Class) { case "Scholar": DoScholarAbility(); return; case "Burner": DoBurnerAbility(); return; case "Parkourist": DoParkouristAbility(); return; case "Illusionist": DoIllusionistAbility(); return; } } // If someone other than burner is holding fireball they take damage if (CanBeBurned && grabbed && HeldItem.name == "Fireball" && Class != "Burner") { CanBeBurned = false; TakeDamage(); Invoke("CanBeBurnedAgain", 1.5f); } }