
package Tutorial1;

import jplay.GameImage;
import jplay.Keyboard;
import jplay.Physics;
import jplay.Sprite;
import jplay.Window;
import org.jbox2d.common.Vec2;

/**
 *
 * @author Leandro Emiliano Guimarães
 * ---UFF---
 * Computer Science
 */
public class Tutorial1 {
    public Tutorial1(){

    Window win = new Window(800,600);
    GameImage fundo = new GameImage("fundot1.png");

    Physics fisica = new Physics();
    fisica.createWorld(800, 600);

   
    Sprite quadrado = new Sprite("square.png");
    
    fisica.createBodyFromSprite(quadrado, false);

    quadrado.setX(400);
    quadrado.setY(0);

    Sprite ground = new Sprite("ground.png");
    fisica.createBodyFromSprite(ground, true);

    ground.setY(500);
    ground.setFriction(0);

   
    while(true){
        
        fundo.draw();
               
        if(quadrado.getX()>0)
            quadrado.applyForceXFromKeyboardLeft(Keyboard.LEFT_KEY,  1, Keyboard.DETECT_EVERY_PRESS);
        else
             quadrado.cancelForcesAndSetBoundsX(0, true);
        if(quadrado.getX()+quadrado.width<800)
            quadrado.applyForceXFromKeyboardRight(Keyboard.RIGHT_KEY, 1, Keyboard.DETECT_EVERY_PRESS);
        else
            quadrado.cancelForcesAndSetBoundsX(800,false);

        // Outra opção :

        quadrado.applyForceXFromKeyboard(Keyboard.LEFT_KEY, Keyboard.RIGHT_KEY, 1, Keyboard.DETECT_EVERY_PRESS,true);

        // O mesmo poderia ser feito no eixo Y(limitar manualmente os limites).
        quadrado.applyForceYFromKeyboard(Keyboard.UP_KEY, Keyboard.DOWN_KEY, 50, Keyboard.DETECT_EVERY_PRESS,true);

        fisica.update();
        win.update();
        win.delay(12);
        
    }
    }
    public static void main(String[] args) {
        Tutorial1 t1 = new Tutorial1();
    }
}
