Example #1
0
        public static string MillisecondsToHours(float time, bool printSeconds)
        {
            time = SBSMath.Max(time, 0.0f);

            int secs  = Mathf.FloorToInt(time) % 60,
                mins  = Mathf.FloorToInt(time / 60.0f),
                hours = Mathf.FloorToInt(mins / 60.0f);

            mins = mins % 60;
#if UNITY_FLASH
            if (printSeconds)
            {
                return(string.Format("{0:D2}:{1:D2}:{2:D2}", hours, mins, secs));
            }
            else
            {
                return(string.Format("{0:D2}:{1:D2}", hours, mins));
            }
#else
            if (printSeconds)
            {
                return(string.Format(CultureInfo.InvariantCulture, "{0:D2}:{1:D2}:{2:D2}", hours, mins, secs));
            }
            else
            {
                return(string.Format(CultureInfo.InvariantCulture, "{0:D2}:{1:D2}", hours, mins));
            }
#endif
        }
Example #2
0
        public static string MillisecondsToTime(float time, bool printCents)
        {
            time = SBSMath.Max(time, 0.0f);

            int cents = Mathf.FloorToInt(time * 100.0f) % 100,
                secs  = Mathf.FloorToInt(time) % 60,
                mins  = Mathf.FloorToInt(time / 60.0f);

#if UNITY_FLASH
            if (printCents)
            {
                return(string.Format("{0:D2}:{1:D2}:{2:D2}", mins, secs, cents));
            }
            else
            {
                return(string.Format("{0:D2}:{1:D2}", mins, secs));
            }
#else
            if (printCents)
            {
                return(string.Format(CultureInfo.InvariantCulture, "{0:D2}:{1:D2}:{2:D2}", mins, secs, cents));
            }
            else
            {
                return(string.Format(CultureInfo.InvariantCulture, "{0:D2}:{1:D2}", mins, secs));
            }
#endif
        }
Example #3
0
        public void RecurseQuery(SBSBounds bbox, SBSMath.ClipStatus clipStatus, string category, int mask, List <LevelObject> results)
        {
            if (!(null == category) && 0 == this.GetNumObjectsOfCategory(category))
            {
                return;
            }

            if (!(-1 == mask) && 0 == this.GetNumObjectsWithMask(mask))
            {
                return;
            }

            if (SBSMath.ClipStatus.Overlapping == clipStatus)
            {
                clipStatus = SBSMath.GetClipStatus(bbox, bounds);
            }

            if (SBSMath.ClipStatus.Outside == clipStatus)
            {
                return;
            }
            else
            {
                bool doMaskCheck     = !(-1 == mask);
                bool doCategoryCheck = !(null == category);
                bool doClipCheck     = (SBSMath.ClipStatus.Overlapping == clipStatus);

                foreach (LevelObject obj in objects)
                {
                    if (doMaskCheck && 0 == (mask & obj.LayersMask))
                    {
                        continue;
                    }

                    if (doCategoryCheck && category != obj.Category)
                    {
                        continue;
                    }

                    if (doClipCheck && SBSMath.ClipStatus.Outside == SBSMath.GetClipStatus(bbox, obj.Bounds))
                    {
                        continue;
                    }

                    results.Add(obj);
                }
            }

            foreach (LevelCell child in children)
            {
                child.RecurseQuery(bbox, clipStatus, category, mask, results);
            }
        }
Example #4
0
        public void Update()
        {
            if (this.IsPaused)
            {
                return;
            }

            float prevRealTime = realTime;

            realTime   = Time.realtimeSinceStartup - totalPauseTime;
            deltaTime  = SBSMath.Min(maxDeltaTime, realTime - prevRealTime) * timeMultiplier;
            totalTime += deltaTime;
        }
Example #5
0
    public void WorldToToken(SBSVector3 worldPos, out float longitudinal, out float trasversal)
    {
        switch (type)
        {
        case TokenType.Cross:
        case TokenType.Rect:
            SBSVector3 localPos = matInvWorld.MultiplyPoint3x4(worldPos);
            longitudinal = SBSVector3.Dot(localPos, SBSVector3.forward) / lengthOrRadius;
            trasversal   = SBSVector3.Dot(localPos, SBSVector3.right) * 2.0f / width;
            break;

        case TokenType.Curve:
            SBSVector3 c  = this.Center,
                       p0 = matWorld.position - c,
                       p1 = worldPos - c;

            p0.DecrementBy(SBSVector3.Dot(p0, SBSVector3.up) * SBSVector3.up);
            p1.DecrementBy(SBSVector3.Dot(p1, SBSVector3.up) * SBSVector3.up);

            float sign  = SBSMath.Sign(SBSVector3.Dot(SBSVector3.Cross(p0, p1), SBSVector3.up));
            float angle = SBSVector3.Angle(p0, p1);

            longitudinal = angle / arcAngle;
            if (sign == curveDir)
            {
                longitudinal *= -1.0f;
            }
            trasversal = (p1.magnitude - lengthOrRadius) * 2.0f * curveDir / width;
            break;

        default:
            longitudinal = 0.0f;
            trasversal   = 0.0f;
            break;
        }
    }
