package ru.vagrant_ai.questionmarkgame.main;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import ru.vagrant_ai.questionmarkgame.obj.Background;
import ru.vagrant_ai.questionmarkgame.obj.BrokenObject;
import ru.vagrant_ai.questionmarkgame.obj.Dispenser;
import ru.vagrant_ai.questionmarkgame.obj.Elements;
import ru.vagrant_ai.questionmarkgame.obj.GUI;
import ru.vagrant_ai.questionmarkgame.obj.Gun;
import ru.vagrant_ai.questionmarkgame.obj.HPBar;
import ru.vagrant_ai.questionmarkgame.obj.Pause;
import ru.vagrant_ai.questionmarkgame.obj.Player;
import ru.vagrant_ai.questionmarkgame.util.DebugConsole;
import ru.vagrant_ai.questionmarkgame.util.MonsterHandler;
import ru.vagrant_ai.questionmarkgame.util.MonsterWaveController;
import ru.vagrant_ai.questionmarkgame.util.Particle;
import ru.vagrant_ai.questionmarkgame.util.SceneHandler;
import ru.vagrant_ai.questionmarkgame.util.Sound;
import ru.vagrant_ai.questionmarkgame.util.Util;
import ru.vagrant_ai.questionmarkgame.util.list.SCENE;
public class GameplayState extends BasicGameState
{
int stateID = 1;
GameplayState( int stateID )
{
this.stateID = stateID;
}
@Override
public int getID()
{
return stateID;
}
/* INIT STATION */
public static Rectangle border;
public final static short ground_level = 368;
public final static short room_level = ground_level+169;
public final static short ramp_level = ground_level-89;
public static Dispenser dispenser;
public static Player player;
public static HPBar hp;
static MonsterHandler monster;
public static MonsterWaveController monster_controller;
public static GUI gui;
public static BrokenObject broken_object;
static Elements elements;
public static Background background;
public static SceneHandler scene_handler;
public static Sound sound_handler;
static Pause pause;
public void init(GameContainer gc, StateBasedGame sbg)
{
pause = new Pause();
gui = new GUI();
broken_object = new BrokenObject();
player = new Player();
player.gun = new Gun();
hp = new HPBar();
monster = new MonsterHandler(); //not a single monster, but a class controlling randomly generated monsters
monster_controller = new MonsterWaveController();
dispenser = new Dispenser();
elements = new Elements();
border = new Rectangle(-5,-5,Game.app_x+5,ground_level+47); //Bullet rectangle border
background = new Background();
scene_handler = new SceneHandler();
sound_handler = new Sound();
if (!Game.util_instantswitch)
scene_handler.activateScene(SCENE.INTRO);
//scene_handler.activateScene(SCENE.SHOP);
}
/* RENDER'n'UPDATE STATION */
public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
{
if (scene_handler.is_playing)
scene_handler.render(g);
else
render_world(g);
if (hp.getHP() == 0)
render_death_screen(sbg, g);
else if (broken_object.repaired() == 100)
render_win_screen(sbg, g);
else if (Pause.util_state_paused)
Pause.render(g);
if (DebugConsole.checkDebug())
DebugConsole.render(g);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta)
{
Util.MouseUpdate();
Pause.update();
update_util();
sound_handler.update();
if (!gamePaused() && util_death == 0 && util_win == 0)
{
broken_object.update(gc);
dispenser.update();
player.update(gc);
monster.update();
hp.update();
monster_controller.update();
gui.update();
elements.update();
}
}
public void enter(GameContainer container, StateBasedGame stateBasedGame)
{
init(container, stateBasedGame); //reinit
util_death = 0;
util_win = 0;
Particle.particle_array.clear();
}
/* MAIN RENDER METHODS */
public static void render_world(Graphics g)
{
background.render_1st_layer(g); //+gui.render_1nd_layer(g); //+Particle.update_and_render_2nd_layer(g);
/*active elements*/
broken_object.render(g);
Particle.update_and_render(g); //thorns and light
player.render(g); //player (inserted here, to save layers)
monster.render(g); //render+update
elements.render(g);
background.render_2nd_layer(g);
dispenser.render(g);
if (hp.getHP() > 0) hp.render(g);
gui.render_2nd_layer(g);
monster_controller.render(g);
Particle.update_and_render_3nd_layer(g); //any other particles
}
/* RENDER METHODS */
static short util_death;
private void render_death_screen(StateBasedGame sbg, Graphics g)
{
util_death++;
if (util_death >= 95) sbg.enterState(Game.GAMEOVERSTATE);
g.setColor(new Color(255,0,0,util_death*255/95));
g.fillRect(0,0,Game.app_x,Game.app_y);
}
static short util_win;
private void render_win_screen(StateBasedGame sbg, Graphics g)
{
util_win++;
if (util_win >= 95) sbg.enterState(Game.GAMEWINSTATE);
g.setColor(new Color(255,255,0,util_win*255/95));
g.fillRect(0,0,Game.app_x,Game.app_y);
}
/* MAIN UPDATE METHODS */
/* UTIL UPDATE */
private void update_util()
{
Input input = Game.app.getInput();
if (input.isKeyPressed(Input.KEY_GRAVE))
DebugConsole.setConsole();
}
/* UTIL METHODS */
public static boolean gamePaused()
{
return (Pause.util_state_paused || scene_handler.is_playing);
}
public static boolean gameStateChanging()
{
return (util_death+util_win>0);
}
}