Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class Inventory : MonoBehaviour {
- // Self explanatory..
- public int InventoryWidth = 400;
- public int InventoryHeight = 400;
- public Item StartingItem; // The item the player start with.
- private ItemStack LeftHand; // Item the player is holding in left hand, this variable isn't used for anything yet.
- private ItemStack RightHand; // Item the player is holding in right hand, this variable isn't used for anything yet.
- private ItemStack[] Items = new ItemStack[26]; // Array of itemstacks. This holds the player's items.
- private ItemStack SelectedItem; // When you click on an item it becomes the selected item, and info about it is
- // shown in a box at the top.
- private bool InventoryOpen = false;
- private bool SeeingItem = false; // Just a bool for if the box with info about item in it is showing or not.
- void Start() {
- AddItem(StartingItem);
- }
- public void AddItem(Item item) {
- // Check if a stack is already there for us to stack the item with
- for(int i = 0; i <= 25; i++) {
- // If nothing is in this inventory slot, check the next one.
- if(Items[i] == null) {
- continue;
- }
- // If stack is found, increase the stack size.
- if(Items[i].ContainingItem.Name == item.Name) {
- Items[i].StackSize++;
- return;
- }
- }
- // Create a new item stack.
- ItemStack st = new ItemStack(item, 1);
- // If no stacks of the same item is found, then
- // check for space in inventory to put a new item stack into.
- for(int i = 0; i <= 25; i++) {
- // If item in inventory slot is null (no itemstack inside)
- if(Items[i] == null) {
- // Add a new item stack.
- Items[i] = st;
- return;
- }
- }
- /*foreach(ItemStack i in Items) {
- Debug.Log(i.ContainingItem.Name);
- }*/
- }
- void Update() {
- if(Input.GetKeyDown(KeyCode.E)) {
- InventoryOpen = !InventoryOpen;
- SeeingItem = false;
- }
- }
- void OnGUI() {
- if(InventoryOpen) {
- var pos = new Vector2(Screen.width / 2 - InventoryWidth / 2, Screen.height / 2 - InventoryHeight / 2);
- if(SeeingItem) {
- pos.y += 75;
- GUI.Box( new Rect(pos.x, pos.y - 110, InventoryWidth, 100), "Selected Item: " + SelectedItem.ContainingItem.Name );
- }
- // Just some fancy maths for the rest of the script, doesn't have anything to do with my problem
- GUI.Box( new Rect(pos.x, pos.y, InventoryWidth, InventoryHeight), "Inventory" );
- var buttonnum = 0;
- for(int y = 1; y <= 5; y++) {
- for(int x = 1; x <= 5; x++) {
- buttonnum++;
- var posx = pos.x + x * 60;
- var posy = pos.y + y * 60;
- if(x == 1) {
- posx = pos.x + x * 55;
- }
- if(y == 1) {
- posy = pos.y + y * 55;
- }
- if( Items[buttonnum] == null ) {
- // NO ITEM
- if( GUI.Button(new Rect(posx, posy, 50, 50), "" ) ) {
- // DO NOTHING
- }
- }else{
- if( GUI.Button(new Rect(posx, posy, 50, 50), Items[buttonnum].ContainingItem.Icon ) ) {
- SelectedItem = Items[buttonnum];
- SeeingItem = true;
- }
- }
- }
- }
- buttonnum = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement