public void InitializeLevel(LevelData levelParams, float[] radiuses)
    {
        CurrentSpeed = Speed;

        initialized = true;
        FindObjectOfType <PathGenerator>().CreateLevelPath(Path, levelParams, radiuses, DeltaY);
        this.levelParams = levelParams;
        BallSizePercent  = 2f / Path.Length;
        Path.InitializeArcs(Path.Count);
        PortalsObject.position = Path.GetPoint(0);
        createStartBalls();
        speedIncrease = (1 - 1 / (Path.Length / 100)) / Radius * speedChangeSpeed;
        CountScore(0);
    }
Ejemplo n.º 2
0
    /// <summary>
    /// Initialisation du Flock
    /// </summary>
    public void InitializeFlock(FlockSettings newFlockSettings, BezierSolution.BezierSpline spawnSpline, Vector3Int sensitivity)
    {
        flockSettings   = newFlockSettings;
        flockInitialRot = transform.rotation;

        inAttack   = false;
        isSpawning = true;
        _Player    = GameObject.FindGameObjectWithTag("Player");

        transform.position = spawnSpline.GetPoint(0);
        bezierWalkerTime.SetNewSpline(spawnSpline);
        bezierWalkerTime.NormalizedT = 0;
        bezierWalkerTime.travelTime  = flockSettings.spawnTimer;

        _mainGuide = gameObject.transform;              //Main guide prends la valeur de this (CF : Variable _mainGuide)

        _GuideList            = new List <Transform>(); //Instanciation de la guide list
        _curCurveDistanceList = new List <Vector3>();   // Instanciation de la list de distance sur les courbes pour chaque guide

        _BoidSettings = new BoidSettings[6][];

        spawnSettings       = flockSettings.spawnSettings;
        roamSettings        = flockSettings.roamSettings;
        attackSettings      = flockSettings.attackSettings;
        destructionSettings = flockSettings.destructionSettings;
        getAwaySettings     = flockSettings.getAwaySettings;
        hitReactionSettings = flockSettings.hitReactionSettings;

        _BoidSettings[0] = spawnSettings;
        _BoidSettings[1] = roamSettings;
        _BoidSettings[2] = attackSettings;
        _BoidSettings[3] = destructionSettings;
        _BoidSettings[4] = getAwaySettings;
        _BoidSettings[5] = hitReactionSettings;

        _KoaManager   = Instantiate(_KoaPrefab, transform);                                                             //Instantiate Koa
        _SCKoaManager = _KoaManager.GetComponent <SC_KoaManager>();                                                     //Récupère le Koa manager du koa instancié
        _SCKoaManager.Initialize(_mainGuide, flockSettings.boidSpawn, spawnSettings[0], newFlockSettings, sensitivity); //Initialise le Koa | paramètre : Guide a suivre <> Nombre de Boids a spawn <> Comportement des boids voulu
        flockWeaponManager.Initialize(flockSettings, KoaMainAnimator, KoaEmissiveAnimator);

        _splineTab = new BezierSolution.BezierSpline[flockSettings.splines.Length];

        for (int i = 0; i < flockSettings.splines.Length; i++)
        {
            if (flockSettings.splines[i] != null)
            {
                _splineTab[i] = Instantiate(flockSettings.splines[i]);
            }
        }

        Invoke("ActivateFlock", flockSettings.spawnTimer);
    }
Ejemplo n.º 3
0
 private void Awake()
 {
     nextSplineFirstPoint = nextSplineToFollow.GetPoint(0f);
     walkerWithSpeed      = GetComponent <BezierSolution.BezierWalkerWithSpeed>();
     if (walkerWithSpeed == null)
     {
         Debug.Log("Couldn't find walker for " + name);
     }
     else
     {
         walkerWithSpeed.spline = nextSplineToFollow;
     }
     defaultParent = transform.parent;
 }
