package net.liquidpineapple.pang.objects;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import net.liquidpineapple.pang.InputHandler;

import java.awt.event.KeyEvent;

/**
 * @author Jurriaan Den Toonder<jurriaan.toonder@liquidpineapple.net>
 * @date 2016/09/06.
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Slf4j
public class Player extends GameObject {

    private static final String textureLocation = "/sprites/player/p1_front.png";

    private enum PlayerMovement {
        LEFT_DIRECTION(-1),
        RIGHT_DIRECTION(1),
        NO_MOVEMENT(0);

        private final int dx;
        PlayerMovement(int dx) {
            this.dx = dx;
        }
    }
    private final int maxX;
    private int dx;

    public Player(int startX, int startY, int maxX) {
        super(textureLocation, startX, startY);
        this.maxX = maxX;
    }

    public void move() {
        xPos += dx;
        if (xPos < 1) {
            xPos = 1;
        }
        if (xPos > maxX) {
            xPos = maxX;
        }
    }

    @Override
    public void doUpdate() {
        super.doUpdate();

        if (InputHandler.isKeyPressed(KeyEvent.VK_A)) {
            dx = PlayerMovement.LEFT_DIRECTION.dx;
            move();
        } else if (InputHandler.isKeyPressed(KeyEvent.VK_D)) {
            dx = PlayerMovement.RIGHT_DIRECTION.dx;
            move();
        } else {
            dx = PlayerMovement.NO_MOVEMENT.dx;
        }
    }
}

@@ -123,16 +123,61 @@
 .Slf4j;%0A
+import net.liquidpineapple.pang.Application;%0A
 import n
@@ -1040,21 +1040,148 @@
    i
-f (xPos %3E max
+nt boardWidth = Application.getBoard().getWidth();%0A        int playerMaxPosX = boardWidth - this.getWidth();%0A        if (xPos %3E playerMaxPos
 X) %7B
@@ -1200,19 +1200,28 @@
  xPos = 
-max
+playerMaxPos
 X;%0A     

Stop player at end of board, fixes #45