コード例 #1
0
ファイル: MoveTrail.cs プロジェクト: yijianyouxie/MobileGrass
    void AddPoint(TrailPoint point, Vector3 direction, float uv)
    {
        float   lifePercent    = (Time.time - point.creationTime) / lefttime;
        float   halfWidth      = unit;// (1 - lifePercent) * unit;
        float   normalStrength = 1 - lifePercent;
        Vector2 dir            = new Vector2();

        dir.x = direction.x * 0.5f + 0.5f;
        dir.y = direction.z * 0.5f + 0.5f;
        Color   normalStrengthColor = new Color(dir.x, normalStrength * 0.5f, dir.y, 1f);
        Vector3 pos   = point.pos;
        Vector3 right = Vector3.Cross(upDir, direction);

        vertices.Add(pos - right * halfWidth);
        vertices.Add(pos + right * halfWidth);
        colors.Add(normalStrengthColor);
        colors.Add(normalStrengthColor);
        int lastVert = vertices.Count - 1;

        if (lastVert >= 3)
        {
            triangles.Add(lastVert - 1);
            triangles.Add(lastVert);
            triangles.Add(lastVert - 2);
            triangles.Add(lastVert - 2);
            triangles.Add(lastVert - 3);
            triangles.Add(lastVert - 1);
        }
    }
コード例 #2
0
    public TrailPoint AddPoint(ITrailOwner owner)
    {
        TrailPoint point = new TrailPoint(owner);

        queue.Enqueue(point);
        return(point);
    }
コード例 #3
0
ファイル: Trail.cs プロジェクト: yuvadius/need4bit
    public void myUpdate()
    {
        float delta = 0;
        delta = (transform.position - trailPointList.First.Value.pos).magnitude;
        Vector3 newPos = transform.position;
        Quaternion newRot = transform.rotation;
        ++num;

        TrailPoint point = new TrailPoint(newPos, newRot, num, newPos.magnitude);
        if( flyingDevice.isInAir() == true ){
            numOfAir++;
            point.state = SegmentState.AIR;
        }else{
            numOfGround++;
            point.state = SegmentState.GROUND;
        }

        trailPointList.AddFirst(point);

        LinkedListNode<SegmentScript> runner = segmentList.Last; //doesn't matter from which side we start, because we move each at same rate.
        while (runner != null)
        {

            runner.Value.move(delta);
            runner = runner.Previous;
        }

        if (create)
        {
            create_segment();
            create = false;
        }

        trim_tail();
    }
コード例 #4
0
    private void AddPoint(TrailPoint point, Vector3 direction, float uv)
    {
        float lifePercent         = (Time.time - point.creationTime) / lifetime;
        float halfWidth           = width.Evaluate(lifePercent);
        float normalStrength      = strength.Evaluate(lifePercent);
        Color normalStrengthColor = new Color(normalStrength, normalStrength, normalStrength, normalStrength);

        direction.Normalize();
        Vector3 pos   = point.pos;
        Vector3 right = Vector3.Cross(upDir, direction);

        vertices.Add(pos - right * halfWidth);
        vertices.Add(pos + right * halfWidth);
        uvs.Add(new Vector2(0, uv));
        uvs.Add(new Vector2(1, uv));
        normals.Add(upDir);
        normals.Add(upDir);
        tangents.Add(new Vector4(direction.x, direction.y, direction.z, 1));
        tangents.Add(new Vector4(direction.x, direction.y, direction.z, 1));
        colors.Add(normalStrengthColor);
        colors.Add(normalStrengthColor);

        int lastVert = vertices.Count - 1;

        if (lastVert >= 3)
        {
            triangles.Add(lastVert - 1);
            triangles.Add(lastVert);
            triangles.Add(lastVert - 2);

            triangles.Add(lastVert - 2);
            triangles.Add(lastVert - 3);
            triangles.Add(lastVert - 1);
        }
    }
