0915日报

重力模拟器

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);
 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值