Exemplo n.º 1
0
        /// <summary>
        /// Instantiate the item at a random nearby position
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pos"></param>
        /// <param name="amount"></param>
        public static bool InstantiateItem(Item item, Vector3 pos, float amount)
        {
            GameObject g;

            int x = UnityEngine.Random.Range(-1, 1);
            int z = UnityEngine.Random.Range(-1, 1);

            //Try to instantiate the item at a random nearby position
            g = Instantiate(item.ItemObject,
                            new Vector3(pos.x + x, pos.y + 0.2f, pos.z + z), Quaternion.identity) as GameObject;

            Collider col = g.GetComponent <Collider>();

            for (int i = 0; i < 10; i++)
            {
                if (!SimplePhysics.CanPlaceItem(col))
                {
                    x = UnityEngine.Random.Range(-2, 2);
                    z = UnityEngine.Random.Range(-2, 2);
                    g.transform.position = new Vector3(pos.x + x, pos.y + 1, pos.z + z);
                }
                else
                {
                    g.SetActive(true);
                    g.GetComponent <ItemReference>().Amount = amount;
                    return(true);
                }
            }
            Destroy(g);
            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Instantiate the item at a random nearby position
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pos"></param>
        /// <param name="amount"></param>
        public static bool InstantiateItem(string name, Vector3 pos, float amount)
        {
            GameObject g;

            int x = UnityEngine.Random.Range(-1, 1);
            int y = UnityEngine.Random.Range(-1, 1);

            //Try to instantiate the item at a random nearby position
            try{
                g = Instantiate(Resources.Load("Items/" + name, typeof(GameObject)),
                                new Vector3(pos.x + x, pos.y + y, pos.z + 1), Quaternion.identity) as GameObject;
            }
            catch (System.Exception) {
                Debug.Log("Invalid item! Every item must be placed on the SimpleCraft/Items/ folder!");
                throw;
            }
            Collider col = g.GetComponent <Collider>();

            for (int i = 0; i < 10; i++)
            {
                if (!SimplePhysics.CanPlaceItem(col))
                {
                    x = UnityEngine.Random.Range(-1, 1);
                    y = UnityEngine.Random.Range(-1, 1);
                    g.transform.position = new Vector3(pos.x + x, pos.y + y, pos.z + 1);
                }
                else
                {
                    g.SetActive(true);
                    g.GetComponent <Item>().Amount = amount;
                    return(true);
                }
            }
            Destroy(g);
            return(false);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Check if there is any obstruction
 /// </summary>
 /// <returns><c>true</c> if this instance can build; otherwise, <c>false</c>.</returns>
 public bool CanBuild()
 {
     return(SimplePhysics.CanPlaceItem(DetectionCollider));
 }