コード例 #5
0
    private void UpdateMesh(List <TrailPoint> renderPoints)
    {
        mesh.Clear();
        if (renderPoints.Count < 2)
        {
            return;
        }

        //Clear lists
        vertices.Clear();
        triangles.Clear();
        uvs.Clear();
        normals.Clear();
        tangents.Clear();
        colors.Clear();

        float uvFactor = 1.0f / (renderPoints.Count - 1);

        //Iterate though all previous points
        for (int i = 0; i < renderPoints.Count; i++)
        {
            //First point
            TrailPoint point = renderPoints[i];
            if (i == 0)
            {
                AddPoint(point, renderPoints[i + 1].pos - point.pos, 0);
                continue;
            }

            //Last point
            TrailPoint lastPoint = renderPoints[i - 1];
            if (i == renderPoints.Count - 1)
            {
                AddPoint(point, point.pos - lastPoint.pos, 1);
                break;
            }

            //In-between points
            TrailPoint nextPoint = renderPoints[i + 1];

            AddPoint(point, nextPoint.pos - lastPoint.pos, i * uvFactor);
        }

        mesh.vertices  = vertices.ToArray();
        mesh.triangles = triangles.ToArray();
        mesh.uv        = uvs.ToArray();
        mesh.normals   = normals.ToArray();
        mesh.tangents  = tangents.ToArray();
        mesh.colors    = colors.ToArray();

        //Those only work in Unity 5.2 and above... shouldn't be much faster anyways.
        //mesh.SetVertices(vertices);
        //mesh.SetTriangles(triangles, 0);
        //mesh.SetUVs(0, uvs);
        //mesh.SetNormals(normals);
        //mesh.SetTangents(tangents);
        //mesh.SetColors(colors);
    }
コード例 #6
0
        /// <summary>
        /// Adds new <see cref="TrailPoint"></see>.
        /// </summary>
        /// <param name="code">Code of trail point.</param>
        /// <param name="name">Name of trail point.</param>
        public void AddTrailPoint(string code, string name)
        {
            var trailPoint = new TrailPoint
            {
                Code = code,
                Name = name,
            };

            _dataSource.AddTrailPoint(trailPoint);
        }
コード例 #7
0
        private void ShiftMeshArrays()
        {
            bool removeTwo  = _points[0].ConnectsNext && _pointsCount > 1 && !_points[1].ConnectsNext;
            bool removeTris = removeTwo || _points[0].ConnectsNext;

            var newPoints = new TrailPoint[_points.Length];

            Array.Copy(_points, removeTwo ? 2 : 1, newPoints, 0, _points.Length - (removeTwo ? 2 : 1));
            _points = newPoints;

            Vector3[] verts = new Vector3[_mesh.vertices.Length];
            Vector2[] uvs   = new Vector2[_mesh.uv.Length];
            Color[]   cols  = new Color[_mesh.colors.Length];
            Vector3[] norms = new Vector3[_mesh.normals.Length];

            int vertsToRemove = removeTwo ? 4 : 2;

            Array.Copy(_mesh.vertices, vertsToRemove, verts, 0, _mesh.vertices.Length - vertsToRemove);
            Array.Copy(_mesh.uv, vertsToRemove, uvs, 0, _mesh.uv.Length - vertsToRemove);
            Array.Copy(_mesh.colors, vertsToRemove, cols, 0, _mesh.colors.Length - vertsToRemove);
            Array.Copy(_mesh.normals, vertsToRemove, norms, 0, _mesh.normals.Length - vertsToRemove);

            _mesh.vertices = verts;
            _mesh.uv       = uvs;
            _mesh.colors   = cols;
            _mesh.normals  = norms;

            var tris = _mesh.triangles;

            for (int i = 0; i < tris.Length; i++)
            {
                tris[i] -= vertsToRemove;

                if (tris[i] < 0)
                {
                    tris[i] = 0;
                }
            }

            if (removeTris)
            {
                _trisCount -= 2;

                var newTris = new int[_trisCount * 3];
                Array.Copy(tris, 6, newTris, 0, tris.Length - 6);

                tris = newTris;
            }

            _mesh.triangles = tris;

            _pointsCount -= removeTwo ? 2 : 1;
            _points[0].ConnectsPrevious = false;
        }
コード例 #8
0
ファイル: CustomTrail.cs プロジェクト: DeiveEx/Shader_Sandbox
    private void CreatePoint()
    {
        TrailPoint p = new TrailPoint()
        {
            position = transform.position,
            lifeTime = lifeTime
        };

        points.Insert(0, p);
        lastPos = transform.position;
    }
コード例 #9
0
        private bool CheckShouldBeAdded(TrailPoint newPoint, TrailPoint referencePoint)
        {
            Vector3 diff = (newPoint.Point2 + newPoint.Point1) / 2 -
                           (referencePoint.Point2 + referencePoint.Point1) / 2;

            if (diff.sqrMagnitude < _minDistanceBetweenSegmentsSqr)
            {
                return(false);
            }

            if (diff.sqrMagnitude > newPoint.Vector.sqrMagnitude &&
                diff.sqrMagnitude > referencePoint.Vector.sqrMagnitude)
            {
                return(true);
            }

            if (AngleTo90(newPoint.Point1 - newPoint.Point2, referencePoint.Point1 - referencePoint.Point2) < 15f)
            {
                return(true);
            }

            Vector3 referencePerpendicular =
                Vector3.Cross(referencePoint.Point2 - referencePoint.Point1, referencePoint.Normal);
            Vector3 halfPlaneDirection = referencePerpendicular *
                                         Mathf.Sign(Vector3.Dot(referencePerpendicular, referencePoint.DeltaPosition));

            int c = 0;

            if (Vector3.Dot(newPoint.Point1 - referencePoint.Point1, halfPlaneDirection) > 0)
            {
                c++;
            }

            if (Vector3.Dot(newPoint.Point1 - referencePoint.Point2, halfPlaneDirection) > 0)
            {
                c++;
            }

            if (Vector3.Dot(newPoint.Point2 - referencePoint.Point1, halfPlaneDirection) > 0)
            {
                c++;
            }

            if (Vector3.Dot(newPoint.Point2 - referencePoint.Point2, halfPlaneDirection) > 0)
            {
                c++;
            }

            return(c >= 3);
        }
コード例 #10
0
 private void UpdateTrailPoints(float deltaTime)
 {
     for (int i = trailPoints.Length - 1; i >= 0; i--)
     {
         TrailPoint p = trailPoints[i];
         if (p.isObsolete)
         {
             trailPoints.RemoveAtSwapBack(i);
         }
         else
         {
             p.Attenuate(trailAttenuation * deltaTime);
             trailPoints[i] = p;
         }
     }
 }
コード例 #11
0
    // Use this for initialization
    void Start()
    {
        GameObject parent = new GameObject();

        parent.name = "TrailParent";
        for (int i = 0; i < numberOfTrailPoints; i++)
        {
            Debug.Log("Instantiating 'pool'");
            TrailPoint newPoint = Instantiate(trailPointPrefab, transform.position, transform.rotation).GetComponent <TrailPoint>();
            inactiveTrailPoints.Add(newPoint);
            newPoint.gameObject.name  = "TrailPoint";
            newPoint.transform.parent = parent.transform;
            newPoint.gameObject.SetActive(false);
            newPoint.trailHandler = this;
        }
    }
コード例 #12
0
 public void Execute()
 {
     for (int i = 0, n = trailPoints.Length; i < n; i++)
     {
         TrailPoint p = trailPoints[i];
         ushort     x = (ushort)math.floor(length * p.uv.x);
         ushort     y = (ushort)math.floor(length * p.uv.y);
         // Inv. channel -> smaller value represents higher point strength.
         ushort a = colors[y * length + x].r;
         ushort b = (ushort)(255 - math.round(255 * p.strength));
         // Keep a if it's clearly stronger than b.
         // TODO threshold (64) should be derived from life time.
         byte col = (byte)(a + 64 < b ? a : b);
         colors[y * length + x] = new Color32(col, col, col, 255);
     }
 }
