Esempio n. 1
0
        public void CreateItem(GameObject item, int quantity = 1)
        {
            Debug.Assert(quantity >= 1 && quantity <= 4);

            for (int i = 0; i < quantity; ++i)
            {
                GameObject newObj = Instantiate(item);
                newObj.gameObject.name = item.name;

                CharacterInventory inv = GetComponentInChildren <CharacterInventory>();
                Debug.Assert(inv != null);

                PixelItem pixelItem = newObj.GetComponent <PixelItem>();

                bool succeed = inv.AddItem(pixelItem);
                if (succeed)
                {
                    newObj.gameObject.SetActive(false);
                    newObj.transform.parent = inv.transform;
                }
                else
                {
                    Destroy(newObj);
                    Debug.LogWarning("Inventory Full");
                }
            }
        }
Esempio n. 2
0
        public void Steals(int number, string item, Character character)
        {
            CharacterInventory inv = GetComponentInChildren <CharacterInventory>();

            Debug.Assert(inv != null);

            bool hasItem = inv.HasItem(item, number);

            if (!hasItem)
            {
                Debug.LogWarning(this.gameObject.name + " does not have " + number + " " + item);
                return;
            }

            Debug.Assert(number >= 1 && number <= 24);

            CharacterInventory toInv = character.GetComponentInChildren <CharacterInventory>();

            Debug.Assert(toInv != null);

            animator.SetTrigger(Animator.StringToHash("IsInteract"));

            for (int i = 0; i < number; ++i)
            {
                GameObject obj = toInv.GetItem(item);
                Debug.Assert(obj != null);
                PixelItem pixelItem = obj.GetComponent <PixelItem>();
                toInv.AddItem(pixelItem);
                obj.transform.parent = toInv.transform;
            }
        }
Esempio n. 3
0
        public void Takes(int number, string item, PixelStorage pixelStorage)
        {
            CharacterInventory inv = GetComponentInChildren <CharacterInventory>();

            Debug.Assert(inv != null);

            bool hasItem = pixelStorage.HasObject(item, number);

            if (!hasItem)
            {
                Debug.LogWarning(pixelStorage.name + " does not have " + number + " " + item);
                return;
            }

            Debug.Assert(number >= 1 && number <= 24);
            for (int i = 0; i < number; ++i)
            {
                GameObject obj = pixelStorage.TakeObject(item);
                if (obj == null)
                {
                    break;
                }
                PixelItem pixelItem = obj.GetComponent <PixelItem>();
                Debug.Assert(pixelItem != null);

                bool succeed = inv.AddItem(pixelItem);

                if (succeed)
                {
                    //animator.SetTrigger(Animator.StringToHash("IsPickup"));
                    pixelItem.gameObject.SetActive(false);
                    pixelItem.transform.parent = inv.transform;
                }
            }

            animator.SetTrigger(Animator.StringToHash("IsInteract"));
        }
Esempio n. 4
0
        public void InspectObject(PixelCollision pc)
        {
            // Inspected object is a door
            PixelDoor door = pc.pixelCollider.transform.parent.GetComponent <PixelDoor>();

            if (door != null)
            {
                if (door.interactionDirection != Direction.All && pc.direction != Direction.All)
                {
                    if (door.interactionDirection != pc.direction)
                    {
                        return;
                    }
                    if (facingDirection.x > 0 && facingDirection.y > 0 && door.interactionDirection != Direction.NE)
                    {
                        return;
                    }
                    if (facingDirection.x > 0 && facingDirection.y < 0 && door.interactionDirection != Direction.SE)
                    {
                        return;
                    }
                    if (facingDirection.x < 0 && facingDirection.y > 0 && door.interactionDirection != Direction.NW)
                    {
                        return;
                    }
                    if (facingDirection.x < 0 && facingDirection.y < 0 && door.interactionDirection != Direction.SW)
                    {
                        return;
                    }
                }
                animator.SetTrigger(Animator.StringToHash("IsInteract"));
                EnterDoor(door);
                return;
            }

            // Inspected object is an item
            PixelItem item = pc.pixelCollider.transform.parent.GetComponent <PixelItem>();

            if (item != null)
            {
                CharacterInventory inv = GetComponentInChildren <CharacterInventory>();
                Debug.Assert(inv != null);

                bool succeed = inv.AddItem(item);

                if (succeed)
                {
                    animator.SetTrigger(Animator.StringToHash("IsPickup"));
                    item.gameObject.SetActive(false);
                    item.transform.parent = inv.transform;
                }

                return;
            }

            // Inspected object is a character
            currentlySpeakingTo = pc.pixelCollider.transform.parent.GetComponent <Character>();
            if (currentlySpeakingTo == this)
            {
                currentlySpeakingTo = null;                                          // Cannot talk to one self
            }
            if (currentlySpeakingTo != null)
            {
                Talk();
            }
        }
Esempio n. 5
0
    void LoadInventoryInformation()
    {
        PixelItem[]      items     = Resources.LoadAll <PixelItem>("Items");
        List <PixelItem> itemsList = items.ToList();

        Debug.Assert(items.Length != 0);

        // Item Descriptions
        using (var reader = new StreamReader(@"Assets/Configuration/Items.csv"))
        {
            using (var csvreader = new CsvReader(reader))
            {
                csvreader.Configuration.HasHeaderRecord = true;

                while (csvreader.Read())
                {
                    int id;
                    int.TryParse(csvreader.GetField(0), out id);
                    Debug.Assert(id >= 0);

                    string objname = csvreader.GetField(1);
                    if (objname == "Name")
                    {
                        continue;
                    }
                    if (objname == "")
                    {
                        continue;
                    }

                    string   description = csvreader.GetField(2);
                    string[] properties  = csvreader.GetField(3).Replace(" ", "").Split(',');

                    PixelItem item = itemsList.Find((obj) => obj.name == objname);
                    if (item == null)
                    {
                        continue;
                    }
                    item.id          = id;
                    item.description = description;

                    if (!((properties.Length == 0) || (properties[0] == "")))
                    {
                        item.properties = properties;
                    }
                    else
                    {
                        item.properties = null;
                    }
                }
            }
        }

        // Item Combinations
        using (var reader = new StreamReader(@"Assets/Configuration/Item Combine.csv"))
        {
            while (!reader.EndOfStream)
            {
                var      line   = reader.ReadLine();
                string[] values = CsvSplit(line);

                int combinedid, id1, id2;
                int.TryParse(values[0], out combinedid);
                int.TryParse(values[1], out id1);
                int.TryParse(values[2], out id2);
                Debug.Assert(combinedid >= 0);
                Debug.Assert(id1 >= 0);
                Debug.Assert(id2 >= 0);
                if (combinedid == 0 && id1 == 0 && id2 == 0)
                {
                    continue;
                }

                PixelItem item1        = itemsList.Find((obj) => obj.id == id1);
                PixelItem item2        = itemsList.Find((obj) => obj.id == id2);
                PixelItem combinedItem = itemsList.Find((obj) => obj.id == combinedid);

                if (item1 == null)
                {
                    continue;
                }
                if (item2 == null)
                {
                    continue;
                }
                if (combinedItem == null)
                {
                    continue;
                }

                item1.combinations.Clear();
                item2.combinations.Clear();

                PixelItem.Combination combination1 = new PixelItem.Combination();
                combination1.with   = item2;
                combination1.result = combinedItem;
                item1.combinations.Add(combination1);

                PixelItem.Combination combination2 = new PixelItem.Combination();
                combination2.with   = item1;
                combination2.result = combinedItem;
                item2.combinations.Add(combination2);

                combinedItem.breakapart.Clear();
                combinedItem.breakapart.Add(item1);
                combinedItem.breakapart.Add(item2);
            }
        }
    }
