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

2013/10/18

Let objects of the same class interact

sihaco sihaco

2013/10/18

#
Hi people, I'm working on an airport simulation and I need objects of the class Aircraft to interact with each other according to their positions. What should happen is that aircraft come in for landing (this works and every 10 seconds a new object of Aircraft is being created and lands). At the end of the runway, the aircraft goes to a gate and stops. But now, every object of the class Aircraft goes to the same gate. What I need is to let all aircraft know if there's already another aircraft at a specific gate. If this is the case, the aircraft that has just landed must taxi to another gate. This is what I have now:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Aircraft here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Aircraft extends Actor
{
    public int gatePosition;
    /**
     * Act - do whatever the Aircraft wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Aircraft()
    {
        setRotation(-90);
    }
    
    public void act() 
    {
        move();
        checkPlanePosition();
    }   
    
    // landing and taxiing to gate number one
    public void move()
    {
        if (getX() == 70 && getY() == 102){
            turn(90);
            move(2);
        }
        if (getX() == 694 && getY() == 102){
            turn(90);
            move(2);
        }
        if (getX() == 694 && getY() == 444){
            move(0);
            gatePosition = 1;
        }
        else{
            move(2);
        }
    }
    
    // check if gate number 1 is full and taxi to another gate if this is the case
    public void checkPlanePosition()
    {
        if (gatePosition != 1 && getX()!= 694 && getY() != 444){
            // taxi to another gate if gate number 1 is full
        }
    }
}
The field public int gatePosition should have a value. When an aircraft is parked at gate number one, this variable gets a value of 1. This also works, but now I need other objects of the class Aircraft to know that another aircraft already has the value of one. When this is the case, the second aircraft should go to the another gate (the method for taxiing to another gate is not a problem, but I need a correct if statement to let another object understand that the previous aircraft is already at gate number one. Inside this if statement, the method for taxiing to another gate will be called. Who knows how I can do this? Cheers!
danpost danpost

2013/10/18

#
The following is a method that will return whether a specific gate is being used or not.
private boolean isGateUsed(int gNum)
{
    for (Object obj : getWorld().getObjects(Aircraft.class))
    {
        Aircraft aircraft = (Aircraft) obj;
        if (aircraft.gatePosition == gNum) return true;
    }
    return false;
}
You could even randomize the gate usage a bit with the following:
int gNum = Greenfoot.getRandomNumber(/* number of gates */);
while (isGateUsed(gNum)) gNum = Greenfoot.getRandomnumber(/* number of gates */);
// taxiToGate(gNum);
Be aware, however, that this will only work as long as you have gates available for every aircraft. You may want to create another method to check if a gate is available at all and use it before this last code snippet.
sihaco sihaco

2013/10/18

#
Where should I put this method? In Gate or Aircraft?
danpost danpost

2013/10/18

#
In Aircraft. You did want each aircraft to know whether a gate was available or not.
You need to login to post a reply.