美文网首页libGdx专题
解析spine文件,只获取动画属性,使用Action展示动画

解析spine文件,只获取动画属性,使用Action展示动画

作者: 大旺旺的弟弟小旺旺 | 来源:发表于2022-01-30 09:56 被阅读0次
package com.tony.rider.animation02;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.JsonReader;
import com.badlogic.gdx.utils.JsonValue;
import com.esotericsoftware.spine.SkeletonData;
import com.tony.rider.spine.BseInterpolation;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 解析文件,
 */
public class ReadJson {
    public SkeletonData readSkeletonData(String file) throws IOException {
        file = " {\n" +
                "    \"skeleton\":{\n" +
                "        \"hash\":\"UzQWI5f97md5hXviczZjvytbYdc\",\n" +
                "        \"spine\":\"3.6.52\",\n" +
                "        \"width\":1231,\n" +
                "        \"height\":540,\n" +
                "        \"images\":\"./images/\"\n" +
                "    },\n" +
                "    \"bones\":[\n" +
                "        {\n" +
                "            \"name\":\"root\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"name\":\"text\",\n" +
                "            \"parent\":\"root\"\n" +
                "        }\n" +
                "    ],\n" +
                "    \"slots\":[\n" +
                "        {\n" +
                "            \"name\":\"ssss\",\n" +
                "            \"bone\":\"text\",\n" +
                "            \"attachment\":\"ssss\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"name\":\"ssss2\",\n" +
                "            \"bone\":\"text\",\n" +
                "            \"attachment\":\"ssss\"\n" +
                "        }\n" +
                "    ],\n" +
                "    \"skins\":{\n" +
                "        \"default\":{\n" +
                "            \"ssss\":{\n" +
                "                \"ssss\":{\n" +
                "                    \"width\":1231,\n" +
                "                    \"height\":540\n" +
                "                }\n" +
                "            },\n" +
                "            \"ssss2\":{\n" +
                "                \"ssss\":{\n" +
                "                    \"width\":1231,\n" +
                "                    \"height\":540\n" +
                "                }\n" +
                "            }\n" +
                "        }\n" +
                "    },\n" +
                "    \"animations\":{\n" +
                "\n" +
                "        \"2\":{\n" +
                "            \"slots\":{\n" +
                "                \"ssss\":{\n" +
                "                    \"color\":[\n" +
                "                        {\n" +
                "                            \"time\":0,\n" +
                "                            \"color\":\"ffffff00\",\n" +
                "                            \"curve\":[\n" +
                "                                0.25,\n" +
                "                                0,\n" +
                "                                0.75,\n" +
                "                                1\n" +
                "                            ]\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"time\":0.1667,\n" +
                "                            \"color\":\"ffffffff\",\n" +
                "                            \"curve\":\"stepped\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"time\":0.8333,\n" +
                "                            \"color\":\"ffffffff\",\n" +
                "                            \"curve\":[\n" +
                "                                0.25,\n" +
                "                                0,\n" +
                "                                0.75,\n" +
                "                                1\n" +
                "                            ]\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"time\":1.0667,\n" +
                "                            \"color\":\"ffffff00\"\n" +
                "                        }\n" +
                "                    ]\n" +
                "                },\n" +
                "                \"ssss2\":{\n" +
                "                    \"color\":[\n" +
                "                        {\n" +
                "                            \"time\":0,\n" +
                "                            \"color\":\"ffffff00\",\n" +
                "                            \"curve\":[\n" +
                "                                0.25,\n" +
                "                                0,\n" +
                "                                0.75,\n" +
                "                                1\n" +
                "                            ]\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"time\":0.1667,\n" +
                "                            \"color\":\"ffffffff\",\n" +
                "                            \"curve\":\"stepped\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"time\":0.8333,\n" +
                "                            \"color\":\"ffffffff\",\n" +
                "                            \"curve\":[\n" +
                "                                0.25,\n" +
                "                                0,\n" +
                "                                0.75,\n" +
                "                                1\n" +
                "                            ]\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"time\":1.0667,\n" +
                "                            \"color\":\"ffffff00\"\n" +
                "                        }\n" +
                "                    ]\n" +
                "                }\n" +
                "            },\n" +
                "            \"bones\":{\n" +
                "                \"text\":{\n" +
                "                    \"rotate\":[\n" +
                "                        {\n" +
                "                            \"time\":0,\n" +
                "                            \"angle\":25.98,\n" +
                "                            \"curve\":\"stepped\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"time\":0.1667,\n" +
                "                            \"angle\":25.98,\n" +
                "                            \"curve\":\"stepped\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"time\":0.3333,\n" +
                "                            \"angle\":25.98,\n" +
                "                            \"curve\":\"stepped\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"time\":0.9333,\n" +
                "                            \"angle\":25.98,\n" +
                "                            \"curve\":[\n" +
                "                                0.25,\n" +
                "                                0,\n" +
                "                                0.75,\n" +
                "                                1\n" +
                "                            ]\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"time\":1.0667,\n" +
                "                            \"angle\":-39.88\n" +
                "                        }\n" +
                "                    ],\n" +
                "                    \"translate\":[\n" +
                "                        {\n" +
                "                            \"time\":0,\n" +
                "                            \"x\":-444.01,\n" +
                "                            \"y\":587.66,\n" +
                "                            \"curve\":[\n" +
                "                                0.25,\n" +
                "                                0,\n" +
                "                                0.75,\n" +
                "                                1\n" +
                "                            ]\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"time\":0.1667,\n" +
                "                            \"x\":34.82,\n" +
                "                            \"y\":-69.65,\n" +
                "                            \"curve\":[\n" +
                "                                0.25,\n" +
                "                                0,\n" +
                "                                0.75,\n" +
                "                                1\n" +
                "                            ]\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"time\":0.3333,\n" +
                "                            \"x\":0,\n" +
                "                            \"y\":0,\n" +
                "                            \"curve\":\"stepped\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"time\":0.9333,\n" +
                "                            \"x\":0,\n" +
                "                            \"y\":0,\n" +
                "                            \"curve\":[\n" +
                "                                0.25,\n" +
                "                                0,\n" +
                "                                0.75,\n" +
                "                                1\n" +
                "                            ]\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"time\":1.0667,\n" +
                "                            \"x\":931.54,\n" +
                "                            \"y\":966.37\n" +
                "                        }\n" +
                "                    ],\n" +
                "                    \"scale\":[\n" +
                "                        {\n" +
                "                            \"time\":0,\n" +
                "                            \"x\":2.073,\n" +
                "                            \"y\":2.073,\n" +
                "                            \"curve\":[\n" +
                "                                0.25,\n" +
                "                                0,\n" +
                "                                0.75,\n" +
                "                                1\n" +
                "                            ]\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"time\":0.1667,\n" +
                "                            \"x\":0.8,\n" +
                "                            \"y\":0.8,\n" +
                "                            \"curve\":[\n" +
                "                                0.25,\n" +
                "                                0,\n" +
                "                                0.75,\n" +
                "                                1\n" +
                "                            ]\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"time\":0.3333,\n" +
                "                            \"x\":1,\n" +
                "                            \"y\":1,\n" +
                "                            \"curve\":\"stepped\"\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"time\":0.8333,\n" +
                "                            \"x\":1,\n" +
                "                            \"y\":1,\n" +
                "                            \"curve\":[\n" +
                "                                0.25,\n" +
                "                                0,\n" +
                "                                0.75,\n" +
                "                                1\n" +
                "                            ]\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"time\":0.9333,\n" +
                "                            \"x\":0.7,\n" +
                "                            \"y\":0.7,\n" +
                "                            \"curve\":[\n" +
                "                                0.25,\n" +
                "                                0,\n" +
                "                                0.75,\n" +
                "                                1\n" +
                "                            ]\n" +
                "                        },\n" +
                "                        {\n" +
                "                            \"time\":1.0667,\n" +
                "                            \"x\":1.5,\n" +
                "                            \"y\":1.5\n" +
                "                        }\n" +
                "                    ]\n" +
                "                }\n" +
                "            }\n" +
                "        }\n" +
                "    }\n" +
                "}";


        JsonValue root = new JsonReader().parse(file);
        //获取动画
        JsonValue animations = root.get("animations");
        int animationNums = animations.size;


        Array<MyScaleAction> scales = new Array<>();
        Array<MyRotateAction> rotates = new Array<>();
        Array<MyColorAction> colors = new Array<>();
        Array<Translate> translates = new Array<>();

        ArrayList arrays = new ArrayList();

        /**
         * 那个动画  动画名字
         */
        HashMap<String, Action> hashMap = new HashMap<>();

        for (int animationnameIndex = 0; animationnameIndex < animationNums; animationnameIndex++) {
            //获取动画animationnameIndex
            JsonValue animationRes = animations.get(animationnameIndex);
            //slots   bonues
            System.out.println(animationRes.name+"  animation Name  :   ");
//            下面的并行
            for (int animationResIndex = 0; animationResIndex < animationRes.size; animationResIndex++) {
                JsonValue slotOrBonus = animationRes.get(animationResIndex);
                HashMap<String,Action> actionHashMap = new HashMap<>();
                //下面有多个
                System.out.println(slotOrBonus.name+"   slot or bonus  : ");
                for (int xz = 0; xz < slotOrBonus.size; xz++) {
                    JsonValue childzz = slotOrBonus.get(xz);
                    //slot 或者  bonus下的动画文件
                    //线性加入     一个完整的   它属于那个资源的
                    System.out.println(childzz.name+"   texture name : ");
                    SequenceAction seqTemp = Actions.sequence();
                    for (int zz = 0; zz < childzz.size; zz++) {
                        //一系列动画
                        JsonValue child = childzz.get(zz);
                        if (child.name.equals("translate")) {
                            System.out.println("translate ------------------------------------------------");
                            int size = child.size;
                            for (int i = 0; i < size; i++) {
                                JsonValue jsonValue1 = child.get(i);
                                JsonValue time = jsonValue1.get("time");
                                JsonValue x = jsonValue1.get("x");
                                JsonValue y = jsonValue1.get("y");
                                JsonValue curve = jsonValue1.get("curve");
                                Translate translate = new Translate();
                                translates.add(translate);
                                translate.time = time.asFloat();
                                translate.x = x.asFloat();
                                translate.y = y.asFloat();


                                /**
                                 * 加入
                                 */


                                if (curve != null && curve.type() == JsonValue.ValueType.array) {
                                    float[] floats = curve.asFloatArray();
                                    System.out.println(curve);
                                    BseInterpolation interpolation = new BseInterpolation(floats);
                                    translate.bseInterpolation = interpolation;
                                }
                            }
                        } else if (child.name.equals("color")) {
                            System.out.println("color ------------------------------------------------");
                            int size = child.size;
                            for (int i = 0; i < size; i++) {
                                JsonValue jsonValue1 = child.get(i);
                                JsonValue time = jsonValue1.get("time");
                                JsonValue colorV = jsonValue1.get("color");
                                JsonValue curve = jsonValue1.get("curve");
                                MyColorAction myColorAction = new MyColorAction();
                                colors.add(myColorAction);
                                myColorAction.time = time.asFloat();
                                myColorAction.color = Color.valueOf(colorV.asString());
                                if (curve != null && curve.type() == JsonValue.ValueType.array) {
                                    float[] floats = curve.asFloatArray();

                                    BseInterpolation interpolation = new BseInterpolation(floats);
                                    myColorAction.interpolation = interpolation;
                                }
                            }
                        } else if (child.name.equals("rotate")) {
                            System.out.println("rotate ------------------------------------------------");
                            int size = child.size;
                            for (int i = 0; i < size; i++) {
                                JsonValue jsonValue1 = child.get(i);
                                JsonValue time = jsonValue1.get("time");
                                JsonValue colorV = jsonValue1.get("angle");
                                JsonValue curve = jsonValue1.get("curve");
                                MyRotateAction myColorAction = new MyRotateAction();
                                rotates.add(myColorAction);
                                myColorAction.time = time.asFloat();
                                myColorAction.angle = colorV.asFloat();
                                if (curve != null && curve.type() == JsonValue.ValueType.array) {
                                    float[] floats = curve.asFloatArray();

                                    BseInterpolation interpolation = new BseInterpolation(floats);
                                    myColorAction.interpolation = interpolation;
                                }
                            }
                        }else if (child.name.equals("scale")){
                            System.out.println("scale ------------------------------------------------");
                            int size = child.size;
                            for (int i = 0; i < size; i++) {
                                JsonValue jsonValue1 = child.get(i);
                                JsonValue time = jsonValue1.get("time");
                                JsonValue scX = jsonValue1.get("x");
                                JsonValue scY = jsonValue1.get("y");
                                JsonValue curve = jsonValue1.get("curve");
                                MyScaleAction myScaleAction = new MyScaleAction();
                                scales.add(myScaleAction);
                                myScaleAction.time = time.asFloat();
                                myScaleAction.x = scX.asFloat();
                                myScaleAction.y = scY.asFloat();
                                if (curve != null && curve.type() == JsonValue.ValueType.array) {
                                    float[] floats = curve.asFloatArray();
                                    BseInterpolation interpolation = new BseInterpolation(floats);
                                    myScaleAction.interpolation = interpolation;
                                }
                            }
                        }
                    }
                }
            }
            arrays.add(rotates);
            arrays.add(translates);
            arrays.add(colors);
        }
        return null;
    }

    public static void main(String[] args) throws IOException {
        ReadJson readJson = new ReadJson();
        readJson.readSkeletonData("E:\\rider\\rider\\Assets\\asset\\spine\\propSpeedUp.json");
        System.out.println("");
    }
}

相关文章

网友评论

    本文标题:解析spine文件,只获取动画属性,使用Action展示动画

    本文链接:https://www.haomeiwen.com/subject/bosqkrtx.html