package com.thomshutt.fappybird.drawable;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Circle;
import com.thomshutt.fappybird.Drawable;
import static java.lang.Math.max;
import static java.lang.Math.min;
public class Bird implements Drawable {
public static final float TOUCH_ACCELERATION = 19f;
private static final int GRAVITY = 55;
private float velocityY = 0;
private float y = 0;
private float animationTime = 0f;
private Animation animation;
private TextureRegion textureBird;
private Sprite spriteBird;
private float x;
private float birdHeight;
private float birdWidth;
private float maxBirdY;
public Bird(Animation animation) {
this.animation = animation;
textureBird = animation.getKeyFrame(0);
spriteBird = new Sprite(new TextureRegion(textureBird, 0, 0, 128, 128));
}
@Override
public void tick(final float deltaTime){
velocityY += GRAVITY * deltaTime;
y = min(this.maxBirdY, y -= (0.4f * velocityY));
animationTime += deltaTime;
this.textureBird = animation.getKeyFrame(animationTime);
}
@Override
public void screenTouched(boolean touched){
if(touched){
velocityY = -TOUCH_ACCELERATION;
}
}
@Override
public void draw(SpriteBatch batch) {
spriteBird = new Sprite(new TextureRegion(textureBird, 0, 0, 128, 128));
spriteBird.setSize(birdWidth, birdHeight);
spriteBird.setOrigin(spriteBird.getWidth() / 2, spriteBird.getHeight() / 2);
spriteBird.setPosition(this.x, this.y);
spriteBird.setRotation(getTiltDegrees());
spriteBird.draw(batch);
}
@Override
public void resize(int width, int height) {
this.birdWidth = height / 10f;
this.birdHeight = height / 10f;
this.x = -(width / 4);
this.maxBirdY = (height / 2);
}
@Override
public boolean isInCollisionWith(Bird bird) {
return false;
}
private float getTiltDegrees() {
if(this.velocityY <= 0) return min(30, -(this.velocityY * 3f));
if(this.velocityY > 0) return max(-85, -(this.velocityY * 3f));
return 0;
}
public Circle getCircle(){
return new Circle(this.x + (this.birdWidth / 2), this.y + (this.birdHeight / 2), this.birdHeight / 2.5f);
}
public float getX() { return this.x; }
public float getY() { return this.y; }
}