Example #1
0
        private static void ReadObjects(DS1 ds1, BinaryReader reader, int act)
        {
            if (ds1.version < 2)
            {
                return;
            }
            int objectCount = reader.ReadInt32();

            //Debug.Log("Objects " + objectCount);
            ds1.objects = new ObjectSpawnInfo[objectCount];

            for (int i = 0; i < objectCount; i++)
            {
                var info = new ObjectSpawnInfo();
                int type = reader.ReadInt32();
                int id   = reader.ReadInt32();
                info.x = reader.ReadInt32();
                info.y = reader.ReadInt32();

                if (ds1.version > 5)
                {
                    reader.ReadInt32(); // flags
                }

                info.preset    = SpawnPreset.Find(act, type, id);
                ds1.objects[i] = info;
            }
        }
    public List <GameObject> initialPathSpawn(Transform pathContainer, ObjectSpawnInfo spawnObject)
    {
        CheckCollisions   template   = spawnObject.gameObject.AddComponent <CheckCollisions>();
        List <GameObject> toBePlaced = new List <GameObject>();

        for (int i = 0; i < spawnObject.count; i++)
        {
            int index = spawnType == PathSpawnType.Cylinderical ? PositionInCylinder(template) : PositionInRectangle(template);
            if (index >= 0)
            {
                GameObject prop = Instantiate(template.gameObject, template.transform.position, template.transform.rotation, ContainerFromSegmentNumber(pathContainer, index));
                ScaleRotateProp(prop, spawnObject);

                prop.transform.localScale = new Vector3(prop.transform.localScale.x / prop.transform.parent.localScale.x, prop.transform.localScale.y / prop.transform.parent.localScale.y, prop.transform.localScale.z / prop.transform.parent.localScale.z);

                prop.name = template.gameObject.name + " " + i;
                prop.GetComponent <CheckCollisions>().ignoreEnvironmentCollisions = true;
                toBePlaced.Add(prop);
            }
            else
            {
                Debug.Log("Missing path, you need to have one generated before placing path objects.");
                break;
            }
        }
        return(toBePlaced);
    }
    IEnumerator SpawnStaticObject(GameObject[] spawnSurfaces, ObjectSpawnInfo spawnObject)
    {
        foreach (GameObject go in spawnSurfaces)
        {
            if (go.GetComponent <Collider>())
            {
                go.GetComponent <Collider>().enabled = false;
            }
        }
        List <GameObject> toBePlaced = initialStaticSpawn(spawnSurfaces, spawnObject);

        yield return(StartCoroutine(RepositionCollidingStaticObjects(spawnSurfaces, spawnObject, toBePlaced, spawnObject.attempts)));

        foreach (GameObject go in spawnSurfaces)
        {
            if (go.GetComponent <Collider>())
            {
                go.GetComponent <Collider>().enabled = true;
            }
        }

        foreach (GameObject go in toBePlaced)
        {
            DestroyImmediate(go);
        }
        totalPlaced += spawnObject.count - toBePlaced.Count;
        Debug.Log((spawnObject.count - toBePlaced.Count) + " / " + spawnObject.count + " " + spawnObject.gameObject.name + " were sucessfully placed, the remainder were destroyed.");
        yield break;
    }
Example #4
0
    private Vector3 GetSpawnCoords(ObjectSpawnInfo item)
    {
        var yOffset     = UnityEngine.Random.Range(-1f, 1f) * volumeHalfHeight;
        var xOffset     = UnityEngine.Random.Range(-1f, 1f) * volumeHalfWidth;
        var worldCoords = solipsist.transform.position + new Vector3(xOffset, yOffset, 0);

        worldCoords.z = item.atZ;
        return(worldCoords);
    }
Example #5
0
    private void DoSpawnObject(ObjectSpawnInfo info)
    {
        var spawnedObject = Instantiate(info.prefab) as GameObject;

        spawnedObject.transform.position      = info.coords;
        spawnedObject.transform.localScale   *= UnityEngine.Random.Range(1f - scaleVariance, 1f + scaleVariance);
        spawnedObject.transform.localRotation = Utility.GetRandomRotation();
        ObjectGenerated?.Invoke(spawnedObject);
        maintainedObjects.Add(spawnedObject);
    }