Esempio n. 6
0
        public void SelectLargeItem(int itemnum)
        {
            Debug.Assert(itemnum <= 2 && itemnum > 0);

            GameObject     player         = GameObject.Find("Player");
            PixelInventory pixelInventory = player.GetComponentInChildren <PixelInventory>();

            Debug.Assert(pixelInventory != null);

            Transform inventoryGUI    = transform.Find("Inventory GUI");
            Transform largeItemParent = inventoryGUI.Find("Big Items");

            RefreshItems();

            if (isCombining)
            {
                itemSelectedNum = 0;
                itemSelected    = null;

                Transform button    = transform.Find("Inventory GUI").Find("Inventory Buttons").Find("Combine");
                Image     buttonImg = button.GetComponent <Image>();
                buttonImg.color = new Color(255, 255, 255);
                isCombining     = false;

                RefreshItems();
            }

            itemSelectedNum = 0;
            itemSelected    = pixelInventory.bigItems[itemnum - 1];
            if (itemSelected.empty)
            {
                return;
            }
            itemSelectedNum = itemnum;

            // Display Item Info
            Text itemname        = transform.Find("Item Description Panel").Find("Name").GetComponent <Text>();
            Text itemdescription = transform.Find("Item Description Panel").Find("Description").GetComponent <Text>();
            Text itemproperties  = transform.Find("Item Description Panel").Find("Properties").GetComponent <Text>();

            Debug.Assert(itemname != null);
            Debug.Assert(itemdescription != null);
            Debug.Assert(itemproperties != null);

            PixelItem itemToDescribe = pixelInventory.bigItems[itemnum - 1].item;

            itemname.text        = itemToDescribe.name;
            itemdescription.text = itemToDescribe.description;
            if (itemToDescribe.properties == null)
            {
                itemproperties.text = "";
            }
            else
            {
                string propertyString = "Properties: \n";
                for (int i = 0; i < itemToDescribe.properties.Length; ++i)
                {
                    propertyString = propertyString + itemToDescribe.properties[i] + " ";
                }
                itemproperties.text = propertyString;
            }

            Transform slotUIselected = largeItemParent.Find("Stack " + itemnum);
            Image     imgSelected    = slotUIselected.GetComponent <Image>();

            imgSelected.color = new Color(255, 255, 0, 1.0f);
        }
Esempio n. 7
0
    static void CreateAllItems()
    {
        PixelItem[]      existingItems = Resources.LoadAll <PixelItem>("Items");
        List <PixelItem> itemsList     = existingItems.ToList();

        Debug.Assert(existingItems.Length != 0);

        PixelItem defaultObject = itemsList.Find(x => x.name == "Carrot");

        UnityEngine.Object[] sprites = AssetDatabase.LoadAllAssetsAtPath("Assets/Spritesheets/Items/Generic.png");

        // Item Descriptions
        using (var reader = new StreamReader(@"Assets/Configuration/Items.csv"))
        {
            using (var csvreader = new CsvReader(reader))
            {
                csvreader.Configuration.HasHeaderRecord = true;

                while (csvreader.Read())
                {
                    int id;
                    int.TryParse(csvreader.GetField(0), out id);
                    Debug.Assert(id >= 0);

                    string objname = csvreader.GetField(1);
                    if (objname == "Name")
                    {
                        continue;
                    }
                    if (objname == "")
                    {
                        continue;
                    }

                    string   description = csvreader.GetField(2);
                    string[] properties  = csvreader.GetField(3).Replace(" ", "").Split(',');

                    PixelItem  item          = itemsList.Find((obj) => obj.name == objname);
                    GameObject newGameObject = null;
                    if (item == null)
                    {
                        // Create new item
                        newGameObject      = Instantiate(defaultObject.gameObject);
                        newGameObject.name = objname;
                        item = newGameObject.GetComponent <PixelItem>();
                    }

                    item.id          = id;
                    item.description = description;

                    if (!((properties.Length == 0) || (properties[0] == "")))
                    {
                        item.properties = properties;
                    }
                    else
                    {
                        item.properties = null;
                    }

                    if (newGameObject != null)
                    {
                        SpriteRenderer sr = newGameObject.GetComponent <SpriteRenderer>();
                        sr.sprite = sprites.Where((x) => x.name == "Generic_" + (id - 1)).First() as Sprite;

                        UnityEngine.Object prefab = PrefabUtility.CreateEmptyPrefab("Assets/Resources/Items/" + objname + ".prefab");
                        PrefabUtility.ReplacePrefab(newGameObject, prefab, ReplacePrefabOptions.ConnectToPrefab);
                        DestroyImmediate(newGameObject);
                    }
                }
            }
        }
    }