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

2016/9/18

Random timed movement, ranged player detection and camera tracking

2
3
4
5
6
7
8
danpost danpost

2016/9/28

#
For the other error, there should not be any line of sight checking in the 'obstructedMovement' method. It should only contain the code for the actions of the enemy when we already know that the view is obstructed. If you want that the enemy move randomly, you might just try removing the 'obstructedMovement' method and changing the call to it at line 64 to 'randomMovement'.
Parte Parte

2016/9/28

#
danpost wrote...
For the other error, there should not be any line of sight checking in the 'obstructedMovement' method. It should only contain the code for the actions of the enemy when we already know that the view is obstructed. If you want that the enemy move randomly, you might just try removing the 'obstructedMovement' method and changing the call to it at line 64 to 'randomMovement'.
Well actually there are two things that I want the enemy class to do which involve line of sight being obstructed; one of them being that as you said for when line of sight is obstructed the enemy will execute the randomMovement method. The second mechanic that I want to use is that if the player line of sight becomes obstructed during the pursue method the enemy will move forward for x amount of seconds which if during the forward movement if the player is within range and has no objects obstructing its line of sight it will pursue the player, else the enemy will proceed to do randomMovement method. Also I tried to add some classes to which would be considered obstructions however the Enemy AI ignores those walls and proceeds to pursue the player regardless of there being a wall class there.
public class Enemy_Test3 extends Animal
{
    private SimpleTimer timer = new SimpleTimer();
    private boolean turnPhase;
    private int phaseTimer = -1;
    private Actor pursued;
    private int obstacleTimer;
    private int bulletTimer;
    
    public void act() 
    {
        
        if (pursued != null && pursued.getWorld() == null)
        {
            pursued = null;
        }
        
        if (pursued == null)
        {
            if (getObjectsInRange(800, Player_Test1.class).isEmpty())
            {
                randomMovement(); // replace with appropriate code or method call
                return;
            }
            else
            {
                pursued = (Actor)getObjectsInRange(800, Player_Test1.class).get(0);
            }
           
        }
        
        if (pursued != null && pursued.getWorld() != null)
        {
            if (obstacleTimer == 0)
            {   
                if (!los.obstructedView(this, pursued))
                {
                    obstacleTimer = 100;
                }
            
                else
                {
                    pursue(); // replace with appropriate code or method call
                    return;
                }
            }   
        }
        
        if (obstacleTimer > 0)
        {
            if (--obstacleTimer == 0)
            {
                if (!los.obstructedView(this, pursued))
                {   
                    pursued = null;
                }
            }
            else
            {
                if (!los.obstructedView(this, pursued, Walltest1.class, Walltest2.class))
                {
                    randomMovement(); // replace with appropriate code or method call
                    return;
                }
                else
                {
                    obstacleTimer= 0;
                    pursue(); // replace with appropriate code or method call
                    return;
                }
            }
        }
            else
            {
                pursue(); // replace with appropriate code or method call
            }
    
        }   
    
    
    public void randomMovement()
    {
        // control code (when out of range of or path obstructed toward player)
        int sign = (int)Math.signum(phaseTimer); // '1' means moving or turning and '-1' means stopped after moving or turning
        phaseTimer -= sign; // step toward zero
        if (phaseTimer == 0)
        {
            if (sign == -1)
            {
                if (turnPhase) phaseTimer = 20+Greenfoot.getRandomNumber(150);
                else phaseTimer = 1;
                turnPhase = !turnPhase;
            }
            else
            {
                if (turnPhase) phaseTimer = -(20+Greenfoot.getRandomNumber(360));
                else phaseTimer = -(20+Greenfoot.getRandomNumber(50));
            }
        }
             
        if (sign > 0)
        {
            if (turnPhase) turn(Greenfoot.getRandomNumber(360)); else move(6);
        }

        if (atWorldEdge() || isTouching(Walltest1.class) || isTouching(Walltest2.class))
        {
            turn(160+Greenfoot.getRandomNumber(180)-90);
        }
         
        int x1 = getX(), y1 = getY();
        
        if (isTouching(Walltest1.class) || isTouching(Walltest2.class))
        {
            setLocation(x1, y1);
        }
    }
    
