Example #1
0
    private void shengchengdiren()
    {
        WayLine[]  usableWayline = SelectUsableWayline();
        WayLine    line          = usableWayline[Random.Range(0, usableWayline.Length)];
        int        random        = Random.Range(0, Enemys.Length);
        GameObject go            = Instantiate(Enemys[random], line.WayPoints[0], Quaternion.identity) as GameObject;
        mada       mada          = go.GetComponent <mada>();

        mada.line = line;
    }
Example #2
0
 //将每个路线上的点的位置放好
 private void CalculateWayPoints()
 {
     lines = new WayLine[transform.childCount];
     for (int i = 0; i < lines.Length; i++)
     {
         lines[i] = new WayLine(transform.GetChild(i).childCount);
         for (int index = 0; index < lines[i].WayPoints.Length; index++)
         {
             lines[i].WayPoints[index] = transform.GetChild(i).GetChild(index).position;
         }
     }
 }
Example #3
0
 /// <summary>
 /// 选择路径
 /// </summary>
 /// <param name="wayName"></param>
 /// <returns></returns>
 public WayLine ChoiseWayLine(string wayName)
 {
     for (int i = 0; i < wayLines.Length; i++)
     {
         if (wayLines[i].name_WayLine == wayName)
         {
             currentChioseWayLine = wayLines[i];
             return(wayLines[i]);
         }
     }
     Debug.Log("您所查找的路线不存在!");
     return(null);
 }
Example #4
0
 //路线生成器
 public void SpawnWayLine(WayLine wayLine)
 {
     lisGuidePost.Clear();//清空路标
     //根据选择路线创建寻路路标
     for (int i = 0; i < wayLine.WayPoints.Length; i++)
     {
         if (guidePost)
         {
             GameObject go = GameObjectPool.Instance.CreateObject("路标" + i.ToString(), guidePost, wayLine.WayPoints[i].position, wayLine.WayPoints[i].rotation);
             lisGuidePost.Add(go);
         }
     }
 }
Example #5
0
    private void createLines()
    {
        GameObject linesParent = new GameObject("Lines");

        linesParent.transform.parent        = transform;
        linesParent.transform.localPosition = center + new Vector3(0f, 0f, -0.01f);

        // Side lines
        Vector3    sideLineOuter = new Vector3(LINE_THICKNESS, 0f, 0f);
        Vector3    sideLineInner = new Vector3(width - LINE_THICKNESS, LINE_THICKNESS, 0f);
        GameObject leftLine      = MapSurface.createPlaneMeshForPoints(sideLineOuter, sideLineInner);
        GameObject rightLine     = MapSurface.createPlaneMeshForPoints(sideLineOuter, sideLineInner);

        // Use WayLine for setting white material
        WayLine.SetWhiteMaterial(leftLine);
        WayLine.SetWhiteMaterial(rightLine);

        // Locate left line correctly
        leftLine.name                    = "Left outer";
        leftLine.transform.parent        = linesParent.transform;
        leftLine.transform.localPosition = (Quaternion.Euler(0f, 0f, rotation) * new Vector3(-LINE_THICKNESS, -height / 2f + LINE_THICKNESS * 1.5f, 0f));
        leftLine.transform.rotation      = Quaternion.Euler(0f, 0f, rotation);

        // Locate right line correctly
        rightLine.name                    = "Right outer";
        rightLine.transform.parent        = linesParent.transform;
        rightLine.transform.localPosition = (Quaternion.Euler(0f, 0f, rotation) * new Vector3(LINE_THICKNESS, height / 2f - LINE_THICKNESS * 1.5f, 0f));
        rightLine.transform.rotation      = Quaternion.Euler(0f, 0f, rotation);

        // Goal lines
        Vector3    goalLineOuter = new Vector3(0f, LINE_THICKNESS, 0f);
        Vector3    goalLineInner = new Vector3(LINE_THICKNESS, height - LINE_THICKNESS, 0f);
        GameObject goalLineClose = MapSurface.createPlaneMeshForPoints(goalLineOuter, goalLineInner);
        GameObject goalLineFar   = MapSurface.createPlaneMeshForPoints(goalLineOuter, goalLineInner);

        // Use WayLine for setting white material
        WayLine.SetWhiteMaterial(goalLineClose);
        WayLine.SetWhiteMaterial(goalLineFar);

        // Locate close goal line correctly
        goalLineClose.name                    = "Close goal line";
        goalLineClose.transform.parent        = linesParent.transform;
        goalLineClose.transform.localPosition = (Quaternion.Euler(0f, 0f, rotation) * new Vector3(-width / 2f + LINE_THICKNESS * 1.5f, -LINE_THICKNESS, 0f));
        goalLineClose.transform.rotation      = Quaternion.Euler(0f, 0f, rotation);

        // Locate far goal line correctly
        goalLineFar.name                    = "Far goal line";
        goalLineFar.transform.parent        = linesParent.transform;
        goalLineFar.transform.localPosition = (Quaternion.Euler(0f, 0f, rotation) * new Vector3(width / 2f - LINE_THICKNESS * 1.5f, LINE_THICKNESS, 0f));
        goalLineFar.transform.rotation      = Quaternion.Euler(0f, 0f, rotation);
    }
Example #6
0
 public void CalculateWayLines()
 {
     lines = new WayLine[this.transform.childCount];
     for (int i = 0; i < lines.Length; i++)
     {
         Transform wayline    = transform.GetChild(i);
         int       childCount = wayline.childCount;
         lines[i] = new WayLine(childCount);
         for (int j = 0; j < childCount; j++)
         {
             lines[i].wayPoints[j] = wayline.GetChild(j).position;
         }
     }
 }
Example #7
0
    void DoPathCommon(string wayName)
    {
        //查找路线
        WayLine line = waySpawn.ChoiseWayLine(wayName);

        if (line != null)
        {
            //生成路线
            waySpawn.SpawnWayLine(line);
            //更改当前玩家状态
            actorAI.state = ActorState.Pathfinding;
            actorAI.motor.targetWayLine = line;
        }
    }
Example #8
0
 private void CreateEnemy()
 {
     WayLine[] IsAbledLines = SelectIsAbled();
     if (IsAbledLines != null)
     {
         WayLine enemyline = IsAbledLines[Random.Range(0, IsAbledLines.Length)];
         enemyline.isAbled = false;
         GameObject enemyGO    = Instantiate(enemy, enemyline.WayPoints[0], Quaternion.identity) as GameObject;
         Navigation navigation = enemyGO.GetComponent <Navigation>();
         navigation.line            = enemyline;
         allSpawnEnemys[SpawnIndex] = enemyGO;
         SpawnIndex++;
     }
 }
Example #9
0
    private GameObject getDashedLineForWay(Vector3 way)
    {
        GameObject dashedLine = new GameObject();

        dashedLine.name = "Dashed line";
        for (float xStart = CITY_DASHED_LINE_GAP; xStart <= way.x - DASHED_LINE_WIDTH; xStart += DASHED_LINE_WIDTH + CITY_DASHED_LINE_GAP)
        {
            Vector3    lineVector = new Vector3(DASHED_LINE_WIDTH, way.y, way.z);
            GameObject linePart   = getLineForWay(lineVector, GetDashedLineHeight());
            linePart.name = "Dash";

            WayLine.SetWhiteMaterial(linePart);
            linePart.transform.SetParent(dashedLine.transform);
            linePart.transform.localPosition = new Vector3(xStart - way.x / 2f, 0f, 0f);
        }
        return(dashedLine);
    }
Example #10
0
 private void CalculateWayLines()
 {
     //WayLine 路线  与 子物体   对应
     //lines[0].Points 该路线所有路点坐标   与  子物体的子物体.Position对应
     lines = new WayLine[transform.childCount];
     //创建路线
     for (int i = 0; i < lines.Length; i++)//0    1     2
     {
         Transform waylineTF = transform.GetChild(i);
         lines[i] = new WayLine(waylineTF.childCount);
         for (int pointIndex = 0; pointIndex < waylineTF.childCount; pointIndex++)
         {
             //获取每个路点坐标
             lines[i].Points[pointIndex] = waylineTF.GetChild(pointIndex).position;
         }
     }
 }
Example #11
0
    private void CalculateWayLine()
    {
        lines = new WayLine[transform.childCount];
        for (int i = 0; i < lines.Length; i++)
        {//每一条路线
            //路线变换组件的引用
            Transform wayLineTF = transform.GetChild(i);
            //创建路线对象
            lines[i] = new WayLine(wayLineTF.childCount);

            //创建路点对象

            for (int pointIndex = 0; pointIndex < wayLineTF.childCount; pointIndex++)
            {
                lines[i].WayPoints[pointIndex] = wayLineTF.GetChild(pointIndex).position;
            }
        }
    }
Example #12
0
    private void CreateEnemy()
    {
        //选择一条可用路线
        WayLine[] usableLines = FindUsaleWayLines();
        WayLine   way         = usableLines[Random.Range(0, usableLines.Length)];
        //延迟时间随机
        int        index = Random.Range(0, enemyTypes.Length);
        GameObject enemy = Instantiate(enemyTypes[index], way.wayPoints[0], Quaternion.identity) as GameObject;

        EnemyManager.monsters.Add(enemy);

        EnemyMotor motor = enemy.GetComponent <EnemyMotor>();
        Enemy      e     = enemy.GetComponent <Enemy>();

        e.spawn      = this;
        motor.line   = way;
        way.isUsable = false;
    }
Example #13
0
    private void createOuterLines(WayReference reference)
    {
        GameObject way       = reference.gameObject;
        float      wayHeight = way.transform.localScale.y;

        GameObject lineUpper = getLineForWay(drawSize);

        lineUpper.name = "Line border 1";
        WayLine.SetWhiteMaterial(lineUpper);
        lineUpper.transform.SetParent(transform);
        lineUpper.transform.localPosition = new Vector3(0, -wayHeight / 2f, -0.01f);

        GameObject lineLower = getLineForWay(drawSize);

        lineLower.name = "Line border 2";
        WayLine.SetWhiteMaterial(lineLower);
        lineLower.transform.SetParent(transform);
        lineLower.transform.localPosition = new Vector3(0, wayHeight / 2f - GetLineHeight(), -0.01f);
    }
Example #14
0
    private void CreateEnemy()
    {
        //查找所有可用路线
        var usableWaylines = SelectUsableWayLines();
        //随机选择一条
        WayLine wayLine = usableWaylines[Random.Range(0, usableWaylines.Length)];

        //创建一个敌人
        //GameObject enemyGO = Instantiate(敌人预制件, 第一个路点, Quaternion.identity) as GameObject;
        int        enemyTypeIndex = Random.Range(0, enemyTypes.Length);
        GameObject enemyGO        = Instantiate(enemyTypes[enemyTypeIndex], wayLine.Points[0], Quaternion.identity) as GameObject;

        //传递信息
        enemyGO.GetComponent <EnemyMotor>().wayline = wayLine;
        wayLine.IsUsable = false;//该路线不可用
        //传递当前生成器对象引用,便于敌人死亡时调用当前对象的生成敌人方法
        //[建议使用委托代替]
        enemyGO.GetComponent <EnemyStatusInfo>().spawn = this;
    }
Example #15
0
    private void CreateEnemy()
    {
        WayLine[] result = SelectUsableWayLine();
        WayLine   line   = result[Random.Range(0, result.Length)];

        //创建敌人
        //Object.Instantiate(敌人预制件,第一个点的位置,旋转角度)
        int        count = Random.Range(0, enemyType.Length);
        GameObject go    = Instantiate(enemyType[count], line.WayPoints[0], Quaternion.identity);
        //设置参数
        EnemyMotor motor = go.GetComponent <EnemyMotor>();

        motor.line    = line;
        line.isUsable = false;//不可使用
        //********************************

        //传递生成器引用给EnemyStatusInfo
        go.GetComponent <EnemyStatusInfo>().spawn = this;
    }
Example #16
0
    /// <summary>
    /// 计算所有路线
    /// </summary>
    public void CalculateWaylines()
    {
        lines = new WayLine[transform.childCount];
        for (int i = 0; i < lines.Length; i++)
        {
            //每一个路线
            Transform wayLineTF = transform.GetChild(i);
            //创建路线对象
            lines[i]          = new WayLine();
            lines[i].IsUsable = true;

            //创建路点数组对象
            lines[i].WayPoints = new Vector3[wayLineTF.childCount];
            for (int pointindex = 0; pointindex < wayLineTF.childCount; pointindex++)
            {
                //路点赋值
                lines[i].WayPoints[pointindex] = wayLineTF.GetChild(pointindex).position;
            }
        }
    }
