You do not plan to ever have 2 on the same panel; but, you could have more than one in the world :\
What about the astronaut being on the same panel as the player; is that something that could happen?
public boolean actorAhead(Class cls) { int x = getX(), y = getY(); // save current location move(50); // move forward if (Math.abs(x-getX())+Math.abs(y-getY()) != 50) { // on edge panel facing edge setLocation(x, y); // return to original location return false; // indicate no instances found } // get root coordinates of panel x = (getX()/50)*50; y = (getY()/50)*50; move(-50); // move back to original location for (Object obj : getWorld().getObjects(Astronaut.class)) { // for all astronauts in world Actor actor = (Actor)obj; // reference an astronaut as Actor // if not within limits, process next astronaut if (actor.getX() < x) continue; if (actor.getX() >= x+50) continue; if (actor.getY() < y) continue; if (actor.getY() >= y+50) continue; // astronaut within limits if here return true; // indicate one was found } return false; // no astronauts found ahead }
public Actor actorAhead(Class cls) { int x = getX(), y = getY(); // save current location setRotation(90*direction); move(50); // move forward if (Math.abs(x-getX())+Math.abs(y-getY()) != 50) { // on edge panel facing edge setLocation(x, y); // return to original location setRotation(0); return null; // indicate not found } // get root coordinates of panel x = (getX()/50)*50; y = (getY()/50)*50; move(-50); // move back to original location setRotation(0); for (Object obj : getWorld().getObjects(Astronaut.class)) { // for all instances of given class in world Actor actor = (Actor)obj; // reference an instance as Actor // if not within limits, process next actor if (actor.getX() < x) continue; if (actor.getX() >= x+50) continue; if (actor.getY() < y) continue; if (actor.getY() >= y+50) continue; // astronaut within limits if here return actor; // indicate which one found } return null; // no actors of given class found ahead }
public void interact() { Actor astronaut = actorAhead(Astronaut.class); String key = Greenfoot.getKey(); if(astronaut != null) { Astronaut astro = (Astronaut)astronaut; if (key.equals("enter")) { getWorld().addObject(new DialogueWindow(astro.headImage, astro.line0), getWorld().getWidth() / 2, getWorld().getHeight() / 2); } } }