Guest User

Untitled

a guest
Dec 17th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. public class Animation {
  2. Array<TextureRegion> frames;
  3. float maxFrameTime;
  4. float currentFrameTime;
  5. int frameCount;
  6. int frame;
  7. Boolean playedOnce;
  8.  
  9. public void dispose(){
  10. frames=null;
  11. }
  12.  
  13. public Animation(TextureRegion region, int frameCount, float cycleTime){
  14. playedOnce=false;
  15. frames = new Array<TextureRegion>();
  16. TextureRegion temp;
  17. int frameWidth = region.getRegionWidth() / frameCount;
  18. for(int i = 0; i < frameCount; i++){
  19. temp = new TextureRegion(region, i * frameWidth, 0, frameWidth, region.getRegionHeight());
  20. frames.add(temp);
  21. }
  22. this.frameCount = frameCount;
  23. this.maxFrameTime = cycleTime / frameCount;
  24. frame = 0;
  25. }
  26.  
  27. public void update(float dt){
  28. currentFrameTime += dt;
  29. if(currentFrameTime > maxFrameTime){
  30. frame++;
  31. currentFrameTime = 0;
  32. if(frame >= frameCount-1){
  33. playedOnce=true;
  34. }
  35. if(frame >= frameCount){
  36. frame = 0;
  37. }
  38. }
  39. }
  40.  
  41. public Boolean isPlayedOnce(){
  42. return playedOnce;
  43. }
  44.  
  45. public void flip(){
  46. for(TextureRegion region : frames)
  47. region.flip(true, false);
  48. }
  49.  
  50. public TextureRegion getFrame(){
  51. return frames.get(frame);
  52. }
  53.  
  54. public void update(float dt){
  55. float deltaX=next.getX()-shadow.getX();
  56. float deltaY=next.getY()-shadow.getY();
  57. float angle=(float)Math.atan2(deltaY,deltaX);
  58. float newX=5f*(float)Math.cos(angle);
  59. float newY=5f*(float)Math.sin(angle);
  60. shadow.move(newX,newY);
  61. Vector2 delta=pad.getDelta();
  62. if(delta.len()>0){
  63. if(delta.x<0) next.flip(true);
  64. else next.flip(false);
  65. next.move(delta);
  66. }
  67. if(next.isTouched(shadow.getBody())){
  68. next.hit(shadow);
  69. shadow.move(400,400);
  70. }
  71. cam.position.set(cam.viewportWidth/2f,cam.viewportHeight/2f,0);//next.getX(), next.getY(), 0);
  72. cam.update();
  73. next.update(dt);
  74. pad.setCenter(cam.position.x-cam.viewportWidth/2f+pad.getPad().getWidth(), cam.position.y-cam.viewportHeight/2f+pad.getPad().getHeight());
  75. render(main.spriteBatch);
  76. }
  77.  
  78. @Override
  79. public void render(SpriteBatch sb){
  80. sb.setProjectionMatrix(cam.combined);
  81. sb.begin();
  82. for(Piece p:pieces){
  83. sb.draw(p.getTexture(), p.getX()*step, p.getY()*step, step, step);
  84. }
  85. sb.draw(shadow.getTexture(), shadow.getX()-shadow.getTexture().getRegionWidth()/2, shadow.getY());
  86. sb.draw(next.getTexture(), next.getX()-next.getTexture().getRegionWidth()/2, next.getY());
  87. sb.setColor(1,1,1,0.5f);
  88. sb.draw(pad.getPad(), pad.getCenter().x-64, pad.getCenter().y-64, 128, 128);
  89. sb.draw(pad.getPadTo(), pad.getPos().x-pad.getPadTo().getWidth(), pad.getPos().y-pad.getPadTo().getWidth(), 32, 32);
  90. sb.setColor(1,1,1,1f);
  91. String s=(int)next.getX()+" "+(int)next.getMotion().x;
  92. glyph.setText(fnt, s);
  93. fnt.draw(sb, s, cam.position.x-cam.viewportWidth/2f, cam.position.y+cam.viewportHeight/2f);
  94. sb.end();
  95. shr.setProjectionMatrix(cam.combined);
  96. shr.begin(ShapeRenderer.ShapeType.Line);
  97. shr.setColor(1, 1, 0, 1);
  98. shr.rect(next.getBody().x, next.getBody().y, next.getBody().width, next.getBody().height);
  99. shr.setColor(1, 0, 0, 1);
  100. shr.rect(shadow.getBody().x, shadow.getBody().y, shadow.getBody().width, shadow.getBody().height);
  101. shr.end();
  102. }
Add Comment
Please, Sign In to add comment