Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Animation {
- Array<TextureRegion> frames;
- float maxFrameTime;
- float currentFrameTime;
- int frameCount;
- int frame;
- Boolean playedOnce;
- public void dispose(){
- frames=null;
- }
- public Animation(TextureRegion region, int frameCount, float cycleTime){
- playedOnce=false;
- frames = new Array<TextureRegion>();
- TextureRegion temp;
- int frameWidth = region.getRegionWidth() / frameCount;
- for(int i = 0; i < frameCount; i++){
- temp = new TextureRegion(region, i * frameWidth, 0, frameWidth, region.getRegionHeight());
- frames.add(temp);
- }
- this.frameCount = frameCount;
- this.maxFrameTime = cycleTime / frameCount;
- frame = 0;
- }
- public void update(float dt){
- currentFrameTime += dt;
- if(currentFrameTime > maxFrameTime){
- frame++;
- currentFrameTime = 0;
- if(frame >= frameCount-1){
- playedOnce=true;
- }
- if(frame >= frameCount){
- frame = 0;
- }
- }
- }
- public Boolean isPlayedOnce(){
- return playedOnce;
- }
- public void flip(){
- for(TextureRegion region : frames)
- region.flip(true, false);
- }
- public TextureRegion getFrame(){
- return frames.get(frame);
- }
- public void update(float dt){
- float deltaX=next.getX()-shadow.getX();
- float deltaY=next.getY()-shadow.getY();
- float angle=(float)Math.atan2(deltaY,deltaX);
- float newX=5f*(float)Math.cos(angle);
- float newY=5f*(float)Math.sin(angle);
- shadow.move(newX,newY);
- Vector2 delta=pad.getDelta();
- if(delta.len()>0){
- if(delta.x<0) next.flip(true);
- else next.flip(false);
- next.move(delta);
- }
- if(next.isTouched(shadow.getBody())){
- next.hit(shadow);
- shadow.move(400,400);
- }
- cam.position.set(cam.viewportWidth/2f,cam.viewportHeight/2f,0);//next.getX(), next.getY(), 0);
- cam.update();
- next.update(dt);
- pad.setCenter(cam.position.x-cam.viewportWidth/2f+pad.getPad().getWidth(), cam.position.y-cam.viewportHeight/2f+pad.getPad().getHeight());
- render(main.spriteBatch);
- }
- @Override
- public void render(SpriteBatch sb){
- sb.setProjectionMatrix(cam.combined);
- sb.begin();
- for(Piece p:pieces){
- sb.draw(p.getTexture(), p.getX()*step, p.getY()*step, step, step);
- }
- sb.draw(shadow.getTexture(), shadow.getX()-shadow.getTexture().getRegionWidth()/2, shadow.getY());
- sb.draw(next.getTexture(), next.getX()-next.getTexture().getRegionWidth()/2, next.getY());
- sb.setColor(1,1,1,0.5f);
- sb.draw(pad.getPad(), pad.getCenter().x-64, pad.getCenter().y-64, 128, 128);
- sb.draw(pad.getPadTo(), pad.getPos().x-pad.getPadTo().getWidth(), pad.getPos().y-pad.getPadTo().getWidth(), 32, 32);
- sb.setColor(1,1,1,1f);
- String s=(int)next.getX()+" "+(int)next.getMotion().x;
- glyph.setText(fnt, s);
- fnt.draw(sb, s, cam.position.x-cam.viewportWidth/2f, cam.position.y+cam.viewportHeight/2f);
- sb.end();
- shr.setProjectionMatrix(cam.combined);
- shr.begin(ShapeRenderer.ShapeType.Line);
- shr.setColor(1, 1, 0, 1);
- shr.rect(next.getBody().x, next.getBody().y, next.getBody().width, next.getBody().height);
- shr.setColor(1, 0, 0, 1);
- shr.rect(shadow.getBody().x, shadow.getBody().y, shadow.getBody().width, shadow.getBody().height);
- shr.end();
- }
Add Comment
Please, Sign In to add comment