public bool [ , ] GetSkippedSlots(OSItem except) { bool [ , ] skip = new bool [width, height]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { OSSlot slot = inventory.GetSlot(x, y); if (slot == null || slot.hidden || (slot.item != null && slot.item == except)) { continue; } for (int sx = 0; sx < slot.scale.x; sx++) { for (int sy = 0; sy < slot.scale.y; sy++) { if ((sx == 0 && sy == 0) || x + sx >= width || y + sy >= height) { continue; } else { skip [x + sx, y + sy] = true; } } } } } return(skip); }
public void Move ( OSSlot slot, int x, int y ) { if ( !slot.item ) { return; } if ( CheckSlot ( x, y, slot.item ) ) { slot.x = x; slot.y = y; } }
public void DecreaseSlot(OSSlot slot) { slot.quantity--; if (slot.quantity < 1) { RemoveSlot(slot); } }
public void DecreaseItem(OSItem item) { OSSlot slot = GetSlot(item); if (slot != null) { DecreaseSlot(slot); } }
public void Move(OSSlot slot, int x, int y) { if (!slot.item) { return; } if (CheckSlot(x, y, slot.item)) { slot.x = x; slot.y = y; } }
public void SetQuickSlotEquipped(int i) { if (i < quickSlots.Count && quickSlots[i] < slots.Count) { OSSlot slot = slots [quickSlots[i]]; if (slot.item) { UnequipAll(); SetEquipped(slot.item); } } }
// Get/set items public void SpawnSlot(OSSlot slot, Transform parent, Vector3 position) { if (!slot.item) { return; } for (int i = 0; i < slot.quantity; i++) { GameObject go = (GameObject)Instantiate(Resources.Load(slot.item.prefabPath)); Vector3 scale = go.transform.localScale; OSItem oldItem = slot.item; OSItem newItem = go.GetComponent <OSItem> (); newItem.AdoptValues(oldItem); go.transform.parent = parent; go.transform.position = position; go.transform.localScale = scale; } }
public OSPoint GetAvailableCell(OSItem item) { bool [ , ] skip = GetSkippedSlots(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { bool cancel = false; OSPoint point = new OSPoint(x, y); for (int sx = 0; sx < item.slotSize.x; sx++) { for (int sy = 0; sy < item.slotSize.y; sy++) { OSSlot slot = inventory.GetSlot(x + sx, y + sy); if (slot != null && !slot.hidden && slot.item != null || x + sx >= width || y + sy >= height || skip [x + sx, y + sy]) { cancel = true; } } } if (cancel) { continue; } else { return(point); } } } return(null); }
public bool CheckSlot(int x, int y, OSItem item) { if (x < 0 || y < 0) { return(false); } bool [ , ] skip = GetSkippedSlots(item); for (int sx = 0; sx < item.slotSize.x; sx++) { for (int sy = 0; sy < item.slotSize.y; sy++) { OSSlot slot = inventory.GetSlot(x + sx, y + sy); if ((x + sx < skip.GetLength(0) && y + sy < skip.GetLength(1) && skip [x + sx, y + sy]) || (slot != null && !slot.hidden && slot.item != null && slot.item != item) || x + sx >= width || y + sy >= height) { return(false); } } } return(true); }
public void RemoveSlot ( OSSlot slot ) { slot.equipped = false; slots.Remove ( slot ); }
public void DecreaseSlot ( OSSlot slot ) { slot.quantity--; if ( slot.quantity < 1 ) { RemoveSlot ( slot ); } }
// Get/set items public void SpawnSlot ( OSSlot slot, Transform parent, Vector3 position ) { if ( !slot.item ) { return; } for ( int i = 0; i < slot.quantity; i++ ) { GameObject go = (GameObject) Instantiate ( Resources.Load ( slot.item.prefabPath ) ); Vector3 scale = go.transform.localScale; OSItem oldItem = slot.item; OSItem newItem = go.GetComponent< OSItem > (); newItem.AdoptValues ( oldItem ); go.transform.parent = parent; go.transform.position = position; go.transform.localScale = scale; } }
public override void OnInspectorGUI() { OSInventory inventory = (OSInventory)target; inventory.definitions = (OSDefinitions)EditorGUILayout.ObjectField("Definitions", inventory.definitions, typeof(OSDefinitions), false); if (!inventory.definitions) { GUI.color = Color.red; EditorGUILayout.LabelField("You need to link an OSDefinitions prefab with this inventory", EditorStyles.boldLabel); GUI.color = Color.white; return; } EditorGUILayout.Space(); EditorGUILayout.LabelField("Currency amounts", EditorStyles.boldLabel); for (int i = 0; i < inventory.definitions.currencies.Length; i++) { OSCurrency def = inventory.definitions.currencies[i]; EditorGUILayout.BeginHorizontal(); inventory.CheckCurrency(i); int oldAmount = inventory.GetCurrencyAmount(def.name); int newAmount = EditorGUILayout.IntField(def.name, oldAmount); if (oldAmount != newAmount) { inventory.SetCurrencyAmount(def.name, newAmount); } EditorGUILayout.EndHorizontal(); } EditorGUILayout.Space(); Event evt = Event.current; inventory.grid.inventory = inventory; // Grid OSSlot slot = null; int slotSize = 60; bool mouseDown = evt.type == EventType.MouseDown; bool keyLeft = evt.type == EventType.KeyDown && evt.keyCode == KeyCode.LeftArrow; bool keyRight = evt.type == EventType.KeyDown && evt.keyCode == KeyCode.RightArrow; bool keyUp = evt.type == EventType.KeyDown && evt.keyCode == KeyCode.UpArrow; bool keyDown = evt.type == EventType.KeyDown && evt.keyCode == KeyCode.DownArrow; bool keyTab = evt.type == EventType.KeyDown && evt.keyCode == KeyCode.Tab; bool keyBackspace = evt.type == EventType.KeyDown && evt.keyCode == KeyCode.Backspace; EditorGUILayout.BeginHorizontal(); GUILayout.Space(34); inventory.grid.width = EditorGUILayout.IntField(inventory.grid.width, GUILayout.Width(30)); EditorGUILayout.EndHorizontal(); GUILayout.Space(4); EditorGUILayout.BeginHorizontal(); inventory.grid.height = EditorGUILayout.IntField(inventory.grid.height, GUILayout.Width(30)); Rect rect = EditorGUILayout.GetControlRect(GUILayout.Width(slotSize * inventory.grid.width), GUILayout.Height(slotSize * inventory.grid.height)); int xPos = (int)rect.x; int yPos = (int)rect.y; bool [ , ] skip = inventory.grid.GetSkippedSlots(); if (mouseDown && !rect.Contains(evt.mousePosition)) { selected = null; } for (int x = 0; x < inventory.grid.width; x++) { for (int y = 0; y < inventory.grid.height; y++) { if (skip [x, y] == true) { continue; } else { Texture2D tex = null; OSItem item; Rect slotRect; slot = inventory.GetSlot(x, y); xPos = (int)rect.x + x * slotSize; yPos = (int)rect.y + y * slotSize; if (slot != null && slot.item && !slot.hidden) { item = slot.item; tex = item.preview; slotRect = new Rect(xPos, yPos, slotSize * slot.scale.x, slotSize * slot.scale.y); if (slot == selected) { GUI.backgroundColor = Color.green; GUI.SetNextControlName("Selected"); } GUI.Box(slotRect, ""); GUI.backgroundColor = Color.white; if (tex) { GUI.DrawTexture(slotRect, tex); } if (slot.quantity > 1) { GUI.Label(new Rect(xPos + 4, yPos + slot.scale.y * slotSize - 20, slot.scale.x * slotSize, 20), slot.quantity.ToString()); } if (slotRect.Contains(evt.mousePosition) && mouseDown) { selected = slot; } } else { slotRect = new Rect(xPos, yPos, slotSize, slotSize); if (slotRect.Contains(evt.mousePosition) && mouseDown) { selected = null; } GUI.Box(slotRect, ""); } } } } EditorGUILayout.BeginVertical(); if (selected != null && selected.item) { EditorGUILayout.LabelField(selected.item.id, EditorStyles.boldLabel); EditorGUILayout.LabelField(selected.item.description); EditorGUILayout.Space(); foreach (OSAttribute attribute in selected.item.attributes) { EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField(attribute.name + ":", GUILayout.Width(80)); EditorGUILayout.LabelField(attribute.value + " " + attribute.suffix, GUILayout.Width(80)); EditorGUILayout.EndHorizontal(); } EditorGUILayout.Space(); EditorGUILayout.LabelField("[ " + selected.item.category + " : " + selected.item.subcategory + " ]"); } EditorGUILayout.EndVertical(); EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); OSItem addItem = null; addItem = (OSItem)EditorGUILayout.ObjectField("Add item", addItem, typeof(OSItem), true); if (addItem) { inventory.AddItem(addItem); } GUI.backgroundColor = Color.red; if (GUILayout.Button("Clear inventory")) { inventory.slots.Clear(); } GUI.backgroundColor = Color.white; // ^ Move slot if (selected != null && selected.item) { if (keyLeft) { inventory.grid.Move(selected, selected.x - 1, selected.y); evt.Use(); } else if (keyRight) { inventory.grid.Move(selected, selected.x + 1, selected.y); evt.Use(); } else if (keyDown) { inventory.grid.Move(selected, selected.x, selected.y + 1); evt.Use(); } else if (keyUp) { inventory.grid.Move(selected, selected.x, selected.y - 1); evt.Use(); } else if (keyBackspace) { inventory.RemoveItem(selected.item); evt.Use(); } } if (keyTab) { evt.Use(); if (selected != null && selected.item) { int i = inventory.GetItemIndex(selected.item); if (i < inventory.slots.Count - 1) { i++; } else { i = 0; } selected = inventory.slots[i]; } else if (inventory.slots.Count > 0) { selected = inventory.slots[0]; } GUI.FocusControl("Selected"); } Repaint(); if (GUI.changed) { SavePrefab(target); } }
public override void OnInspectorGUI () { OSInventory inventory = (OSInventory) target; inventory.definitions = (OSDefinitions) EditorGUILayout.ObjectField ( "Definitions", inventory.definitions, typeof ( OSDefinitions ), false ); if ( !inventory.definitions ) { GUI.color = Color.red; EditorGUILayout.LabelField ( "You need to link an OSDefinitions prefab with this inventory", EditorStyles.boldLabel ); GUI.color = Color.white; return; } EditorGUILayout.Space (); EditorGUILayout.LabelField ( "Currency amounts", EditorStyles.boldLabel ); for ( int i = 0; i < inventory.definitions.currencies.Length; i++ ) { OSCurrency def = inventory.definitions.currencies[i]; EditorGUILayout.BeginHorizontal (); inventory.CheckCurrency ( i ); int oldAmount = inventory.GetCurrencyAmount ( def.name ); int newAmount = EditorGUILayout.IntField ( def.name, oldAmount ); if ( oldAmount != newAmount ) { inventory.SetCurrencyAmount ( def.name, newAmount ); } EditorGUILayout.EndHorizontal (); } EditorGUILayout.Space (); Event evt = Event.current; inventory.grid.inventory = inventory; // Grid OSSlot slot = null; int slotSize = 60; bool mouseDown = evt.type == EventType.MouseDown; bool keyLeft = evt.type == EventType.KeyDown && evt.keyCode == KeyCode.LeftArrow; bool keyRight = evt.type == EventType.KeyDown && evt.keyCode == KeyCode.RightArrow; bool keyUp = evt.type == EventType.KeyDown && evt.keyCode == KeyCode.UpArrow; bool keyDown = evt.type == EventType.KeyDown && evt.keyCode == KeyCode.DownArrow; bool keyTab = evt.type == EventType.KeyDown && evt.keyCode == KeyCode.Tab; bool keyBackspace = evt.type == EventType.KeyDown && evt.keyCode == KeyCode.Backspace; EditorGUILayout.BeginHorizontal (); GUILayout.Space ( 34 ); inventory.grid.width = EditorGUILayout.IntField ( inventory.grid.width, GUILayout.Width ( 30 ) ); EditorGUILayout.EndHorizontal (); GUILayout.Space ( 4 ); EditorGUILayout.BeginHorizontal (); inventory.grid.height = EditorGUILayout.IntField ( inventory.grid.height, GUILayout.Width ( 30 ) ); Rect rect = EditorGUILayout.GetControlRect ( GUILayout.Width ( slotSize * inventory.grid.width ), GUILayout.Height ( slotSize * inventory.grid.height ) ); int xPos = (int)rect.x; int yPos = (int)rect.y; bool [ , ] skip = inventory.grid.GetSkippedSlots(); if ( mouseDown && !rect.Contains ( evt.mousePosition ) ) { selected = null; } for ( int x = 0; x < inventory.grid.width; x++ ) { for ( int y = 0; y < inventory.grid.height; y++ ) { if ( skip [ x, y ] == true ) { continue; } else { Texture2D tex = null; OSItem item; Rect slotRect; slot = inventory.GetSlot ( x, y ); xPos = (int)rect.x + x * slotSize; yPos = (int)rect.y + y * slotSize; if ( slot != null && slot.item && !slot.hidden ) { item = slot.item; tex = item.preview; slotRect = new Rect ( xPos, yPos, slotSize * slot.scale.x, slotSize * slot.scale.y ); if ( slot == selected ) { GUI.backgroundColor = Color.green; GUI.SetNextControlName ( "Selected" ); } GUI.Box ( slotRect, "" ); GUI.backgroundColor = Color.white; if ( tex ) { GUI.DrawTexture ( slotRect, tex ); } if ( slot.quantity > 1 ) { GUI.Label ( new Rect ( xPos + 4, yPos + slot.scale.y * slotSize - 20, slot.scale.x * slotSize, 20 ), slot.quantity.ToString() ); } if ( slotRect.Contains ( evt.mousePosition ) && mouseDown ) { selected = slot; } } else { slotRect = new Rect ( xPos, yPos, slotSize, slotSize ); if ( slotRect.Contains ( evt.mousePosition ) && mouseDown ) { selected = null; } GUI.Box ( slotRect, "" ); } } } } EditorGUILayout.BeginVertical (); if ( selected != null && selected.item ) { EditorGUILayout.LabelField ( selected.item.id, EditorStyles.boldLabel ); EditorGUILayout.LabelField ( selected.item.description ); EditorGUILayout.Space (); foreach ( OSAttribute attribute in selected.item.attributes ) { EditorGUILayout.BeginHorizontal (); EditorGUILayout.LabelField ( attribute.name + ":", GUILayout.Width ( 80 ) ); EditorGUILayout.LabelField ( attribute.value + " " + attribute.suffix, GUILayout.Width ( 80 ) ); EditorGUILayout.EndHorizontal (); } EditorGUILayout.Space (); EditorGUILayout.LabelField ( "[ " + selected.item.category + " : " + selected.item.subcategory + " ]" ); } EditorGUILayout.EndVertical (); EditorGUILayout.EndHorizontal (); EditorGUILayout.Space (); OSItem addItem = null; addItem = (OSItem) EditorGUILayout.ObjectField ( "Add item", addItem, typeof(OSItem), true ); if ( addItem ) { inventory.AddItem ( addItem ); } GUI.backgroundColor = Color.red; if ( GUILayout.Button ( "Clear inventory" ) ) { inventory.slots.Clear (); } GUI.backgroundColor = Color.white; // ^ Move slot if ( selected != null && selected.item ) { if ( keyLeft ) { inventory.grid.Move ( selected, selected.x - 1, selected.y ); evt.Use (); } else if ( keyRight ) { inventory.grid.Move ( selected, selected.x + 1, selected.y ); evt.Use (); } else if ( keyDown ) { inventory.grid.Move ( selected, selected.x, selected.y + 1 ); evt.Use (); } else if ( keyUp ) { inventory.grid.Move ( selected, selected.x, selected.y - 1 ); evt.Use (); } else if ( keyBackspace ) { inventory.RemoveItem ( selected.item ); evt.Use (); } } if ( keyTab ) { evt.Use (); if ( selected != null && selected.item ) { int i = inventory.GetItemIndex ( selected.item ); if ( i < inventory.slots.Count - 1 ) { i++; } else { i = 0; } selected = inventory.slots[i]; } else if ( inventory.slots.Count > 0 ) { selected = inventory.slots[0]; } GUI.FocusControl ( "Selected" ); } Repaint (); if ( GUI.changed ) { SavePrefab ( target ); } }
public void RemoveSlot(OSSlot slot) { slot.equipped = false; slots.Remove(slot); }