    public void pursue()
    {
        
        if (!getObjectsInRange(800,Player_Test1.class).isEmpty())
        {
            int x = getX(), y = getY();
            Player_Test1 player_test1 = (Player_Test1) getWorld().getObjects(Player_Test1.class).get(0);
            turnTowards(player_test1.getX(), player_test1.getY());
            move(10);
            shoot();
            
        }
        
        if (atWorldEdge() || isTouching(Walltest1.class) || isTouching(Walltest2.class))
        {
            turn(160+Greenfoot.getRandomNumber(180)-90);
        }
         
        if (isTouching(Walltest1.class) || isTouching(Walltest2.class))
        {
            int x1 = getX(), y1 = getY();
            setLocation(x1, y1);
        }
    }
Super_Hippo Super_Hippo

2016/9/28

#
The obstructedView class returns true when there is something between the two checked objects, so the pursue method should execute if false is returned. So remove the '!' in lines 36, 53 and 60. Btw, the extra '&& pursued.getWorld() != null' I suggested to add last time (line 32) is not needed anymore with the new beginning of the act method.
Parte Parte

2016/9/28

#
Super_Hippo wrote...
The obstructedView class returns true when there is something between the two checked objects, so the pursue method should execute if false is returned. So remove the '!' in lines 36, 53 and 60. Btw, the extra '&& pursued.getWorld() != null' I suggested to add last time (line 32) is not needed anymore with the new beginning of the act method.
Thanks, fixed part of the problem. However I have tested that add some classes to which would be considered obstructions however if there are 2 or more Enemy_Test3 classes that are within range and have a wall class obstructing their view, they will ignore those walls and proceeds to pursue the player regardless of there being a wall class there.
danpost danpost

2016/9/28

#
You might want to create a subclass of Actor called Obstruction and then have all your wall classes extend it. That way, you only need place the one class name in as a parameter for the 'obstructedView' method. In your 'persue' method, you do not need to get the actor to pursue. It is already assigned to the 'persued' field. So, lines 122 through 130 can simply be this:
int x = getX(), y = getY();
turnTowards(pursued.getX(), persued.getY());
move(10);
shoot();
Parte Parte

2016/9/28

#
danpost wrote...
You might want to create a subclass of Actor called Obstruction and then have all your wall classes extend it. That way, you only need place the one class name in as a parameter for the 'obstructedView' method. In your 'persue' method, you do not need to get the actor to pursue. It is already assigned to the 'persued' field. So, lines 122 through 130 can simply be this:
int x = getX(), y = getY();
turnTowards(pursued.getX(), persued.getY());
move(10);
shoot();
Thanks, however the issue of my Enemy_Test3 class pursuing the player if there are 2 or more Enemy_Test3 classes in the world that have the player in range but have obstructions in the way still ignore those obstructions and proceed to attack the player and sometimes even go through the walls
danpost danpost

2016/9/28

#
Parte wrote...
Thanks, however the issue of my Enemy_Test3 class pursuing the player if there are 2 or more Enemy_Test3 classes in the world that have the player in range but have obstructions in the way still ignore those obstructions and proceed to attack the player and sometimes even go through the walls
As far as going through walls, that code is part of the movement and should be dealt with in the 'randomMovement' and 'pursue' methods (or in methods they call). As for the other thing, what exactly is the issue of your Enemy_Test3 class pursuing the player?
Parte Parte

2016/9/28

#
danpost wrote...
Parte wrote...
Thanks, however the issue of my Enemy_Test3 class pursuing the player if there are 2 or more Enemy_Test3 classes in the world that have the player in range but have obstructions in the way still ignore those obstructions and proceed to attack the player and sometimes even go through the walls
As far as going through walls, that code is part of the movement and should be dealt with in the 'randomMovement' and 'pursue' methods (or in methods they call). As for the other thing, what exactly is the issue of your Enemy_Test3 class pursuing the player?
The issue is that if there is 2 or more Enemy_Test3 objects in the world they will still pursue the player if the player is in range and have wall classes obstructing their view. If you would like I could try and upload my scenario so that you can better understand what I am talking about.
danpost danpost

2016/9/28

#
Parte wrote...
If you would like I could try and upload my scenario so that you can better understand what I am talking about.
That might be best at this point.
Parte Parte

2016/9/28

#
danpost wrote...
Parte wrote...
If you would like I could try and upload my scenario so that you can better understand what I am talking about.
That might be best at this point.
I tried to publish it, apparently it is too large. How else could you see it?
danpost danpost

2016/9/28

#
Chances are that you have multiple large sound files. Create a copy of the scenario (use "Save as") and reduce it by eliminating any and all sound files. Then, remove all code relate to those files (you can let the error checking lead you to those). Then, upload was remains. Btw, you will have to go to the "sounds" folder of the scenario to delete the sound files.
Parte Parte

2016/9/29

#
danpost wrote...
Chances are that you have multiple large sound files. Create a copy of the scenario (use "Save as") and reduce it by eliminating any and all sound files. Then, remove all code relate to those files (you can let the error checking lead you to those). Then, upload was remains. Btw, you will have to go to the "sounds" folder of the scenario to delete the sound files.
Sorry for the late reply. Here it is http://www.greenfoot.org/scenarios/17690 If you move the player towards the wall the Enemies will still "pursue" the player if there are more than one Enemy_Test3 objects that have obstructedView to the player
Super_Hippo Super_Hippo

2016/9/29

#
Are all three Enemies from the same class? Well, they also move through the wall. To avoid this, you should put line 106 through 116 at the end of the act-method, line 111 at the beginning of act method and remove 132 through 141. As it is right now, it reads like 'if it touches a wall, do nothing'. Btw, I am not sure if line 108 is what it should. Right now it is the same as:
turn(70+Greenfoot.getRandomNumber(180));
But shouldn't it be the following?
turn(90+Greenfoot.getRandomNumber(181));
Maybe this problem will be fixed then. Even the player can move into the wall...
Parte Parte

2016/9/29

#
Super_Hippo wrote...
Are all three Enemies from the same class? Well, they also move through the wall. To avoid this, you should put line 106 through 116 at the end of the act-method, line 111 at the beginning of act method and remove 132 through 141. As it is right now, it reads like 'if it touches a wall, do nothing'. Btw, I am not sure if line 108 is what it should. Right now it is the same as:
turn(70+Greenfoot.getRandomNumber(180));
But shouldn't it be the following?
turn(90+Greenfoot.getRandomNumber(181));
Maybe this problem will be fixed then. Even the player can move into the wall...
After changes suggested the Enemy class completely ignores the walls (note that the enemies did turn if they reached a wall if the player was not in range). As for the player moving through the wall is partially true, what I mean by this is that the Player class is attached to a Movement class (so it follows the Movement class) that is solid and cannot go through walls; so essentially the player goes through the walls partially. This was done to avoid the player being caught in walls if the player was changing the direction it was facing while up against a wall class (player would've merged with the wall class if I did not separate the movement code to another class). The values at line 108 were just arbitrary values except for the negative values which would allow the enemy to turn in both directions. Also a final note is that I am not having trouble with the Enemy class detecting walls during randomMovement, it is when the player is being pursued is when it ignores those wall classes.
Super_Hippo Super_Hippo

2016/9/29

#
Ah, I know why. Hm, you could try to remove all 'return' in the act method.
There are more replies on the next page.
2
3
4
5
6
7
8