public IsometricTrackKeyframeData(IsometricTrackKeyframeData keyframe)
 {
     Position      = keyframe.Position;
     Rotation      = keyframe.Rotation;
     RotationPower = keyframe.RotationPower;
     CarRotation   = keyframe.CarRotation;
 }
    void Update()
    {
        if (IsMoving)
        {
            TrackProgress += Time.deltaTime * MovementSpeed;

            if (TrackProgress > 1)
            {
                TrackProgress -= 1;
                KeyframeIndex++;
                if (KeyframeIndex >= TrackData.Keyframes.Count - 1)
                {
                    IsMoving = false;
                    return;
                }
            }

            IsometricTrackKeyframeData keyframe0 = TrackData.Keyframes[KeyframeIndex];
            IsometricTrackKeyframeData keyframe1 = TrackData.Keyframes[KeyframeIndex + 1];
            Vector3 p1 = keyframe0.Position;
            Vector3 p2 = keyframe1.Position;
            Vector3 t1 =
                p1 - (new Vector3(Mathf.Sin(keyframe0.Rotation * Mathf.Deg2Rad),
                                  Mathf.Cos(keyframe0.Rotation * Mathf.Deg2Rad), 0)).normalized * keyframe0.RotationPower;
            Vector3 t2 =
                p2 + (new Vector3(Mathf.Sin(keyframe1.Rotation * Mathf.Deg2Rad),
                                  Mathf.Cos(keyframe1.Rotation * Mathf.Deg2Rad), 0)).normalized * keyframe1.RotationPower;
            Vector3 pointOnCurve = GetPointOnBezierCurve(p1, t1, t2, p2, TrackProgress);

            Vector3 position = GetPointWorldPosition(pointOnCurve);
            ObjectToMove.position = position;

            Vector3 delta    = position - PrevPosition;
            float   rotation = Mathf.Atan2(delta.y, delta.x) * Mathf.Rad2Deg;
            PrevPosition = position;

            ObjectToRotate.localEulerAngles = new Vector3(0, GetPointWorldRotation(rotation) + 90, 0);
        }
    }
예제 #3
0
    private void DrawTrackEdit(float posX, float posY)
    {
        GUI.Box(new Rect(posX, posY + 5, RightWidth - 2 * Offset, 1), "");

        posY += LineHeight;
        if (WorldData == null)
        {
            EditorGUI.LabelField(new Rect(posX + 20, posY, RightWidth, LineHeight), "Select or create world first!");
            return;
        }

        if (GUI.Button(new Rect(posX, posY, RightWidth - 2 * Offset, LineHeight), "SYNC rotation to carRotation"))
        {
            for (int i = 0; i < WorldData.TrackData.Keyframes.Count; i++)
            {
                WorldData.TrackData.Keyframes[i].CarRotation = WorldData.TrackData.Keyframes[i].Rotation;
            }
        }
        posY += LineHeight;

        if (SelectedKeyframeIndex >= WorldData.TrackData.Keyframes.Count)
        {
            SelectedKeyframeIndex = -1;
        }

        if (SelectedKeyframeIndex < 0)
        {
            posY += LineHeight;
            EditorGUI.LabelField(new Rect(posX + 20, posY, RightWidth, LineHeight), "Select or add a keyframe!");
            return;
        }

        float tagWidth      = 20;
        float intFieldWidth = (((float)RightWidth - Offset) / 2) - tagWidth - Offset;
        float buttonWidth   = (float)(RightWidth - Offset) / 2 - Offset;
        IsometricTrackKeyframeData keyframe = WorldData.TrackData.Keyframes[SelectedKeyframeIndex];

        posY += Offset;
        if (GUI.Button(new Rect(posX, posY, buttonWidth, LineHeight), "DELETE"))
        {
            WorldData.TrackData.Keyframes.Remove(keyframe);
            SelectedKeyframeIndex = -1;
            return;
        }

        EditorGUI.BeginDisabledGroup(SelectedKeyframeIndex == 0);
        if (GUI.Button(new Rect(posX + buttonWidth + Offset, posY, buttonWidth, LineHeight), "INSERT NEW KEY"))
        {
            IsometricTrackKeyframeData newKey;
            IsometricTrackKeyframeData prevKey = WorldData.TrackData.Keyframes[SelectedKeyframeIndex - 1];
            newKey = new IsometricTrackKeyframeData((prevKey.Position + keyframe.Position) * 0.5f, (prevKey.Rotation + keyframe.Rotation) * 0.5f, (prevKey.RotationPower + keyframe.RotationPower) * 0.5f, (prevKey.Rotation + keyframe.Rotation) * 0.5f);
            WorldData.TrackData.Keyframes.Insert(SelectedKeyframeIndex, newKey);
        }
        EditorGUI.EndDisabledGroup();

        posY += 2 * LineHeight;

        EditorGUI.LabelField(new Rect(posX, posY, RightWidth, LineHeight), "Position");
        posY += LineHeight;
        EditorGUI.LabelField(new Rect(posX, posY, tagWidth, LineHeight), "X:");
        keyframe.Position.x = EditorGUI.FloatField(new Rect(posX + tagWidth, posY, intFieldWidth, LineHeight), keyframe.Position.x);
        EditorGUI.LabelField(new Rect(posX + tagWidth + intFieldWidth + Offset, posY, tagWidth, LineHeight), "Y:");
        keyframe.Position.y = EditorGUI.FloatField(new Rect(posX + 2 * tagWidth + intFieldWidth + Offset, posY, intFieldWidth, LineHeight), keyframe.Position.y);
        posY += 2 * LineHeight + Offset;

        EditorGUI.LabelField(new Rect(posX, posY, RightWidth, LineHeight), "Rotation");
        posY += LineHeight;
        keyframe.Rotation = EditorGUI.Slider(new Rect(posX, posY, RightWidth - 2 * Offset, LineHeight), keyframe.Rotation, 0, 360);
        posY += 2 * LineHeight;
        EditorGUI.LabelField(new Rect(posX, posY, RightWidth, LineHeight), "Power");
        posY += LineHeight;
        keyframe.RotationPower = EditorGUI.Slider(new Rect(posX, posY, RightWidth - 2 * Offset, LineHeight), keyframe.RotationPower, 1, 200);
        posY += 2 * LineHeight;
        EditorGUI.LabelField(new Rect(posX, posY, RightWidth, LineHeight), "CarRotation");
        posY += LineHeight;
        keyframe.CarRotation = EditorGUI.Slider(new Rect(posX, posY, RightWidth - 2 * Offset, LineHeight), keyframe.CarRotation, 0, 360);
    }