コード例 #13
0
ファイル: Trail.cs プロジェクト: yuvadius/need4bit
 public void AddSyncedTrailPoints(Vector3[] positions)
 {
     trailPointList.Clear(); //reset
     for(int i=0; i<positions.Length; ++i) {
         TrailPoint newPoint = new TrailPoint(
             positions[i],
             trailNum++
         );
         if(shouldShowTrail) {
             GameObject newTrailGizmo = Instantiate(trailGizmo) as GameObject;
             newTrailGizmo.transform.SetParent(null);
             newTrailGizmo.name = trailNum.ToString();
             newTrailGizmo.transform.position = positions[i];
             newTrailGizmo.transform.GetChild(0).GetComponent<MeshRenderer>().material.color = Color.blue;
             newTrailGizmo.transform.LookAt(Vector3.zero);
         }
         trailPointList.AddLast(newPoint);
     }
     isSet = true;
 }
コード例 #14
0
    void RemoveOldPoints(List <TrailPoint> pointList)
    {
        List <TrailPoint> remove = new List <TrailPoint>();


        for (int i = 0; i < pointList.Count; i++)
        {
            TrailPoint p = pointList[i];

            if (Time.time - p.timeCreated > lifeTime)
            {
                remove.Add(p);
            }
        }

        foreach (TrailPoint p in remove)
        {
            pointList.Remove(p);
        }
    }
コード例 #15
0
    // Update is called once per frame
    void Update()
    {
        if (trailTimer + trailFrequencyInSeconds < Time.time)
        {
            if (inactiveTrailPoints.Count > 0)
            {
                trailTimer = Time.time;
                TrailPoint newPoint = inactiveTrailPoints[0];
                activeTrailPoints.Add(newPoint);
                inactiveTrailPoints.RemoveAt(0);

                newPoint.deactivationTime   = trailPointLifetime;
                newPoint.trailPointType     = currentTrailType;
                newPoint.transform.position = transform.position;
                newPoint.gameObject.SetActive(true);

                if (lastTrailPoint)
                {
                    newPoint.connectedTrailPoint = lastTrailPoint;
                }
                lastTrailPoint = newPoint;
            }
        }
    }
コード例 #16
0
ファイル: MoveTrail.cs プロジェクト: yijianyouxie/MobileGrass
    // Update is called once per frame
    void Update()
    {
        while (points.Count > 0)
        {
            if (Time.time - points[0].creationTime > lefttime)
            {
                points.RemoveAt(0);
            }
            else
            {
                break;
            }
        }

        Vector3 pos        = transform.position;
        bool    addedPoint = false;

        if (points.Count == 0 || Vector3.Distance(points[points.Count - 1].pos, pos) > minVertexDistance)
        {
            points.Add(new TrailPoint(pos, Time.time));
            addedPoint = true;
        }

        List <TrailPoint> renderPoints = new List <TrailPoint>(points);

        if (!addedPoint)
        {
            renderPoints.Add(new TrailPoint(pos, Time.time));
        }

        if (renderPoints.Count < 2)
        {
            return;
        }

        mesh.Clear();
        if (renderPoints.Count < 2)
        {
            return;
        }

        vertices.Clear();
        triangles.Clear();
        colors.Clear();
        float uvFactor = 1f / (renderPoints.Count - 1);

        for (int i = 0; i < renderPoints.Count; i++)
        {
            TrailPoint point = renderPoints[i];
            if (i == 0)
            {
                AddPoint(point, renderPoints[i + 1].pos - point.pos, 0);
                continue;
            }

            TrailPoint lastPoint = renderPoints[i - 1];
            if (i == renderPoints.Count - 1)
            {
                AddPoint(point, point.pos - lastPoint.pos, 1f);
                break;
            }

            TrailPoint nextPoint = renderPoints[i + 1];
            AddPoint(point, nextPoint.pos - lastPoint.pos, i * uvFactor);
        }

        mesh.vertices  = vertices.ToArray();
        mesh.triangles = triangles.ToArray();
        mesh.colors    = colors.ToArray();
        Graphics.DrawMesh(mesh, Matrix4x4.identity, material, layer);
    }