Example #6
0
    public void Build()
    {
        int count = startTransforms.Length;

        startTokens  = new Token[count];
        startOffsets = new float[count];
        rootBranches = new TrackBranch[count];

        TokensManager.TokenHit[] hits = new TokensManager.TokenHit[count];
        for (int i = 0; i < count; ++i)
        {
            SBSVector3 p = startTransforms[i].position;

            hits[i] = new TokensManager.TokenHit();
            TokensManager.Instance.GetToken(p, out hits[i]);
            Asserts.Assert(hits[i].token != null);

            TrackBranch branch = hits[i].token.TrackBranch;
            startTokens[i]  = hits[i].token;
            rootBranches[i] = branch;
        }
        // ToDo
        foreach (KeyValuePair <TrackBranch, BranchNode> item in branchNodes)
        {
            item.Value.Initialize();
        }
        Stack <TrackBranch> branches        = new Stack <TrackBranch>();
        List <TrackBranch>  visitedBranches = new List <TrackBranch>();

        branches.Push(rootBranches[0]);
        visitedBranches.Add(rootBranches[0]);
        rootBranches[0].startLength = 0.0f;
        while (branches.Count > 0)
        {
            TrackBranch branch = branches.Pop();

            BranchNode node = branchNodes[branch];

            float maxBranchLength = 0.0f;
            foreach (BranchNode nextNode in node.NextLinks)
            {
                maxBranchLength = SBSMath.Max(maxBranchLength, nextNode.Branch.Length);
            }

            foreach (BranchNode nextNode in node.NextLinks)
            {
                if (!branches.Contains(nextNode.Branch) && !visitedBranches.Contains(nextNode.Branch))
                {
                    nextNode.Branch.LengthScale = maxBranchLength / nextNode.Branch.Length;
                    nextNode.Branch.startLength = Mathf.Max(nextNode.Branch.startLength, branch.startLength + branch.Length);
                    visitedBranches.Add(branch);
                    branches.Push(nextNode.Branch);
                }
            }
        }/*
          * foreach (KeyValuePair<TrackBranch, BranchNode> item in branchNodes)
          * Debug.Log(item.Value.Branch.startLength + ", " + item.Value.Branch.Length);
          */
        for (int i = 0; i < count; ++i)
        {
            TrackBranch branch = hits[i].token.TrackBranch;
            startOffsets[i] = (branch.startLength + hits[i].longitudinal * hits[i].token.Length) * branch.LengthScale;
            Debug.Log("startOffsets[" + i + "] = " + startOffsets[i]);
        }

        maxLength = 0.0f;
        foreach (KeyValuePair <TrackBranch, BranchNode> item in branchNodes)
        {
            maxLength = SBSMath.Max(maxLength, item.Key.startLength + item.Key.Length);
        }
        Debug.Log("Track length: " + maxLength);
    }
Example #7
0
    public void CreateBoxColliders(int numSegments, float trasversalOffset, float wallsHeight, float depth)
    {
        if (numSegments < 1)
        {
            return;
        }

        BoxCollider[] colliders = gameObject.GetComponentsInChildren <BoxCollider>();
        foreach (BoxCollider coll in colliders)
        {
            GameObject.DestroyImmediate(coll.gameObject);
        }
        colliders = null;

        float l = 0.0f,
              s = 1.0f / (float)numSegments;

        for (int i = 0; i < numSegments; ++i)
        {
#if UNITY_FLASH
            SBSVector3 p0 = new SBSVector3(), p1 = new SBSVector3(), q0 = new SBSVector3(), q1 = new SBSVector3(), t = new SBSVector3();

            this.TokenToWorld(l, -1.0f - trasversalOffset, p0, t);
            this.TokenToWorld(l + s, -1.0f - trasversalOffset, p1, t);
            this.TokenToWorld(l, 1.0f + trasversalOffset, q0, t);
            this.TokenToWorld(l + s, 1.0f + trasversalOffset, q1, t);
#else
            SBSVector3 p0, p1, q0, q1, t;

            this.TokenToWorld(l, -1.0f - trasversalOffset, out p0, out t);
            this.TokenToWorld(l + s, -1.0f - trasversalOffset, out p1, out t);
            this.TokenToWorld(l, 1.0f + trasversalOffset, out q0, out t);
            this.TokenToWorld(l + s, 1.0f + trasversalOffset, out q1, out t);
#endif

            SBSVector3 tanL  = p1 - p0,
                       tanR  = q1 - q0,
                       biTan = q0 - p0;
            float sizeX      = biTan.Normalize(),
                  sizeZL     = tanL.Normalize() + 0.2f,
                  sizeZR     = tanR.Normalize() + 0.2f;

            SBSQuaternion rotL = SBSQuaternion.AngleAxis(SBSVector3.Angle(SBSVector3.forward, tanL), SBSVector3.Cross(SBSVector3.forward, tanL)),
                          rotR = SBSQuaternion.AngleAxis(SBSVector3.Angle(SBSVector3.forward, tanR), SBSVector3.Cross(SBSVector3.forward, tanR));

            SBSVector3 groundCenter    = (p0 + p1 + q0 + q1) * 0.25f - SBSVector3.up * depth * 0.5f,
                       wallLeftCenter  = (p0 + p1 + SBSVector3.up * wallsHeight - biTan * depth) * 0.5f,
                       wallRightCenter = (q0 + q1 + SBSVector3.up * wallsHeight + biTan * depth) * 0.5f,
                       groundSize      = new SBSVector3(sizeX, depth, SBSMath.Max(sizeZL, sizeZR)),
                       wallLeftSize    = new SBSVector3(depth, wallsHeight, sizeZL),
                       wallRightSize   = new SBSVector3(depth, wallsHeight, sizeZR);

            GameObject ground = new GameObject("Ground", typeof(BoxCollider));
            ground.transform.parent   = transform;
            ground.transform.rotation = rotL;
            ground.transform.position = groundCenter;
            ground.GetComponent <BoxCollider>().size = groundSize;

            GameObject wallLeft  = new GameObject("WallLeft", typeof(BoxCollider)),
                       wallRight = new GameObject("WallRight", typeof(BoxCollider));
            wallLeft.transform.parent = wallRight.transform.parent = transform;

            wallLeft.transform.rotation = rotL;
            wallLeft.GetComponent <BoxCollider>().size = wallLeftSize;
            wallLeft.transform.position = wallLeftCenter;

            wallRight.transform.rotation = rotR;
            wallRight.GetComponent <BoxCollider>().size = wallRightSize;
            wallRight.transform.position = wallRightCenter;

            l += s;
        }
    }
Example #8
0
    public SBSBounds ComputeBounds()
    {
        int   i    = 0;
        float minX = float.MaxValue,
              minZ = float.MaxValue,
              maxX = float.MinValue,
              maxZ = float.MinValue;

        switch (type)
        {
        case TokenType.Curve:
            float offsetAngle     = transform.rotation.y * SBSMath.ToRadians,
                  oneOverArc      = 1.0f / arcAngle;
            float[] longitudinals =
            {
                0.0f,
                (SBSMath.PI * 0.5f - offsetAngle) * oneOverArc,
                (SBSMath.PI * 1.0f - offsetAngle) * oneOverArc,
                (SBSMath.PI * 1.5f - offsetAngle) * oneOverArc,
                (SBSMath.PI * 2.0f - offsetAngle) * oneOverArc,
                1.0f
            };
#if UNITY_FLASH
            SBSVector3 pos0 = new SBSVector3(), pos1 = new SBSVector3(), tang = new SBSVector3();
#else
            SBSVector3 pos0, pos1, tang;
#endif
            for (; i < 6; ++i)
            {
#if UNITY_FLASH
                this.TokenToWorld(Mathf.Clamp01(longitudinals[i]), -1.0f, pos0, tang);
                this.TokenToWorld(Mathf.Clamp01(longitudinals[i]), 1.0f, pos1, tang);
#else
                this.TokenToWorld(Mathf.Clamp01(longitudinals[i]), -1.0f, out pos0, out tang);
                this.TokenToWorld(Mathf.Clamp01(longitudinals[i]), 1.0f, out pos1, out tang);
#endif
                minX = SBSMath.Min(minX, SBSMath.Min(pos0.x, pos1.x));
                minZ = SBSMath.Min(minZ, SBSMath.Min(pos0.z, pos1.z));
                maxX = SBSMath.Max(maxX, SBSMath.Max(pos0.x, pos1.x));
                maxZ = SBSMath.Max(maxZ, SBSMath.Max(pos0.z, pos1.z));
            }
            break;

        case TokenType.Cross:
        case TokenType.Rect:
            Vector3[] corners =
            {
                new Vector3(-width * 0.5f, 0.0f,           0.0f),
                new Vector3(width * 0.5f,  0.0f,           0.0f),
                new Vector3(-width * 0.5f, 0.0f, lengthOrRadius),
                new Vector3(width * 0.5f,  0.0f, lengthOrRadius)
            };

            if (isCenterPivot)
            {
                corners[0].z = -lengthOrRadius * 0.5f;
                corners[1].z = -lengthOrRadius * 0.5f;
                corners[2].z = lengthOrRadius * 0.5f;
                corners[3].z = lengthOrRadius * 0.5f;
            }

            for (; i < 4; ++i)
            {
                Vector3 v = transform.TransformPoint(corners[i]);

                minX = SBSMath.Min(minX, v.x);
                minZ = SBSMath.Min(minZ, v.z);
                maxX = SBSMath.Max(maxX, v.x);
                maxZ = SBSMath.Max(maxZ, v.z);
            }
            break;
        }

        SBSBounds b = new SBSBounds();
        b.min.Set(minX, 0.0f, minZ);
        b.max.Set(maxX, 0.0f, maxZ);

        return(b);
    }