1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package com.jme3.cinematic.events;
6
7import com.jme3.animation.LoopMode;
8import com.jme3.app.Application;
9import com.jme3.cinematic.Cinematic;
10import com.jme3.export.InputCapsule;
11import com.jme3.export.JmeExporter;
12import com.jme3.export.JmeImporter;
13import com.jme3.export.OutputCapsule;
14import com.jme3.math.FastMath;
15import com.jme3.math.Vector3f;
16import com.jme3.scene.Spatial;
17import java.io.IOException;
18import java.util.logging.Level;
19import java.util.logging.Logger;
20
21/**
22 *
23 * @author Nehon
24 * @deprecated use spatial animation instead.
25 */
26@Deprecated
27public class PositionTrack extends AbstractCinematicEvent {
28
29    private static final Logger log = Logger.getLogger(PositionTrack.class.getName());
30    private Vector3f startPosition;
31    private Vector3f endPosition;
32    private Spatial spatial;
33    private String spatialName = "";
34    private float value = 0;
35
36    public PositionTrack() {
37    }
38
39    public PositionTrack(Spatial spatial, Vector3f endPosition) {
40        this.endPosition = endPosition;
41        this.spatial = spatial;
42        spatialName = spatial.getName();
43    }
44
45    @Override
46    public void initEvent(Application app, Cinematic cinematic) {
47        super.initEvent(app, cinematic);
48        if (spatial == null) {
49            spatial = cinematic.getScene().getChild(spatialName);
50            if (spatial == null) {
51            } else {
52                log.log(Level.WARNING, "spatial {0} not found in the scene", spatialName);
53            }
54        }
55    }
56
57    public PositionTrack(Spatial spatial, Vector3f endPosition, float initialDuration, LoopMode loopMode) {
58        super(initialDuration, loopMode);
59        this.endPosition = endPosition;
60        this.spatial = spatial;
61        spatialName = spatial.getName();
62    }
63
64    public PositionTrack(Spatial spatial, Vector3f endPosition, LoopMode loopMode) {
65        super(loopMode);
66        this.endPosition = endPosition;
67        this.spatial = spatial;
68        spatialName = spatial.getName();
69    }
70
71    public PositionTrack(Spatial spatial, Vector3f endPosition, float initialDuration) {
72        super(initialDuration);
73        this.endPosition = endPosition;
74        this.spatial = spatial;
75        spatialName = spatial.getName();
76    }
77
78    @Override
79    public void onPlay() {
80        if (playState != playState.Paused) {
81            startPosition = spatial.getWorldTranslation().clone();
82        }
83        if (initialDuration == 0 && spatial != null) {
84
85            spatial.setLocalTranslation(endPosition);
86        }
87    }
88
89    @Override
90    public void onUpdate(float tpf) {
91        if (spatial != null) {
92            value = Math.min(time / initialDuration, 1.0f);
93            Vector3f pos = FastMath.interpolateLinear(value, startPosition, endPosition);
94            spatial.setLocalTranslation(pos);
95        }
96    }
97
98    @Override
99    public void onStop() {
100        value = 0;
101    }
102
103    @Override
104    public void onPause() {
105    }
106
107    @Override
108    public void write(JmeExporter ex) throws IOException {
109        super.write(ex);
110        OutputCapsule oc = ex.getCapsule(this);
111        oc.write(spatialName, "spatialName", "");
112        oc.write(endPosition, "endPosition", null);
113    }
114
115    @Override
116    public void read(JmeImporter im) throws IOException {
117        super.read(im);
118        InputCapsule ic = im.getCapsule(this);
119        spatialName = ic.readString("spatialName", "");
120        endPosition = (Vector3f) ic.readSavable("endPosition", null);
121    }
122}
123