Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package terzaFSimulator;
- import javax.swing.ImageIcon;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import java.awt.Color;
- import java.awt.Rectangle;
- import javax.swing.*;
- public class CombatPanel extends JPanel {
- JPanel areAnima;
- JPanel areaTexture;
- JPanel areaScelte;
- JPanel areaVita;
- JPanel areAttacco;
- JButton fightButton;
- JButton actButton;
- JButton itemButton;
- JButton mercyButton;
- static boolean combat = false;
- boolean animOcchi1 = false;
- boolean animOcchi2 = false;
- Sprite occhioSu = null;
- Sprite occhioGiù = null;
- Sprite occhioDestra = null;
- Sprite occhioSinistra = null;
- Sprite occhioSu2 = null;
- Sprite occhioGiù2 = null;
- Sprite occhioDestra2 = null;
- Sprite occhioSinistra2 = null;
- Rectangle boxAnima;
- int xInizio = 140;
- int yInizio = 10;
- int sizeX = 600;
- int sizeY = 180;
- Sprite tempInversione1 = null; //4 Possibili inversionicaricate contemporaneamente
- Sprite tempInversione2 = null;
- Sprite tempInversione3 = null;
- Sprite tempInversione4 = null;
- Enemy e;
- CombatPanel(Enemy e) {
- this.e = e;
- combat = true;
- creaAmbiente();
- }
- void creaAmbiente() {
- this.setLayout(null);
- areaTexture = new JPanel();
- areaTexture.setBounds(0, 0, 880, 268);
- areaTexture.setBackground(new Color(0, 0, 0, 0));
- areaTexture.setLayout(null);
- int numLabel = 0;
- for(Sprite sprite: e.sprites) {
- sprite.setOpaque(false);
- if(sprite.getTag().contains("occhio")) {
- raccogliOcchi(sprite, numLabel);
- numLabel++;
- continue;
- }else if(sprite.getTag().contains("inverti_")) {
- raccoglInversioni(sprite, numLabel);
- numLabel++;
- continue;
- }
- sprite.setBounds(e.posSprites[numLabel][0], e.posSprites[numLabel][1], e.posSprites[numLabel][2], e.posSprites[numLabel][3]);
- areaTexture.add(sprite);
- numLabel++;
- }
- boxAnima = new Rectangle(xInizio, yInizio, sizeX, sizeY);
- areAnima = new JPanel() {
- @Override
- protected void paintComponent(java.awt.Graphics g) {
- super.paintComponent(g);
- java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
- g2.setColor(Color.WHITE);
- g2.setStroke(new java.awt.BasicStroke(5));
- if (boxAnima != null) {
- g2.draw(boxAnima);
- }
- }
- };
- areAnima.setBounds(0, 268, 880, 201);
- areAnima.setOpaque(false);
- areAnima.setBackground(new Color(0, 0, 0, 0));
- areaVita = new JPanel();
- areaVita.setBounds(0, 603, 880, 67);
- areaVita.setBackground(new Color(0, 0, 0, 0));
- areaScelte = new JPanel();
- areaScelte.setBounds(0, 469, 880, 134);
- areaScelte.setBackground(new Color(0, 0, 0, 0));
- areaScelte.setLayout(null);
- fightButton = new JButton(new ImageIcon("Texture/fight_button.png"));
- fightButton.setBounds(15, 40, 150, 52);
- actButton = new JButton(new ImageIcon("Texture/act_button.png"));
- actButton.setBounds(210, 40, 150, 52);
- itemButton = new JButton(new ImageIcon("Texture/item_button.png"));
- itemButton.setBounds(405, 40, 150, 52);
- mercyButton = new JButton(new ImageIcon("Texture/mercy_button.png"));
- mercyButton.setBounds(600, 40, 150, 52);
- areaScelte.add(fightButton);
- areaScelte.add(actButton);
- areaScelte.add(itemButton);
- areaScelte.add(mercyButton);
- areAttacco = new JPanel();
- areAttacco.setBounds(0, 261, 880, 200);
- areAttacco.setBackground(new Color(0, 0, 0, 0));
- JLabel sfondo = new JLabel(new ImageIcon("Texture/inizio.png"));
- sfondo.setBounds(0, 0, 880, 671);
- this.add(areaTexture);
- this.add(areaVita);
- this.add(areaScelte);
- this.add(areAnima);
- this.add(sfondo);
- this.setComponentZOrder(sfondo, this.getComponentCount() - 1);
- this.setComponentZOrder(areaScelte, 1);
- Main.mappaClasse.setVisible(false);
- Main.finestra.add(this);
- this.setVisible(true);
- Main.finestra.revalidate();
- Main.finestra.repaint();
- }
- void raccogliOcchi(Sprite occhio, int numLabel) { //This method "captures" the eyes founded in "creaAmbiente"
- if("occhio_su".equals(occhio.getTag()) && this.occhioSu == null) {
- this.occhioSu = occhio;
- }else if("occhio_su".equals(occhio.getTag()) && this.occhioSu != null) {
- this.occhioSu2 = occhio;
- } else if("occhio_giù".equals(occhio.getTag()) && this.occhioGiù == null) {
- this.occhioGiù = occhio;
- } else if("occhio_giù".equals(occhio.getTag()) && this.occhioGiù != null) {
- this.occhioGiù2 = occhio;
- }else if("occhio_destra".equals(occhio.getTag()) && this.occhioDestra == null) {
- this.occhioDestra = occhio;
- }else if("occhio_destra".equals(occhio.getTag()) && this.occhioDestra != null) {
- this.occhioDestra2 = occhio;
- }else if("occhio_sinistra".equals(occhio.getTag()) && this.occhioSinistra == null) {
- this.occhioSinistra = occhio;
- }else if("occhio_sinistra".equals(occhio.getTag()) && this.occhioSinistra != null) {
- this.occhioSinistra2 = occhio;
- }
- if(this.occhioSu != null && this.occhioGiù != null && this.occhioDestra != null && this.occhioSinistra != null && !this.animOcchi1) {
- this.animOcchi1 = true;
- Sprite[] occhi = {this.occhioSu, this.occhioGiù, this.occhioDestra, this.occhioSinistra};
- animOcchi(numLabel, occhi);
- }
- if(this.occhioSu2 != null && this.occhioGiù2 != null && this.occhioDestra2 != null && this.occhioSinistra2 != null && !this.animOcchi2) {
- this.animOcchi2 = true;
- Sprite[] occhi = {this.occhioSu2, this.occhioGiù2, this.occhioDestra2, this.occhioSinistra2};
- animOcchi(numLabel, occhi);
- }
- }
- void animOcchi(int numLabel, Sprite[] occhi) { //This method makes the eyes animation (in italian "occhio" means eye)
- Icon[] frameIcone = new Icon[4];
- frameIcone[0] = occhi[0].getIcon();
- frameIcone[1] = occhi[1].getIcon();
- frameIcone[2] = occhi[2].getIcon();
- frameIcone[3] = occhi[3].getIcon();
- occhi[0].setBounds(e.posSprites[numLabel][0], e.posSprites[numLabel][1], e.posSprites[numLabel][2], e.posSprites[numLabel][3]);
- this.areaTexture.add(occhi[0]);
- int[] numOcchio = {0};
- Timer timer = new Timer(500, e -> {
- if (numOcchio[0] < 3) {
- numOcchio[0]++;
- } else {
- numOcchio[0] = 0;
- }
- occhi[0].setIcon(frameIcone[numOcchio[0]]);
- });
- timer.setRepeats(true);
- timer.start();
- }
- void raccoglInversioni(Sprite inversione, int numLabel) {
- String tagInv = inversione.getTag().substring(inversione.getTag().indexOf("inverti_") + 8);
- if (tempInversione1 != null && tagInv.equals(tempInversione1.getTag())) {
- invertiSprites(inversione, tempInversione1, numLabel);
- tempInversione1 = null;
- } else if (tempInversione2 != null && tagInv.equals(tempInversione2.getTag())) {
- invertiSprites(inversione, tempInversione2, numLabel);
- tempInversione2 = null;
- } else if (tempInversione3 != null && tagInv.equals(tempInversione3.getTag())) {
- invertiSprites(inversione, tempInversione3, numLabel);
- tempInversione3 = null;
- } else if (tempInversione4 != null && tagInv.equals(tempInversione4.getTag())) {
- invertiSprites(inversione, tempInversione4, numLabel);
- tempInversione4 = null;
- } else {
- if (tempInversione1 == null) { tempInversione1 = inversione; tempInversione1.setTag(tagInv); }
- else if (tempInversione2 == null) { tempInversione2 = inversione; tempInversione2.setTag(tagInv); }
- else if (tempInversione3 == null) { tempInversione3 = inversione; tempInversione3.setTag(tagInv); }
- else if (tempInversione4 == null) { tempInversione4 = inversione; tempInversione4.setTag(tagInv); }
- }
- }
- void invertiSprites(Sprite inv1, Sprite inv2, int numLabel) { //"invertiSprites" in italian means invertSprites
- /*
- * This method alternates the images of the two sprites with the same tag,
- * commenting this method solves the problem, although the mop obviously does not appear
- *
- */
- inv1.setBounds(e.posSprites[numLabel][0], e.posSprites[numLabel][1], e.posSprites[numLabel][2], e.posSprites[numLabel][3]);
- this.areaTexture.add(inv1);
- Icon img1 = inv1.getIcon();
- Icon img2 = inv2.getIcon();
- boolean[] fase = {false};
- Timer timer = new Timer(500, e -> {
- fase[0] = !fase[0];
- inv1.setIcon(fase[0] ? img2 : img1);
- this.areaTexture.repaint();
- });
- timer.setRepeats(true);
- timer.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment