Example usage for com.badlogic.gdx.utils Array shrink

List of usage examples for com.badlogic.gdx.utils Array shrink

Introduction

In this page you can find the example usage for com.badlogic.gdx.utils Array shrink.

Prototype

public T[] shrink() 

Source Link

Document

Reduces the size of the backing array to the size of the actual items.

Usage

From source file:com.andgate.ikou.model.MasterSector.java

License:Open Source License

public void shrink() {
    //sectors.shrink();

    for (Array<TileSector> sectorsRow : sectors) {
        sectorsRow.shrink();
    }/*from ww w.java  2 s  . co m*/
}

From source file:com.company.minery.utils.spine.SkeletonBinary.java

License:Open Source License

private void readAnimation(String name, DataInput input, SkeletonData skeletonData) {
    Array<Timeline> timelines = new Array();
    float scale = this.scale;
    float duration = 0;

    try {/*from   www .j  a  va  2s .c o  m*/
        // Slot timelines.
        for (int i = 0, n = input.readInt(true); i < n; i++) {
            int slotIndex = input.readInt(true);
            for (int ii = 0, nn = input.readInt(true); ii < nn; ii++) {
                int timelineType = input.readByte();
                int frameCount = input.readInt(true);
                switch (timelineType) {
                case TIMELINE_COLOR: {
                    ColorTimeline timeline = new ColorTimeline(frameCount);
                    timeline.slotIndex = slotIndex;
                    for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
                        float time = input.readFloat();
                        Color.rgba8888ToColor(tempColor, input.readInt());
                        timeline.setFrame(frameIndex, time, tempColor.r, tempColor.g, tempColor.b, tempColor.a);
                        if (frameIndex < frameCount - 1)
                            readCurve(input, frameIndex, timeline);
                    }
                    timelines.add(timeline);
                    duration = Math.max(duration, timeline.getFrames()[frameCount * 5 - 5]);
                    break;
                }
                case TIMELINE_ATTACHMENT:
                    AttachmentTimeline timeline = new AttachmentTimeline(frameCount);
                    timeline.slotIndex = slotIndex;
                    for (int frameIndex = 0; frameIndex < frameCount; frameIndex++)
                        timeline.setFrame(frameIndex, input.readFloat(), input.readString());
                    timelines.add(timeline);
                    duration = Math.max(duration, timeline.getFrames()[frameCount - 1]);
                    break;
                }
            }
        }

        // Bone timelines.
        for (int i = 0, n = input.readInt(true); i < n; i++) {
            int boneIndex = input.readInt(true);
            for (int ii = 0, nn = input.readInt(true); ii < nn; ii++) {
                int timelineType = input.readByte();
                int frameCount = input.readInt(true);
                switch (timelineType) {
                case TIMELINE_ROTATE: {
                    RotateTimeline timeline = new RotateTimeline(frameCount);
                    timeline.boneIndex = boneIndex;
                    for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
                        timeline.setFrame(frameIndex, input.readFloat(), input.readFloat());
                        if (frameIndex < frameCount - 1)
                            readCurve(input, frameIndex, timeline);
                    }
                    timelines.add(timeline);
                    duration = Math.max(duration, timeline.getFrames()[frameCount * 2 - 2]);
                    break;
                }
                case TIMELINE_TRANSLATE:
                case TIMELINE_SCALE: {
                    TranslateTimeline timeline;
                    float timelineScale = 1;
                    if (timelineType == TIMELINE_SCALE)
                        timeline = new ScaleTimeline(frameCount);
                    else {
                        timeline = new TranslateTimeline(frameCount);
                        timelineScale = scale;
                    }
                    timeline.boneIndex = boneIndex;
                    for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
                        timeline.setFrame(frameIndex, input.readFloat(), input.readFloat() * timelineScale,
                                input.readFloat() * timelineScale);
                        if (frameIndex < frameCount - 1)
                            readCurve(input, frameIndex, timeline);
                    }
                    timelines.add(timeline);
                    duration = Math.max(duration, timeline.getFrames()[frameCount * 3 - 3]);
                    break;
                }
                case TIMELINE_FLIPX:
                case TIMELINE_FLIPY: {
                    FlipXTimeline timeline = timelineType == TIMELINE_FLIPX ? new FlipXTimeline(frameCount)
                            : new FlipYTimeline(frameCount);
                    timeline.boneIndex = boneIndex;
                    for (int frameIndex = 0; frameIndex < frameCount; frameIndex++)
                        timeline.setFrame(frameIndex, input.readFloat(), input.readBoolean());
                    timelines.add(timeline);
                    duration = Math.max(duration, timeline.getFrames()[frameCount * 2 - 2]);
                    break;
                }
                }
            }
        }

        // IK timelines.
        for (int i = 0, n = input.readInt(true); i < n; i++) {
            IkConstraintData ikConstraint = skeletonData.ikConstraints.get(input.readInt(true));
            int frameCount = input.readInt(true);
            IkConstraintTimeline timeline = new IkConstraintTimeline(frameCount);
            timeline.ikConstraintIndex = skeletonData.getIkConstraints().indexOf(ikConstraint, true);
            for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
                timeline.setFrame(frameIndex, input.readFloat(), input.readFloat(), input.readByte());
                if (frameIndex < frameCount - 1)
                    readCurve(input, frameIndex, timeline);
            }
            timelines.add(timeline);
            duration = Math.max(duration, timeline.getFrames()[frameCount * 3 - 3]);
        }

        // FFD timelines.
        for (int i = 0, n = input.readInt(true); i < n; i++) {
            Skin skin = skeletonData.skins.get(input.readInt(true));
            for (int ii = 0, nn = input.readInt(true); ii < nn; ii++) {
                int slotIndex = input.readInt(true);
                for (int iii = 0, nnn = input.readInt(true); iii < nnn; iii++) {
                    Attachment attachment = skin.getAttachment(slotIndex, input.readString());
                    int frameCount = input.readInt(true);
                    FfdTimeline timeline = new FfdTimeline(frameCount);
                    timeline.slotIndex = slotIndex;
                    timeline.attachment = attachment;
                    for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
                        float time = input.readFloat();

                        float[] vertices;
                        int vertexCount;
                        if (attachment instanceof MeshAttachment)
                            vertexCount = ((MeshAttachment) attachment).getVertices().length;
                        else
                            vertexCount = ((SkinnedMeshAttachment) attachment).getWeights().length / 3 * 2;

                        int end = input.readInt(true);
                        if (end == 0) {
                            if (attachment instanceof MeshAttachment)
                                vertices = ((MeshAttachment) attachment).getVertices();
                            else
                                vertices = new float[vertexCount];
                        } else {
                            vertices = new float[vertexCount];
                            int start = input.readInt(true);
                            end += start;
                            if (scale == 1) {
                                for (int v = start; v < end; v++)
                                    vertices[v] = input.readFloat();
                            } else {
                                for (int v = start; v < end; v++)
                                    vertices[v] = input.readFloat() * scale;
                            }
                            if (attachment instanceof MeshAttachment) {
                                float[] meshVertices = ((MeshAttachment) attachment).getVertices();
                                for (int v = 0, vn = vertices.length; v < vn; v++)
                                    vertices[v] += meshVertices[v];
                            }
                        }

                        timeline.setFrame(frameIndex, time, vertices);
                        if (frameIndex < frameCount - 1)
                            readCurve(input, frameIndex, timeline);
                    }
                    timelines.add(timeline);
                    duration = Math.max(duration, timeline.getFrames()[frameCount - 1]);
                }
            }
        }

        // Draw order timeline.
        int drawOrderCount = input.readInt(true);
        if (drawOrderCount > 0) {
            DrawOrderTimeline timeline = new DrawOrderTimeline(drawOrderCount);
            int slotCount = skeletonData.slots.size;
            for (int i = 0; i < drawOrderCount; i++) {
                int offsetCount = input.readInt(true);
                int[] drawOrder = new int[slotCount];
                for (int ii = slotCount - 1; ii >= 0; ii--)
                    drawOrder[ii] = -1;
                int[] unchanged = new int[slotCount - offsetCount];
                int originalIndex = 0, unchangedIndex = 0;
                for (int ii = 0; ii < offsetCount; ii++) {
                    int slotIndex = input.readInt(true);
                    // Collect unchanged items.
                    while (originalIndex != slotIndex)
                        unchanged[unchangedIndex++] = originalIndex++;
                    // Set changed items.
                    drawOrder[originalIndex + input.readInt(true)] = originalIndex++;
                }
                // Collect remaining unchanged items.
                while (originalIndex < slotCount)
                    unchanged[unchangedIndex++] = originalIndex++;
                // Fill in unchanged items.
                for (int ii = slotCount - 1; ii >= 0; ii--)
                    if (drawOrder[ii] == -1)
                        drawOrder[ii] = unchanged[--unchangedIndex];
                timeline.setFrame(i, input.readFloat(), drawOrder);
            }
            timelines.add(timeline);
            duration = Math.max(duration, timeline.getFrames()[drawOrderCount - 1]);
        }

        // Event timeline.
        int eventCount = input.readInt(true);
        if (eventCount > 0) {
            EventTimeline timeline = new EventTimeline(eventCount);
            for (int i = 0; i < eventCount; i++) {
                float time = input.readFloat();
                EventData eventData = skeletonData.events.get(input.readInt(true));
                Event event = new Event(eventData);
                event.intValue = input.readInt(false);
                event.floatValue = input.readFloat();
                event.stringValue = input.readBoolean() ? input.readString() : eventData.stringValue;
                timeline.setFrame(i, time, event);
            }
            timelines.add(timeline);
            duration = Math.max(duration, timeline.getFrames()[eventCount - 1]);
        }
    } catch (IOException ex) {
        throw new SerializationException("Error reading skeleton file.", ex);
    }

    timelines.shrink();
    skeletonData.animations.add(new Animation(name, timelines, duration));
}