예제 #4
0
    private void DrawMiddle()
    {
        if (WorldData == null)
        {
            return;
        }
        HandleTouch();
        GUI.BeginGroup(new Rect(LeftWidth, 0, position.width - LeftWidth - RightWidth, position.height));

        // draw tiles
        TileCenterPositions.Clear();
        float   minBorder = 200;
        Vector2 worldSize = new Vector2(WorldData.Columns * 0.5f * DrawTileOffset.x + WorldData.Rows * 0.5f * DrawTileOffset.x, WorldData.Columns * 0.5f * DrawTileOffset.y + WorldData.Rows * 0.5f * DrawTileOffset.y);
        float   posx      = Mathf.Clamp(StartDrawWorldPosition.x, -worldSize.x, position.width - RightWidth - LeftWidth - minBorder + worldSize.x);
        float   posy      = Mathf.Clamp(StartDrawWorldPosition.y, -worldSize.y, position.height - DrawTileOffset.y * 4 + worldSize.y);

        StartDrawWorldPosition = new Vector2(posx, posy);
        List <List <IsometricTileData> > tileData = WorldData.GetTileData();

        for (int i = 0; i < tileData.Count; i++)
        {
            List <Vector2> tileCenterRow = new List <Vector2>();
            Vector2        drawPos       = StartDrawWorldPosition + new Vector2(-DrawTileOffset.x, DrawTileOffset.y) * i;
            for (int j = 0; j < tileData[i].Count; j++)
            {
                drawPos += DrawTileOffset;

                Sprite  sprite      = tileData[i][j].TilePrefab.GetSpriteForRotation(tileData[i][j].Rotation);
                Texture tex         = sprite.texture;
                Vector2 tileDrawPos = drawPos - new Vector2(0, tex.height);
                GUI.DrawTexture(new Rect(tileDrawPos.x, tileDrawPos.y, tex.width, tex.height), tex);

                Vector2 centerPos = tileDrawPos + DrawTileOffset + Vector2.right * LeftWidth;
                tileCenterRow.Add(centerPos);
            }
            TileCenterPositions.Add(tileCenterRow);
        }

        //draw track edit if selected
        if (SelectedEdit == EditType.Track)
        {
            float keySize = 20;
            for (int i = 0; i < WorldData.TrackData.Keyframes.Count; i++)
            {
                IsometricTrackKeyframeData keyframe1 = WorldData.TrackData.Keyframes[i];
                Vector2 pos        = keyframe1.Position + StartDrawWorldPosition - new Vector2(LeftWidth, 0);
                Color   startColor = GUI.color;

                if (i > 0)
                {
                    IsometricTrackKeyframeData keyframe0 = WorldData.TrackData.Keyframes[i - 1];
                    Vector2 prevPos = keyframe0.Position + StartDrawWorldPosition - new Vector2(LeftWidth, 0);
                    Vector3 p1      = new Vector3(prevPos.x, prevPos.y, 0);
                    Vector3 p2      = new Vector3(pos.x, pos.y, 0);
                    Vector3 t1      = p1 - (new Vector3(Mathf.Sin(keyframe0.Rotation * Mathf.Deg2Rad), Mathf.Cos(keyframe0.Rotation * Mathf.Deg2Rad), 0)).normalized * keyframe0.RotationPower;
                    Vector3 t2      = p2 + (new Vector3(Mathf.Sin(keyframe1.Rotation * Mathf.Deg2Rad), Mathf.Cos(keyframe1.Rotation * Mathf.Deg2Rad), 0)).normalized * keyframe1.RotationPower;
                    Handles.DrawBezier(p1, p2, t1, t2, Color.blue, null, 3);
                }

                GUI.color = i == SelectedKeyframeIndex ? Color.green : Color.red;
                GUI.Box(new Rect(pos.x - keySize * 0.5f, pos.y - keySize * 0.5f, keySize, keySize), "");
                GUI.color = startColor;
            }
        }

        GUI.EndGroup();
    }