animationcontroller.java

来自「java 3d game jme 工程开发源代码」· Java 代码 · 共 503 行 · 第 1/2 页

JAVA
503
字号
            // Make sure there is at least one matching sync tag to use
            boolean match = false;
            for (String name : activeTags) {
                if (blendTags.contains(name)) {
                    match = true;
                    break;
                }
            }

            if (!match) {
                needsSync = false;
                return;
            }

            // We can sync, so let's try.
            ArrayList<String> currentSyncTags = activeAnimation
                    .getSyncNames(previousTest);

            // We can't sync yet, wait a bit.
            if (currentSyncTags.size() == 0) {
                return;
            }

            for (String tag : currentSyncTags) {
                if (blendTags.contains(tag)) {
                    // we can sync here
                    int[] frames = blendAnimation.getSyncFrames(tag);
                    // find the closest match to our frame.
                    int old = (int) FastMath.abs(frames[0] - previousTest);
                    int diff = 0;
                    int i = 1;
                    for (i = 1; i < frames.length; i++) {
                        diff = (int) FastMath.abs(frames[i] - previousTest);
                        if (diff > old) {
                            // old is the sync frame
                            break;
                        }
                    }
                    blendAnimation.setInitialFrame(frames[i - 1]);
                    needsSync = false;
                    return;
                }
            }

            // TODO: If we made it this far, we need to wait a few more frames
            // before
            // syncing. This can be precalculated so that we don't do this each
            // frame.
        }
    }
    
    public void updateProps() {
        if(props.isAllowTranslation() && skeleton != null && modelNode != null) {
            if(blendAnimation != null) {
                blendAnimation.setSourceBone(skeleton);
                blendAnimation.setDestSpatial(modelNode);
                blendAnimation.setAnimationProperties(props);
                
            }
            
            if(activeAnimation != null) {
                activeAnimation.setSourceBone(skeleton);
                activeAnimation.setDestSpatial(modelNode);
                activeAnimation.setAnimationProperties(props);
                
            }
        } else {
            if(blendAnimation != null) {
                blendAnimation.setSourceBone(null);
                blendAnimation.setDestSpatial(null);
            }
            
            if(activeAnimation != null) {
                activeAnimation.setSourceBone(null);
                activeAnimation.setDestSpatial(null);
            }
        }
    }

    public void setActiveAnimation(String name) {
        setActiveAnimation(name, false, 0, null);
    }
    
    public void setActiveAnimation(String name, boolean blend, float time,
            AnimationProperties props) {
        this.props = props;
        if (blend) {
            if (animationSets != null) {
                for (int i = 0; i < animationSets.size(); i++) {
                    if (animationSets.get(i).getName().equalsIgnoreCase(name)) {
                        setBlendAnimation(animationSets.get(i), time, !props.isOneOff());
                        return;
                    }
                }
            }
        } else {
            BoneAnimation old = activeAnimation;
            if (animationSets != null) {
                for (int i = 0; i < animationSets.size(); i++) {
                    if (animationSets.get(i).getName().equalsIgnoreCase(name)) {
                        activeAnimation = animationSets.get(i);
                        if (old != activeAnimation) {
                            this.blendAnimation = null;
                        }
                        return;
                    }
                }
            }

            // Invalid animation, set active to null
            clearActiveAnimation();
        }
    }

    public void setActiveAnimation(BoneAnimation bac) {
        setActiveAnimation(bac, false, 0, false);
    }

    public void setActiveAnimation(BoneAnimation bac, boolean blend,
            float time, boolean sync) {
        if (blend) {
            setBlendAnimation(bac, time, sync);
            return;
        } else {
            BoneAnimation old = activeAnimation;
            if (animationSets != null) {
                for (int i = 0; i < animationSets.size(); i++) {
                    if (animationSets.get(i) == bac) {
                        activeAnimation = animationSets.get(i);
                        if (old != activeAnimation) {
                            this.blendAnimation = null;
                        }
                        return;
                    }
                }
            }
            // Invalid animation, set active to null
            clearActiveAnimation();
        }
    }

    public void setActiveAnimation(int index) {
        setActiveAnimation(index, false, 0, false);
    }

    public void setActiveAnimation(int index, boolean blend, float time,
            boolean sync) {
        if (blend) {
            if (animationSets != null && animationSets.size() > index) {
                setBlendAnimation(animationSets.get(index), time, sync);
                return;
            }
        } else {
            BoneAnimation old = activeAnimation;
            if (animationSets != null && animationSets.size() > index) {
                activeAnimation = animationSets.get(index);
                if (old != activeAnimation) {
                    this.blendAnimation = null;
                }
                return;
            }
            // Invalid animation, set active to null
            clearActiveAnimation();
        }
    }

    public void setSkeleton(Bone b) {
        this.skeleton = b;
        if (animationSets != null) {
            for (int i = 0; i < animationSets.size(); i++) {
                animationSets.get(i).assignSkeleton(skeleton);
            }
        }
    }

    @Override
    public void update(float time) {
    	//We are blending into nothing, so just set this as active.
    	if(blendAnimation != null && activeAnimation == null) {
    		activeAnimation = blendAnimation;
    		blendAnimation = null;
    	}
    	
        if (blendAnimation != null && !needsSync) {
            currentBlendTime += time / endBlendTime;
            if (currentBlendTime >= 1.0f) {
//                activeAnimationsList.clear();
                
                activeAnimation = blendAnimation;
                blendAnimation = null;
                updateProps();
            }
        }

        if (activeAnimation != null) {
            activeAnimation.update(time, getRepeatType(), getSpeed());
//            for (ModifierData modifierData : activeAnimationsList) {
//                modifierData.animation.update(time, getRepeatType(), getSpeed(),
//                        modifierData.blendTime);                
//            }

            if (blendAnimation != null) {
                if (!needsSync) {
                    blendAnimation.update(time, getRepeatType(), getSpeed(),
                            currentBlendTime);
                } else {
                    calculateSyncFrame();
                }
            }
        }
    }

    public void write(JMEExporter e) throws IOException {
        super.write(e);
        OutputCapsule cap = e.getCapsule(this);
        cap.writeSavableArrayList(animationSets, "animationSets", null);
        cap.write(skeleton, "skeleton", null);
        cap.write(activeAnimation, "activeAnimation", null);
    }

    @SuppressWarnings("unchecked")
    public void read(JMEImporter e) throws IOException {
        super.read(e);
        InputCapsule cap = e.getCapsule(this);
        animationSets = cap.readSavableArrayList("animationSets", null);
        skeleton = (Bone) cap.readSavable("skeleton", null);
        activeAnimation = (BoneAnimation) cap.readSavable("activeAnimation",
                null);
    }

    public BoneAnimation getBlendAnimation() {
        return blendAnimation;
    }

    public Spatial getModelNode() {
        return modelNode;
    }

    public void setModelNode(Spatial modelNode) {
        this.modelNode = modelNode;
    }

    public AnimationProperties getProps() {
        return props;
    }

    public void setProps(AnimationProperties props) {
        this.props = props;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?