package com.pqbyte.coherence;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.scenes.scene2d.Actor;

/**
 * The player being controlled.
 */
public class Player extends Actor {
  private static final int PLAYER_SIZE = 2;
  private static final float STEP_SIZE = 0.5f;

  private boolean goingLeft = false;
  private boolean goingRight = false;
  private boolean goingUp = false;
  private boolean goingDown = false;

  private Sprite sprite;

  public Player(Texture texture, float startX, float startY) {
    sprite = new Sprite(texture);
    setBounds(startX, startY, PLAYER_SIZE, PLAYER_SIZE);
  }

  @Override
  public void act(float delta) {
    float horizontal = 0;
    float vertical = 0;

    if (goingLeft && getX() > 0) {
      horizontal -= STEP_SIZE;
    }
    if (goingRight && getX() + getWidth() < Constants.WORLD_WIDTH) {
      horizontal += STEP_SIZE;
    }
    if (goingUp && getY() + getHeight() < Constants.WORLD_HEIGHT) {
      vertical += STEP_SIZE;
    }
    if (goingDown && getY() > 0) {
      vertical -= STEP_SIZE;
    }

    setPosition(getX() + horizontal, getY() + vertical);

    getStage().getCamera().position.set(
        getX() + getWidth() / 2f,
        getY() + getHeight() / 2f,
        0
    );
  }

  @Override
  public void draw(Batch batch, float parentAlpha) {
    batch.draw(sprite, getX(), getY(), getWidth(), getHeight());
  }

  public void setGoingLeft(boolean going) {
    goingLeft = going;
  }

  public void setGoingRight(boolean going) {
    goingRight = going;
  }

  public void setGoingUp(boolean going) {
    goingUp = going;
  }

  public void setGoingDown(boolean going) {
    goingDown = going;
  }
}

@@ -155,16 +155,292 @@
 Sprite;%0A
+import com.badlogic.gdx.math.Vector2;%0Aimport com.badlogic.gdx.physics.box2d.Body;%0Aimport com.badlogic.gdx.physics.box2d.BodyDef;%0Aimport com.badlogic.gdx.physics.box2d.FixtureDef;%0Aimport com.badlogic.gdx.physics.box2d.PolygonShape;%0Aimport com.badlogic.gdx.physics.box2d.World;%0A
 import c
@@ -643,12 +643,10 @@
 E = 
+2
 0
-.5f
 ;%0A%0A 
@@ -816,16 +816,37 @@
  sprite;
+%0A  private Body body;
 %0A%0A  publ
@@ -898,16 +898,29 @@
 t startY
+, World world
 ) %7B%0A    
@@ -1006,16 +1006,52 @@
 _SIZE);%0A
+    body = createPlayerBody(world);%0A
   %7D%0A%0A  @
@@ -1164,22 +1164,8 @@
 Left
- && getX() %3E 0
 ) %7B%0A
@@ -1223,55 +1223,8 @@
 ight
- && getX() + getWidth() %3C Constants.WORLD_WIDTH
 ) %7B%0A
@@ -1279,57 +1279,8 @@
 ngUp
- && getY() + getHeight() %3C Constants.WORLD_HEIGHT
 ) %7B%0A
@@ -1335,22 +1335,8 @@
 Down
- && getY() %3E 0
 ) %7B%0A
@@ -1379,58 +1379,189 @@
     
-setPosition(getX() + horizontal, getY() + vertical
+body.setLinearVelocity(horizontal, vertical);%0A    Vector2 bodyPos = body.getPosition();%0A    setPosition(%0A        bodyPos.x - getWidth() / 2,%0A        bodyPos.y - getHeight() / 2%0A    
 );%0A%0A
@@ -2111,11 +2111,849 @@
 ing;%0A  %7D
+%0A%0A  /**%0A   * Creates the Box2D physical body of the player.%0A   *%0A   * @param world The world object.%0A   * @return The player's body.%0A   */%0A  private Body createPlayerBody(World world) %7B%0A    BodyDef bodyDef = new BodyDef();%0A    bodyDef.type = BodyDef.BodyType.DynamicBody;%0A    bodyDef.position.set(%0A        getX() + getWidth() / 2,%0A        getY() + getHeight() / 2%0A    );%0A    Body body = world.createBody(bodyDef);%0A    PolygonShape shape = new PolygonShape();%0A    shape.setAsBox(%0A        getWidth() / 2,%0A        getHeight() / 2%0A    );%0A%0A    FixtureDef fixtureDef = new FixtureDef();%0A    fixtureDef.shape = shape;%0A    fixtureDef.density = 1;%0A    fixtureDef.filter.categoryBits = Constants.PHYSICS_ENTITY;%0A    fixtureDef.filter.maskBits = Constants.WORLD_ENTITY;%0A%0A    body.createFixture(fixtureDef);%0A    shape.dispose();%0A%0A    return body;%0A  %7D
 %0A%7D%0A

Add Box2D physical body for Player