Ejemplo n.º 4
0
        //added
        void DrawVectors()
        {
            Color beforeColor = Handles.color;
            int   lineCount   = spline.Count * 10;
            float f_lineCount = (float)lineCount;

            for (int i = 0; i < lineCount; i++)
            {
                float      t        = ((float)i / f_lineCount);
                Vector3    point    = spline.GetPoint(t);
                Quaternion rotation = spline.GetRotation(t);
                Vector3    up       = rotation * Vector3.up;
                Handles.color = Color.green;
                Handles.DrawLine(point, point + (up * 0.2f));
                Vector3 forward = rotation * Vector3.forward;
                Handles.color = Color.blue;
                Handles.DrawLine(point, point + (forward * 0.2f));
                Vector3 right = rotation * Vector3.right;
                Handles.color = Color.red;
                Handles.DrawLine(point, point + (right * 0.2f));
            }
            Handles.color = beforeColor;
        }
        void LateUpdate()
        {
            if (spline == null || cachedPS == null)
            {
                return;
            }

            if (particles.Length < cachedMainModule.maxParticles && particles.Length < MAX_PARTICLE_COUNT)
            {
                particles = new ParticleSystem.Particle[Mathf.Min(cachedMainModule.maxParticles, MAX_PARTICLE_COUNT)];
            }

            bool isLocalSpace   = cachedMainModule.simulationSpace != ParticleSystemSimulationSpace.World;
            int  aliveParticles = cachedPS.GetParticles(particles);

            if (followMode == FollowMode.Relaxed)
            {
                if (particleData == null)
                {
                    particleData = new List <Vector4>(particles.Length);
                }

                cachedPS.GetCustomParticleData(particleData, ParticleSystemCustomData.Custom1);

                // Credit: https://forum.unity3d.com/threads/access-to-the-particle-system-lifecycle-events.328918/#post-2295977
                for (int i = 0; i < aliveParticles; i++)
                {
                    Vector4 particleDat = particleData[i];
                    Vector3 point       = spline.GetPoint(1f - (particles[i].remainingLifetime / particles[i].startLifetime));
                    if (isLocalSpace)
                    {
                        point = cachedTransform.InverseTransformPoint(point);
                    }

                    Vector3 startOffset = (Vector3)particleDat;
                    if (particleDat.w == 0f)
                    {
                        // We assume the particle has not travelled yet on the first update,
                        // we can now find the offset from the particle system origin
                        startOffset = transform.position - particles[i].position;

                        if (isLocalSpace)
                        {
                            startOffset = cachedTransform.InverseTransformPoint(startOffset);
                        }

                        particleDat     = startOffset;
                        particleDat.w   = 1f;
                        particleData[i] = particleDat;
                        cachedPS.SetCustomParticleData(particleData, ParticleSystemCustomData.Custom1);
                    }

                    // Move particles alongside the spline
                    particles[i].position = point + startOffset;
                }
            }
            else
            {
                Vector3 deltaPosition = cachedTransform.position - spline.GetPoint(0f);
                for (int i = 0; i < aliveParticles; i++)
                {
                    Vector3 point = spline.GetPoint(1f - (particles[i].remainingLifetime / particles[i].startLifetime)) + deltaPosition;
                    if (isLocalSpace)
                    {
                        point = cachedTransform.InverseTransformPoint(point);
                    }

                    particles[i].position = point;
                }
            }

            cachedPS.SetParticles(particles, aliveParticles);
        }
