package net.josh.minecraftplatformer;

import java.applet.*;
import java.awt.*;

import javax.swing.*;

public class Component extends Applet implements Runnable {													//calls the class
	private static final long serialVersionUID = 1L;		
	
	private static int pixelSize = 2;																		//declares the pixel size.
	
	public static int moveFromBorder = 0;
	public static double sX = moveFromBorder, sY = moveFromBorder;											//declares the variables for side scrolling.
	public static double dir = 0;

	
	public static Dimension size = new Dimension(700, 560);													//declares screen size.
	public static Dimension pixel = new Dimension(size.width / pixelSize, size.height / pixelSize);			//sets the pixel size.
	
	public static String name = "Minecraft 2D Platformer.";													//sets the variable for the name
	
	public static boolean isRunning = false;																//boolean for game running.
	public static boolean isMoving = false;																	//boolean for whether the character is moving.
	public static boolean isJumping = false;																//boolean for whether the character is jumping
	
	public static Level level;																				//creates the level.
	public static Character character;																		//creates the character
	
	private Image screen;																					//makes new Image variable called screen.
	
	public Component() {																					//Constructor for the Component class.
		setPreferredSize(size);																				//sets the size for the java applet.
		
		addKeyListener(new Listening());																	//starts a new key listener.
	}
	
	public void start() {																					//Start method for game loop and defining objects
		//Defining objects etc.
		new Tile(); //loading images.																		//makes a new tile to load images.	
		level = new Level();																				//new level
		character = new Character(Tile.tileSize, Tile.tileSize * 2);										//new character
		
		//Starting game loop.
		isRunning = true;																					//sets is running to true
		new Thread(this).start();																			//game loop I think	
	}
	
	public void stop() {																					//method to stop the game.
		isRunning = false;																					//sets isRunning to false
	}
	
	public static void main(String args[]){
		Component component = new Component();																//makes a new Component
		
		JFrame frame = new JFrame();																		//calls a new frame
		frame.add(component);																				//adds component to frame
		frame.setVisible(true);																				//makes the frame visible
		frame.pack();																						//sets the window to the proffered size
		frame.setTitle(name);																				//adds the name
		frame.setResizable(false);																			//stops it being resizable.
		frame.setLocationRelativeTo(null);																	//puts the window in the middle of the screen.
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);												//closes the program when the window is closed.

		
		component.start();																					//calls component.start()
	}

	public void tick() {																					//starts the refresh
		level.tick();																						//calls the level tick
		character.tick();																					//calls the character tick
	}
	
	public void render() {																					//renders graphics
		Graphics g = screen.getGraphics();
		
		//Drawing things.
		g.setColor(new Color(110, 110 ,255));
		g.fillRect(0, 0, pixel.width, pixel.height);
		
		level.render(g);
		character.render(g);
		
		g = getGraphics();

		g.drawImage(screen, 0, 0, size.width, size.height, 0, 0, pixel.width, pixel.height, null);			//creates the screen
		g.dispose();																						//disposes of the screen freeing up system resources
	}
	
	/*
	 *	Game Loop!
	 * 
	 */
	
	
	public void run() {
		screen = createVolatileImage(pixel.width, pixel.height);											//creates performance friendly image called screen
		
		while(isRunning) {																					//ticks and renders as long as the game is running
			tick();
			render();
					
			
			try {																							//makes the loop slower
				Thread.sleep(5);
			} catch(Exception e) { }
		}
	}
}
