package com.hyperling.apps.games;

import android.graphics.Bitmap;

/**
 * Created by ling on 12/23/16.
 */

public class Chicken extends Player {

    private boolean jumping, flapping;
    private int lastY, lastDY;
    private int leftFootX, rightFootX;

    public Chicken(Bitmap b, int w, int h, int numFrames){
        super(b, w, h, numFrames);
    }

    public boolean isJumping() {
        return jumping;
    }

    public void setJumping(boolean jumping) {
        this.jumping = jumping;
    }

    public boolean isFlapping() {
        return flapping;
    }

    public void setFlapping(boolean flapping) {
        this.flapping = flapping;
    }

    public void jump(){
        // If we are already jumping then we should flap instead
        if (jumping){
            flap();
        }
        // Jumps are more powerful than flaps
        else{
            jumping = true;
            dy -= 20;
        }
    }

    private void flap(){
        // Only allow a flap after we start falling
        if (!flapping){
            flapping = true;
            dy -= 10;
        }
    }

    public void update(int platformY) {
        // If we are in the same Y position and are not flapping, we are on solid ground.
        if (lastY == y && !flapping){
            jumping = false;
        }
        // Allow flapping once the last flap wears off.
        if (dy > 0){
            flapping = false;
        }

        // Apply gravity
        dy += 1;

        // Do not allow super flapping
        if (dy < -5){
            dy = -5;
        }
        // Fall slower if we have jumped
        else if (dy > 5 && jumping){
            dy = 5;
        }
        // Otherwise cap gravity
        else if (dy > 15 && !jumping){
            dy = 15;
        }

        // Do not fall through platforms
        if (y + dy > platformY - getHeight()){
            setY(platformY - getHeight());
            setDy(0);
        }
        else{
            y += dy;
        }

        // Do not leave top of map
        if (y < 0){
            y = 0;
        }

        // Remember where we used to be
        lastY = y;
        lastDY = dy;

        super.update();


    }
}
