/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package NetSpace;
import NetSpace.weapons.WeaponsEnum;
import NetSpace.weapons.Weapon;
import NetSpace.weapons.WeaponType;
import NetSpace.aliens.Enemy;
import NetSpace.aliens.EnemyRepresentation;
import NetSpace.update.UpdatePatch;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Image;
import org.newdawn.slick.tiled.*;
import org.newdawn.slick.geom.*;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Input;
/**
 *
 * @author Aidan Malone
 */
public class game extends BasicGame {
    //Single-player constructor
    public game()
    {
        super("game");
    }
    
    //Multi-player constructor
    public game(ObjectOutputStream out, ObjectInputStream in, String user)
    {   
        super("game");
        
        try {
            Username = user;                
            Soutput = out;            
            Sinput = in;
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }           
    }
    //This is the width and height of your game window
    final static int viewW = 1024, viewH = 690;
    //Image [] shipSprites;
    //Animation ship;
    float theta = 0f;
    TiledMap StarMap;
    int mapw, maph;
    int spritew = 100, spriteh = 100;
    float Shipx = viewW/2-spritew/2, Shipy = viewH/2-spriteh/2;
    
    final int phaseShift = 90;
    final int speed = 5;
    float destx = Shipx + spritew/2, desty = Shipy + spriteh/2;
    float viewx = 0, viewy = 0;   
    HUD display;
    //This contains all of the methods for actually controlling the ship's motion
    Player myPlayer;
    
    //This contains the data for all of the players currently registered with
    //  the server, including yours
    ArrayList <PlayerInfo> players = new ArrayList<PlayerInfo>();
    Rectangle camera;
    WeaponType[] myWeapons;
    WeaponType auto;
    
    //Socket serv;    
    ObjectInputStream Sinput;
    ObjectOutputStream Soutput;  
    
    final int MAXMSGS = 6;
    ArrayList <String>Messages = new ArrayList<String>(MAXMSGS);       
    ArrayList <EnemyRepresentation> ennemies = new ArrayList<EnemyRepresentation>();
    ArrayList <Weapon> ActiveWeapons = new ArrayList<Weapon>();
    
    String Username;
    float[][] todraw;
    
    InputThread inputReader;
    
    SpriteBank spriteBank; 
    
    
    public static void create()        
    {
        try
        {
            AppGameContainer app = new AppGameContainer(new game());
            app.setDisplayMode(viewW, viewH, false);
            app.setVSync(true);
            app.start();
        }
        catch (SlickException e)
        {
            e.printStackTrace();
        }
    }
    
    public static void createNetGame(ObjectOutputStream out, ObjectInputStream in, String user)
    { 
        try
        {            
            AppGameContainer app = new AppGameContainer(new game(out, in, user));
            app.setDisplayMode(viewW, viewH, false);
            app.setVSync(true);             
            app.start();
            
        }
        catch (SlickException e)
        {
            e.printStackTrace();
        }
    }    
    @Override
    public void init(GameContainer container) throws SlickException
    {
        //Load all map variables
        StarMap = new TiledMap("data/StarMap.tmx");
        mapw = StarMap.getWidth()*StarMap.getTileWidth();
        maph = StarMap.getHeight()*StarMap.getTileHeight();
        //Load the HUD
        display = new HUD(viewW, viewH);
        
        //Initialize the sprites
        spriteBank = new SpriteBank();
        myPlayer = new Player(Username,50,50);        
        
//        for(int i = 0; i<bots.size(); i++){
//            ennemies.add(new Enemy((int)(Math.random()*mapw),(int)(Math.random()*maph)));
//        }
        camera = new Rectangle(viewx,viewy,viewW,viewH);
        //Sets which Weapons the player is using
        myWeapons = new WeaponType[4];
        myWeapons[0] = new WeaponType(WeaponsEnum.PULSE);
        myWeapons[1] = new WeaponType(WeaponsEnum.BLAST);
        myWeapons[2] = new WeaponType(WeaponsEnum.BLAST);
        myWeapons[3] = new WeaponType(WeaponsEnum.MINE);
        auto = new WeaponType(WeaponsEnum.LASER);
        display.loadWeapons(myWeapons);
        
        inputReader = new InputThread ();
        inputReader.start();                
    }
    @Override
    public void update(GameContainer container, int delta) throws SlickException
    {
        
        
        Input input = container.getInput();
        float x = input.getMouseX();
        float y = input.getMouseY();
        //Checks for the left mouse button.
        if (input.isMouseButtonDown(0))
        {            
             if (display.minimap.contains(x, y)) {
                myPlayer.newDestination((int)display.getMapX(x, mapw),(int)display.getMapY(y, maph));
            }
            else
            {
                myPlayer.newDestination((int)(x + viewx),(int)(y + viewy));
            }
        }
        //Checks for key input
        /*
        if (inputReader.isKeyDown(Input.KEY_Q))
        {  
            //Deals with Player targetting
            int target = -1;
            for(int i = 0; i<bots.size(); i++)
            {
                if((ennemies.get(i)).isAlive()){
                    if(ennemies.get(i).hitbox.contains(x + viewx, y + viewy)){
                        target = i;
                        break;
                    }
                }
            }
            player.target = target;
        }
        */
        //Debugging button
        if (input.isKeyDown(Input.KEY_A))
        {
            //System.out.println(ennemies.get(0).ID);            
            for (int i = 0; i<ennemies.size(); i++){
                System.out.println("Bot #" + i + " x: " + ennemies.get(i).x);
            }
        }
        
        
        if (input.isKeyDown(Input.KEY_1))
        {  
            if(myWeapons[0].offCD()){
                Weapon a = myPlayer.fireWeapon((int)(x + viewx),(int)(y+ viewy), myWeapons[0]);
                a.init(0, Username);
                ActiveWeapons.add(a); 
                send(a);
            }            
        }
        if (input.isKeyDown(Input.KEY_2))
        {
            if(myWeapons[1].offCD()){
                Weapon a = myPlayer.fireWeapon((int)(x + viewx),(int)(y+ viewy), myWeapons[1]);
                a.init(1, Username);
                ActiveWeapons.add(a);
                send(a);
            }
        }
        if (input.isKeyDown(Input.KEY_3))
        {
            if(myWeapons[2].offCD()){
                Weapon a = myPlayer.fireWeapon((int)(x + viewx),(int)(y+ viewy), myWeapons[2]);
                a.init(2, Username);
                ActiveWeapons.add(a); 
                send(a);
            }
        }
        if (input.isKeyDown(Input.KEY_4))
        {
            
            if(myWeapons[3].offCD()){
                Weapon a = myPlayer.fireWeapon((int)(x + viewx),(int)(y+ viewy), myWeapons[3]);
                a.init(3, Username);                
                ActiveWeapons.add(a);
                send(a);
            }
        }
        //Updates the player
        myPlayer.update(delta,mapw,maph);
        
        //Updates the player's location on the server
        send(myPlayer.getUpdate());
        
        //Updates the other player's animations and ship positions
        for(int i = 0; i < players.size(); i++){
            PlayerInfo player2 = players.get(i);            
            player2.Ship.update(delta);
            player2.moveShip(delta);
        }
        
        for(int i = 0; i < ennemies.size(); i++){
            ennemies.get(i).move(delta);
        }       
        //updates Weapon cooldowns


Answer:         for(int i = 0; i< myWeapons.length; i++) {