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

2015/1/5

Basic AI Difficulties

KingCowWill KingCowWill

2015/1/5

#
I am creating a Star Wars game for a school project and am having trouble creating an AI system for the enemies (TIE fighters). What I would like them to do is attack our controlled actor (the falcon) and then fly away. With my basic coding knowledge, all I have been able to do is make them follow the falcon around and shoot at it.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class TIEFighter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TIEFighter extends ScrollActor
{
    /**
     * Act - do whatever the TIEFighter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public static int enemyRot;
    private boolean onScreen = false;
    public void act() 
    {
        if(getX() <= 0)
            onScreen = false;
        else if(getX() >= 960)
            onScreen = false;
        else if(getY() <= 0) 
            onScreen = false;
        else if(getY() >= 540)
            onScreen = false;
        else onScreen = true; 
        if(onScreen == true)
        {
            moveTIE();
            shootTIE();
            turnTIE();
        }
        enemyRot = getRotation();

    }
    public void moveTIE()
    {
        move(3);
    }

    public void turnTIE()
    {
        if(Greenfoot.getRandomNumber(2) < 1)
        {
            turnTowards(Spaceship.currentX + (Greenfoot.getRandomNumber(2)), Spaceship.currentY + (Greenfoot.getRandomNumber(2)));
        }
        else turnTowards(Spaceship.currentX - (Greenfoot.getRandomNumber(2)), Spaceship.currentY - (Greenfoot.getRandomNumber(2)));
    }

    public void shootTIE()
    {
        if(Greenfoot.getRandomNumber(50) < 1)
        {
            getWorld().addObject(new Enem_Bullet(), getGlobalX(), getGlobalY());
            Greenfoot.playSound("TIE-Fire.mp3");
        }

    }
}
danpost danpost

2015/1/5

#
Sounds like you need an int field for a timer. Set it to some random number to start and decrease it every act cycle until it reaches zero. Then instead of decreasing it follow the falcon. You will need something to have the TIE fighter stop the current attack, where the timer is reset to a new random number to repeat the cycle. You could add another int field to count act cycles or shots fired for a multiple approach attack. Or, you could use the proximity of the falcon for a one-time approach attack. Just ideas. Hope it helps. I was wondering about your 'static int enemyRot' field. What is the purpose of this field and why is it a class field instead of an object instance field? It just seems to me that it is just excessive (not being used within the class) and duplicative (as all actors already have a current rotation store within them through the Actor class) There also does not appear to be any reason for the 'onScreen' variable to be an instance field. Its values does not need to be retained between act cycles (its value is set each act cycle by the first few lines in the act method). It would do just fine as a local variable within the act method:
public void act()
{
    boolean onScreen = true;
    if ( getX() < 0 ||
         getX() >= 960 ||
         getY() < 0 ||
         getY() > 540 )
            onScreen = false;
    if (onScreen) // etc.
}
KingCowWill KingCowWill

2015/1/5

#
Thanks for your help, I will try to put these ideas to use and the enemyRot is just getting the rotation for the bullet that comes out of the TIE fighter. Most of the code I use is just trial and error until it works, or just grabbed from other people, and I was just trying to get used to the scroll world.
danpost danpost

2015/1/5

#
KingCowWill wrote...
enemyRot is just getting the rotation for the bullet that comes out of the TIE fighter.
Although that method of getting the rotation to the bullet may work, it is not good programming -- to use a class field for something that should belong to an object of the class. There are other ways to get the bullet to rotate the proper way. You can break line 55 above into its components, like this:
Enem_Bullet bullet = new Enem_Bullet();
getWorld().addObject(bullet, getGlobalX(), getGlobalY());
The first line gets a local reference to the bullet created. Then insert the following line in between those two lines:
bullet.setRotation(getRotation());
You need to login to post a reply.