Beispiel #1
0
    // Update is called once per frame
    void Update()
    {
        if (GameManager.instance.InGameController.Airship != null)
        {
            // Clear the hit colliders.
            this.hitColliders.Clear();

            // Get all hit colliders.
            Ray          r = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2.0f, Screen.height / 2.0f, 0.0f));
            RaycastHit[] hits;
            //hits = Physics.RaycastAll (GameManager.instance.InGameController.Airship.transform.position, -r.direction, 100.0F, this.layerMask);
            hits = Physics.SphereCastAll(GameManager.instance.InGameController.Airship.transform.position, 1.0f, -r.direction, 100.0F, this.layerMask);

            /*for (int i = 0; i < hits.Length; i++) {
             *      RaycastHit hit = hits[i];
             *
             *      EnvironmentCollider ec = hit.collider.GetComponent <EnvironmentCollider> ();
             *      if (ec != null) {
             *              this.hitColliders.Add (ec);
             *      }
             * }*/

            // Save each environment collider that has been hit (that needs to be occluded).
            foreach (RaycastHit hit in hits)
            {
                EnvironmentCollider ec = hit.collider.GetComponent <EnvironmentCollider> ();
                if (ec != null)
                {
                    if (ec.m_linked2DLayer != null)
                    {
                        hitColliders.Add(ec.m_linked2DLayer.m_sortingLayer);
                    }
                }
            }

            Environment2DLayerManager.Instance.ProcessLayers(hitColliders);

            // Set each environment collider

            /*foreach (EnvironmentCollider ec in FindObjectsOfType <EnvironmentCollider>()) {
             *      if (this.hitColliders.Contains (ec))
             *              ec.SetTransparent (true);
             *      else
             *              ec.SetTransparent (false);
             * }*/
        }
    }
    private EnvironmentCollider[] GetColliders()
    {
        GameObject[] collGOs = GameObject.FindGameObjectsWithTag("EnvironmentCollider");

        EnvironmentCollider[] result = new EnvironmentCollider[collGOs.Length];
        int i = collGOs.Length;
        while(--i > -1)
        {
            MeshFilter filter = collGOs[i].gameObject.GetComponentInChildren<MeshFilter>();
            if(filter != null)
            {
                Vector3 sizeRadius = Vector3.Scale( filter.mesh.bounds.extents, collGOs[i].transform.localScale);
                result[i] = new EnvironmentCollider(collGOs[i].transform.position, Mathf.Max(sizeRadius.x, sizeRadius.y, sizeRadius.z) * 1.2f);
            }
        }
        return result;
    }
    private EnvironmentCollider[] GetColliders()
    {
        GameObject[] collGOs = GameObject.FindGameObjectsWithTag("EnvironmentCollider");

        EnvironmentCollider[] result = new EnvironmentCollider[collGOs.Length];
        int i = collGOs.Length;

        while (--i > -1)
        {
            MeshFilter filter = collGOs[i].gameObject.GetComponentInChildren <MeshFilter>();
            if (filter != null)
            {
                Vector3 sizeRadius = Vector3.Scale(filter.mesh.bounds.extents, collGOs[i].transform.localScale);
                result[i] = new EnvironmentCollider(collGOs[i].transform.position, Mathf.Max(sizeRadius.x, sizeRadius.y, sizeRadius.z) * 1.2f);
            }
        }
        return(result);
    }