コード例 #17
0
ファイル: Trail.cs プロジェクト: yuvadius/need4bit
    void Start()
    {
        flyingDevice = Flying.instance;
        tailLength = firstTrailSize;
        segmentList = new LinkedList<SegmentScript>();
        trailPointList = new LinkedList<TrailPoint>();

        Vector3 newPos = transform.position;
        Quaternion newRot = transform.rotation;
        ++num;
        TrailPoint point = new TrailPoint(newPos, newRot, num, newPos.magnitude);
        point.state = SegmentState.GROUND;
        ++numOfGround;
        trailPointList.AddFirst(point);
    }
コード例 #18
0
    void LateUpdate()
    {
        if (!_isInitialized)
        {
            return;
        }
        int numPoints = dots.Count;

        if (Emit)
        {
            TrailPoint p = new TrailPoint();
            p.basePosition   = _baseAnchor.position;
            p.circlePoints   = MakeCircle();
            p.rotationVector = _baseAnchor.rotation.eulerAngles;
            p.timeCreated    = Time.time;
            points.Add(p);
        }

        RemoveOldPoints(points);

        if (points.Count == 0)
        {
            trailMesh.Clear();
        }


        if (points.Count > 0)
        {
            newVertices  = new Vector3[points.Count * dots.Count];
            newUV        = new Vector2[points.Count * dots.Count];
            newTriangles = new int[points.Count * (dots.Count * 6 + 6)]; //+6
            colors       = new Color[points.Count * dots.Count];

            int trianglesCounter = 0;

            float uRatio    = 1.0f / (float)(points.Count);
            float vRatio    = 1.0f / (points.Count);
            float colorPart = 1.0f / points.Count;

            float coneDecresCoef = 1f;//(1.0f - coneCoef) / points.Count;

            Vector3[] prevPos = null;

            trianglesCounter = (numPoints) * (points.Count) * 6 - 1;
            for (int i = points.Count - 1; i >= 0; i--)
            {
                TrailPoint p = points[i];

                Color curColor = _gradient.Evaluate((points.Count - i) * colorPart);

                for (int j = numPoints - 1; j >= 0; j--)
                {
                    Vector3 currentPoint = p.circlePoints[j];
                    Vector3 newPoint     = Quaternion.Euler(p.rotationVector) * currentPoint + p.basePosition;// ((currentPoint) * (coneCoef + i * coneDecresCoef))  + p.basePosition;
                    // Vector3 newPoint = ((currentPoint - p.basePosition) * (coneCoef + i * coneDecresCoef)) + p.basePosition;
                    newVertices[i * numPoints + j] = newPoint;
                    newUV[i * numPoints + j]       = new Vector2((numPoints - j) * uRatio, (points.Count - i) * vRatio);

                    colors[i * numPoints + j] = curColor;

                    if (j % n == 0)
                    {
                        int index = j / (numPoints - n);
                        if (prevPos != null)
                        {
                            Debug.DrawLine(prevPos[index], newPoint, Color.blue);
                        }
                        else
                        {
                            prevPos = new Vector3[n];
                        }

                        prevPos[index] = newPoint;
                    }
                }

                if (i > 0)
                {
                    for (int j = numPoints - 2; j >= 0; j--)
                    {
                        newTriangles[trianglesCounter--] = (i - 1) * numPoints + j;
                        newTriangles[trianglesCounter--] = (i - 1) * numPoints + (j + 1);
                        newTriangles[trianglesCounter--] = i * numPoints + j;

                        newTriangles[trianglesCounter--] = (i - 1) * numPoints + (j + 1);
                        newTriangles[trianglesCounter--] = i * numPoints + (j + 1);
                        newTriangles[trianglesCounter--] = i * numPoints + j;
                    }

                    newTriangles[trianglesCounter--] = (i - 1) * numPoints;
                    newTriangles[trianglesCounter--] = i * numPoints;
                    newTriangles[trianglesCounter--] = (i - 1) * numPoints + (numPoints - 1);

                    newTriangles[trianglesCounter--] = i * numPoints;
                    newTriangles[trianglesCounter--] = i * numPoints + (numPoints - 1);
                    newTriangles[trianglesCounter--] = (i - 1) * numPoints + (numPoints - 1);
                }
            }


            // for(int i = 0; i < newTriangles.Length - 1; i++)
            // {
            //     Debug.DrawLine(newVertices[newTriangles[i]],newVertices[newTriangles[i + 1]], Color.red);
            // }

            trailMesh.Clear();

            trailMesh.vertices  = newVertices;
            trailMesh.uv        = newUV;
            trailMesh.colors    = colors;
            trailMesh.triangles = newTriangles;

            if (meshCollider != null)
            {
                meshCollider.sharedMesh = trailMesh;
            }

            trailMesh.RecalculateNormals();
        }
    }
