Exemplo n.º 1
0
    /// <summary>
    /// Funktion, ist für das Spawnen eines Objektes zuständig. Bestimmt zufällig Typ, Position und des
    /// jeweiligen Objektes und fügt das Objekt bei Erfolg der Objektliste hinzu.
    /// </summary>
    private void SpawnObject()
    {
        //Bestimme welches Objekt spawnen soll
        var index = DetermineNextObstacle();

        //Bestimme die z-Position
        var z = _lastSpawnPosZ + Random.Range(MinDistance, MaxDistance);

        if (z >= PortalSpawnPosition - MaxDistance)
        {
            return;
        }

        //Bestimme die x-Position
        var x = Obstacles[index].IgnoreLanePosition ? 0 : DetermineNextLanePos();

        //Ausnahme für die Skulpturen
        if (Obstacles[index].IsStoneFigure)
        {
            x = _rnd.Next(0, 2) != 0 ? 0 : 2;
        }

        //Bestimme die Rotation des Objektes
        var rotation = Obstacles[index].IsStoneFigure
            ? new Quaternion(
            Obstacles[index].Obstcls.transform.localRotation.x * (x == 0 ? -1.0f : 1.0f),
            Obstacles[index].Obstcls.transform.localRotation.y,
            Obstacles[index].Obstcls.transform.localRotation.z,
            Obstacles[index].Obstcls.transform.localRotation.w * (x == 0 ? -1.0f : 1.0f)
            )
            : Obstacles[index].Obstcls.transform.rotation;

        //Setze die Position
        var position = new Vector3(
            Obstacles[index].IsStoneFigure ? Obstacles[index].Obstcls.transform.position.x - (x == 0 ? 10 : 0) : x,
            Obstacles[index].Obstcls.transform.position.y,
            z
            );

        //Instanziiere das Objekt
        var go = Instantiate(Obstacles[index].Obstcls, position, rotation);

        go.transform.SetParent(transform);

        //Füge das Objekt der Objektliste hinzu. Wenn dies nicht möglich ist,
        //lösche das Objekt
        if (_objList.AddItem(go, false))
        {
            _lastSpawnPosZ = z;
        }
        else
        {
            Destroy(go);
        }
    }