Exemplo n.º 1
0
    /// <summary>
    /// Positions are determined by the spline positions of Train Controller.
    /// </summary>
    /// <returns></returns>
    public List <Vector3> GetTrainControllerPositions()
    {
        List <Vector3> positions = new List <Vector3>();

#if TRAIN_CONTROLLER
        WSMGameStudio.Splines.Spline spline = editorTarget.dataSource.GetComponent <WSMGameStudio.Splines.Spline>();

        if (spline)
        {
            int steps = WSMGameStudio.Splines.SplineDefaultValues.StepsPerCurve * spline.CurveCount;

            for (int i = 0; i <= steps; i++)
            {
                float t = i / (float)steps;

                Vector3 position = spline.GetPoint(t);

                positions.Add(position);
            }
        }
#else
        Debug.LogError("Train Controller selected, but scripting define symbol TRAIN_CONTROLLER isn't set");
#endif

        return(positions);
    }
        /// <summary>
        /// Spawn prefabs along spline
        /// </summary>
        public void SpawnPrefabs()
        {
            ResetObjects();

            if (frequency <= 0 || prefabs == null || prefabs.Length == 0)
            {
                return;
            }

            float stepSize = frequency * prefabs.Length;

            if (spline.Loop || stepSize == 1)
            {
                stepSize = 1f / stepSize;
            }
            else
            {
                stepSize = 1f / (stepSize - 1);
            }

            for (int p = 0, f = 0; f < frequency; f++)
            {
                for (int i = 0; i < prefabs.Length; i++, p++)
                {
                    GameObject newClone      = Instantiate(prefabs[i]);;
                    Vector3    pointPosition = spline.GetPoint(p * stepSize);
                    Quaternion pointRotation = spline.GetRotation(p * stepSize);

                    newClone.transform.localPosition = pointPosition;

                    newClone.transform.LookAt(pointPosition + spline.GetDirection(p * stepSize));
                    newClone.transform.rotation *= pointRotation;

                    newClone.transform.parent = transform;

                    clones.Add(newClone);
                }
            }
        }
        /// <summary>
        /// Follow currentSpline path
        /// </summary>
        private void FollowSpline()
        {
            if (currentSpline == null)
            {
                return;
            }

            _goingForward  = (speed > 0);
            _cicleDuration = speed == 0f ? 0f : _distance / Mathf.Abs(speed);

            if (_goingForward)
            {
                if (!playingTrackSound)
                {
                    playingTrackSound = true;
                    // Play sound
                    GameObject.Find("TrackSound").GetComponent <ClickSound>().PlaySound("TrackSound");
                }
                if (_cicleDuration > 0f)
                {
                    _progress += Time.deltaTime / _cicleDuration;
                }

                if (_progress > 1f)
                {
                    switch (mode)
                    {
                    case SplineFollowerMode.StopAtTheEnd:
                        _progress = 1f;
                        break;

                    case SplineFollowerMode.Loop:
                        _progress -= 1f;
                        break;

                    case SplineFollowerMode.PingPong:
                        _progress = 1f;
                        speed    *= -1;
                        break;
                    }
                }
            }
            else
            {
                if (_cicleDuration > 0f)
                {
                    _progress -= Time.deltaTime / _cicleDuration;
                }

                if (_progress < 0f)
                {
                    switch (mode)
                    {
                    case SplineFollowerMode.StopAtTheEnd:
                        _progress = 0f;
                        break;

                    case SplineFollowerMode.Loop:
                        _progress += 1f;
                        break;

                    case SplineFollowerMode.PingPong:
                        _progress = 0;
                        speed    *= -1;
                        break;
                    }
                }
            }
            Vector3 position = currentSpline.GetPoint(_progress);

            transform.localPosition = Vector3.Lerp(transform.localPosition, position, 1f);
            Vector3 LookPos = position + currentSpline.GetDirection(_progress);

            transform.LookAt(LookPos);



            // Rotate the cube by converting the angles into a quaternion.
            // Quaternion reverseRotation = new Quaternion(0, 0, 180.0f);



            if (derivative(_progress).x < 0)
            {
                GameObject.Find("LuggCart").transform.localScale = new Vector3(1, -1, 1);
                try
                {
                    Vector3 rot = GameObject.Find("First Person Camera").transform.rotation.eulerAngles;
                    rot = new Vector3(rot.x, rot.y, 180);
                    GameObject.Find("First Person Camera").transform.rotation = Quaternion.Euler(rot);
                }
                catch (System.NullReferenceException)
                {
                }
            }
            else
            {
                GameObject.Find("LuggCart").transform.localScale = new Vector3(1, 1, 1);

                try
                {
                    Vector3 rot = GameObject.Find("First Person Camera").transform.rotation.eulerAngles;
                    rot = new Vector3(rot.x, rot.y, 0);
                    GameObject.Find("First Person Camera").transform.rotation = Quaternion.Euler(rot);
                }
                catch (System.NullReferenceException)
                {
                }
            }
            // TODO: fix line 135.
            // this line determines the position of the tranform. try to modify it!
        }
        /// <summary>
        /// Spawn prefabs along spline
        /// </summary>
        public void SpawnPrefabs()
        {
            if (spline == null)
            {
                Debug.Log("Please select a reference spline to spawn prefabs.");
                return;
            }

            ResetObjects();

            instances = Mathf.Abs(instances);

            if (instances <= 0 || prefabs == null || prefabs.Length == 0)
            {
                return;
            }

            float stepSize = instances * prefabs.Length;
            float t;

            // if loop does not spawn a double at the end
            stepSize = (spline.Loop || stepSize == 1) ? (1f / stepSize) : (1f / (stepSize - 1));

            GameObject newClone;
            Vector3    clonePosition;
            Quaternion cloneRotation;
            Vector3    cloneDirection;

            for (int positionIndex = 0, instanceIndex = 0; instanceIndex < instances; instanceIndex++)
            {
                for (int prefabIndex = 0; prefabIndex < prefabs.Length; prefabIndex++, positionIndex++)
                {
                    newClone = Instantiate(prefabs[prefabIndex]);
                    t        = positionIndex * stepSize;

                    if (spline.FollowTerrain)
                    {
                        ValidateOrientedPoints();

                        int index = spline.GetClosestOrientedPointIndex(t);
                        clonePosition = spline.OrientedPoints[index].Position;
                        cloneRotation = spline.OrientedPoints[index].Rotation;

                        int nextIndex = index + 1;

                        if (nextIndex > spline.OrientedPoints.Length - 1)
                        {
                            if (spline.Loop)
                            {
                                nextIndex = 0;
                            }
                            else
                            {
                                nextIndex = index;
                                index--;
                            }
                        }

                        cloneDirection = (spline.OrientedPoints[nextIndex].Position - spline.OrientedPoints[index].Position).normalized;
                    }
                    else
                    {
                        clonePosition  = spline.GetPoint(t) + spawnOffset;
                        cloneRotation  = spline.GetRotation(t);
                        cloneDirection = spline.GetDirection(t);
                    }

                    //cloneDirection = spline.GetDirection(t);

                    newClone.transform.localPosition = clonePosition;

                    newClone.transform.rotation = cloneRotation;
                    newClone.transform.LookAt(clonePosition + cloneDirection, newClone.transform.up);

                    newClone.transform.parent = transform;
                }
            }
        }