public void AddItem(int id, int amount) { idleTimer.Active(); Item item = itemDataBase.GetItemByID(id); for (int i = 0; i < slots.Count; i++) { if (slots[i].id == id && amounts[i] + amount <= item.maxStackSize) { if (amounts[i] + amount <= item.maxStackSize) { //We can fill up this slot amounts[i] += amount; return; } else { //Add the remaining to another slot int previousNumber = amounts[i]; int newAmount = item.maxStackSize; int leftOver = amount - (newAmount - previousNumber); amounts[i] = newAmount; AddItem(id, leftOver); return; } } if (slots[i].id == -1) { slots[i] = item; amounts[i] = amount; Debug.Log(string.Format("Added item into player inventory at slot {0}", i)); return; } } Debug.Log("Couldn't find a free slot to place the item in."); if (backPanel.activeInHierarchy) { UpdateUI(); } }
private void Movement() { //Checking if the player is grounded isGrounded = Physics.CheckSphere(groundChecker.position, groundDistance, floorMask, QueryTriggerInteraction.Ignore); hitHead = Physics.CheckSphere(headCheacker.position, groundDistance, floorMask, QueryTriggerInteraction.Ignore); //Checking to see if they are sprinting if (Input.GetKeyDown(kSprint)) { maxSpeed = maxSprintSpeed; idleTimer.Active(); } else if (Input.GetKeyUp(kSprint)) { maxSpeed = maxWalkingSpeed; idleTimer.Active(); } //Checking for keys being pressed if (Input.GetKeyDown(kJump) && isGrounded && canJump) { isGrounded = false; _inputs.y = jumpHeight; idleTimer.Active(); } if (Input.GetKey(kForward) && canMove) { _inputs.z += movementSpeed; idleTimer.Active(); } else if (Input.GetKey(kBackward) && canMove) { _inputs.z -= movementSpeed; idleTimer.Active(); } if (Input.GetKey(kLeft) && canMove) { _inputs.x -= movementSpeed; idleTimer.Active(); } else if (Input.GetKey(kRight) && canMove) { _inputs.x += movementSpeed; idleTimer.Active(); } //Clamping the max speed if (_inputs.x > maxSpeed) { _inputs.x = maxSpeed; } else if (_inputs.x < -maxSpeed) { _inputs.x = -maxSpeed; } if (_inputs.z > maxSpeed) { _inputs.z = maxSpeed; } else if (_inputs.z < -maxSpeed) { _inputs.z = -maxSpeed; } if (_inputs.y < -maxFallSpeed) { _inputs.y = -maxFallSpeed; } //Moving the player at a fixed frame rate characterController.Move(transform.TransformVector(_inputs * Time.deltaTime)); //Slowing down the player back to 0 _inputs.x = Mathf.Lerp(_inputs.x, 0f, breakingSpeed * Time.fixedDeltaTime); _inputs.z = Mathf.Lerp(_inputs.z, 0f, breakingSpeed * Time.fixedDeltaTime); }