From source file:com.company.minery.utils.spine.SkeletonJson.java

License:Open Source License

private void readAnimation(String name, JsonValue map, SkeletonData skeletonData) {
    float scale = this.scale;
    Array<Timeline> timelines = new Array();
    float duration = 0;

    // Slot timelines.
    for (JsonValue slotMap = map.getChild("slots"); slotMap != null; slotMap = slotMap.next) {
        int slotIndex = skeletonData.findSlotIndex(slotMap.name);
        if (slotIndex == -1)
            throw new SerializationException("Slot not found: " + slotMap.name);

        for (JsonValue timelineMap = slotMap.child; timelineMap != null; timelineMap = timelineMap.next) {
            String timelineName = timelineMap.name;
            if (timelineName.equals("color")) {
                ColorTimeline timeline = new ColorTimeline(timelineMap.size);
                timeline.slotIndex = slotIndex;

                int frameIndex = 0;
                for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) {
                    Color color = Color.valueOf(valueMap.getString("color"));
                    timeline.setFrame(frameIndex, valueMap.getFloat("time"), color.r, color.g, color.b,
                            color.a);//from  w  w w  .  j ava2s . c om
                    readCurve(timeline, frameIndex, valueMap);
                    frameIndex++;
                }
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 5 - 5]);

            } else if (timelineName.equals("attachment")) {
                AttachmentTimeline timeline = new AttachmentTimeline(timelineMap.size);
                timeline.slotIndex = slotIndex;

                int frameIndex = 0;
                for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next)
                    timeline.setFrame(frameIndex++, valueMap.getFloat("time"), valueMap.getString("name"));
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]);
            } else
                throw new RuntimeException(
                        "Invalid timeline type for a slot: " + timelineName + " (" + slotMap.name + ")");
        }
    }

    // Bone timelines.
    for (JsonValue boneMap = map.getChild("bones"); boneMap != null; boneMap = boneMap.next) {
        int boneIndex = skeletonData.findBoneIndex(boneMap.name);
        if (boneIndex == -1)
            throw new SerializationException("Bone not found: " + boneMap.name);

        for (JsonValue timelineMap = boneMap.child; timelineMap != null; timelineMap = timelineMap.next) {
            String timelineName = timelineMap.name;
            if (timelineName.equals("rotate")) {
                RotateTimeline timeline = new RotateTimeline(timelineMap.size);
                timeline.boneIndex = boneIndex;

                int frameIndex = 0;
                for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) {
                    timeline.setFrame(frameIndex, valueMap.getFloat("time"), valueMap.getFloat("angle"));
                    readCurve(timeline, frameIndex, valueMap);
                    frameIndex++;
                }
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 2 - 2]);

            } else if (timelineName.equals("translate") || timelineName.equals("scale")) {
                TranslateTimeline timeline;
                float timelineScale = 1;
                if (timelineName.equals("scale"))
                    timeline = new ScaleTimeline(timelineMap.size);
                else {
                    timeline = new TranslateTimeline(timelineMap.size);
                    timelineScale = scale;
                }
                timeline.boneIndex = boneIndex;

                int frameIndex = 0;
                for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) {
                    float x = valueMap.getFloat("x", 0), y = valueMap.getFloat("y", 0);
                    timeline.setFrame(frameIndex, valueMap.getFloat("time"), x * timelineScale,
                            y * timelineScale);
                    readCurve(timeline, frameIndex, valueMap);
                    frameIndex++;
                }
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 3 - 3]);

            } else if (timelineName.equals("flipX") || timelineName.equals("flipY")) {
                boolean x = timelineName.equals("flipX");
                FlipXTimeline timeline = x ? new FlipXTimeline(timelineMap.size)
                        : new FlipYTimeline(timelineMap.size);
                timeline.boneIndex = boneIndex;

                String field = x ? "x" : "y";
                int frameIndex = 0;
                for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) {
                    timeline.setFrame(frameIndex, valueMap.getFloat("time"), valueMap.getBoolean(field, false));
                    frameIndex++;
                }
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 2 - 2]);

            } else
                throw new RuntimeException(
                        "Invalid timeline type for a bone: " + timelineName + " (" + boneMap.name + ")");
        }
    }

    // IK timelines.
    for (JsonValue ikMap = map.getChild("ik"); ikMap != null; ikMap = ikMap.next) {
        IkConstraintData ikConstraint = skeletonData.findIkConstraint(ikMap.name);
        IkConstraintTimeline timeline = new IkConstraintTimeline(ikMap.size);
        timeline.ikConstraintIndex = skeletonData.getIkConstraints().indexOf(ikConstraint, true);
        int frameIndex = 0;
        for (JsonValue valueMap = ikMap.child; valueMap != null; valueMap = valueMap.next) {
            timeline.setFrame(frameIndex, valueMap.getFloat("time"), valueMap.getFloat("mix"),
                    valueMap.getBoolean("bendPositive") ? 1 : -1);
            readCurve(timeline, frameIndex, valueMap);
            frameIndex++;
        }
        timelines.add(timeline);
        duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 3 - 3]);
    }

    // FFD timelines.
    for (JsonValue ffdMap = map.getChild("ffd"); ffdMap != null; ffdMap = ffdMap.next) {
        Skin skin = skeletonData.findSkin(ffdMap.name);
        if (skin == null)
            throw new SerializationException("Skin not found: " + ffdMap.name);
        for (JsonValue slotMap = ffdMap.child; slotMap != null; slotMap = slotMap.next) {
            int slotIndex = skeletonData.findSlotIndex(slotMap.name);
            if (slotIndex == -1)
                throw new SerializationException("Slot not found: " + slotMap.name);
            for (JsonValue meshMap = slotMap.child; meshMap != null; meshMap = meshMap.next) {
                FfdTimeline timeline = new FfdTimeline(meshMap.size);
                Attachment attachment = skin.getAttachment(slotIndex, meshMap.name);
                if (attachment == null)
                    throw new SerializationException("FFD attachment not found: " + meshMap.name);
                timeline.slotIndex = slotIndex;
                timeline.attachment = attachment;

                int vertexCount;
                if (attachment instanceof MeshAttachment)
                    vertexCount = ((MeshAttachment) attachment).getVertices().length;
                else
                    vertexCount = ((SkinnedMeshAttachment) attachment).getWeights().length / 3 * 2;

                int frameIndex = 0;
                for (JsonValue valueMap = meshMap.child; valueMap != null; valueMap = valueMap.next) {
                    float[] vertices;
                    JsonValue verticesValue = valueMap.get("vertices");
                    if (verticesValue == null) {
                        if (attachment instanceof MeshAttachment)
                            vertices = ((MeshAttachment) attachment).getVertices();
                        else
                            vertices = new float[vertexCount];
                    } else {
                        vertices = new float[vertexCount];
                        int start = valueMap.getInt("offset", 0);
                        System.arraycopy(verticesValue.asFloatArray(), 0, vertices, start, verticesValue.size);
                        if (scale != 1) {
                            for (int i = start, n = i + verticesValue.size; i < n; i++)
                                vertices[i] *= scale;
                        }
                        if (attachment instanceof MeshAttachment) {
                            float[] meshVertices = ((MeshAttachment) attachment).getVertices();
                            for (int i = 0; i < vertexCount; i++)
                                vertices[i] += meshVertices[i];
                        }
                    }

                    timeline.setFrame(frameIndex, valueMap.getFloat("time"), vertices);
                    readCurve(timeline, frameIndex, valueMap);
                    frameIndex++;
                }
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]);
            }
        }
    }

    // Draw order timeline.
    JsonValue drawOrdersMap = map.get("drawOrder");
    if (drawOrdersMap == null)
        drawOrdersMap = map.get("draworder");
    if (drawOrdersMap != null) {
        DrawOrderTimeline timeline = new DrawOrderTimeline(drawOrdersMap.size);
        int slotCount = skeletonData.slots.size;
        int frameIndex = 0;
        for (JsonValue drawOrderMap = drawOrdersMap.child; drawOrderMap != null; drawOrderMap = drawOrderMap.next) {
            int[] drawOrder = null;
            JsonValue offsets = drawOrderMap.get("offsets");
            if (offsets != null) {
                drawOrder = new int[slotCount];
                for (int i = slotCount - 1; i >= 0; i--)
                    drawOrder[i] = -1;
                int[] unchanged = new int[slotCount - offsets.size];
                int originalIndex = 0, unchangedIndex = 0;
                for (JsonValue offsetMap = offsets.child; offsetMap != null; offsetMap = offsetMap.next) {
                    int slotIndex = skeletonData.findSlotIndex(offsetMap.getString("slot"));
                    if (slotIndex == -1)
                        throw new SerializationException("Slot not found: " + offsetMap.getString("slot"));
                    // Collect unchanged items.
                    while (originalIndex != slotIndex)
                        unchanged[unchangedIndex++] = originalIndex++;
                    // Set changed items.
                    drawOrder[originalIndex + offsetMap.getInt("offset")] = originalIndex++;
                }
                // Collect remaining unchanged items.
                while (originalIndex < slotCount)
                    unchanged[unchangedIndex++] = originalIndex++;
                // Fill in unchanged items.
                for (int i = slotCount - 1; i >= 0; i--)
                    if (drawOrder[i] == -1)
                        drawOrder[i] = unchanged[--unchangedIndex];
            }
            timeline.setFrame(frameIndex++, drawOrderMap.getFloat("time"), drawOrder);
        }
        timelines.add(timeline);
        duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]);
    }

    // Event timeline.
    JsonValue eventsMap = map.get("events");
    if (eventsMap != null) {
        EventTimeline timeline = new EventTimeline(eventsMap.size);
        int frameIndex = 0;
        for (JsonValue eventMap = eventsMap.child; eventMap != null; eventMap = eventMap.next) {
            EventData eventData = skeletonData.findEvent(eventMap.getString("name"));
            if (eventData == null)
                throw new SerializationException("Event not found: " + eventMap.getString("name"));
            Event event = new Event(eventData);
            event.intValue = eventMap.getInt("int", eventData.getInt());
            event.floatValue = eventMap.getFloat("float", eventData.getFloat());
            event.stringValue = eventMap.getString("string", eventData.getString());
            timeline.setFrame(frameIndex++, eventMap.getFloat("time"), event);
        }
        timelines.add(timeline);
        duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]);
    }

    timelines.shrink();
    skeletonData.animations.add(new Animation(name, timelines, duration));
}

