package main;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Player {
private Rectangle bounding;
private float ply_x;
private float ply_y;
private float f_dx;
private float f_dy;
private float f_gravity;
private float f_counter;
private float neededTime;
private boolean b_air;
private boolean b_airimg;
private boolean letzterichtung;
private boolean falling;
private boolean jumping;
private boolean b_kill;
private int richtung;
private BufferedImage playerImage;
private BufferedImage[] laufr;
private BufferedImage[] laufl;
public Player() {
laufr = new BufferedImage[3];
laufl = new BufferedImage[3];
ply_x = 0;
ply_y = 640;
f_counter = 0;
neededTime = 10;
f_dx = 5;
f_dy = -9;
f_gravity = 0.4f;
b_air = false;
b_airimg = false;
letzterichtung = true;
falling = false;
jumping = false;
b_kill = false;
richtung = 0;
try {
playerImage = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(0, 0, 64, 64);
laufr[0] = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(64, 0, 64, 64);
laufr[1] = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(128, 0, 64, 64);
laufr[2] = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(192, 0, 64, 64);
laufl[0] = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(64, 64, 64, 64);
laufl[1] = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(128, 64, 64, 64);
laufl[2] = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(192, 64, 64, 64);
} catch (IOException e) {e.printStackTrace();}
bounding = new Rectangle((int) ply_x, (int) ply_y, playerImage.getWidth(), playerImage.getHeight());
}
public void update() {
if (b_kill){
return;
}
for(int i = 0; i < Frame.teilesArray.size();i++){ //teilesArray is a Array with all tiles from the map (every block has 64x64 px)
if(Frame.teilesArray.get(i).getimgInt()== 0 || Frame.teilesArray.get(i).getimgInt()== 1){ // means that only the two special blocks will check of collision not all blocks only this blocks
}
}
}
if(getBounding().x >= 400){
ply_x = 399;
}
if(getBounding().x <=0 ){
ply_x = 0;
}
if(keyCheck.keysCheck(KeyEvent.VK_SHIFT)){
f_dx = 7;
}else {f_dx = 5; }
if(keyCheck.keysCheck(KeyEvent.VK_A)){
ply_x -= f_dx;
if(b_air == false){
if(f_counter >= neededTime*1.5f){
f_counter = 0;
}
if(f_counter == 0) {
playerImage = laufl[0];
f_counter++;
}else if(f_counter == 5){
playerImage = laufl[1];
f_counter++;
}else if(f_counter == 10){
playerImage = laufl[2];
f_counter++;
}else {
f_counter++;
}
richtung = 2;
letzterichtung = false;
}
}
if(keyCheck.keysCheck(KeyEvent.VK_D)){
ply_x += f_dx;
if(b_air == false){
if(f_counter >= neededTime*1.5f){
f_counter = 0;
}
if(f_counter == 0) {
playerImage = laufr[0];
f_counter++;
}else if(f_counter == 5){
playerImage = laufr[1];
f_counter++;
}else if(f_counter == 10){
playerImage = laufr[2];
f_counter++;
}else {
f_counter++;
}
}
richtung = 1;
letzterichtung = true;
}
if(keyCheck.keysCheck(KeyEvent.VK_D)==false && keyCheck.keysCheck(KeyEvent.VK_A)==false){
if(richtung == 1 || letzterichtung == true && b_air == false && b_airimg ==false){
try {
playerImage = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(0, 0, 64, 64);
} catch (IOException e) {e.printStackTrace();}
}
else if(richtung == 2 || letzterichtung == false && b_air == false && b_airimg ==false){
try {
playerImage = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(0, 64, 64, 64);
} catch (IOException e) {e.printStackTrace();}
}
richtung = 0;
}
if(keyCheck.keysCheck(KeyEvent.VK_SPACE)){
b_air = true;
}
if(b_air ==true){
if(b_airimg == false){
if (letzterichtung == true)
{
try {
playerImage = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(256, 0, 64, 64);
} catch (IOException e) {e.printStackTrace();}
}else {
try {
playerImage = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic\\player1.png")).getSubimage(256, 64, 64, 64);
} catch (IOException e) {e.printStackTrace();}
}
b_airimg = true;
}
if(f_dy < 0 && f_dy > -9){
jumping = true;
falling = false;
}else jumping = false;
if(f_dy >= 0){
jumping = false;
falling = true;
}else falling = false;
f_dy += f_gravity;
ply_y += f_dy;
//System.out.println("falling= "+falling+", jumping= "+jumping+", f_dy= "+f_dy);
}
if (ply_y > 800){
b_kill = true;
}
bounding.x = (int) ply_x;
bounding.y = (int) ply_y;
}
public Rectangle getBounding() {
return bounding;
}
public boolean getPlayerKill() {
return b_kill;
}
public BufferedImage getPlayerimage() {
return playerImage;
}
public void setPlayerLocation(int ply_x, int ply_y){
this.ply_x = ply_x;
this.ply_y = ply_y;
bounding.x = (int) this.ply_x;
bounding.y = (int) this.ply_y;
System.out.println(""+this.ply_x+" "+this.ply_y);
}
}