Example #17
0
    private void createMiddleLine(WayReference reference)
    {
        // Number of fields in opposite direction
        float fieldsFromPos2 = reference.getNumberOfFieldsInDirection(false);

        // Number of fields in total
        float numberOfFields = reference.getNumberOfFields();

        // Percentual position of way width, where to put middle line
        float percentualPositionY = fieldsFromPos2 / numberOfFields;

        // Way width
        GameObject way       = reference.gameObject;
        float      wayHeight = way.transform.localScale.y - GetLineHeight() * 2f;


        GameObject lineMiddle = getLineForWay(drawSize);

        lineMiddle.name = "Middle line";
        WayLine.SetWhiteMaterial(lineMiddle);
        lineMiddle.transform.SetParent(transform);
        lineMiddle.transform.localPosition = new Vector3(0, GetLineHeight() + percentualPositionY * wayHeight - wayHeight / 2f - GetLineHeight() / 2f, -0.01f);
    }
Example #18
0
    private void CalculateWayLines()
    {
        /*
         * 当前脚本附加到根路线中。
         * 子物体为路线(WayLine对象)
         * 孙子为路点(Vector3)
         */
        //1.创建路线数组
        wayLines = new WayLine[transform.childCount];

        for (int lineIndex = 0; lineIndex < wayLines.Length; lineIndex++)
        {
            Transform childTF = transform.GetChild(lineIndex);
            //2.创建路线
            wayLines[lineIndex] = new WayLine(childTF.childCount);
            //给路线一个名称
            wayLines[lineIndex].name_WayLine = childTF.name;
            for (int pointIndex = 0; pointIndex < childTF.childCount; pointIndex++)
            {//3.指定坐标
                Transform pos = childTF.GetChild(pointIndex);
                wayLines[lineIndex].SetPoint(pointIndex, pos);
            }
        }
    }
Example #19
0
    private static void CreateCurvedMiddleLine(GameObject parent, long key, List <WayReference> wayReferences)
    {
        Pos     centerPos   = NodeIndex.getPosById(key);
        Vector3 posPosition = Game.getCameraPosition(centerPos);

        WayReference firstReference  = wayReferences [0];
        WayReference secondReference = wayReferences [1];

        bool firstIsNode1  = firstReference.isNode1(centerPos);
        bool secondIsNode1 = secondReference.isNode1(centerPos);

        GameObject wayFirst  = firstReference.gameObject;
        GameObject waySecond = secondReference.gameObject;

        // Number of fields in opposite direction
        float fieldsFromPos2 = firstReference.getNumberOfFieldsInDirection(false);

        // Number of fields in total
        float numberOfFields = firstReference.getNumberOfFields();

        // Percentual position of way width, where to put middle line
        float percentualPositionY = fieldsFromPos2 / numberOfFields;

        // Way width
        float wayHeight = wayFirst.transform.localScale.y - GetLineHeight() * 2f;

        // TODO - What if the wayReference is REALLY short
        Vector3    wayFirstSize                = wayFirst.transform.localScale;
        Quaternion wayFirstRotation            = wayFirst.transform.rotation;
        float      wayFirstWayWidth            = (firstIsNode1 ? 1 : -1) * wayFirstSize.y;
        float      lineFirstPositionAdjustment = percentualPositionY * wayHeight;
        Vector3    wayFirstMiddleLineTopPos    = posPosition + wayFirstRotation * new Vector3(wayFirstWayWidth / 2f, -wayHeight / 2f + lineFirstPositionAdjustment, 0f);
        Vector3    wayFirstMiddleLineBottomPos = posPosition + wayFirstRotation * new Vector3(wayFirstWayWidth / 2f, -wayHeight / 2f + GetLineHeight() + lineFirstPositionAdjustment, 0f);

        Vector3    waySecondSize                = waySecond.transform.localScale;
        Quaternion waySecondRotation            = waySecond.transform.rotation;
        float      waySecondWayWidth            = (secondIsNode1 ? 1 : -1) * waySecondSize.y;
        float      lineSecondPositionAdjustment = percentualPositionY * wayHeight;
        Vector3    waySecondMiddleLineTopPos    = posPosition + waySecondRotation * new Vector3(waySecondWayWidth / 2f, -wayHeight / 2f + lineSecondPositionAdjustment, 0f);
        Vector3    waySecondMiddleLineBottomPos = posPosition + waySecondRotation * new Vector3(waySecondWayWidth / 2f, -wayHeight / 2f + GetLineHeight() + lineSecondPositionAdjustment, 0f);

        if (IsTopAndBottomCrossing(wayFirstMiddleLineTopPos, waySecondMiddleLineTopPos, wayFirstMiddleLineBottomPos, waySecondMiddleLineBottomPos))
        {
            Vector3 tmp = wayFirstMiddleLineBottomPos;
            wayFirstMiddleLineBottomPos = wayFirstMiddleLineTopPos;
            wayFirstMiddleLineTopPos    = tmp;
        }

//		DebugFn.square (wayFirstMiddleLineTopPos);
//		DebugFn.square (wayFirstMiddleLineBottomPos);
//		DebugFn.square (waySecondMiddleLineTopPos);
//		DebugFn.square (waySecondMiddleLineBottomPos);

        List <Vector3> linePoints = new List <Vector3> ();

        linePoints.Add(wayFirstMiddleLineTopPos);
        AddBezierPoints(linePoints, wayFirstMiddleLineTopPos, wayFirstRotation, waySecondMiddleLineTopPos, waySecondRotation);
        linePoints.Add(waySecondMiddleLineBottomPos);
        AddBezierPoints(linePoints, waySecondMiddleLineBottomPos, waySecondRotation, wayFirstMiddleLineBottomPos, wayFirstRotation);

        GameObject lineMiddle = MapSurface.createPlaneMeshForPoints(linePoints);

        lineMiddle.name = "Curved middle line";
        WayLine.SetWhiteMaterial(lineMiddle);
        lineMiddle.transform.SetParent(parent.transform);
        lineMiddle.transform.localPosition = new Vector3(0, 0, -0.01f);
    }