Beispiel #4
0
    /// <summary>
    /// Update this instance.
    /// </summary>
    void Update()
    {
        this.hitColliders.Clear();

        // Raycast to determine y elevation.
        RaycastHit hit;

        if (m_target != null)
        {
            if (Physics.Raycast(m_target.transform.position, Vector3.down, out hit, 100.0f, m_layerMask))
            {
                this.transform.position = hit.point;
            }
        }

        // Raycast to determine when to render the shadow.
        Ray        r = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2.0f, Screen.height / 2.0f, 0.0f));
        RaycastHit cameraHit;

        if (Physics.Raycast(this.transform.position, -r.direction, out cameraHit, 100.0F, this.m_layerMask))
        {
            EnvironmentCollider ec = cameraHit.transform.GetComponent <EnvironmentCollider> ();
            if (ec != null)
            {
                if (ec.m_linked2DLayer != null)
                {
                    m_spriteRenderer.sortingOrder = ec.m_linked2DLayer.m_sortingLayer - 2;
                }
            }
        }
        else
        {
            m_spriteRenderer.sortingOrder = 10;
        }

        /*foreach (RaycastHit h in hits) {
         *      EnvironmentCollider ec = hit.collider.GetComponent <EnvironmentCollider> ();
         *      if (ec != null) {
         *              if (ec.m_linked2DLayer != null) {
         *                      hitColliders.Add (ec.m_linked2DLayer.m_sortingLayer);
         *              }
         *      }
         * }*/
    }
    //--------------- IThreadWorker Implementation --------------------



    public void ExecuteThreadedWork()
    {
        startWorkIndex = Mathf.Clamp(startWorkIndex, 0, AllFlocks.Length);
        endWorkIndex   = Mathf.Clamp(endWorkIndex, 0, AllFlocks.Length);

        //--------------- Creat list with indexes --------------------
        int[] randomIndexes = new int[AllFlocks.Length];
        int   i             = AllFlocks.Length;

        while (--i > -1)
        {
            randomIndexes[i] = i;
        }

        //--------------- Creat list with indexes --------------------


        for (i = startWorkIndex; i < endWorkIndex && !isAborted; i++)
        {
            //--------------- Shuffle indexes every-something-flocks --------------------
            if ((float)i % (float)maxRandomSiblingsTested == 0f)
            {
                randomIndexes.Shuffle();
            }
            //--------------- Shuffle indexes every-something-flocks --------------------

            FlockData flock = AllFlocks[i];

            if (flock.position.y < 0f) //Through the ground....
            {
                flock.position.y        = -flock.position.y;
                flock.targetDirection.y = Mathf.Abs(flock.targetDirection.y);
                flock.currentDirection  = flock.targetDirection;
                flock.currentColor      = flock.targetColor = new Color(1f, 1f, 0f, 0.65f);
                flock.currentVelocity   = flock.targetVelocity;
            }
            else
            {
                Vector3 toCenterVec = universeCenter - flock.position;
                if (toCenterVec.sqrMagnitude > maxBoundsRadius * maxBoundsRadius) //If outside of univere limits
                {
                    flock.targetDirection = toCenterVec.normalized;
                    flock.targetColor     = new Color(1f, 0f, 1f, 0.25f);
                    flock.currentVelocity = flock.targetVelocity;
                }
                else
                {
                    //--------------- Check for Collisions --------------------
                    bool isColliding = false;
                    int  j           = AllColliders.Length;

                    Vector3 collisionAvoidenceDir = Vector3.zero;
                    while (--j > -1 && !isAborted)
                    {
                        EnvironmentCollider coll    = AllColliders[j];
                        Vector3             diffVec = coll.position - flock.position;

                        if (diffVec.sqrMagnitude < coll.radiusSqr)
                        {
                            isColliding = true;
                            Vector3 invDiff       = -diffVec.normalized;
                            Vector3 reflectionDir = FlockHelper.ReflectVector(invDiff, flock.currentDirection);
                            collisionAvoidenceDir += reflectionDir;
                            flock.position         = coll.position + (invDiff * coll.radius);
                        }
                    }
                    //--------------- Check for Collisions --------------------

                    if (isColliding) //If Colliding
                    {
                        flock.targetDirection = flock.currentDirection = collisionAvoidenceDir.normalized;
                        flock.currentColor    = flock.targetColor = new Color(1f, 0.1f, 0f, 0.85f);
                        flock.currentVelocity = flock.targetVelocity * 2f;
                    }
                    else
                    {
                        Vector3 diffDestionation = flock.destinationPoint - flock.position;
                        if (diffDestionation.sqrMagnitude < destinationAttractRadiusSqr) //If close to unique target point...
                        {
                            flock.targetDirection = diffDestionation.normalized;
                            float lerpT = Mathf.Clamp01(diffDestionation.magnitude / destinationReachedRadius);
                            flock.currentVelocity = flock.targetVelocity * lerpT;
                            flock.targetColor     = Color.Lerp(new Color(0f, 1f, 0f, 1f), new Color(1f, 1f, 1f, 0.15f), lerpT);
                        }
                        else
                        {
                            Vector3 combinedDirection = flock.targetDirection;
                            Vector3 combinedColor     = Vector3.zero;

                            j = Mathf.Min(AllFlocks.Length, maxRandomSiblingsTested);
                            while (--j > -1 && !isAborted)
                            {
                                //--------------- Get a random sibling --------------------
                                int randomIndex = randomIndexes[j];
                                if (i == randomIndex)
                                {
                                    continue;
                                }
                                FlockData sibling = AllFlocks[randomIndex];
                                //--------------- Get a random sibling --------------------


                                Vector3 diffVec = sibling.position - flock.position;
                                float   distSqr = diffVec.sqrMagnitude;

                                if (distSqr < seperationRadiusSqr)
                                {
                                    combinedDirection += Vector3.Cross(flock.currentDirection, diffVec.normalized) * seperationWeight;
                                    combinedColor.y   += seperationWeight; //y == green;
                                }
                                //else if (distSqr < alignmentRadiusSqr)
                                if (distSqr < alignmentRadiusSqr)
                                {
                                    combinedDirection += sibling.currentDirection * alignmentWeight;
                                    combinedColor.z   += alignmentWeight; //z == blue;
                                }
                                //else if (distSqr < cohesionRadiusSqr)
                                if (distSqr < cohesionRadiusSqr)
                                {
                                    combinedDirection += diffVec.normalized * cohesionWeight;
                                    combinedColor     += new Vector3(cohesionWeight, cohesionWeight, cohesionWeight); //Add white
                                }
                            }

                            flock.targetDirection = combinedDirection.normalized;
                            flock.currentVelocity = flock.targetVelocity;
                            combinedColor.Normalize();
                            flock.targetColor = new Color(combinedColor.x, combinedColor.y, combinedColor.z, 0.15f);
                        }
                    }
                }
            }
        }
    }