This site requires JavaScript, please enable it in your browser!
Greenfoot back
larg5847
larg5847 wrote ...

2014/5/21

getNeighbours or getOneObjectAtOffset?

larg5847 larg5847

2014/5/21

#
hi, im working in a turn strategy game (like front mission, advance wars...) and i want to know which method could help me to get the adjacents enemies when i move my player because getNeighbours gets me a list of the actors around my player, but, i dont know what actors are around my player and if i get the neighbours how can in access to a certain enemy actor to attack him? and with getoneobjectatoffset i could search more specific but its so complicated (because i need to search for at least 3 enemy actors per position) thanks for the attetion
danpost danpost

2014/5/21

#
I fear that no matter how you end up going about it, a list of actors is going to be involved somewhere. If you are at all familiar with lists, you can do a brute-force check for which class each actor in the list is of using the 'instanceof' keyword; for example : if (actorList.get(0) instanceof Boss) will check to see if the first actor in the 'actorList' list is an object of the Boss class.
larg5847 larg5847

2014/5/21

#
ok, i have the class "navesJugador"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
public class naves extends jugador
{
    public int dineroDar;   //cantidad de dinero que da al enemigo cada que destruyen una nave de cierto tipo
    public int avanze;      //el numero de espacios maximos q pueden avanzar cierto tipo de nave
    public int alcanzeAtaque;   //el espacio maximo de ataque que tiene una nave (que es 1 menor al avanze)
     
    public boolean avanzo;     //bandera que dice si una nave ya avanzo y de ser asi ya no avanzara hasta el proximo turno
     
    public boolean pAvanzar;    //da la pauta para poder avanzar si se puede avanzar
     
    public boolean hayLugar;   //para no avanzar encima de alguna cosa
     
    public int atkVSFragatas;   //el poder que tienen la nave contra las fragatas
    public int atkVSNavesdebatalla;   //ataque contra naves de batalla
    public int atkVSCazas;   //ataque contra cazas
     
    public int Xactual;      //coordenada actual en X del objeto
    public int Yactual;      //coordenada actual en Y del objeto
     
    public int Xavanze;      ////coordenada final para avanzar
    public int Yavanze;     //coordenada final para avanzar
     
    public boolean select;  //bandera de seleccion de unidad
     
    public int AVZ;    //indica el tipo de nave que es para cuando se obtiene el neighbour para atacar
     
    //funcion para obtener x
    public int obtenerX()
    {
        return(this.getX());
    }
    //funcion para ontener y
    public int obtenerY()
    {
        return(this.getY());
    }
     
    public boolean checarAvanze(int Xactual, int Yactual, int Xavanze, int Yavanze, int avanze)
    {
         
        /*llegue a la conclusion que dado la posicion actual del objeto menos la posicion final y si se suman ambas
          posiciones (por medio de valores absolutos) deben de dar el avanze maximo de la unidad o menos*/
        /*System.out.println(Xactual+ " Xactual antes de la conversion a absoluto");   
        System.out.println(Yactual+" Yactual antes de la conversion");
        System.out.println(Xavanze+ " Xavanze antes de la conversion");   
        System.out.println(Yavanze+" Yavanze antes de la conversion");*/
        int resX = Math.abs( Xavanze-Xactual);   //primer y segundo valor (resta de posicion X y Y para obtener un valor de avanze)  
        int resY = Math.abs( Yavanze-Yactual);
        /*System.out.println(resX+ " resta de X");   
        System.out.println(resY+" resta de Y");*/
         
        if((resX+resY)>=0 && (resX+resY)<=avanze)    //evaluacion para regresar el booleano que diga si puede avanzar o no regresa verdadero en dado caso que la suma de Xs y Ys sea igual o menor al avanze y mayores o iguales a cero
        return(true);                                
        else
        return(false);
    }
     
    public void menuAvanze(int Xavanze, int Yavanze)
    {
        final JFrame a = new JFrame("Movimiento");
        final int x=Xavanze, y=Yavanze;
                 JButton botonA = new JButton("Avanza");
         
                 a.setLocation(350,300);
                 a.setSize(200,150);
                 a.getContentPane().setLayout(null);
         
                 botonA.setLocation(20,60);
                 botonA.setSize(90,40);
                 botonA.setEnabled(true);
                  
                 a.getContentPane().add(botonA);
                 a.setVisible(true);
         
                 botonA.addActionListener(new ActionListener(){
                 public void actionPerformed(ActionEvent e){
                      a.setVisible(false);
                      //if((pAvanzar=checarAvanze(Xactual,Yactual,Xavanze,Yavanze,avanze))==true)
                      setLocation(x,y);   //deberia avanzar a la locacion determinada
                      select=false;
                       
                 }
                 });
                 //return(b2);
                 //a.getContentPane().add(botonA);
                 Greenfoot.delay(100);
                  
    }
     
    public boolean puedoAvanzar(int Xactual, int Yactual, int Xavanze, int Yavanze)   //regresa falso si hay algo (excepto el mundo)
    {
        int xa = Math.abs( Xavanze-Xactual);   //primer y segundo valor (resta de posicion X y Y para obtener un valor de avanze)  
        int ya = Math.abs( Yavanze-Yactual);
         
        List algo = getObjectsAtOffset(xa,ya,null);   //son valores absolutos por que getobjectsatoffset maneja la posicion relativa al actor actual
         
        if(algo.isEmpty())     //checa si la lista esta vacia, de serlo asi no puedes avanzar a ea posicion
        {
            return true;
        }
        else
        {
            return false;
                    }
    }
     
    public void evaluarEnemigos()
    {
        //here i need to instanciate getNeighbours or getOneObjectAtOffset
         
    }
}
and his sons are 3 "NCjugador", "Fjugador" and "NBjugador" (here an exaple of NCjugador, the other 2 gets the same)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
public class NCjugador extends naves
{
     
    private int boton=0;
    public NCjugador()
    {
        //variables varias
        dineroDar = 500;
        avanze=3;
        alcanzeAtaque=4;
        //variable que indica si se puede avanzar o no
        avanzo=true;
        //variable que indica que si la casilla seleccionada para avanzar sea la adecuada en base el alcanze
        pAvanzar=false;
         
        hayLugar = true;
        //stats de ataques
        atkVSFragatas=1000;
        atkVSNavesdebatalla=5000;
        atkVSCazas=1000;
         
        //iniciacion de coordenadas actuales
        Xactual=0;
        Yactual=0;
         
        Xavanze=0;
        Yavanze=0;
         
        select=false;
         
        AVZ = 0;     //0 para cazas, 1 para fragatas y 2 para Naves de batalla
        //private int boton=0;
         
    }
     
    public boolean seleccionado()   //accion para entrar a movimiento
    {
        return select;
    }
     
     
    public void act()
    {      
        Xactual=obtenerX();     //obtencion de las coordenadas actuales del actor
        //System.out.println(Xactual+" coordenada en X");
        Yactual=obtenerY();
        //System.out.println(Yactual+" coordenada en Y");
        //select = true;
        //como ordenarle al raton que obtenga las nuevas coordenadas para que avanze?
        //Greenfoot.delay(200);
        //if(Greenfoot.mouseClicked(null))
        posicionMouse();
        System.out.println(Xavanze + "posicion a donde avanzo X");
        System.out.println(Yavanze+"posicion a donde avanzo Y" );
        System.out.println("*******finalizo movimiento\n\n\n");
    }   
     
     
    //intento por obtener nuevas coordenadas
    private void posicionMouse()  //control sobre el movimiento y ataque y todo
    {
          MouseInfo mouse = Greenfoot.getMouseInfo();  //obtencion de informacion sobre el mouse
          if(mouse != null)
          {
            int b1= Greenfoot.getMouseInfo().getButton();  //obtencion del click para el avanze
            if(b1==1 && select)
            
               Xavanze=mouse.getX();
               Yavanze=mouse.getY();
                
               //evalua que se pueda avanzar
               if(hayLugar=puedoAvanzar(Xactual,Yactual,Xavanze,Yavanze)==true)
               {
                   Greenfoot.delay(50);
                   if((pAvanzar=checarAvanze(Xactual,Yactual,Xavanze,Yavanze,avanze))==true)
                   {
                       //Greenfoot.delay(200);
                       menuAvanze(Xavanze,Yavanze);
                       evaluarEnemigos();
                   }
                   select=false;
                   hayLugar=false;
               }
               else
               {
                   select= false;
                   hayLugar=false;
                }
            }
            else if(Greenfoot.mouseClicked(this) && select!=true)    //checa si se clickeo y entra a
            {
                select = true;
            }
          }
    }
}
the enemies gets the same structure (they serch for the enemy, advance and if they are 1 square close attack) and the same class structure ( "navesEnemigo" is the father and "NCenemigo", "Fenemigo" and "NBenemigo" are the sons) if i use "instanciateof" i need to call "if(actorList(0) instanceof naves enemigo)" i could reach any of the sons of that class? (really i dont understand how to reach the sons) thanks for your help and your time
danpost danpost