From source file:kyle.game.besiege.battle.Battle.java

License:Open Source License

public void victory(Array<Army> victor) {
    //      System.out.println("victory in " + name);
    //      System.out.println("battle over");
    if (isOver)/*w w  w  .  j  ava  2s.c  o m*/
        System.out.println(getName() + " ENDING BATTLE TWICE!!!?!");
    isOver = true;
    if (victor == aArmies)
        didAtkWin = true;
    else if (victor == dArmies)
        didAtkWin = false;

    int[] victorContribution = new int[victor.size]; // should depend on how much an army sacrificed in battle
    int totalContribution = 0; // maybe number of troops they killed + their own troops killed.

    victor.shrink();

    // change faction of city if siege
    if (didAtkWin && siegeOf != null) {
        Faction newOwner;
        if (siegeOf.getSiege() == null) {
            //            System.out.println("ERROR: no siege!");
            newOwner = aArmies.first().getFaction();
        } else
            newOwner = siegeOf.getSiege().besieging;
        siegeOf.changeFaction(newOwner);
    }
    if (!didAtkWin && siegeOf != null) {
        if (siegeOf.getSiege() != null) {
            siegeOf.getSiege().destroy();
            System.out.println("destroying siege of " + siegeOf.getName());
        } else
            System.out.println("getSiege() == null");
    }

    // manage victorious armies and calculate total rewards
    for (int i = 0; i < victor.size; i++) {
        Army army = victor.get(i);
        army.endBattle();
        army.setStopped(false);
        army.forceWait(WAIT);
        if (army.getParty().player) {
            kingdom.getMapScreen().getSidePanel().setStay(false);
            kingdom.getMapScreen().getSidePanel().setDefault();
        }

        //   log(army.getName() + " has won a battle", "cyan");
        if (!army.isGarrisoned())
            army.setVisible(true);
        army.nextTarget(); // 

        if (army.getParty().player) {
            //            army.setStopped(true);
            army.setTarget(null);
        }

        victorContribution[i] = army.getParty().getAtk(); // for now just do atk.
        totalContribution += victorContribution[i];
    }
    // distribute rewards
    for (int i = 0; i < victor.size; i++) {
        double contribution = victorContribution[i] / 1.0d / totalContribution;
        int reward = (int) (contribution * spoils);
        int expReward;
        int moraleReward;
        if (victor == aArmies) {
            expReward = (int) (contribution * expA);
            moraleReward = (int) (initBalanceA * baseMoraleReward);
        } else {
            expReward = (int) (contribution * expD);
            moraleReward = (int) (initBalanceD * baseMoraleReward);
        }
        expReward *= EXP_FACTOR; // just to beef it up
        log(victor.get(i).getName() + " receives " + moraleReward + " morale, " + reward + " gold and "
                + expReward + " experience!", "green");
        victor.get(i).getParty().wealth += reward;
        victor.get(i).getParty().distributeExp(expReward);
        victor.get(i).setMomentum(victor.get(i).getMomentum() + moraleReward);
    }

    // TESTING
    if (victor == aArmies) {
        for (Army leftOver : dArmies) {
            System.out.println("***** " + leftOver.getName() + " is still in battle!?!");
        }
    } else if (victor == dArmies) {
        for (Army leftOver : aArmies) {
            System.out.println("***** " + leftOver.getName() + " is still in battle!?!");
        }
    }

    //   log("battle ended");

    aArmies.clear();
    dArmies.clear();
    kingdom.removeBattle(this);
    this.remove();
}