Example #20
0
    private static void CreateCurvedDashedLines(GameObject parent, long key, List <WayReference> wayReferences)
    {
        GameObject curveDashedLines = new GameObject();

        curveDashedLines.name = "Curved dashed lines";
        curveDashedLines.transform.SetParent(parent.transform);
        curveDashedLines.transform.localPosition = Vector3.zero;

        Pos     centerPos   = NodeIndex.getPosById(key);
        Vector3 posPosition = Game.getCameraPosition(centerPos);

        WayReference firstReference  = wayReferences [0];
        WayReference secondReference = wayReferences [1];

        bool firstIsNode1  = firstReference.isNode1(centerPos);
        bool secondIsNode1 = secondReference.isNode1(centerPos);

        GameObject wayFirst  = firstReference.gameObject;
        GameObject waySecond = secondReference.gameObject;

        // Number of fields in opposite direction
        float fieldsFromPos2 = firstReference.getNumberOfFieldsInDirection(false);

        // Number of fields in own direction
        float fieldsFromPos1 = firstReference.getNumberOfFieldsInDirection(true);

        // Number of fields in total
        float numberOfFields = firstReference.getNumberOfFields();

        List <float> dashedLineFields = new List <float> ();

        for (float field = 1; field < fieldsFromPos2; field++)
        {
            dashedLineFields.Add(field);
        }

        for (float field = 1; field < fieldsFromPos1; field++)
        {
            dashedLineFields.Add(fieldsFromPos2 + field);
        }

        // If ways have same "isNode1", invert y-axis for second way
        bool isSameNode1 = firstIsNode1 == secondIsNode1;

        // Way width
        float wayHeight            = wayFirst.transform.localScale.y;
        float wayHeightCompensated = wayHeight - GetLineHeight() * 2f;

        foreach (float field in dashedLineFields)
        {
            GameObject curveDashes = new GameObject();
            curveDashes.name = "Curved dashes";
            curveDashes.transform.SetParent(curveDashedLines.transform);
            curveDashes.transform.localPosition = Vector3.zero;

            // Percentual position of way width, where to put middle line
            float percentualPositionY = field / numberOfFields;

            // Get our points (center in y-axis)
            float yPositionInWay       = percentualPositionY * wayHeightCompensated;
            float dashYMiddle          = -wayHeightCompensated / 2f + GetLineHeight() + yPositionInWay - GetDashedLineHeight() / 2f;
            float dashYMiddleSameNode1 = wayHeightCompensated / 2f - yPositionInWay + GetDashedLineHeight() / 2f;

            Vector3 firstPosMiddle  = posPosition + wayFirst.transform.rotation * new Vector3((firstIsNode1 ? 1 : -1) * wayHeight / 2f, dashYMiddle, 0);
            Vector3 secondPosMiddle = posPosition + waySecond.transform.rotation * new Vector3((secondIsNode1 ? 1 : -1) * wayHeight / 2f, isSameNode1 ? dashYMiddleSameNode1 : dashYMiddle, 0);

            Vector3 halfDashedLineHeight = new Vector3(0, GetDashedLineHeight() / 2f, 0);

            // Get our points (top in y-axis)
            Vector3 wayFirstHalfDashedHeight  = wayFirst.transform.rotation * halfDashedLineHeight;
            Vector3 waySecondHalfDashedHeight = waySecond.transform.rotation * halfDashedLineHeight;
            Vector3 firstPosTop  = firstPosMiddle - wayFirstHalfDashedHeight;
            Vector3 secondPosTop = secondPosMiddle + (isSameNode1 ? 1 : -1) * waySecondHalfDashedHeight;

            // Get our points (bottom in y-axis)
            Vector3 firstPosBottom  = firstPosMiddle + wayFirstHalfDashedHeight;
            Vector3 secondPosBottom = secondPosMiddle + (isSameNode1 ? -1 : 1) * waySecondHalfDashedHeight;

            Quaternion firstRotation   = firstIsNode1 ? WayHelper.ONEEIGHTY_DEGREES * wayFirst.transform.rotation : wayFirst.transform.rotation;
            Quaternion secondRotation  = secondIsNode1 ? WayHelper.ONEEIGHTY_DEGREES * waySecond.transform.rotation : waySecond.transform.rotation;
            Vector3    firstDirection  = firstRotation * Vector3.right;
            Vector3    secondDirection = secondRotation * Vector3.right;

            Vector3 intersectionPoint;
            Vector3 intersectionPointTop;
            Vector3 intersectionPointBottom;
            bool    intersectionFound = Math3d.LineLineIntersection(out intersectionPoint, firstPosMiddle, firstDirection, secondPosMiddle, secondDirection);
            if (!intersectionFound && firstRotation.eulerAngles.z == secondRotation.eulerAngles.z)
            {
                intersectionFound       = true;
                intersectionPoint       = firstPosMiddle + ((secondPosMiddle - firstPosMiddle) / 2);
                intersectionPointTop    = firstPosTop + ((secondPosTop - firstPosTop) / 2);
                intersectionPointBottom = firstPosBottom + ((secondPosBottom - firstPosBottom) / 2);
            }
            else
            {
                Math3d.LineLineIntersection(out intersectionPointTop, firstPosTop, firstDirection, secondPosTop, secondDirection);
                Math3d.LineLineIntersection(out intersectionPointBottom, firstPosBottom, firstDirection, secondPosBottom, secondDirection);
            }

            // TODO - Shouldn't be needed - debug only
            if (!intersectionFound)
            {
                Debug.Log("ERR: " + key);
                return;
            }

            // 1. Get bezier length for curve
            float bezierLength = Math3d.GetBezierLength(firstPosMiddle, intersectionPoint, secondPosMiddle);

            // 2. Decide how many dashes to fit, with gaps (also calculate each dash and gap length)
            // If only one line
            float numberOfLines   = 1f;
            float dashedLineWidth = bezierLength;
            float dashedLineGap   = 0f;
            // If more lines
            if (bezierLength > DASHED_LINE_WIDTH + CITY_DASHED_LINE_GAP)
            {
                float totalWidth = 0f;
                for (numberOfLines = 2f;  ; numberOfLines++)
                {
                    totalWidth = DASHED_LINE_WIDTH + (DASHED_LINE_WIDTH + CITY_DASHED_LINE_GAP) * (numberOfLines - 1);
                    if (totalWidth >= bezierLength)
                    {
                        break;
                    }
                }
                dashedLineWidth = DASHED_LINE_WIDTH * bezierLength / totalWidth;
                dashedLineGap   = CITY_DASHED_LINE_GAP * bezierLength / totalWidth;
            }


            // 3. Calculate each dash along the line t (time) on bezier curve
            List <KeyValuePair <float, float> > dashTimes = new List <KeyValuePair <float, float> > ();
            if (numberOfLines == 1f)
            {
                dashTimes.Add(new KeyValuePair <float, float>(0f, 1f));
            }
            else
            {
                dashTimes.Add(new KeyValuePair <float, float>(0f, dashedLineWidth / bezierLength));
                for (float lineStart = dashedLineWidth + dashedLineGap; lineStart < bezierLength; lineStart += dashedLineWidth + dashedLineGap)
                {
                    float lineStartTime = lineStart / bezierLength;
                    dashTimes.Add(new KeyValuePair <float, float>(lineStartTime, lineStartTime + dashedLineWidth / bezierLength));
                }
            }

            foreach (KeyValuePair <float, float> dashTime in dashTimes)
            {
                float startTime         = dashTime.Key;
                float endTime           = dashTime.Value;
                float dashLengthPercent = endTime - startTime;
                float numberOfPoints    = Mathf.Max(bezierLength / dashLengthPercent * WayHelper.BEZIER_RESOLUTION, 4f);
                float eachPointTime     = dashLengthPercent / numberOfPoints;

                List <Vector3> dashPoints = new List <Vector3>();

                // Top line
                for (float t = startTime; t <= endTime; t += eachPointTime)
                {
                    dashPoints.Add(Math3d.GetVectorInBezierAtTime(t, firstPosTop, intersectionPointTop, secondPosTop));
                }

                // Bottom line
                for (float t = endTime; t >= startTime; t -= eachPointTime)
                {
                    dashPoints.Add(Math3d.GetVectorInBezierAtTime(t, firstPosBottom, intersectionPointBottom, secondPosBottom));
                }

                GameObject lineMiddle = MapSurface.createPlaneMeshForPoints(dashPoints);
                lineMiddle.name = "Curved dash";
                WayLine.SetWhiteMaterial(lineMiddle);
                lineMiddle.transform.SetParent(curveDashes.transform);
                lineMiddle.transform.localPosition = new Vector3(0, 0, -0.01f);
            }
        }
    }
