Package world

Source Code of world.Game

package world;

import java.awt.Point;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Random;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.media.opengl.GL2;

import View.Camera;
import View.Perso;
import obj.Bullet;
import obj.Element;
import obj.Enemy;
import obj.Forme;

public class Game implements World{

  private int[][] grid;
  private int nbCol = 0;
  private int nbLig = 0;

  // forme de base
  private Forme forme = new Forme();

  // aleatoire
  Random rand = new Random();

  // perso
  public Perso perso = new Perso(1.5f,0.6f,1.5f,0.0f);

  // spawn
  public Vector<Point2D.Double> debut = new Vector<Point2D.Double>();

  // Elements = perso + enemies + bullets
  public Vector<Element> elements = new Vector<Element>();

  public Game(){ 

    loadFile(null);

  }
 
  @Override
  public void init(Point p){

    elements.clear();
    debut.clear();

    elements.addElement(perso);

  }

  // selecteur exterieur de loader
  @Override
  public void loadFile(File fic){

    if ( fic == null ){
      try {
        loadImgFile( getClass().getClassLoader().getResource("arena1.png").openStream() );
      } catch (IOException e) {
        e.printStackTrace();
      }
    }else{

      if ( fic.getName().endsWith(".png") ){
        loadImgFile(fic);
      }

    }

  }

  @Override
  public void render(GL2 gl){

    // dessine le sol
    forme.drawFloor(gl, 0.0f, 0.1f);

    // dessine le plafond
    forme.drawFloor(gl, 1.0f, 0.3f);

    // dessine les cubes et les spawns
    for ( int i = 0 ; i < nbLig ; i++ ){
      for ( int j = 0 ; j < nbCol ; j++ ){
        switch(grid[i][j]){
        case 1:
          //bloc
          forme.drawQuads(gl, i, j);
          break;
        case 3:
          //spawn
          forme.drawRespawn(gl, i, j, 'g');
          break;
        default:
          //autres
        }

      }
    }

    // dessine tout les elements
    // elements = perso + enemies + bullets
    Iterator<Element> iteratorElem = elements.iterator();
    while ( iteratorElem.hasNext() ) {
      Element elem = iteratorElem.next();
      if (elem.actualise(grid)) {
        iteratorElem.remove();
      }else{
        elem.draw(gl);
      }
    }
  }
 
  @Override
  public int[][] getGrid(){
    return grid;
  }
 
  /* *** fin Override *** */

  // lecture de l'image
  public void loadImgFile(File fic){
    try {
      loadImgFile( new FileInputStream(fic) );
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }

  public void loadImgFile(InputStream link) {

    init(null);

    try {

      BufferedImage bi = ImageIO.read(link);

      int height = bi.getHeight();
      int width = bi.getWidth();

      grid = new int[height][width];

      for(int x = 0 ; x < height ; x++) {

        for(int y = 0 ; y < width ; y++) {

          int rgb = bi.getRGB(x, y);

          @SuppressWarnings("unused")
          int alpha = (rgb >> 24) & 0xFF;
          int red =   (rgb >> 16) & 0xFF;
          int green = (rgb >>  8) & 0xFF;
          int blue =  (rgb      ) & 0xFF;

          grid[x][y] = getType(red, green, blue);


          // si c'est un spawn, on ajoute les coordonn�es dans le tableau des spawns possible
          if ( grid[x][y] == 3 ){
            //spawn
            debut.add(new Point2D.Double(x+0.5, y+0.5));

            //si c'est un enemy, on l'ajoute dans le tableau des enemies
          }else if ( grid[x][y] == 2 ){
            //enemy
            elements.add(new Enemy(perso, (float)(x+0.5), (float)(y+0.5)));
          }

        }
      }

      nbLig = height;
      nbCol = width;

      //on place le perso sur un spawn
      reset();

    } catch (IOException e) {
      e.printStackTrace();
    }
  }// fin loadImgFile

  private int getType(int R, int G, int B) {
    if(R == 255 && G == 255 && B == 255) {
      //blanc = vide
      return 0;
    } else if(R == 0 && G == 0 && B == 0) {
      //noir = mur
      return 1;
    } else if(R == 255 && G == 0 && B == 0) {
      //rouge = enemy
      return 2;
    } else if(R == 0 && G == 255 && B == 0) {
      //vert = spawn
      return 3;
    }
    return -1;
  }

  /* ******** AUTRES ******** */

  @Override
  public Camera getCamera(){
    return perso;
  }

  @Override
  public void fire(){
    elements.add(new Bullet(perso.getX(), perso.getZ(), perso.getDir()));
  }

  @Override
  public void reset(){
    int nbDebut = debut.size();

    int i = rand.nextInt(nbDebut);

    perso.reset((float)debut.get(i).getX(), (float)debut.get(i).getY());
  }

}
TOP

Related Classes of world.Game

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.