package Hexel.rendering.overlays;
import java.awt.Color;
import java.awt.Graphics2D;
import java.util.Queue;
import javax.media.opengl.GLAutoDrawable;
import Hexel.Config;
import Hexel.Engine;
import Hexel.Hexel;
import Hexel.PCInput;
import Hexel.Session;
import Hexel.Stage;
import Hexel.blocks.types.BlockDirt;
import Hexel.blocks.types.BlockGlass;
import Hexel.blocks.types.BlockLeaf;
import Hexel.blocks.types.BlockStone;
import Hexel.blocks.types.BlockWater;
import Hexel.blocks.types.BlockWood;
import Hexel.math.Vector2i;
import Hexel.things.types.Controllable;
import Hexel.things.types.Player;
import Hexel.things.types.Wanderer;
import Hexel.things.types.Zombie;
import Hexel.blocks.types.BlockDebug;
import com.jogamp.opengl.util.awt.Overlay;
public class GameOverlay {
private Color TRANSPARENT_COLOR = new Color(0.0f, 0.0f, 0.0f, 0.0f);
private Overlay overlay;
private Config config = new Config();
private Player player;
private Stage stage;
private Menu startMenu;
private Menu loadMenu;
private Menu inGameMenu;
public HUD hud;
private Engine engine;
private int w;
private int h;
public GameOverlay(final Engine engine){
this.engine = engine;
MenuItem[] startMenuItems = new MenuItem[]{
new MenuItem("New Game", new Runnable(){
@Override
public void run() {
Hexel.setSession(new Session());
double x = 0;
double y = 0;
Vector2i tmp = new Vector2i();
double z = Math.max(engine.chunks.chunkGenerator.tm.getHeightMapAt((int)x, (int)y, tmp)+16, 16);
player = new Player(x, y, z, 0, 0, engine.getThingBridge());
engine.setControllable(player);
player.blockToPlace = BlockDebug.class;
engine.setCamera(player);
engine.addThing(player);
// Zombie zombie = new Zombie(x, y, z, engine.getThingBridge());
// engine.addThing(zombie);
engine.setStage(Stage.INGAME);
}
}),
new MenuItem("Load Game", new Runnable(){
@Override
public void run(){
loadMenu = new Menu(LoadGameMenuItems.getLoadGameMenuItems(engine, new Runnable() {
@Override
public void run() {
engine.setStage(Stage.START_MENU);
}
}));
engine.setStage(Stage.LOAD_MENU);
}
}),
// new MenuItem("Settings"),
new MenuItem("About", null, new Runnable(){
@Override
public void run(){
startMenu.enableSecondary(new String[]{
"Version: " + Hexel.version,
"https://github.com/es92/Hexel"
});
}
}, new Runnable(){
@Override
public void run(){
startMenu.disableSecondary();
}
}),
new MenuItem("Quit", new Runnable(){
@Override
public void run() {
engine.quit();
}
})
};
MenuItem[] inGameMenuItems = new MenuItem[]{
new MenuItem("Quit", new Runnable(){
@Override
public void run() {
engine.quit();
}
}),
new MenuItem("Resume", new Runnable(){
@Override
public void run() {
engine.setStage(Stage.INGAME);
}
}),
};
startMenu = new Menu(startMenuItems);
inGameMenu = new Menu(inGameMenuItems);
hud = new HUD(engine);
}
public void setStage(Stage stage) {
this.stage = stage;
this.updateSize(w, h);
}
public void init(GLAutoDrawable drawable) {
this.overlay = new Overlay(drawable);
}
public void updateSize(int w, int h) {
this.w = w;
this.h = h;
if (this.stage == Stage.START_MENU){
this.startMenu.updateSize(w, h);
}
else if (this.stage == Stage.INGAME){
this.hud.updateSize(w, h);
}
else if (this.stage == Stage.LOAD_MENU){
this.loadMenu.updateSize(w, h);
}
else if (this.stage == Stage.INGAME_MENU){
this.inGameMenu.updateSize(w, h);
}
this.display();
}
public void setControllable(Controllable controllable) {
if (controllable instanceof Player)
this.hud.setPlayer((Player)controllable);
}
public void sendInputToCurrentOverlay(PCInput input){
if (this.stage == Stage.START_MENU){
this.startMenu.sendInput(input);
}
else if (this.stage == Stage.LOAD_MENU){
this.loadMenu.sendInput(input);
}
else if (this.stage == Stage.INGAME_MENU){
Queue<Integer> extra = this.inGameMenu.sendInput(input);
while (!extra.isEmpty()){
int keyTapped = extra.poll();
if (config.controls.get("pause").contains(keyTapped)){
engine.setStage(Stage.INGAME);
}
}
}
}
public void display() {
if (this.overlay == null)
return;
Graphics2D page = this.overlay.createGraphics();
page.setBackground(this.TRANSPARENT_COLOR);
page.clearRect(0, 0, this.w, this.h);
if (this.stage == Stage.START_MENU){
this.startMenu.draw(page);
}
else if (this.stage == Stage.INGAME){
this.hud.draw(page);
}
else if (this.stage == Stage.LOAD_MENU){
this.loadMenu.draw(page);
}
else if (this.stage == Stage.INGAME_MENU){
this.inGameMenu.draw(page);
}
this.overlay.markDirty(0, 0, this.w, this.h);
this.overlay.drawAll();
page.dispose();
}
}