Example #6
0
    private bool CanSpawn(ObjectSpawnInfo item)
    {
        return(true);

        var coord          = GetSpawnCoords(item);
        var sphereCollider = item.prefab.GetComponent <SphereCollider>();

        if (sphereCollider != null)
        {
            var radius = sphereCollider.radius * MaxDimensionOf(item.prefab.transform.localScale);
            return(Physics.OverlapSphere(coord, radius).Length == 0);
        }
        return(true);
    }
    static int SortByVolume(ObjectSpawnInfo info1, ObjectSpawnInfo info2)
    {
        Collider[] colliders1   = info1.gameObject.GetComponentsInChildren <Collider>();
        float      go1SumVolume = VolumeOfColliders(colliders1);

        go1SumVolume *= Mathf.Pow(Mathf.Max(info1.staticScale, info1.maxConsistantScale, info1.maxIndependentScale.magnitude), 3);

        Collider[] colliders2   = info2.gameObject.GetComponentsInChildren <Collider>();
        float      go2SumVolume = VolumeOfColliders(colliders2);

        go2SumVolume *= Mathf.Pow(Mathf.Max(info2.staticScale, info2.maxConsistantScale, info2.maxIndependentScale.magnitude), 3);

        return(go2SumVolume.CompareTo(go1SumVolume));
    }
    IEnumerator SpawnPathObject(Transform pathContainer, ObjectSpawnInfo spawnObject)
    {
        List <GameObject> toBePlaced = initialPathSpawn(pathContainer, spawnObject);

        yield return(StartCoroutine(RepositionCollidingPathObjects(toBePlaced, spawnObject.attempts, pathContainer)));

        totalPlaced += spawnObject.count - toBePlaced.Count;
        Debug.Log((spawnObject.count - toBePlaced.Count) + " / " + spawnObject.count + " " + spawnObject.gameObject.name + " were sucessfully placed, the remainder were destroyed.");
        foreach (GameObject go in toBePlaced)
        {
            DestroyImmediate(go);
        }
        yield break;
    }
 public GameObject spawnObject(ObjectSpawnInfo osi, Vector2 pos, Vector2 dir, float addSpawnBuffer = 0)
 {
     if (PV.IsMine)
     {
         //Make sure dir is a unit vector
         if (!Mathf.Approximately(dir.sqrMagnitude, 1))
         {
             throw new System.ArgumentException(
                       $"dir needs to be a unit vector! dir: {dir}, " +
                       $"sqrMagnitude: {dir.sqrMagnitude}"
                       );
         }
         //Initialize arguments
         string  pathName = Path.Combine("PhotonPrefabs", folderName, osi.objectName);
         Vector2 position = pos + (dir * (osi.spawnBuffer + addSpawnBuffer));
         position += osi.spawnOffset;
         Quaternion rotation = (osi.rotateShot)
             ? Quaternion.Euler(0, 0, Vector2.SignedAngle(Vector2.up, dir))
             : Quaternion.Euler(0, 0, 0);
         //Instantiate
         GameObject go = (SpawnMaster)
                         //Spawn as scene object (doesn't disappear when player disconnects)
             ? PhotonNetwork.InstantiateRoomObject(pathName, position, rotation)
                         //Spawn as player object (disappears when player disconnects)
             : PhotonNetwork.Instantiate(pathName, position, rotation)
         ;
         //Team Token
         if (osi.inheritTeamToken)
         {
             TeamToken.seeRecruiter(go, teamToken.owner, true);
         }
         //Color
         if (osi.inheritColor)
         {
             ColorInitializer colorInit = go.FindComponent <ColorInitializer>();
             if (colorInit)
             {
                 colorInit.setColor(PlayerColor);
             }
         }
         //Delegate
         onObjectSpawned?.Invoke(go, position, dir);
         //Return
         return(go);
     }
     return(null);
 }
    public void ScaleRotateProp(GameObject obj, ObjectSpawnInfo info)
    {
        obj.transform.localScale = new Vector3(info.gameObject.transform.localScale.x / obj.transform.parent.transform.localScale.x, info.gameObject.transform.localScale.y / obj.transform.parent.transform.localScale.y, info.gameObject.transform.localScale.z / obj.transform.parent.transform.localScale.z);


        if (info.scaleType == ScaleType.Static)
        {
            ScaleObject(obj.transform, info.staticScale);
        }
        else if (info.scaleType == ScaleType.ConsistantAxis)
        {
            ScaleObject(obj.transform, info.minConsistantScale, info.maxConsistantScale);
        }
        else
        {
            ScaleObject(obj.transform, info.minIndependentScale, info.maxIndependentScale);
        }
    }
    public IEnumerator RepositionCollidingStaticObjects(GameObject[] spawnSurfaces, ObjectSpawnInfo info, List <GameObject> toBePlaced, int maxAttempts)
    {
        int currentAttempts = 0;

        while (toBePlaced.Count > 0 && currentAttempts < maxAttempts)
        {
            yield return(StartCoroutine(RemoveValid(toBePlaced)));

            currentAttempts++;
            for (int i = toBePlaced.Count - 1; i >= 0; i--)
            {
                int index = PositionOnMeshRandomizer(spawnSurfaces, toBePlaced[i].GetComponent <CheckCollisions>(), info);
                if (index >= 0)
                {
                    toBePlaced[i].transform.parent = spawnSurfaces[index].transform;
                    ScaleRotateProp(toBePlaced[i], info);
                }
            }
        }
    }
    public List <GameObject> initialStaticSpawn(GameObject[] spawnSurfaces, ObjectSpawnInfo spawnObject)
    {
        List <GameObject> toBePlaced = new List <GameObject>();

        for (int i = 0; i < spawnObject.count; i++)
        {
            if (spawnSurfaces.Length > 0)
            {
                GameObject prop = Instantiate(spawnObject.gameObject, spawnObject.gameObject.transform);

                CheckCollisions collisionScript = prop.GetComponent <CheckCollisions>() ? prop.GetComponent <CheckCollisions>() : prop.AddComponent <CheckCollisions>();
                int             index           = PositionOnMeshRandomizer(spawnSurfaces, collisionScript, spawnObject);
                if (index >= 0)
                {
                    prop.transform.parent = spawnSurfaces[index].transform;

                    collisionScript.order = order;
                    order++;

                    prop.transform.localScale = new Vector3(spawnObject.gameObject.transform.localScale.x / spawnSurfaces[index].transform.localScale.x, spawnObject.gameObject.transform.localScale.y / spawnSurfaces[index].transform.localScale.y, spawnObject.gameObject.transform.localScale.z / spawnSurfaces[index].transform.localScale.z);

                    ScaleRotateProp(prop, spawnObject);


                    prop.name = spawnObject.gameObject.name + " " + i;
                    prop.tag  = "Environmental";
                    toBePlaced.Add(prop);
                }
                else
                {
                    DestroyImmediate(prop);
                }
            }
            else
            {
                Debug.Log("There are no current spawn surfaces, must be created before object placement.");
            }
        }
        return(toBePlaced);
    }