From source file:spine.SkeletonBinary.java

License:Open Source License

private void readAnimation(String name, DataInput input, SkeletonData skeletonData) {
    Array<Timeline> timelines = new Array();
    float scale = this.scale;
    float duration = 0;

    try {/* w  w w .jav a2 s  .  co m*/
        // Slot timelines.
        for (int i = 0, n = input.readInt(true); i < n; i++) {
            int slotIndex = input.readInt(true);
            for (int ii = 0, nn = input.readInt(true); ii < nn; ii++) {
                int timelineType = input.readByte();
                int frameCount = input.readInt(true);
                switch (timelineType) {
                case TIMELINE_COLOR: {
                    ColorTimeline timeline = new ColorTimeline(frameCount);
                    timeline.slotIndex = slotIndex;
                    for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
                        float time = input.readFloat();
                        Color.rgba8888ToColor(tempColor, input.readInt());
                        timeline.setFrame(frameIndex, time, tempColor.r, tempColor.g, tempColor.b, tempColor.a);
                        if (frameIndex < frameCount - 1)
                            readCurve(input, frameIndex, timeline);
                    }
                    timelines.add(timeline);
                    duration = Math.max(duration, timeline.getFrames()[frameCount * 5 - 5]);
                    break;
                }
                case TIMELINE_ATTACHMENT:
                    AttachmentTimeline timeline = new AttachmentTimeline(frameCount);
                    timeline.slotIndex = slotIndex;
                    for (int frameIndex = 0; frameIndex < frameCount; frameIndex++)
                        timeline.setFrame(frameIndex, input.readFloat(), input.readString());
                    timelines.add(timeline);
                    duration = Math.max(duration, timeline.getFrames()[frameCount - 1]);
                    break;
                }
            }
        }

        // Bone timelines.
        for (int i = 0, n = input.readInt(true); i < n; i++) {
            int boneIndex = input.readInt(true);
            for (int ii = 0, nn = input.readInt(true); ii < nn; ii++) {
                int timelineType = input.readByte();
                int frameCount = input.readInt(true);
                switch (timelineType) {
                case TIMELINE_ROTATE: {
                    RotateTimeline timeline = new RotateTimeline(frameCount);
                    timeline.boneIndex = boneIndex;
                    for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
                        timeline.setFrame(frameIndex, input.readFloat(), input.readFloat());
                        if (frameIndex < frameCount - 1)
                            readCurve(input, frameIndex, timeline);
                    }
                    timelines.add(timeline);
                    duration = Math.max(duration, timeline.getFrames()[frameCount * 2 - 2]);
                    break;
                }
                case TIMELINE_TRANSLATE:
                case TIMELINE_SCALE: {
                    TranslateTimeline timeline;
                    float timelineScale = 1;
                    if (timelineType == TIMELINE_SCALE)
                        timeline = new ScaleTimeline(frameCount);
                    else {
                        timeline = new TranslateTimeline(frameCount);
                        timelineScale = scale;
                    }
                    timeline.boneIndex = boneIndex;
                    for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
                        timeline.setFrame(frameIndex, input.readFloat(), input.readFloat() * timelineScale,
                                input.readFloat() * timelineScale);
                        if (frameIndex < frameCount - 1)
                            readCurve(input, frameIndex, timeline);
                    }
                    timelines.add(timeline);
                    duration = Math.max(duration, timeline.getFrames()[frameCount * 3 - 3]);
                    break;
                }
                }
            }
        }

        // IK timelines.
        for (int i = 0, n = input.readInt(true); i < n; i++) {
            IkConstraintData ikConstraint = skeletonData.ikConstraints.get(input.readInt(true));
            int frameCount = input.readInt(true);
            IkConstraintTimeline timeline = new IkConstraintTimeline(frameCount);
            timeline.ikConstraintIndex = skeletonData.getIkConstraints().indexOf(ikConstraint, true);
            for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
                timeline.setFrame(frameIndex, input.readFloat(), input.readFloat(), input.readByte());
                if (frameIndex < frameCount - 1)
                    readCurve(input, frameIndex, timeline);
            }
            timelines.add(timeline);
            duration = Math.max(duration, timeline.getFrames()[frameCount * 3 - 3]);
        }

        // FFD timelines.
        for (int i = 0, n = input.readInt(true); i < n; i++) {
            Skin skin = skeletonData.skins.get(input.readInt(true));
            for (int ii = 0, nn = input.readInt(true); ii < nn; ii++) {
                int slotIndex = input.readInt(true);
                for (int iii = 0, nnn = input.readInt(true); iii < nnn; iii++) {
                    Attachment attachment = skin.getAttachment(slotIndex, input.readString());
                    int frameCount = input.readInt(true);
                    FfdTimeline timeline = new FfdTimeline(frameCount);
                    timeline.slotIndex = slotIndex;
                    timeline.attachment = attachment;
                    for (int frameIndex = 0; frameIndex < frameCount; frameIndex++) {
                        float time = input.readFloat();

                        float[] vertices;
                        int vertexCount;
                        if (attachment instanceof MeshAttachment)
                            vertexCount = ((MeshAttachment) attachment).getVertices().length;
                        else
                            vertexCount = ((SkinnedMeshAttachment) attachment).getWeights().length / 3 * 2;

                        int end = input.readInt(true);
                        if (end == 0) {
                            if (attachment instanceof MeshAttachment)
                                vertices = ((MeshAttachment) attachment).getVertices();
                            else
                                vertices = new float[vertexCount];
                        } else {
                            vertices = new float[vertexCount];
                            int start = input.readInt(true);
                            end += start;
                            if (scale == 1) {
                                for (int v = start; v < end; v++)
                                    vertices[v] = input.readFloat();
                            } else {
                                for (int v = start; v < end; v++)
                                    vertices[v] = input.readFloat() * scale;
                            }
                            if (attachment instanceof MeshAttachment) {
                                float[] meshVertices = ((MeshAttachment) attachment).getVertices();
                                for (int v = 0, vn = vertices.length; v < vn; v++)
                                    vertices[v] += meshVertices[v];
                            }
                        }

                        timeline.setFrame(frameIndex, time, vertices);
                        if (frameIndex < frameCount - 1)
                            readCurve(input, frameIndex, timeline);
                    }
                    timelines.add(timeline);
                    duration = Math.max(duration, timeline.getFrames()[frameCount - 1]);
                }
            }
        }

        // Draw order timeline.
        int drawOrderCount = input.readInt(true);
        if (drawOrderCount > 0) {
            DrawOrderTimeline timeline = new DrawOrderTimeline(drawOrderCount);
            int slotCount = skeletonData.slots.size;
            for (int i = 0; i < drawOrderCount; i++) {
                int offsetCount = input.readInt(true);
                int[] drawOrder = new int[slotCount];
                for (int ii = slotCount - 1; ii >= 0; ii--)
                    drawOrder[ii] = -1;
                int[] unchanged = new int[slotCount - offsetCount];
                int originalIndex = 0, unchangedIndex = 0;
                for (int ii = 0; ii < offsetCount; ii++) {
                    int slotIndex = input.readInt(true);
                    // Collect unchanged items.
                    while (originalIndex != slotIndex)
                        unchanged[unchangedIndex++] = originalIndex++;
                    // Set changed items.
                    drawOrder[originalIndex + input.readInt(true)] = originalIndex++;
                }
                // Collect remaining unchanged items.
                while (originalIndex < slotCount)
                    unchanged[unchangedIndex++] = originalIndex++;
                // Fill in unchanged items.
                for (int ii = slotCount - 1; ii >= 0; ii--)
                    if (drawOrder[ii] == -1)
                        drawOrder[ii] = unchanged[--unchangedIndex];
                timeline.setFrame(i, input.readFloat(), drawOrder);
            }
            timelines.add(timeline);
            duration = Math.max(duration, timeline.getFrames()[drawOrderCount - 1]);
        }

        // Event timeline.
        int eventCount = input.readInt(true);
        if (eventCount > 0) {
            EventTimeline timeline = new EventTimeline(eventCount);
            for (int i = 0; i < eventCount; i++) {
                float time = input.readFloat();
                EventData eventData = skeletonData.events.get(input.readInt(true));
                Event event = new Event(time, eventData);
                event.intValue = input.readInt(false);
                event.floatValue = input.readFloat();
                event.stringValue = input.readBoolean() ? input.readString() : eventData.stringValue;
                timeline.setFrame(i, event);
            }
            timelines.add(timeline);
            duration = Math.max(duration, timeline.getFrames()[eventCount - 1]);
        }
    } catch (IOException ex) {
        throw new SerializationException("Error reading skeleton file.", ex);
    }

    timelines.shrink();
    skeletonData.animations.add(new Animation(name, timelines, duration));
}