Ejemplo n.º 6
0
        public override void Execute(float deltaTime)
        {
            if (curSpline != null)
            {
                transform.position = Vector3.Lerp(transform.position, curSpline.GetPoint(m_normalizedT), movementLerpModifier * deltaTime);

                if (lookAt == LookAtMode.Forward)
                {
                    Quaternion targetRotation;
                    if (isGoingForward)
                    {
                        targetRotation = Quaternion.LookRotation(curSpline.GetTangent(m_normalizedT));
                    }
                    else
                    {
                        targetRotation = Quaternion.LookRotation(-curSpline.GetTangent(m_normalizedT));
                    }

                    transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationLerpModifier * deltaTime);
                }
                else if (lookAt == LookAtMode.SplineExtraData)
                {
                    transform.rotation = Quaternion.Lerp(transform.rotation, curSpline.GetExtraData(m_normalizedT, InterpolateExtraDataAsQuaternion), rotationLerpModifier * deltaTime);
                }

                if (isGoingForward)
                {
                    m_normalizedT += deltaTime / n_travelTime;

                    if (m_normalizedT > 1f)
                    {
                        if (!onPathCompletedCalledAt1)
                        {
                            onPathCompletedCalledAt1 = true;
#if UNITY_EDITOR
                            if (UnityEditor.EditorApplication.isPlaying)
#endif
                            onPathCompleted.Invoke();
                        }

                        if (travelMode == TravelMode.Once)
                        {
                            m_normalizedT = 1f;
                        }
                        else if (travelMode == TravelMode.Loop)
                        {
                            m_normalizedT -= 1f;
                        }
                        else
                        {
                            m_normalizedT  = 2f - m_normalizedT;
                            isGoingForward = false;
                        }
                    }
                    else
                    {
                        onPathCompletedCalledAt1 = false;
                    }
                }
                else
                {
                    m_normalizedT -= deltaTime / n_travelTime;

                    if (m_normalizedT < 0f)
                    {
                        if (!onPathCompletedCalledAt0)
                        {
                            onPathCompletedCalledAt0 = true;
#if UNITY_EDITOR
                            if (UnityEditor.EditorApplication.isPlaying)
#endif
                            onPathCompleted.Invoke();
                        }

                        if (travelMode == TravelMode.Once)
                        {
                            m_normalizedT = 0f;
                        }
                        else if (travelMode == TravelMode.Loop)
                        {
                            m_normalizedT += 1f;
                        }
                        else
                        {
                            m_normalizedT  = -m_normalizedT;
                            isGoingForward = true;
                        }
                    }
                    else
                    {
                        onPathCompletedCalledAt0 = false;
                    }
                }
            }
        }
        public override void Execute(float deltaTime)
        {
            float _normalizedT = highQuality ? spline.evenlySpacedPoints.GetNormalizedTAtPercentage(m_normalizedT) : m_normalizedT;

            transform.position = Vector3.Lerp(transform.position, spline.GetPoint(_normalizedT), movementLerpModifier * deltaTime);

            if (lookAt == LookAtMode.Forward)
            {
                BezierSpline.Segment segment = spline.GetSegmentAt(_normalizedT);
                Quaternion           targetRotation;
                if (isGoingForward)
                {
                    targetRotation = Quaternion.LookRotation(segment.GetTangent(), segment.GetNormal());
                }
                else
                {
                    targetRotation = Quaternion.LookRotation(-segment.GetTangent(), segment.GetNormal());
                }

                transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, rotationLerpModifier * deltaTime);
            }
            else if (lookAt == LookAtMode.SplineExtraData)
            {
                transform.rotation = Quaternion.Lerp(transform.rotation, spline.GetExtraData(_normalizedT, extraDataLerpAsQuaternionFunction), rotationLerpModifier * deltaTime);
            }

            if (isGoingForward)
            {
                m_normalizedT += deltaTime / travelTime;

                if (m_normalizedT > 1f)
                {
                    if (travelMode == TravelMode.Once)
                    {
                        m_normalizedT = 1f;
                    }
                    else if (travelMode == TravelMode.Loop)
                    {
                        m_normalizedT -= 1f;
                    }
                    else
                    {
                        m_normalizedT  = 2f - m_normalizedT;
                        isGoingForward = false;
                    }

                    if (!onPathCompletedCalledAt1)
                    {
                        onPathCompletedCalledAt1 = true;
#if UNITY_EDITOR
                        if (UnityEditor.EditorApplication.isPlaying)
#endif
                        onPathCompleted.Invoke();
                    }
                }
                else
                {
                    onPathCompletedCalledAt1 = false;
                }
            }
            else
            {
                m_normalizedT -= deltaTime / travelTime;

                if (m_normalizedT < 0f)
                {
                    if (travelMode == TravelMode.Once)
                    {
                        m_normalizedT = 0f;
                    }
                    else if (travelMode == TravelMode.Loop)
                    {
                        m_normalizedT += 1f;
                    }
                    else
                    {
                        m_normalizedT  = -m_normalizedT;
                        isGoingForward = true;
                    }

                    if (!onPathCompletedCalledAt0)
                    {
                        onPathCompletedCalledAt0 = true;
#if UNITY_EDITOR
                        if (UnityEditor.EditorApplication.isPlaying)
#endif
                        onPathCompleted.Invoke();
                    }
                }
                else
                {
                    onPathCompletedCalledAt0 = false;
                }
            }
        }
