重力模拟器
public class JackGravityEmulator {
private Image image;
protected int jumpTimes;
protected float target;
protected float alpha;
private List<Action> actions;
private Interpolator dec;
private Interpolator acc;
public JackGravityEmulator(Image image, int jumpTimes, float target,float alpha) {
this.image = image;
if(jumpTimes<=0) jumpTimes=1;
if(alpha<=0||alpha>=1) alpha=Math.abs(alpha/1000);
setParams(jumpTimes, target, alpha);
}
public void setParams(int jumpTimes,float target,float alpha){
this.jumpTimes=jumpTimes;
this.target=target;
this.alpha=alpha;
if(image==null){
System.out.println("image cannot be null");
}
float tempDeltaHigh;
float tempTime;
if(actions==null) actions=new ArrayList<Action>();
tempDeltaHigh=image.y-target;//y
tempTime=Math.abs(tempDeltaHigh*alpha);//origin duration
final float g=tempDeltaHigh*2/(tempTime*tempTime);
System.out.println(g);
setGravity();
Action firstDive=MoveTo.$(image.x, target, tempTime).setInterpolator(acc);
actions.add(firstDive);
float beta=1f/jumpTimes;
for(int i=0;i<jumpTimes;i++){
tempTime*=1-beta;
tempDeltaHigh=0.5f*g*tempTime*tempTime;
actions.add(Sequence.$(
MoveBy.$(0, tempDeltaHigh, Math.abs(tempDeltaHigh*alpha))
.setInterpolator(dec),
MoveTo.$(image.x, target, Math.abs(tempDeltaHigh*alpha))
.setInterpolator(acc)));
}
}
/**
*
*/
public void go(){
image.action(Sequence.$(actions.toArray(new Action[actions.size()])));
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
private void setGravity() {
dec=DecelerateInterpolator.$();
acc=AccelerateInterpolator.$();
}
/**
* 高端玩家调试用的方法
* @param gamma
*/
public void setgravity(float gamma){
dec=DecelerateInterpolator.$(gamma);
acc=AccelerateInterpolator.$(gamma);
}
}