コード例 #19
0
    private void AddPoint(TrailPoint point, Vector3 direction, float uv)
    {
        float lifePercent = (Time.time - point.creationTime) / lifetime;
        float halfWidth = width.Evaluate(lifePercent);
        float normalStrength = strength.Evaluate(lifePercent);
        Color normalStrengthColor = new Color(normalStrength, normalStrength, normalStrength, normalStrength);

        direction.Normalize();
        Vector3 pos = point.pos;
        Vector3 right = Vector3.Cross(upDir, direction);

        vertices.Add(pos - right * halfWidth);
        vertices.Add(pos + right * halfWidth);
        uvs.Add(new Vector2(0, uv));
        uvs.Add(new Vector2(1, uv));
        normals.Add(upDir);
        normals.Add(upDir);
        tangents.Add(new Vector4(direction.x, direction.y, direction.z, 1));
        tangents.Add(new Vector4(direction.x, direction.y, direction.z, 1));
        colors.Add(normalStrengthColor);
        colors.Add(normalStrengthColor);

        int lastVert = vertices.Count-1;
        if (lastVert >= 3)
        {
            triangles.Add(lastVert - 1);
            triangles.Add(lastVert);
            triangles.Add(lastVert - 2);

            triangles.Add(lastVert - 2);
            triangles.Add(lastVert - 3);
            triangles.Add(lastVert - 1);
        }
    }
コード例 #20
0
 public void DeactivatePoint(TrailPoint trailPoint)
 {
     activeTrailPoints.Remove(trailPoint);
     trailPoint.gameObject.SetActive(false);
     inactiveTrailPoints.Add(trailPoint);
 }