2014/5/21

#
To simplify things (and also, so you will have no need to try and figure out which son you are dealing with), you should move any and all code that is duplicated in the classes of the sons out of all those classes and into the class of their father. That means if all three son classes have methods (like 'act', 'posicionMouse', and 'seleccionado') with the exact same code, then the method should go in the class of the father and removed from all the classes of the sons. The same goes with the navesEnemigo class and its sons. This way if you need a field value or need to run a method on an object belonging to one of the sons, you can do it from the class of the father without the need to figure out which sons class it is from. I was looking at your 'checarAvanze' method; I was a bit bewildered when I saw that you were getting the absolute value of the differences in the coordinates. With that, you will always be looking to the right and below -- never anywhere else. I think you just need the differences -- not the absolute value of them.
larg5847 larg5847

2014/5/21

#
Hi, ehm, no, in "checarAvanze" i compare if ia can advance to a certain position, the absolute value is to chech if i not passing the amount of square i can advance (in this case, if i could advance 3 squares the sum of the abs will be 3 or less (example: if my player are in the (6,3) position in the wolrd (Xactual, Yactual) and i need to move to (4,4) ill substract (4-6) and (4-3) equal to (2 & 1) and if i plus it the result are (3) (the value of "avanze") and i can advance (because actually have the next position (Xavanze, Yavanze). and i got your point, i move all my methods to the father and experiment with it thanks for your support n_n
danpost danpost

2014/5/21

#
Sorry, I mentioned the wrong method above. I was meaning the 'puedoAvanzar' method where you use the absolute values in the 'getObjectsAtOffset' call at line 95. That is what I meant by only looking to the right and below. That is what was bewildering me.
larg5847 larg5847

2014/5/21

#
ah, yes, in that method, yes, i posted first and then noticed that, and yes, i couldnt move a player backwards. and i been exprimented with a list like List <FCenemigo> ,<NCenemigo....> enemigos = getNeighbours(1,false,FCenemigo.class, NCenemigo....); (check 3 times with 3 diferent list) if i check like that ill always get the type of the enemy directly? i got all the enemies with that class (but i still dont know how to attack a certain enemy, i know that the way to get the neibourghs are getting 1 2 Player 3 4
danpost danpost

2014/5/21

#
You should be able to use just:
1
List<navesEnemigo> nearEnemies = getNeighbours(1, false, navesEnemigo.class);
This gets one list with all neighboring enemies. Then you can randomly pick one with:
1
navesEnemigo enemy = (navesEnemigo) nearEnemies.get(Greenfoot.getRandomNumber(nearEnemies.size()));
if the list is not empty:
1
if (!nearEnemies.isEmpty())
In total, this would look like the following:
1
2
3
4
5
6
List<navesEnemigo> nearEnemies = getNeighbours(1, false, navesEnemigo.class);
if (!nearEnemies.isEmpty())
{
    navesEnemigo enemy = (navesEnemigo) nearEnemies.get(Greenfoot.getRandomNumber(nearEnemies.size()));
    // etc. (attack 'enemy')
}
larg5847 larg5847

2014/5/22

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void evaluarEnemigos()
    {
        List <navesenemigo> nearEnemies = getNeighbours(1,false,navesenemigo.class);
         
        if(!nearEnemies.isEmpty())
        {
            for(navesenemigo a : nearEnemies)
            {
                 
               navesenemigo enemy = (navesenemigo) a;
               enemy.soy();
            }
        }
    }
thanks, i evaluated with that method. enemy.soy(); access to a method in the sons that print which enemy are, and works, now i could evaluate the 4 enemies and attack. thanks for the help and support.
larg5847 larg5847

2014/5/22

#
PD: isn't my final method, i only experiment if i could get (object).soy();
You need to login to post a reply.