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

2021/2/18

Ennemy checking if player is close?

AnJoMorto AnJoMorto

2021/2/18

#
I'm trying to make a game where a skeleton moves in the direction of the player if he sees him, so when he is close enough and in the more and less same Y if possible. I need to complete this code to do it, but I can't figure out how. Does anyone have an idea?
        int currentX = this.getX();
        int currentY = this.getY();
        int sight    = 150;
        List players = getWorld().getObjects(Player.class);
        if (!players.isEmpty())
        {
            
            Actor player    = (Actor)players.get(0);
            int playerX     = player.getX();
            int playerY     = player.getY();
            int dir = 0;
            
            if(playerX > currentX) /** && if Player is in sight --> 150px away in X (and only 10 away in Y if possible) */
            {
                
                setImage(walk.getCurrentImage());
                move(speed);
                dir++;
                
            }        
            if(playerX < currentX) /** && if Player is in sight --> 150px away in X (and only 10 away in Y if possible) */
            {
                
                setImage(walkL.getCurrentImage());
                move(-speed);
                dir--;
                
            }        
}
danpost danpost

2021/2/18

#
List players = getWorld().getObjects(Player.class);
if ( ! players.isEmpty())
{
    Actor player = (Actor)players.get(0);
    int offX = player.getX()-getX();
    int distX = Math.abs(offX);
    int dirX = (int)Math.signum(offX);
    int offY = player.getY()-getY();
    int distY = Math.abs(offY);
    if (distX <= 150 && distY <= 10)
    {
        move(dirX*speed);
        setImage( (dirX < 0 ?  walk : walkL).getCurrentImage() );
    }
}
RcCookie RcCookie

2021/2/18

#
The distances can easily be calculated from the player‘s and the skeletons coordinates:
int xDist = Math.abs(currentX - playerX);
int yDist = Math.abs(currentY - playerY);
Now you just need to check weather that’s less than the allowed distance inside the if clauses. Note: Line 4: Don’t write
List players = …
List is a generic type, that means that it can work with different types. In the case of list this means: what’s in the list? And you specify that with the call
getWorld().getObjects(Player.class);
This method from world only returns Player objects. To be more precise, it returns a list of players. So the variable players should be of type List<Player>:
List<Player> players = …
Now Java knows that players is a list that can only contain players. And this is an advantage: you can remove the „(Actor)“ cast in line 8. Before there could have been anything in the list, but now there can only be players. so you could even say:
Player player = players.get(0);
RcCookie RcCookie

2021/2/18

#
One more thing: Looking at performance, it does not make a lot of sense to first get the location of the object (lines 1+2) and then check if you even need it in line 5. Move lines 1 and 2 into the if statement, then they won’t check if there is no player in range. This may not have a big impact on this case but still is good practice.
RcCookie RcCookie

2021/2/18

#
danpost wrote...
List players = getWorld().getObjects(Player.class);
if ( ! players.isEmpty())
{
    Actor player = (Actor)players.get(0);
    int offX = player.getX()-getX();
    int distX = Math.abs(offX);
    int dirX = (int)Math.signum(offX);
    int offY = player.getY()-getY();
    int distY = Math.abs(offY);
    if (distX <= 150 && distY <= 10)
    {
        move(dirX*speed);
        setImage( (dirX < 0 ?  walk : walkL).getCurrentImage() );
    }
}
What does Math.signum(double)?
danpost danpost

2021/2/18

#
RcCookie wrote...
What does Math.signum(double)?
It returns the sign of the argument (signum means "sign number"): if double > 0 (positive), returns 1; if double = 0 (zero), returns 0; if double < 0 (negative), returns -1;
danpost danpost

2021/2/18

#
danpost wrote...
setImage( (dirX < 0 ?  walk : walkL).getCurrentImage() );
I have "walk" and walkL" reversed, there.
RcCookie RcCookie

2021/2/18

#
Oh, that’s helpful
AnJoMorto AnJoMorto

2021/2/19

#
Thank you for both of you! It was really what I needed plus I got to understand it a little more! Many thanks!
You need to login to post a reply.