Thursday 8 January 2009

The Scene and Zone classes

The Scene and Zone classes. The comments say it all. The Scene is the equivalent of the "Room" in adventure-speak.
I can see a future need for a method that changes the destination of a zone. It would be of the form - change the destination of the nth zone in the scene which points to scene A to scene B (the normal value of n would be zero - i.e. the only zone).

// Scene class - this is the background to a scene and includes:
// the Zone class - a Zone is a rectangular area within a scene,
// usually for the purpose of enabling the player to click at
// a portion of the scene in order to move to another scene.

import java.util.Vector;
import java.awt.*;
public class Scene extends Object
{
public String name; // Text name of scene
public String imagename; // image file name for scene
public String songname; // text song name
private Vector zones; // list of zones within the scene

// Zone Class
public class Zone extends Object
{
public Rectangle rect; // extent of zone
public String destination; // text name of destination scene
public int cursor; // cursor to display while in zone
public boolean cursoron; // whether cursor displays
public Zone(Rectangle r, String d)
{
rect = new Rectangle(r);
destination = new String(d);
cursoron = false;
}
public Zone(int x, int y, int w, int h, String d, int Cursor)
{
rect = new Rectangle(x, y, w, h);
destination = new String(d);
cursor = Cursor;
cursoron = true;
}
public boolean inZone(int x, int y)
{
if (rect.contains(x, y))
return true;
return false;
}
}
public Scene(String n, String in)
{
name = new String(n);
imagename = new String(in);
zones = new Vector();
songname = new String("");
}
public Scene(String n, String in, String song)
{
name = new String(n);
imagename = new String(in);
zones = new Vector();
songname = new String(song);
}
public void addZone(int x, int y, int w, int h, String d, int e)
{
Zone z = new Zone(x, y, w, h, d, e);
zones.addElement(z);
}
public String getDest(int x, int y)
{
int ii;
Zone tz;
if (zones == null)
return null;
for (ii = 0; ii < zones.size(); ii++)
{
tz = (Zone)zones.elementAt(ii);
if (tz.inZone(x, y))
return tz.destination;
}
return null;
}
public int getZoneCursor(int x, int y)
{
int ii;
Zone tz;
if (zones == null)
return -1;
for (ii = 0; ii < zones.size(); ii++)
{
tz = (Zone)zones.elementAt(ii);
if (tz.inZone(x, y) && tz.cursoron)
return tz.cursor;
}
return -1;
}
}

No comments: