Package Board

Source Code of Board.Square

package Board;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JPanel;

import Character.Enum_GameCharacters;
import Character.GameCharacter;
import Engine.Engine;
import IHM.Erreur;
import IHM.MainFrame;

@SuppressWarnings("serial")
public class Square extends JComponent implements MouseListener {

  private int x, y;
  private Enum_Land land;
  private Color couleur;
  private final static Color select = new Color(255, 0, 0, 100),
      poss = new Color(34, 66, 124, 100);
  private GameCharacter charac;
  private boolean selected = false, possible = false;
  private static Square orig = null;
  private Engine moteur;
  private JPanel pan;
  private static boolean init = false;
  private static Image bow, war, mag, kni, cen, kin, bow2, war2, mag2, kni2,
      cen2;
  private static Image def, hill, moun, oasi, fore;
  private MainFrame frame;
  private static ArrayList<Square> list = new ArrayList<Square>();

  public Square(int i, int j, Enum_Land l) {
    x = i;
    y = j;
    land = l;
    charac = null;
    setBackground(Color.CYAN);
    setBorder(BorderFactory.createLineBorder(Color.BLACK));

    // Partie affichage
    addMouseListener(this);
    if (!init) {
      init = true;
      try {
        bow = ImageIO.read(new File("Pictures/archer.png"));
        bow2 = ImageIO.read(new File("Pictures/archer2.png"));
        war = ImageIO.read(new File("Pictures/guerrier.png"));
        war2 = ImageIO.read(new File("Pictures/guerrier2.png"));
        mag = ImageIO.read(new File("Pictures/mage.png"));
        mag2 = ImageIO.read(new File("Pictures/mage2.png"));
        kni = ImageIO.read(new File("Pictures/cheval.png"));
        kni2 = ImageIO.read(new File("Pictures/cheval2.png"));
        kin = ImageIO.read(new File("Pictures/roi.png"));
        cen = ImageIO.read(new File("Pictures/chevalierArcher.png"));
        cen2 = ImageIO.read(new File("Pictures/chevalierArcher2.png"));
        def = ImageIO.read(new File("Pictures/default.jpg"));
        hill = ImageIO.read(new File("Pictures/colline.gif"));
        moun = ImageIO.read(new File("Pictures/montagne.gif"));
        oasi = ImageIO.read(new File("Pictures/oasis.gif"));
        fore = ImageIO.read(new File("Pictures/foret.gif"));
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    /*
     * setPreferredSize(new Dimension(25, 25)); setSize(getPreferredSize());
     */
  }

  public void mouseClicked(MouseEvent e) {
  }

  public void mouseEntered(MouseEvent e) {
    setCaracLand(land);
    if (charac == null)
      setCaracteristique(null);
    else
      setCaracteristique(this);
  }

  public void mouseExited(MouseEvent e) {
  }

  public void mousePressed(MouseEvent e) {
    if (!selected) {
      if (orig != null) {
        if (orig.charac == null) {
          orig.selected = false;
          orig.repaint();
          selected = true;
          orig = this;
        } else if (charac != null
            && charac.getTeam() == orig.getCharac().getTeam()) {
          orig.selected = false;
          orig.repaint();
          for (int i = 0; i < list.size(); i++) {
            list.get(i).possible = false;
            list.get(i).repaint();
          }
          list.clear();
          selected = true;
          orig = this;
          // affichage coup possible
          list = coupsPossible();
          for (int i = 0; i < list.size(); i++) {
            list.get(i).possible = true;
            list.get(i).repaint();
          }
        } else {
          if (!moteur.action(orig, this))
            new Erreur(pan);
          else {
            frame.setTokens(moteur.getJ1().getToken(), moteur
                .getJ2().getToken());
            if (moteur.myTurn(moteur.getJ1()))
              frame.setTurn(1);
            else
              frame.setTurn(2);
            orig.repaint();
            orig.selected = false;
            orig = null;
            for (int i = 0; i < list.size(); i++) {
              list.get(i).possible = false;
              list.get(i).repaint();
            }
            list.clear();
          }

        }
      } else {
        if (charac != null && moteur.myTurn(charac.getTeam())) {
          selected = true;
          orig = this;
          // affichage coup possible
          list = coupsPossible();
          for (int i = 0; i < list.size(); i++) {
            list.get(i).possible = true;
            list.get(i).repaint();
          }
        } else
          new Erreur(pan);
      }
    } else {
      if (charac != null) {
        selected = false;
        orig = null;
        for (int i = 0; i < list.size(); i++) {
          list.get(i).possible = false;
          list.get(i).repaint();
        }
      }
    }
    repaint();
  }

  private ArrayList<Square> coupsPossible() {
    ArrayList<Square> list = new ArrayList<Square>();
    if (x - 1 >= 0
        && moteur.getBoard().getCase(x - 1, y).getCharac() == null
        && moteur.getBoard().getCase(x - 1, y).getLand() != Enum_Land.MOUNTAIN)
      list.add(moteur.getBoard().getCase(x - 1, y));
    if (x + 1 < Board.LINES
        && moteur.getBoard().getCase(x + 1, y).getCharac() == null
        && moteur.getBoard().getCase(x + 1, y).getLand() != Enum_Land.MOUNTAIN)
      list.add(moteur.getBoard().getCase(x + 1, y));
    if (y - 1 >= 0
        && moteur.getBoard().getCase(x, y - 1).getCharac() == null
        && moteur.getBoard().getCase(x, y - 1).getLand() != Enum_Land.MOUNTAIN)
      list.add(moteur.getBoard().getCase(x, y - 1));
    if (y + 1 < Board.COLUMNS
        && moteur.getBoard().getCase(x, y + 1).getCharac() == null
        && moteur.getBoard().getCase(x, y + 1).getLand() != Enum_Land.MOUNTAIN)
      list.add(moteur.getBoard().getCase(x, y + 1));
    ArrayList<Square> tmp = new ArrayList<Square>();
    tmp.addAll(list);
    for (int i = 1; i != charac.getStamina(); i++) {
      ArrayList<Square> tmp2 = new ArrayList<Square>();
      for (int j = 0; j < tmp.size(); j++) {
        if (tmp.get(j).get_X() - 1 >= 0
            && moteur
                .getBoard()
                .getCase(tmp.get(j).get_X() - 1,
                    tmp.get(j).get_Y()).getCharac() == null
            && moteur
                .getBoard()
                .getCase(tmp.get(j).get_X() - 1,
                    tmp.get(j).get_Y()).getLand() != Enum_Land.MOUNTAIN)
          tmp2.add(moteur.getBoard().getCase(tmp.get(j).get_X() - 1,
              tmp.get(j).get_Y()));
        if (tmp.get(j).get_X() + 1 < Board.LINES
            && moteur
                .getBoard()
                .getCase(tmp.get(j).get_X() + 1,
                    tmp.get(j).get_Y()).getCharac() == null
            && moteur
                .getBoard()
                .getCase(tmp.get(j).get_X() + 1,
                    tmp.get(j).get_Y()).getLand() != Enum_Land.MOUNTAIN)
          tmp2.add(moteur.getBoard().getCase(tmp.get(j).get_X() + 1,
              tmp.get(j).get_Y()));
        if (tmp.get(j).get_Y() - 1 >= 0
            && moteur
                .getBoard()
                .getCase(tmp.get(j).get_X(),
                    tmp.get(j).get_Y() - 1).getCharac() == null
            && moteur
                .getBoard()
                .getCase(tmp.get(j).get_X(),
                    tmp.get(j).get_Y() - 1).getLand() != Enum_Land.MOUNTAIN)
          tmp2.add(moteur.getBoard().getCase(tmp.get(j).get_X(),
              tmp.get(j).get_Y() - 1));
        if (tmp.get(j).get_Y() + 1 < Board.COLUMNS
            && moteur
                .getBoard()
                .getCase(tmp.get(j).get_X(),
                    tmp.get(j).get_Y() + 1).getCharac() == null
            && moteur
                .getBoard()
                .getCase(tmp.get(j).get_X(),
                    tmp.get(j).get_Y() + 1).getLand() != Enum_Land.MOUNTAIN)
          tmp2.add(moteur.getBoard().getCase(tmp.get(j).get_X(),
              tmp.get(j).get_Y() + 1));
      }
      tmp.clear();
      tmp.addAll(tmp2);
      list.addAll(tmp2);
    }
    return list;
  }

  private void setCaracLand(Enum_Land l) {
    if (l == Enum_Land.FOREST)
      frame.setLand("Foret : range diminuee, armor augmentee");
    else if (l == Enum_Land.HILL)
      frame.setLand("Colline : range augmentee (si attaque � distance)");
    else if (l == Enum_Land.MOUNTAIN)
      frame.setLand("Montagne : obstacle");
    else if (l == Enum_Land.OASIS)
      frame.setLand("Oasis : +5 HP/tour mais attaque impossible");
    else
      frame.setLand("Terrain sans bonus");
  }

  private void setCaracteristique(Square s) {
    if (s != null) {
      frame.setAcc("Accuracy : " + charac.getAccuracy() + "%");
      frame.setArm("Armor : " + charac.getArmor() + "%");
      frame.setCri("Critical Chance : " + charac.getCriticalChance()
          + "%");
      frame.setHea("Health : " + charac.getHealth() + " / "
          + charac.getInitialHealth());
      frame.setHer("Heritage : " + charac.getHeritage() + "%");
      frame.setRan("Range : " + charac.getRange());
      if (charac.getType() == Enum_GameCharacters.BOWMAN)
        frame.setType("Bowman");
      else if (charac.getType() == Enum_GameCharacters.BOWKNIGHT)
        frame.setType("BowKnight");
      else if (charac.getType() == Enum_GameCharacters.KING)
        frame.setType("King");
      else if (charac.getType() == Enum_GameCharacters.KNIGHT)
        frame.setType("Knight");
      else if (charac.getType() == Enum_GameCharacters.MAGUS)
        frame.setType("Magus");
      else if (charac.getType() == Enum_GameCharacters.WARRIOR)
        frame.setType("Warrior");
      frame.setStr("Strength : " + charac.getStrength());
      frame.setSta("Stamina : " + charac.getStamina());
    } else {
      frame.setAcc("");
      frame.setArm("Aucun");
      frame.setCri("");
      frame.setHea("");
      frame.setHer("");
      frame.setRan("");
      frame.setType("");
      frame.setStr("");
      frame.setSta("");
    }
  }

  public void mouseReleased(MouseEvent e) {
  }

  public void paintComponent(Graphics g) {
    Graphics2D draw = (Graphics2D) g;

    // dessin image terrain
    draw.drawImage(def, 0, 0, getWidth(), getHeight(), null);
    if (land == Enum_Land.MOUNTAIN)
      draw.drawImage(moun, 0, 0, getWidth(), getHeight(), null);
    else if (land == Enum_Land.FOREST)
      draw.drawImage(fore, 0, 0, getWidth(), getHeight(), null);
    else if (land == Enum_Land.HILL)
      draw.drawImage(hill, 0, 0, getWidth(), getHeight(), null);
    else if (land == Enum_Land.OASIS)
      draw.drawImage(oasi, 0, 0, getWidth(), getHeight(), null);
    // dessin image si besoin (personnage)
    if (charac != null) {
      if (charac.getTeam() == moteur.getJ1()) {
        if (charac.getType() == Enum_GameCharacters.BOWMAN)
          draw.drawImage(bow, 0, 0, getWidth(), getHeight(), null);
        else if (charac.getType() == Enum_GameCharacters.KING)
          draw.drawImage(kin, 0, 0, getWidth(), getHeight(), null);
        else if (charac.getType() == Enum_GameCharacters.KNIGHT)
          draw.drawImage(kni, 0, 0, getWidth(), getHeight(), null);
        else if (charac.getType() == Enum_GameCharacters.MAGUS)
          draw.drawImage(mag, 0, 0, getWidth(), getHeight(), null);
        else if (charac.getType() == Enum_GameCharacters.WARRIOR)
          draw.drawImage(war, 0, 0, getWidth(), getHeight(), null);
        else
          draw.drawImage(cen, 0, 0, getWidth(), getHeight(), null);
      } else {
        if (charac.getType() == Enum_GameCharacters.BOWMAN)
          draw.drawImage(bow2, 0, 0, getWidth(), getHeight(), null);
        else if (charac.getType() == Enum_GameCharacters.KING)
          draw.drawImage(kin, 0, 0, getWidth(), getHeight(), null);
        else if (charac.getType() == Enum_GameCharacters.KNIGHT)
          draw.drawImage(kni2, 0, 0, getWidth(), getHeight(), null);
        else if (charac.getType() == Enum_GameCharacters.MAGUS)
          draw.drawImage(mag2, 0, 0, getWidth(), getHeight(), null);
        else if (charac.getType() == Enum_GameCharacters.WARRIOR)
          draw.drawImage(war2, 0, 0, getWidth(), getHeight(), null);
        else
          draw.drawImage(cen2, 0, 0, getWidth(), getHeight(), null);
      }
    }
    if (selected) {
      draw.setPaint(select);
      draw.fillRect(0, 0, getWidth(), getHeight());
    }
    if (possible) {
      draw.setPaint(poss);
      draw.fillRect(0, 0, getWidth(), getHeight());
    }
  }

  public void setColor(Color c) {
    couleur = c;
  }

  public void setPan(JPanel p) {
    pan = p;
  }

  public void resetColor() {
    setBackground(couleur);
  }

  public GameCharacter getCharac() {
    return charac;
  }

  public void setCharac(GameCharacter character) {
    charac = character;
  }

  public int get_X() {
    return x;
  }

  public int get_Y() {
    return y;
  }

  public void setFrame(MainFrame f) {
    frame = f;
  }

  public Enum_Land getLand() {
    return land;
  }

  public void setEngine(Engine e) {
    moteur = e;
  }

  public boolean canAccross() {
    return (getCharac() == null && getLand().canAccross());
  }
}
TOP

Related Classes of Board.Square

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.