Guest User

Absurd BUG

a guest
Feb 6th, 2026
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.16 KB | Source Code | 0 0
  1. package terzaFSimulator;
  2. import javax.swing.*;
  3. import javax.swing.Timer;
  4.  
  5. import java.awt.*;
  6. import java.awt.event.ActionListener;
  7. import java.util.List;
  8. import java.util.*;
  9. import java.util.function.*;
  10.  
  11. public class Enemy extends JLabel{
  12.     static boolean attaccato = false;
  13.    
  14.     String nome;
  15.    
  16.     int danno;
  17.     int salute;
  18.     int difesa;
  19.    
  20.     String[] frasi;
  21.     boolean ordineFrasi;
  22.    
  23.     List<Consumer<Persona>> attacchi;
  24.     boolean ordineAttacchi;
  25.    
  26.     List<Consumer<Persona>> metodiSpeciali; //Opzionale, questo per cose particolari
  27.    
  28.     ImageIcon spriteOverworld;
  29.     int xOver;
  30.     int yOver;
  31.     String metodoDiInit;
  32.     Sprite[] sprites;
  33.     int[][] posSprites;
  34.     int numSprites;// X, Y HEIGHT, WITHGT (o inveriti), X, Y Height, ecc...
  35.    
  36.     byte movOver;
  37.    
  38.     HashMap<String , ActionListener> azioni; //Cose fattibili nel Menù ACT
  39.    
  40.     Enemy(String nome, int danno, int salute, int difesa, String[] frasi, boolean ordineFrasi, List<Consumer<Persona>> attacchi, boolean ordineAttacchi, List<Consumer<Persona>> metodiSpeciali, ImageIcon spriteOverworld, int xOver, int yOver, String metodoDiInit, Sprite[] sprites, int[][] posSprites, int numSprites, HashMap<String, ActionListener> azioni, byte movOver) {
  41.         this.nome = nome;
  42.         this.danno = danno;
  43.         this.salute = salute;
  44.         this.difesa = difesa;
  45.        
  46.         this.frasi = frasi;
  47.         this.ordineFrasi = ordineFrasi;
  48.         this.attacchi = attacchi;
  49.         this.ordineAttacchi = ordineAttacchi;
  50.         this.metodiSpeciali = metodiSpeciali;
  51.        
  52.         this.spriteOverworld = spriteOverworld;
  53.         this.xOver = xOver;
  54.         this.yOver = yOver;
  55.         this.sprites = sprites;
  56.         this.numSprites = numSprites;
  57.         this.posSprites = posSprites;
  58.         this.azioni = azioni;
  59.         this.movOver = movOver;
  60.        
  61.         this.spawn();
  62.          
  63.        
  64.        
  65.         if(movOver == 0) {
  66.             this.muoviInOrizzontale();
  67.         }else if(movOver == 1) {
  68.             this.muoviInVerticale();
  69.         }else if(movOver == 2) {
  70.             this.muoviAInseguimento();
  71.         }
  72.     }
  73.    
  74.     void spawn() {
  75.            this.setVisible(false);
  76.            MetodiUtili.getCurrentPanel().add(this);
  77.            this.setIcon(this.spriteOverworld);
  78.            this.setSize(this.spriteOverworld.getIconWidth(), this.spriteOverworld.getIconHeight());
  79.            this.setLocation(this.xOver, this.yOver);
  80.            MetodiUtili.getCurrentPanel().setComponentZOrder(Main.personaggioSelezionato.icona, 0);
  81.            this.setVisible(true);
  82.            MetodiUtili.getCurrentPanel().revalidate();
  83.            MetodiUtili.getCurrentPanel().repaint();
  84.         }
  85.    
  86.     void muoviInOrizzontale() {
  87.         boolean[] dir = {true}; //TRUE: DESTRA, FALSE SINISTRA
  88.         Timer timer = new Timer(10, e -> {
  89.             if(MetodiUtili.seTocca(this.xOver -34, this.yOver)) {
  90.             dir[0] = !dir[0];
  91.         }
  92.        
  93.             if(dir[0]) {
  94.             this.setLocation(this.xOver +1, this.yOver);
  95.             this.xOver++;
  96.         }else if(!dir[0]) {
  97.             this.setLocation(this.xOver -1, this.yOver);
  98.             this.xOver--;
  99.         }
  100.             if (checkArrivato()) {
  101.                 System.out.println("Raggiunto durante movimento!");
  102.                 attaccato = true;
  103.                 Main.personaggioSelezionato.icona.setIcon(
  104.                     new ImageIcon("Texture/personaggio_attaccato.png")
  105.                 );
  106.                 ((Timer)e.getSource()).stop(); // Ferma movimento
  107.             }
  108.         });
  109.         timer.setRepeats(true);
  110.         timer.start();
  111.        
  112.     }
  113.    
  114.     void muoviInVerticale() {
  115.         boolean[] dir = {true}; //TRUE: SU, FALSE GIù
  116.         Timer timer = new Timer(10, e -> {
  117.             if(MetodiUtili.seTocca(this.xOver, this.yOver -34)) {
  118.             dir[0] = !dir[0];
  119.         }
  120.        
  121.             if(dir[0]) {
  122.             this.setLocation(this.xOver, this.yOver +1);
  123.             this.yOver++;
  124.         }else if(!dir[0]) {
  125.             this.setLocation(this.xOver, this.yOver -1);
  126.             this.yOver--;
  127.         }
  128.             if (checkArrivato()) {
  129.                 System.out.println("Raggiunto durante movimento!");
  130.                 attaccato = true;
  131.                 Main.personaggioSelezionato.icona.setIcon(
  132.                     new ImageIcon("Texture/personaggio_attaccato.png")
  133.                 );
  134.                 ((Timer)e.getSource()).stop(); // Ferma movimento
  135.             }
  136.         });
  137.         timer.setRepeats(true);
  138.         timer.start();
  139.     }
  140.    
  141.     void muoviAInseguimento() {
  142.         boolean[] si = {false};
  143.        
  144.         boolean[] xBS = {true};
  145.         boolean[] xBD = {true};
  146.        
  147.         boolean[] yBU = {true};
  148.         boolean[] yBD = {true};
  149.        
  150.         Timer timer = new Timer(10, e -> {
  151.             if(!MetodiUtili.seTocca(this.xOver -33, this.yOver -34)) {
  152.                 xBD[0] = false;
  153.             } else {
  154.                 xBD[0] = true;
  155.             }
  156.             if(!MetodiUtili.seTocca(this.xOver -35, this.yOver -34)) {
  157.                 xBS[0] = false;
  158.             } else {
  159.                 xBS[0] = true;
  160.             }
  161.             if(!MetodiUtili.seTocca(this.xOver -34, this.yOver -33)) {
  162.                 yBD[0] = false;
  163.             } else {
  164.                 yBD[0] = true;
  165.             }
  166.             if(!MetodiUtili.seTocca(this.xOver -34, this.yOver -35)) {
  167.                 yBU[0] = false;
  168.             } else {
  169.                 yBU[0] = true;
  170.             }
  171.            
  172.             si[0] = true;
  173.            
  174.             int playerX = Main.personaggioSelezionato.posizioneX + 60;
  175.             int playerY = Main.personaggioSelezionato.posizioneY +55;
  176.  
  177.             int difX = playerX - this.xOver;
  178.             int difY = playerY - this.yOver;
  179.            
  180.             if(Math.abs(difX) > Math.abs(difY)) {
  181.                 if(difX > 0 && !xBD[0]) {
  182.                     this.xOver++;
  183.                     this.setLocation(this.xOver, this.yOver);
  184.                 } else if(difX < 0 && !xBS[0]) {
  185.                     this.xOver--;
  186.                     this.setLocation(this.xOver, this.yOver);
  187.                 } else if(difY > 0 && !yBD[0]) {
  188.                     this.yOver++;
  189.                     this.setLocation(this.xOver, this.yOver);
  190.                 } else if(difY < 0 && !yBU[0]) {
  191.                     this.yOver--;
  192.                     this.setLocation(this.xOver, this.yOver);
  193.                 }
  194.             } else {
  195.                 if(difY > 0 && !yBD[0]) {
  196.                     this.yOver++;
  197.                     this.setLocation(this.xOver, this.yOver);
  198.                 } else if(difY < 0 && !yBU[0]) {
  199.                     this.yOver--;
  200.                     this.setLocation(this.xOver, this.yOver);
  201.                 } else if(difX > 0 && !xBD[0]) {
  202.                     this.xOver++;
  203.                     this.setLocation(this.xOver, this.yOver);
  204.                 } else if(difX < 0 && !xBS[0]) {
  205.                     this.xOver--;
  206.                     this.setLocation(this.xOver, this.yOver);
  207.                 }
  208.             }
  209.             if (checkArrivato()) {
  210.                 System.out.println("Raggiunto durante movimento!");
  211.                 attaccato = true;
  212.                 Main.personaggioSelezionato.icona.setIcon(
  213.                     new ImageIcon("Texture/personaggio_attaccato.png")
  214.                 );
  215.                 ((Timer)e.getSource()).stop(); // Ferma movimento
  216.                 CombatPanel panel = new CombatPanel(this);
  217.                
  218.             }
  219.      
  220.         });
  221.        
  222.         timer.setRepeats(true);
  223.         timer.start();
  224.     }
  225.    
  226.     boolean checkArrivato() {
  227.         boolean x = false;
  228.         boolean y = false;
  229.  
  230.         int difX = Math.abs(Main.personaggioSelezionato.posizioneX - this.xOver);
  231.         int difY = Math.abs(Main.personaggioSelezionato.posizioneY - this.yOver);
  232.          
  233.         if(difX < 50) {
  234.             x = true;
  235.         }
  236.         if(difY < 50) {
  237.             y = true;
  238.         }
  239.         if(x && y) {
  240.             return true;
  241.         }else {
  242.             return false;
  243.         }
  244.     }
  245.    
  246. }
  247.  
Tags: Code
Advertisement
Add Comment
Please, Sign In to add comment