コード例 #21
0
ファイル: SimpleAStar.cs プロジェクト: cahurley/codewars
        public static List<Point> CalculatePath(GameMap map, Point start, Point end)
        {
            // should never happen but just to be sure
            if (start == end)
                return new List<Point> {start};

            // nodes are points we have walked to
            Dictionary<Point, TrailPoint> nodes = new Dictionary<Point, TrailPoint>();
            // points we have in a TrailPoint, but not yet evaluated.
            List<TrailPoint> notEvaluated = new List<TrailPoint>();

            TrailPoint tpOn = new TrailPoint(start, end, 0);
            while (true)
            {
                nodes.Add(tpOn.MapTile, tpOn);

                // get the neighbors
                TrailPoint tpClosest = null;
                foreach (Point ptOffset in offsets)
                {
                    Point pt = new Point(tpOn.MapTile.X + ptOffset.X, tpOn.MapTile.Y + ptOffset.Y);
                    MapSquare square = map.SquareOrDefault(pt);
                    // off the map or not a road/bus stop
                    if ((square == null) || (!square.Tile.IsDriveable))
                        continue;

                    // already evaluated - add it in
                    if (nodes.ContainsKey(pt))
                    {
                        TrailPoint tpAlreadyEvaluated = nodes[pt];
                        tpAlreadyEvaluated.Cost = Math.Min(tpAlreadyEvaluated.Cost, tpOn.Cost + 1);
                        tpOn.Neighbors.Add(tpAlreadyEvaluated);
                        continue;
                    }

                    // add this one in
                    TrailPoint tpNeighbor = new TrailPoint(pt, end, tpOn.Cost + 1);
                    tpOn.Neighbors.Add(tpNeighbor);
                    // may already be in notEvaluated. If so remove it as this is a more recent cost estimate
                    int indTp = notEvaluated.FindIndex(tp => tp.MapTile == tpNeighbor.MapTile);
                    if (indTp != -1)
                        notEvaluated.RemoveAt(indTp);

                    // we only assign to tpClosest if it is closer to the destination. If it's further away, then we
                    // use notEvaluated below to find the one closest to the dest that we have not walked yet.
                    if (tpClosest == null)
                    {
                        if (tpNeighbor.Distance < tpOn.Distance)
                            // new neighbor is closer - work from this next.
                            tpClosest = tpNeighbor;
                        else
                            // this is further away - put in the list to try if a better route is not found
                            notEvaluated.Add(tpNeighbor);
                    }
                    else
                        if (tpClosest.Distance <= tpNeighbor.Distance)
                            // this is further away - put in the list to try if a better route is not found
                            notEvaluated.Add(tpNeighbor);
                        else
                        {
                            // this is closer than tpOn and another neighbor - use it next.
                            notEvaluated.Add(tpClosest);
                            tpClosest = tpNeighbor;
                        }
                }

                // re-calc based on neighbors
                tpOn.RecalculateDistance(ptOffMap, map.Width);

                // if no closest, then get from notEvaluated. This is where it guarantees that we are getting the shortest
                // route - we go in here if the above did not move a step closer. This may not either as the best choice
                // may be the neighbor we didn't go with above - but we drop into this to find the closest based on what we know.
                if (tpClosest == null)
                {
                    if (notEvaluated.Count == 0)
                    {
                        Trap.trap();
                        break;
                    }
                    // We need the closest one as that's how we find the shortest path.
                    tpClosest = notEvaluated[0];
                    int index = 0;
                    for (int ind = 1; ind < notEvaluated.Count; ind++ )
                    {
                        TrailPoint tpNotEval = notEvaluated[ind];
                        if (tpNotEval.Distance >= tpClosest.Distance)
                            continue;
                        tpClosest = tpNotEval;
                        index = ind;
                    }
                    notEvaluated.RemoveAt(index);
                }

                // if we're at end - we're done!
                if (tpClosest.MapTile == end)
                {
                    tpClosest.Neighbors.Add(tpOn);
                    nodes.Add(tpClosest.MapTile, tpClosest);
                    break;
                }

                // try this one
                tpOn = tpClosest;
            }

            List<Point> path = new List<Point>();
            if (! nodes.ContainsKey(end))
            {
                Trap.trap();
                return path;
            }

            // Create the return path - from end back to beginning.
            tpOn = nodes[end];
            path.Add(tpOn.MapTile);
            while (tpOn.MapTile != start)
            {
                List<TrailPoint> neighbors = tpOn.Neighbors;
                int cost = tpOn.Cost;

                tpOn = tpOn.Neighbors[0];
                for (int ind = 1; ind < neighbors.Count; ind++)
                    if (neighbors[ind].Cost < tpOn.Cost)
                        tpOn = neighbors[ind];

                // we didn't get to the start.
                if (tpOn.Cost >= cost)
                {
                    Trap.trap();
                    return path;
                }
                path.Insert(0, tpOn.MapTile);
            }

            return path;
        }
コード例 #22
0
    // Update is called once per frame
    void Update()
    {
        // If the follow died, then die.
        if (Follow == null)
        {
            // Disable it
            Enabled = false;
        }
        else
        {
            // Cooldown
            cooldown -= Time.deltaTime;
            if (cooldown < 0)
            {
                // Reset it.
                cooldown = DropRate;

                // Check if we have a rigid body.
                Vector2 normal = Vector2.zero;
                Vector2 outdir = OutDir;

                Rigidbody rigidBody = Follow.GetComponent <Rigidbody>();
                if (rigidBody)
                {
                    normal = new Vector2(-rigidBody.velocity.z, rigidBody.velocity.x).normalized;
                }
                else
                {
                    normal = new Vector2(-BackwardsDir.y, BackwardsDir.x).normalized;
                    outdir = BackwardsDir;
                }

                // Drop down some points.
                TrailPoint left = new TrailPoint();
                left.Normal    = normal;
                left.Forwards  = outdir;
                left.Pos       = new Vector2(Follow.position.x, Follow.position.z) + normal * StartWidth;
                left.Speed     = DispersionRate + Random.Range(-0.5f, 0.5f) * DispersionRate;
                left.BackSpeed = BackSpeed;                // + Follow.rigidbody.velocity.magnitude;
                TrailPoint right = new TrailPoint();
                right.Normal    = normal;
                right.Forwards  = outdir;
                right.Pos       = new Vector2(Follow.position.x, Follow.position.z) - normal * StartWidth;
                right.Speed     = -DispersionRate + Random.Range(-0.5f, 0.5f) * DispersionRate;
                right.BackSpeed = BackSpeed;                // + Follow.rigidbody.velocity.magnitude;

                // Set the color
                if (BoostEnabled)
                {
                    left.Color  = BoostColor;
                    right.Color = BoostColor;
                }
                else
                {
                    left.Color  = Color;
                    right.Color = Color;
                }

                // Make them invisible if it is disabled.
                if (!Enabled)
                {
                    left.Color  *= 0;
                    right.Color *= 0;
                }

                points.AddLast(right);
                points.AddLast(left);
            }
        }

        // If the point list is over the maximimum.
        while (points.Count >= MaxVerts)
        {
            points.RemoveFirst();
        }

        // Update the points.
        int  count    = 0;
        bool allAlpha = true;

        foreach (TrailPoint p in points)
        {
            p.Pos          += p.Normal * p.Speed * Time.deltaTime;
            p.Pos          -= p.Forwards * p.BackSpeed * Time.deltaTime;
            p.Color.a      -= p.Color.a * AlphaDecay * Time.deltaTime;
            vertices[count] = new Vector3(p.Pos.x, 0, p.Pos.y);
            colors[count]   = p.Color;
            ++count;

            if (p.Color.a > 0.01f)
            {
                allAlpha = false;
            }
        }
        if (allAlpha && Follow == null)
        {
            Destroy(gameObject);
        }

        // Create the mesh.
        filter.mesh.vertices = vertices;
        filter.mesh.uv       = uvs;
        filter.mesh.colors   = colors;

        // Generate indices
        indices = new int[(points.Count - 2) * 3];
        for (int i = 0; i < points.Count - 2; ++i)
        {
            if (i % 2 == 0)
            {
                indices[i * 3 + 0] = i;
                indices[i * 3 + 1] = i + 1;
                indices[i * 3 + 2] = i + 2;
            }
            else
            {
                indices[i * 3 + 0] = i + 2;
                indices[i * 3 + 1] = i + 1;
                indices[i * 3 + 2] = i;
            }
        }
        //filter.mesh.SetTriangleStrip(indices, 0);
        // Temp
        filter.mesh.SetTriangles(indices, 0);
        //filter.mesh.RecalculateNormals();

        // Set the mesh bounds
        filter.mesh.RecalculateBounds();
    }
コード例 #23
0
ファイル: Trail.cs プロジェクト: yuvadius/need4bit
 void addTrailPoint()
 {
     Vector3 newPos = transform.position.normalized;
     TrailPoint point = new TrailPoint(newPos, trailNum++);
     trailPointList.AddFirst(point);
     if( shouldShowTrail) {
         GameObject newTrailGizmo = Instantiate(trailGizmo) as GameObject;
         newTrailGizmo.transform.SetParent(null);
         newTrailGizmo.name = trailNum.ToString();
         newTrailGizmo.transform.position = newPos;
         newTrailGizmo.transform.LookAt(Vector3.zero);
     }
 }
コード例 #24
0
 public void AddTrailPoint(TrailPoint trailPoint)
 {
     TrailPoints.Add(trailPoint.Code, trailPoint);
 }