Example #21
0
    public static void create(long key, List <WayReference> wayReferences, string materialId)
    {
        if (off)
        {
            return;
        }
        Pos pos = NodeIndex.getPosById(key);

        // Sort based on rotation in the point closest to the intersection
        wayReferences.Sort(delegate(WayReference x, WayReference y) {
            float angleDiff = AngleAroundNode(pos, x) - AngleAroundNode(pos, y);
            // Ignoring float to int issues (if there are any)
            int angleDiffInt = (int)angleDiff;
            return(angleDiffInt);
        });

        // Gather our way bounds (used for checking if ways intersects each other)
        List <Bounds> wayBounds = getWayBounds(wayReferences);

        // List of positions where to draw the mesh
        List <Vector3> meshPoints;

        List <WayReference> intersectionList = Misc.CloneBaseNodeList(wayReferences);
        bool isComplex = false;

        if (intersectionList.Count == 2 && wayBounds [0].Intersects(wayBounds [1]))
        {
            // Only two ways and they intersect, special logic
            meshPoints = getMeshPointsForComplexTwoWay(intersectionList, wayBounds, pos);
            isComplex  = true;
        }
        else
        {
            meshPoints = getMeshPointsForNonComplex(intersectionList, wayBounds, pos);
        }

//		Debug.Log ("Intersection");
        GameObject intersectionObj = MapSurface.createPlaneMeshForPoints(meshPoints);

        intersectionObj.name = "Intersection " + (isComplex ? "complex " : "") + (intersectionList.Count - 1) + "-way (" + key + ")";
        Vector3 zOffset = new Vector3(0, 0, Game.WAYS_Z_POSITION);

        intersectionObj.transform.position = intersectionObj.transform.position + zOffset;
        intersectionObj.transform.parent   = Game.instance.waysParent;
        AutomaticMaterialObject intersectionMaterialObject = intersectionObj.AddComponent <AutomaticMaterialObject> () as AutomaticMaterialObject;

        intersectionMaterialObject.requestMaterial(materialId, null);          // TODO - Should have same material as connecting way(s)
        Misc.AddGravityToWay(intersectionObj);
        Misc.AddWayObjectComponent(intersectionObj);

        // Need waylines for all straight ways
        if (wayReferences.Count == 2)
        {
            bool wayQualifiedForCrossing = wayReferences[0].way.WayWidthFactor >= WayHelper.LIMIT_WAYWIDTH && wayReferences[0].way.CarWay;
            if (pos.getTagValue("highway") == "crossing" && wayQualifiedForCrossing)
            {
                WayCrossing.Create(intersectionObj, key, wayReferences);
            }
            else
            {
                WayLine.CreateCurved(intersectionObj, key, wayReferences);
            }
        }
    }