package Game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import Blocks.Block;
import Blocks.BlockStone;
import Entity.EntityPlayer;
import NewUniverseMain.Keyboard;
import NewUniverseMain.Textures;
import NewUniverseMain.main;
import Tiles.Tile;
import Tiles.TileGrass;
public class World {
public static float X = (int) (main.width * 0.5);
public static float Y = (int) (main.height * 0.5);
public float xpos;
public float ypos;
private int[] keyfield = {-1,-1};
private Tile[][] tiles;
private BufferedImage[] img = {Textures.player};
private EntityPlayer player;
private ArrayList<RenderObject> ro = new ArrayList<>();
private Block testblock = new BlockStone(new Location(3.5F, 3.5F));
public World(Tile[][] tiles, ArrayList<RenderObject> ro, float startxpos, float startypos){
xpos = startxpos;
ypos = startypos;
Tile[][] tiles00 = new Tile[main.WORLD_SIZE][main.WORLD_SIZE];
for(int x = 0; x < main.WORLD_SIZE; x++){
for(int y = 0; y < main.WORLD_SIZE; y++){
tiles00[x][y] = new TileGrass();
}
}
this.ro.add(new BlockStone(new Location(3.5F, 3.5F)));
this.tiles = tiles00;
Location loc = new Location(xpos, ypos);
HitBox hitbox = new HitBox(loc, img[0].getWidth() / Tile.TILE_SIZE, img[0].getHeight() / Tile.TILE_SIZE, 10, true);
player = new EntityPlayer(img, 1, 1, loc, hitbox);
this.ro.add(player);
}
public void draw(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0, 0, main.width, main.height);
int ix = 0;
int iy = 0;
for(int x = 0; x < main.WORLD_SIZE; x++){
ix++;
for(int y = 0; y < main.WORLD_SIZE; y++){
iy++;
tiles[x][y].draw(g, (int)(X - xpos * Tile.TILE_SIZE + ix * Tile.TILE_SIZE), (int)(Y - ypos * Tile.TILE_SIZE + iy * Tile.TILE_SIZE));
}
iy = 0;
}
RenderObject[] objects = new RenderObject[ro.size()];
ArrayList<RenderObject> ro2 = ro;
int counter = 0;
for(int i2 = 1; i2 < ro.size(); i2++){
int small = 0;
for(int i = 1; i < ro2.size(); i++){
System.out.println(small + ": " + ro2.get(small).getLocation() + " " + player.getLocation().getX());
System.out.println(testblock.getLocation());
if(ro2.get(small).getLocation().getY() > ro2.get(i).getLocation().getY()){
small = i;
}
}
objects[counter] = ro2.get(small);
ro2.remove(small);
counter++;
}
for(int i = 0; i < objects.length; i++){
if(objects[i] != null) objects[i].draw(g);
}
}
public void update(float tslf){
if(Keyboard.isKeyPressed(KeyEvent.VK_W)){
if(keyfield[0] != 0 && keyfield[1] != 0){
keyfield[1] = keyfield[0];
keyfield[0] = 0;
}
}
if(Keyboard.isKeyPressed(KeyEvent.VK_S)){
if(keyfield[0] != 2 && keyfield[1] != 2){
keyfield[1] = keyfield[0];
keyfield[0] = 2;
}
}
if(Keyboard.isKeyPressed(KeyEvent.VK_A)){
if(keyfield[0] != 3 && keyfield[1] != 3){
keyfield[1] = keyfield[0];
keyfield[0] = 3;
}
}
if(Keyboard.isKeyPressed(KeyEvent.VK_D)){
if(keyfield[0] != 1 && keyfield[1] != 1){
keyfield[1] = keyfield[0];
keyfield[0] = 1;
}
}
if(!Keyboard.isKeyPressed(KeyEvent.VK_W)){
if(keyfield[0] == 0) keyfield[0] = -1;
if(keyfield[1] == 0) keyfield[1] = -1;
}
if(!Keyboard.isKeyPressed(KeyEvent.VK_D)){
if(keyfield[0] == 1) keyfield[0] = -1;
if(keyfield[1] == 1) keyfield[1] = -1;
}
if(!Keyboard.isKeyPressed(KeyEvent.VK_S)){
if(keyfield[0] == 2) keyfield[0] = -1;
if(keyfield[1] == 2) keyfield[1] = -1;
}
if(!Keyboard.isKeyPressed(KeyEvent.VK_A)){
if(keyfield[0] == 3) keyfield[0] = -1;
if(keyfield[1] == 3) keyfield[1] = -1;
}
if(keyfield[0] == -1){
keyfield[0] = keyfield[1];
keyfield[1] = -1;
}
for(int i = 0; i < ro.size(); i++){
}
player.move(keyfield[0], tslf);
player.update();
}
public void move(int x, int y){
X += x;
Y += y;
}
}