package aitgame.tilegame;
import java.awt.*;
import java.util.LinkedList;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.sound.sampled.AudioFormat;
import aitgame.diploma.GradeManager;
import aitgame.game.GameCore;
import aitgame.graphics.*;
import aitgame.input.*;
import aitgame.sound.*;
import aitgame.tilegame.sprites.*;
import aitgame.menu.*;
import aitgame.tilegame.ingameDisplay.DisplayContacts;
import aitgame.tilegame.minigames.*;
import aitgame.tilegame.minigames.calcgame.CalcGame;
import aitgame.tilegame.minigames.othello.Othello;
import aitgame.tilegame.minigames.historygame.HistoryGame;
/**
GameManager manages all parts of the game.
*/
public class GameManager extends GameCore {
public static void main(String[] args) {
new GameManager().run();
}
// uncompressed, 44100Hz, 16-bit, mono, signed, little-endian
private static final AudioFormat PLAYBACK_FORMAT = new AudioFormat(44100, 16, 1, true, false);
private static final int DRUM_TRACK = 1;
//public static final float GRAVITY = 0.002f;
public static final float GRAVITY = 0;
//private Point pointCache = new Point();
private TileMap map;
private MidiPlayer midiPlayer;
private SoundManager soundManager;
private ResourceManager resourceManager;
private InputManager inputManager;
private TileMapRenderer renderer;
private GradeManager gradeManager = new GradeManager();;
private CollisionManager collisionManager;
private float resentVelocityTester = 0;
private boolean paused;
private Othello othello;
private GradeShower gradeShower;
public void init() {
super.init();
// set up input manager
inputManager = new InputManager(screen.getFullScreenWindow());
// start resource manager
resourceManager = new ResourceManager(screen.getFullScreenWindow().getGraphicsConfiguration());
// load resources
renderer = new TileMapRenderer();
//load CollisionManager
collisionManager = new CollisionManager();
// load first map
map = resourceManager.loadNextMap();
//load Othello
othello = new Othello(inputManager, screen);
// load sounds
soundManager = new SoundManager(PLAYBACK_FORMAT);
// start music
midiPlayer = new MidiPlayer();
Sequence sequence = midiPlayer.getSequence(this.getClass().getResourceAsStream("../../sounds/music.midi"));
midiPlayer.play(sequence, true);
toggleDrumPlayback();
paused = false;
}
/**
Closes any resurces used by the GameManager.
*/
public void stop() {
super.stop();
midiPlayer.close();
soundManager.close();
}
/**
* Checks the input from the player.
*/
private void checkInput(long elapsedTime) {
Player player = (Player)map.getPlayer();
if (player.isAlive()) {
float velocityX = 0;
float velocityY = 0;
if (inputManager.moveLeft.isPressed()) {
velocityX-=player.getMaxSpeed();
}
if (inputManager.moveRight.isPressed()) {
velocityX+=player.getMaxSpeed();
}
if (inputManager.moveUp.isPressed()) {
velocityY-=player.getMaxSpeed();
}
if (inputManager.moveDown.isPressed()) {
velocityY+=player.getMaxSpeed();
}
if(inputManager.enter.isPressed()){
}
if(inputManager.oButton.isPressed()){
}
if (inputManager.action.isPressed()) {
//renderer.addTextToScreen("Action has been pressed!");
if(player.getConnectionList().size() > 0){
//renderer.addTextToScreen("There is a connection");
displayChoices();
}
}
player.setVelocityX(velocityX);
player.setVelocityY(velocityY);
if(resentVelocityTester != (velocityY*velocityY + velocityX*velocityX)){
//renderer.addTextToScreen("velocityX: " + velocityX + "velocityY: " + velocityY);
resentVelocityTester = (velocityY*velocityY + velocityX*velocityX);
}
}
}
/**
* Checks the inputs from the player, this is done even if the game is in pause.
*/
private void checkSystemInput() {
if (inputManager.pause.isPressed()) {
setPaused(!isPaused());
}
if (inputManager.exit.isPressed()) {
stop();
}
}
/**
* Displays the mathgame.
*/
public void displayPopupGame() {
CalcGame calcGame = new CalcGame(screen.getFullScreenWindow(), inputManager);
calcGame.show(screen);
//load GradeShower
gradeShower = new GradeShower(calcGame, inputManager, gradeManager);
displayGradeShower();
calcGame.shutDown();
}
public void displayGradeShower() {
gradeShower.show(screen);
}
/**
* Displays the Historygame to the screen.
*/
public void displayPopupHistoriaGame(){
HistoryGame historyGame = new HistoryGame(screen.getFullScreenWindow(), inputManager);
historyGame.show(screen);
//load GradeShower
gradeShower = new GradeShower(historyGame, inputManager, gradeManager);
displayGradeShower();
historyGame.shutDown();
//historyGame.dispose();
}
/**
* If the player has conection to objects the option that the player then has
* is displayed and the player can make his/her option.
*/
public void displayChoices() {
renderer.addTextToScreen("Making the shit!");
LinkedList contact = ((Creature)map.getPlayer()).getConnectionList();
DisplayContacts contacts = new DisplayContacts(inputManager, contact);
contacts.show(screen);
int contactMenuChoice = contacts.getMenuOption();
if(contactMenuChoice < contact.size()){
runGame(((Creature)contact.get(contactMenuChoice)).getGame());
}
}
/**
* Runs the game of the string.
*/
private void runGame(String game) {
if(game.equals("Matematik")){
displayPopupGame();
}else if (game.equals("Othello")) {
othello.run();
}else if (game.equals("Historia")){
displayPopupHistoriaGame();
}
}
public void draw(Graphics2D g) {
renderer.draw(g, map, screen.getWidth(), screen.getHeight());
}
/**
Gets the current map.
*/
public TileMap getMap() {
return map;
}
/**
Turns on/off drum playback in the midi music (track 1).
*/
public void toggleDrumPlayback() {
Sequencer sequencer = midiPlayer.getSequencer();
if (sequencer != null) {
sequencer.setTrackMute(DRUM_TRACK,
!sequencer.getTrackMute(DRUM_TRACK));
}
}
/**
Updates Animation, position, and velocity of all Sprites
in the current map.
*/
public void update(long elapsedTime) {
checkSystemInput();
if (!isPaused()) {
// get keyboard/mouse input
checkInput(elapsedTime);
//
checkCollision(elapsedTime);
}
}
/**
* checks the collisions.
*/
private void checkCollision(long elapsedTime) {
LinkedList<Sprite> movingSprites = new LinkedList();
LinkedList<Sprite> collidingSprites = new LinkedList();
LinkedList<Sprite> itemSprites = new LinkedList<Sprite>();
movingSprites.add((Creature) map.getPlayer());
collidingSprites = map.getSprites(1);
collisionManager.handleColisionsAndMoves(movingSprites, collidingSprites,
itemSprites, elapsedTime, map, renderer);
}
public boolean isPaused(){
return paused;
}
public void setPaused(boolean p){
if(paused != p){
this.paused = p;
inputManager.resetAllGameActions();
}
}
/**
* Creates and displays the gameControl.
*/
@Override
public void gameControls(ScreenManager screen) {
GameControls gameControls = new GameControls(inputManager);
gameControls.show(screen);
}
/**
* Creates and displays the gameOptions
*/
@Override
public void gameOptions(ScreenManager screen) {
GameOptions gameOption = new GameOptions(inputManager, midiPlayer);
gameOption.show(screen);
}
/**
* Creates and displays the menu.
*/
@Override
public int gameMenu(ScreenManager screen) {
GameMenu gameMenu = new GameMenu(inputManager);
return gameMenu.show(screen);
}
/**
* Creates and displays the diploma.
*/
@Override
public void gameDiploma(ScreenManager screen) {
GameDiploma gameDiploma = new GameDiploma(inputManager, gradeManager);
gameDiploma.show(screen);
}
}