Example #13
0
 private void EnqueueSpawnObject(ObjectSpawnInfo info)
 {
     spawnQueue.Enqueue(info);
 }
    private int PositionOnMeshRandomizer(GameObject[] spawnSurfaces, CheckCollisions position, ObjectSpawnInfo info)
    {
        if (spawnSurfaces.Length == 0)
        {
            return(-1);
        }

        int randomMeshIndex = Random.Range(0, spawnSurfaces.Length);

        if (spawnSurfaces[randomMeshIndex].GetComponent <MeshFilter>())
        {
            int[]     tris  = spawnSurfaces[randomMeshIndex].GetComponent <MeshFilter>().sharedMesh.triangles;
            Vector3[] verts = spawnSurfaces[randomMeshIndex].GetComponent <MeshFilter>().sharedMesh.vertices;

            int randomTris = Random.Range(0, Mathf.RoundToInt(tris.Length / 3f));

            Bounds bounds = spawnSurfaces[randomMeshIndex].GetComponent <MeshFilter>().sharedMesh.bounds;

            Vector3 a = spawnSurfaces[randomMeshIndex].transform.TransformPoint(verts[tris[randomTris * 3]]);
            Vector3 b = spawnSurfaces[randomMeshIndex].transform.TransformPoint(verts[tris[(randomTris * 3) + 1]]);
            Vector3 c = spawnSurfaces[randomMeshIndex].transform.TransformPoint(verts[tris[(randomTris * 3) + 2]]);

            Vector3 side1  = b - a;
            Vector3 side2  = c - a;
            Vector3 normal = Vector3.Cross(side1, side2);

            float randX = Random.value;
            float randY = Random.value;
            float randZ = Random.value;

            Vector3    randPointOnTri = (randX * a + randY * b + randZ * c) / (randX + randY + randZ);
            Quaternion rotation       = Quaternion.FromToRotation(Vector3.up, normal.normalized);

            position.SetNewLocation(randPointOnTri, rotation);
            position.transform.RotateAround(position.transform.position, position.transform.up, Random.Range(0, 360));
            return(randomMeshIndex);
        }
        else
        {
            return(-1);
        }
    }