Beispiel #1
0
        public void DropObjects()
        {
            ObjectHelper.RemoveAllObjectFrom(gameObject, null);

            int count = UnityEngine.Random.Range(Count / 2, Count);

            var collection = ShelfObjectCollection.GetObjects();

            if (collection.Count == 0)
            {
                return;
            }

            List <ObjectOnFloorSpace> spaces = new List <ObjectOnFloorSpace>();

            float sizeScale = Size * 2000;

            for (int i = 0; i < count; i++)
            {
                Vector3 pos = transform.position;
                float   x   = UnityEngine.Random.Range(-sizeScale, sizeScale);
                x /= 2000;
                float y = UnityEngine.Random.Range(-sizeScale, sizeScale);
                y     /= 2000;
                pos.x += x;
                pos.z += y;

                int          itemIndex = UnityEngine.Random.Range(0, collection.Count);
                ShelfObjects item      = collection[itemIndex];

                ObjectOnFloorSpace objectOnFloorSpace = new ObjectOnFloorSpace()
                {
                    Position = pos,
                    Size     = item.Size
                };

                if (!IsSpaceClear(spaces, objectOnFloorSpace))
                {
                    continue;
                }

                string prefabName = item.Name;
                var    go         = PerfabBuilder.CreatePrefab(pos, prefabName);

                go.transform.parent = gameObject.transform;

                go.transform.rotation = new Quaternion();
                float angle = UnityEngine.Random.Range(0, 360);
                go.transform.Rotate(0, 0, angle);
                RandomMaterialHolder.ReplaceMaterials(go);
                spaces.Add(objectOnFloorSpace);
            }
        }
Beispiel #2
0
        private bool IsSpaceClear(List <ObjectOnFloorSpace> spaces, ObjectOnFloorSpace objectOnFloorSpace)
        {
            foreach (var space in spaces)
            {
                var diff = space.Position - objectOnFloorSpace.Position;
                var mag  = diff.magnitude;

                if (mag < (space.Size + objectOnFloorSpace.Size))
                {
                    return(false);
                }
            }
            return(true);
        }