From source file:spine.SkeletonJson.java

License:Open Source License

private void readAnimation(String name, JsonValue map, SkeletonData skeletonData) {
    float scale = this.scale;
    Array<Timeline> timelines = new Array();
    float duration = 0;

    // Slot timelines.
    for (JsonValue slotMap = map.getChild("slots"); slotMap != null; slotMap = slotMap.next) {
        int slotIndex = skeletonData.findSlotIndex(slotMap.name);
        if (slotIndex == -1)
            throw new SerializationException("Slot not found: " + slotMap.name);

        for (JsonValue timelineMap = slotMap.child; timelineMap != null; timelineMap = timelineMap.next) {
            String timelineName = timelineMap.name;
            if (timelineName.equals("color")) {
                ColorTimeline timeline = new ColorTimeline(timelineMap.size);
                timeline.slotIndex = slotIndex;

                int frameIndex = 0;
                for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) {
                    Color color = Color.valueOf(valueMap.getString("color"));
                    timeline.setFrame(frameIndex, valueMap.getFloat("time"), color.r, color.g, color.b,
                            color.a);//from  w  w  w  . java 2s .co  m
                    readCurve(timeline, frameIndex, valueMap);
                    frameIndex++;
                }
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 5 - 5]);

            } else if (timelineName.equals("attachment")) {
                AttachmentTimeline timeline = new AttachmentTimeline(timelineMap.size);
                timeline.slotIndex = slotIndex;

                int frameIndex = 0;
                for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next)
                    timeline.setFrame(frameIndex++, valueMap.getFloat("time"), valueMap.getString("name"));
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]);
            } else
                throw new RuntimeException(
                        "Invalid timeline type for a slot: " + timelineName + " (" + slotMap.name + ")");
        }
    }

    // Bone timelines.
    for (JsonValue boneMap = map.getChild("bones"); boneMap != null; boneMap = boneMap.next) {
        int boneIndex = skeletonData.findBoneIndex(boneMap.name);
        if (boneIndex == -1)
            throw new SerializationException("Bone not found: " + boneMap.name);

        for (JsonValue timelineMap = boneMap.child; timelineMap != null; timelineMap = timelineMap.next) {
            String timelineName = timelineMap.name;
            if (timelineName.equals("rotate")) {
                RotateTimeline timeline = new RotateTimeline(timelineMap.size);
                timeline.boneIndex = boneIndex;

                int frameIndex = 0;
                for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) {
                    timeline.setFrame(frameIndex, valueMap.getFloat("time"), valueMap.getFloat("angle"));
                    readCurve(timeline, frameIndex, valueMap);
                    frameIndex++;
                }
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 2 - 2]);

            } else if (timelineName.equals("translate") || timelineName.equals("scale")) {
                TranslateTimeline timeline;
                float timelineScale = 1;
                if (timelineName.equals("scale"))
                    timeline = new ScaleTimeline(timelineMap.size);
                else {
                    timeline = new TranslateTimeline(timelineMap.size);
                    timelineScale = scale;
                }
                timeline.boneIndex = boneIndex;

                int frameIndex = 0;
                for (JsonValue valueMap = timelineMap.child; valueMap != null; valueMap = valueMap.next) {
                    float x = valueMap.getFloat("x", 0), y = valueMap.getFloat("y", 0);
                    timeline.setFrame(frameIndex, valueMap.getFloat("time"), x * timelineScale,
                            y * timelineScale);
                    readCurve(timeline, frameIndex, valueMap);
                    frameIndex++;
                }
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 3 - 3]);

            } else
                throw new RuntimeException(
                        "Invalid timeline type for a bone: " + timelineName + " (" + boneMap.name + ")");
        }
    }

    // IK timelines.
    for (JsonValue ikMap = map.getChild("ik"); ikMap != null; ikMap = ikMap.next) {
        IkConstraintData ikConstraint = skeletonData.findIkConstraint(ikMap.name);
        IkConstraintTimeline timeline = new IkConstraintTimeline(ikMap.size);
        timeline.ikConstraintIndex = skeletonData.getIkConstraints().indexOf(ikConstraint, true);
        int frameIndex = 0;
        for (JsonValue valueMap = ikMap.child; valueMap != null; valueMap = valueMap.next) {
            timeline.setFrame(frameIndex, valueMap.getFloat("time"), valueMap.getFloat("mix"),
                    valueMap.getBoolean("bendPositive") ? 1 : -1);
            readCurve(timeline, frameIndex, valueMap);
            frameIndex++;
        }
        timelines.add(timeline);
        duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() * 3 - 3]);
    }

    // FFD timelines.
    for (JsonValue ffdMap = map.getChild("ffd"); ffdMap != null; ffdMap = ffdMap.next) {
        Skin skin = skeletonData.findSkin(ffdMap.name);
        if (skin == null)
            throw new SerializationException("Skin not found: " + ffdMap.name);
        for (JsonValue slotMap = ffdMap.child; slotMap != null; slotMap = slotMap.next) {
            int slotIndex = skeletonData.findSlotIndex(slotMap.name);
            if (slotIndex == -1)
                throw new SerializationException("Slot not found: " + slotMap.name);
            for (JsonValue meshMap = slotMap.child; meshMap != null; meshMap = meshMap.next) {
                FfdTimeline timeline = new FfdTimeline(meshMap.size);
                Attachment attachment = skin.getAttachment(slotIndex, meshMap.name);
                if (attachment == null)
                    throw new SerializationException("FFD attachment not found: " + meshMap.name);
                timeline.slotIndex = slotIndex;
                timeline.attachment = attachment;

                int vertexCount;
                if (attachment instanceof MeshAttachment)
                    vertexCount = ((MeshAttachment) attachment).getVertices().length;
                else
                    vertexCount = ((SkinnedMeshAttachment) attachment).getWeights().length / 3 * 2;

                int frameIndex = 0;
                for (JsonValue valueMap = meshMap.child; valueMap != null; valueMap = valueMap.next) {
                    float[] vertices;
                    JsonValue verticesValue = valueMap.get("vertices");
                    if (verticesValue == null) {
                        if (attachment instanceof MeshAttachment)
                            vertices = ((MeshAttachment) attachment).getVertices();
                        else
                            vertices = new float[vertexCount];
                    } else {
                        vertices = new float[vertexCount];
                        int start = valueMap.getInt("offset", 0);
                        System.arraycopy(verticesValue.asFloatArray(), 0, vertices, start, verticesValue.size);
                        if (scale != 1) {
                            for (int i = start, n = i + verticesValue.size; i < n; i++)
                                vertices[i] *= scale;
                        }
                        if (attachment instanceof MeshAttachment) {
                            float[] meshVertices = ((MeshAttachment) attachment).getVertices();
                            for (int i = 0; i < vertexCount; i++)
                                vertices[i] += meshVertices[i];
                        }
                    }

                    timeline.setFrame(frameIndex, valueMap.getFloat("time"), vertices);
                    readCurve(timeline, frameIndex, valueMap);
                    frameIndex++;
                }
                timelines.add(timeline);
                duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]);
            }
        }
    }

    // Draw order timeline.
    JsonValue drawOrdersMap = map.get("drawOrder");
    if (drawOrdersMap == null)
        drawOrdersMap = map.get("draworder");
    if (drawOrdersMap != null) {
        DrawOrderTimeline timeline = new DrawOrderTimeline(drawOrdersMap.size);
        int slotCount = skeletonData.slots.size;
        int frameIndex = 0;
        for (JsonValue drawOrderMap = drawOrdersMap.child; drawOrderMap != null; drawOrderMap = drawOrderMap.next) {
            int[] drawOrder = null;
            JsonValue offsets = drawOrderMap.get("offsets");
            if (offsets != null) {
                drawOrder = new int[slotCount];
                for (int i = slotCount - 1; i >= 0; i--)
                    drawOrder[i] = -1;
                int[] unchanged = new int[slotCount - offsets.size];
                int originalIndex = 0, unchangedIndex = 0;
                for (JsonValue offsetMap = offsets.child; offsetMap != null; offsetMap = offsetMap.next) {
                    int slotIndex = skeletonData.findSlotIndex(offsetMap.getString("slot"));
                    if (slotIndex == -1)
                        throw new SerializationException("Slot not found: " + offsetMap.getString("slot"));
                    // Collect unchanged items.
                    while (originalIndex != slotIndex)
                        unchanged[unchangedIndex++] = originalIndex++;
                    // Set changed items.
                    drawOrder[originalIndex + offsetMap.getInt("offset")] = originalIndex++;
                }
                // Collect remaining unchanged items.
                while (originalIndex < slotCount)
                    unchanged[unchangedIndex++] = originalIndex++;
                // Fill in unchanged items.
                for (int i = slotCount - 1; i >= 0; i--)
                    if (drawOrder[i] == -1)
                        drawOrder[i] = unchanged[--unchangedIndex];
            }
            timeline.setFrame(frameIndex++, drawOrderMap.getFloat("time"), drawOrder);
        }
        timelines.add(timeline);
        duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]);
    }

    // Event timeline.
    JsonValue eventsMap = map.get("events");
    if (eventsMap != null) {
        EventTimeline timeline = new EventTimeline(eventsMap.size);
        int frameIndex = 0;
        for (JsonValue eventMap = eventsMap.child; eventMap != null; eventMap = eventMap.next) {
            EventData eventData = skeletonData.findEvent(eventMap.getString("name"));
            if (eventData == null)
                throw new SerializationException("Event not found: " + eventMap.getString("name"));
            Event event = new Event(eventMap.getFloat("time"), eventData);
            event.intValue = eventMap.getInt("int", eventData.getInt());
            event.floatValue = eventMap.getFloat("float", eventData.getFloat());
            event.stringValue = eventMap.getString("string", eventData.getString());
            timeline.setFrame(frameIndex++, event);
        }
        timelines.add(timeline);
        duration = Math.max(duration, timeline.getFrames()[timeline.getFrameCount() - 1]);
    }

    timelines.shrink();
    skeletonData.animations.add(new Animation(name, timelines, duration));
}