Ejemplo n.º 8
0
        private void FixedUpdate()
        {
            if (spline == null || cachedPS == null)
            {
                return;
            }

            if (particles.Length < cachedMainModule.maxParticles && particles.Length < MAX_PARTICLE_COUNT)
            {
                System.Array.Resize(ref particles, Mathf.Min(cachedMainModule.maxParticles, MAX_PARTICLE_COUNT));
            }

            bool isLocalSpace   = cachedMainModule.simulationSpace != ParticleSystemSimulationSpace.World;
            int  aliveParticles = cachedPS.GetParticles(particles);

            if (followMode == FollowMode.Relaxed)
            {
                if (particleData == null)
                {
                    particleData = new List <Vector4>(particles.Length);
                }

                cachedPS.GetCustomParticleData(particleData, ParticleSystemCustomData.Custom1);

                // Credit: https://forum.unity3d.com/threads/access-to-the-particle-system-lifecycle-events.328918/#post-2295977
                for (int i = 0; i < aliveParticles; i++)
                {
                    Vector4 particleDat = particleData[i];
                    Vector3 point       = spline.GetPoint(1f - (particles[i].remainingLifetime / particles[i].startLifetime));
                    if (isLocalSpace)
                    {
                        point = cachedTransform.InverseTransformPoint(point);
                    }

                    // Move particles alongside the spline
                    if (particleDat.w != 0f)
                    {
                        particles[i].position += point - (Vector3)particleDat;
                    }

                    particleDat     = point;
                    particleDat.w   = 1f;
                    particleData[i] = particleDat;
                }

                cachedPS.SetCustomParticleData(particleData, ParticleSystemCustomData.Custom1);
            }
            else
            {
                Vector3 deltaPosition = cachedTransform.position - spline.GetPoint(0f);
                for (int i = 0; i < aliveParticles; i++)
                {
                    Vector3 point = spline.GetPoint(1f - (particles[i].remainingLifetime / particles[i].startLifetime)) + deltaPosition;
                    if (isLocalSpace)
                    {
                        point = cachedTransform.InverseTransformPoint(point);
                    }

                    particles[i].position = point;
                }
            }

            cachedPS.SetParticles(particles, aliveParticles);
        }
        public void Refresh()
        {
            if (!m_spline)
            {
                return;
            }

            if (!meshFilter || (meshFilter.sharedMesh && meshFilter.sharedMesh != mesh && meshFilter.sharedMesh != originalMesh))
            {
                Initialize();
            }

            if (!originalMesh)
            {
                return;
            }

            if (vertices == null || vertices.Length != originalVertices.Length)
            {
                vertices = new Vector3[originalVertices.Length];
            }

            if (m_normalsMode == VectorMode.ModifyOriginals)
            {
                if (originalNormals == null)
                {
                    originalNormals = originalMesh.normals;
                }

                if (originalNormals == null || originalNormals.Length < originalVertices.Length)                  // If somehow above statement returned null
                {
                    normals = null;
                }
                else if (normals == null || normals.Length != originalNormals.Length)
                {
                    normals = new Vector3[originalNormals.Length];
                }
            }
            else
            {
                normals = null;
            }

            if (m_tangentsMode == VectorMode.ModifyOriginals)
            {
                if (originalTangents == null)
                {
                    originalTangents = originalMesh.tangents;
                }

                if (originalTangents == null || originalTangents.Length < originalVertices.Length)                  // If somehow above statement returned null
                {
                    tangents = null;
                }
                else if (tangents == null || tangents.Length != originalTangents.Length)
                {
                    tangents = new Vector4[originalTangents.Length];
                }
            }
            else
            {
                tangents = null;
            }

            Vector2 _splineSampleRange = m_splineSampleRange;

            if (m_invertDirection)
            {
                float temp = _splineSampleRange.x;
                _splineSampleRange.x = _splineSampleRange.y;
                _splineSampleRange.y = temp;
            }

            bool  isSampleRangeForwards = _splineSampleRange.x <= _splineSampleRange.y;
            float splineSampleLength    = _splineSampleRange.y - _splineSampleRange.x;
            bool  dontInvertModifiedVertexAttributes = (m_thicknessMultiplier.x > 0f && m_thicknessMultiplier.y > 0f);

            BezierSpline.EvenlySpacedPointsHolder evenlySpacedPoints = m_highQuality ? m_spline.evenlySpacedPoints : null;

            Vector3 initialPoint = m_spline.GetPoint(0f);

            for (int i = 0; i < originalVertices.Length; i++)
            {
                Vector3 vertex = originalVertices[i];

                float   vertexPosition;
                Vector3 vertexOffset;
                switch (m_bendAxis)
                {
                case Axis.X:
                    vertexPosition = vertex.x;
                    vertexOffset   = new Vector3(vertex.z * m_thicknessMultiplier.x, 0f, vertex.y * m_thicknessMultiplier.y);
                    break;

                case Axis.Y:
                default:
                    vertexPosition = vertex.y;
                    vertexOffset   = new Vector3(vertex.x * m_thicknessMultiplier.x, 0f, vertex.z * m_thicknessMultiplier.y);
                    break;

                case Axis.Z:
                    vertexPosition = vertex.z;
                    vertexOffset   = new Vector3(vertex.y * m_thicknessMultiplier.x, 0f, vertex.x * m_thicknessMultiplier.y);
                    break;
                }

                float normalizedT = _splineSampleRange.x + (vertexPosition - minVertex) * _1OverVertexRange * splineSampleLength;                   // Remap from [minVertex,maxVertex] to _splineSampleRange
                if (m_highQuality)
                {
                    normalizedT = evenlySpacedPoints.GetNormalizedTAtPercentage(normalizedT);
                }

                BezierSpline.Segment segment = m_spline.GetSegmentAt(normalizedT);

                Vector3    point    = segment.GetPoint() - initialPoint;
                Vector3    tangent  = isSampleRangeForwards ? segment.GetTangent() : -segment.GetTangent();
                Quaternion rotation = Quaternion.AngleAxis(m_extraRotation, tangent) * Quaternion.LookRotation(segment.GetNormal(), tangent);

                Vector3 direction = rotation * vertexOffset;
                vertices[i] = point + direction;

                if (normals != null)                  // The only case this happens is when Normals Mode is ModifyOriginals and the original mesh has normals
                {
                    normals[i] = rotation * (dontInvertModifiedVertexAttributes ? originalNormals[i] : -originalNormals[i]);
                }
                if (tangents != null)                  // The only case this happens is when Tangents Mode is ModifyOriginals and the original mesh has tangents
                {
                    float tangentW = originalTangents[i].w;
                    tangents[i]   = rotation * (dontInvertModifiedVertexAttributes ? originalTangents[i] : -originalTangents[i]);
                    tangents[i].w = tangentW;
                }
            }

            mesh.vertices = vertices;
            if (m_normalsMode == VectorMode.ModifyOriginals)
            {
                mesh.normals = normals;
            }
            if (m_tangentsMode == VectorMode.ModifyOriginals)
            {
                mesh.tangents = tangents;
            }

            if (m_normalsMode == VectorMode.RecalculateFromScratch)
            {
                mesh.RecalculateNormals();

#if UNITY_EDITOR
                // Cache original normals so that we can reset normals in OnValidate when normals are reset back to DontModify
                if (originalNormals == null)
                {
                    originalNormals = originalMesh.normals;
                }
#endif
            }

            if (m_tangentsMode == VectorMode.RecalculateFromScratch)
            {
                mesh.RecalculateTangents();

#if UNITY_EDITOR
                // Cache original tangents so that we can reset tangents in OnValidate when tangents are reset back to DontModify
                if (originalTangents == null)
                {
                    originalTangents = originalMesh.tangents;
                }
#endif
            }

            mesh.RecalculateBounds();
        }
        void Update()
        {
            cachedTransform.position = Vector3.Lerp(cachedTransform.position, spline.GetPoint(progress), movementLerpModifier * Time.deltaTime);

            if (lookForward)
            {
                Quaternion targetRotation;
                if (isGoingForward)
                {
                    targetRotation = Quaternion.LookRotation(spline.GetTangent(progress));
                }
                else
                {
                    targetRotation = Quaternion.LookRotation(-spline.GetTangent(progress));
                }

                cachedTransform.rotation = Quaternion.Lerp(cachedTransform.rotation, targetRotation, rotationLerpModifier * Time.deltaTime);
            }

            if (isGoingForward)
            {
                progress += Time.deltaTime / travelTime;

                if (progress > 1f)
                {
                    if (!onPathCompletedCalledAt1)
                    {
                        onPathCompleted.Invoke();
                        onPathCompletedCalledAt1 = true;
                    }

                    if (travelMode == TravelMode.Once)
                    {
                        progress = 1f;
                    }
                    else if (travelMode == TravelMode.Loop)
                    {
                        progress -= 1f;
                    }
                    else
                    {
                        progress       = 2f - progress;
                        isGoingForward = false;
                    }
                }
                else
                {
                    onPathCompletedCalledAt1 = false;
                }
            }
            else
            {
                progress -= Time.deltaTime / travelTime;

                if (progress < 0f)
                {
                    if (!onPathCompletedCalledAt0)
                    {
                        onPathCompleted.Invoke();
                        onPathCompletedCalledAt0 = true;
                    }

                    if (travelMode == TravelMode.Once)
                    {
                        progress = 0f;
                    }
                    else if (travelMode == TravelMode.Loop)
                    {
                        progress += 1f;
                    }
                    else
                    {
                        progress       = -progress;
                        isGoingForward = true;
                    }
                }
                else
                {
                    onPathCompletedCalledAt0 = false;
                }
            }
        }
