The player has to "explore" the environment first in order to get that information. Once an area has been "explored", then it becomes visible.
We will demonstrate how to do this, by using the ants scenario. First we need a new class of objects which act as a cover. We name it Fog in this case. The object actually does nothing. If it exists it hides a part of the world behind it. If the covered area is "explored" the corresponding object shall be removed and the area becomes "clear" and visible.
After creating the Fog class, open the Ant class and make a modification as displayed below.
private void openScene()
{
Fog fog = (Fog) getOneIntersectingObject(Fog.class);
if(fog != null)
getWorld().removeObject(fog);
}
Public void act()
{
// ...
openScene();
}
The method OpenScene() will remove a Fog object that intersects with the corresponding Ant object. There could actually be more than one Fog object intersecting with the ant. But since in the ants scenario we are usually dealing with many Ant objects that continuously move, we do not necessarily remove all intersecting Fog objects at a time. The situation is different for the AntHill object. Here we need to remove all intersecting Fog objects (see the code snippet below)
private void openScene()
{
List fogs = getIntersectingObjects(Fog.class);
Iterator i = fogs.iterator();
while(i.hasNext()) {
Actor a = (Actor) i.next();
getWorld().removeObject(a);
}
}
public void act()
{
// ...
openScene();
}
public void addedToWorld(World world)
{
openScene();
}
The method addedToWorld() needs to be defined, to get the effect immediately works, once an AntHill object is created and placed to the world.
Finally, we need to initialise and create Fog objects to hide all regions in the world for the first time. As can be seen below, this is handled by the method makeFog() which has to be called before scenario3() in the AntWorld() constructor.
public AntWorld()
{
// ...
makeFog();
scenario3();
}
public void makeFog()
{
for(int i=0;i<=SIZE-1;i =25) {
for(int j=0;j<=SIZE-1;j =25) {
addObject(new Fog(), i, j);
}
}
}
The following two screenshots illustrating the effect of the above mentioned modifications.

Starting condition indicating the effect of makeFog() in the AntWorld constructor and openScene() inside the AntHill class.

A condition in the AntWorld after the simulation runs for a while
Webpage Keywords : Java programming, Java game, Greenfoot scenario
No comments:
Post a Comment