Ejemplo n.º 11
0
        public void Refresh(int smoothness)
        {
            if (!m_spline || m_spline.Count < 2)
            {
                return;
            }

            if (!lineRenderer)
            {
                lineRenderer = GetComponent <LineRenderer>();
            }

            smoothness = Mathf.Clamp(smoothness, 1, 30);

            int numberOfPoints = (m_spline.Count - 1) * smoothness;

            if (!m_spline.loop)
            {
                numberOfPoints++;                 // spline.GetPoint( 1f )
            }
            else
            {
                numberOfPoints += smoothness;                 // Final point is connected to the first point via lineRenderer.loop, so no "numberOfPoints++" here
            }
            if (lineRendererPoints == null || lineRendererPoints.Length != numberOfPoints)
            {
                lineRendererPoints = new Vector3[numberOfPoints];
            }

            if (m_splineSampleRange.x <= 0f && m_splineSampleRange.y >= 1f)
            {
                int   pointIndex     = 0;
                float smoothnessStep = 1f / smoothness;
                for (int i = 0; i < m_spline.Count - 1; i++)
                {
                    BezierSpline.Segment segment = new BezierSpline.Segment(m_spline[i], m_spline[i + 1], 0f);
                    for (int j = 0; j < smoothness; j++, pointIndex++)
                    {
                        lineRendererPoints[pointIndex] = segment.GetPoint(j * smoothnessStep);
                    }
                }

                if (!m_spline.loop)
                {
                    lineRendererPoints[numberOfPoints - 1] = m_spline.GetPoint(1f);
                }
                else
                {
                    BezierSpline.Segment segment = new BezierSpline.Segment(m_spline[m_spline.Count - 1], m_spline[0], 0f);
                    for (int j = 0; j < smoothness; j++, pointIndex++)
                    {
                        lineRendererPoints[pointIndex] = segment.GetPoint(j * smoothnessStep);
                    }
                }
            }
            else
            {
                float smoothnessStep = (m_splineSampleRange.y - m_splineSampleRange.x) / (numberOfPoints - 1);
                for (int i = 0; i < numberOfPoints; i++)
                {
                    lineRendererPoints[i] = spline.GetPoint(m_splineSampleRange.x + i * smoothnessStep);
                }
            }

#if UNITY_EDITOR
            lineRendererUseWorldSpace = lineRenderer.useWorldSpace;
#endif
            if (!lineRenderer.useWorldSpace)
            {
                Vector3 initialPoint = m_spline.GetPoint(0f);
                for (int i = 0; i < numberOfPoints; i++)
                {
                    lineRendererPoints[i] -= initialPoint;
                }
            }

            lineRenderer.positionCount = lineRendererPoints.Length;
            lineRenderer.SetPositions(lineRendererPoints);
            lineRenderer.loop = m_spline.loop && m_splineSampleRange.x <= 0f && m_splineSampleRange.y >= 1f;
        }