Saturday, 10 January 2009

The BridgeData class

The BridgeData class varies from game to game. The first part defines all the scenes, zones and sprites. The second part has a number of service routines that deliver the information to calling methods.

There are a couple of method names that I would change for the sake of clarity, specifically:
setCurrentScene() and getCurrentScene(), which really should be called: setCurrentSceneName() and getCurrentSceneName().


// This is the class that defines all the scenes, zones and sprites
import java.util.Vector;
import java.awt.*;
import java.applet.AudioClip;
import java.net.URL;

public class BridgeData extends Object
{
private Vector scenes;
private Vector sprites;
private Scene scene;
private String currscene;
public String startup = "waitintro";
protected Bridge owner;
public BridgeData(Bridge theOwner)
{
owner = theOwner;
scenes = new Vector();
sprites = new Vector();
// Put scenes in here
//inventory
scene = new Scene ("inventory", "inventory.gif");
scene.addZone(20, 20, 80, 80, "", Cursor.CROSSHAIR_CURSOR);
scenes.addElement(scene);
//waitintro
scene = new Scene ("waitintro", "instr.gif");
scene.addZone(238, 176, 164, 124, "intro", Cursor.CROSSHAIR_CURSOR);
scenes.addElement(scene);
//wait
scene = new Scene ("wait", "hourglass.jpg");
scenes.addElement(scene);
//intro
scene = new Scene ("intro", "china.jpg", "c12");
scene.addZone(100, 310, 70, 60, "dock", Cursor.CROSSHAIR_CURSOR);
scenes.addElement(scene);
//dock
scene = new Scene ("dock", "dock.jpg", "ara");
scene.addZone(200, 210, 100, 70, "doornbox", Cursor.CROSSHAIR_CURSOR);
scene.addZone(0, 0, 640, 60, "dockup", Cursor.S_RESIZE_CURSOR);
scene.addZone(0, 0, 60, 480, "dockback", Cursor.E_RESIZE_CURSOR);
scene.addZone(580, 0, 60, 480, "dockback", Cursor.E_RESIZE_CURSOR);
scenes.addElement(scene);
//dockback
scene = new Scene ("dockback", "dockback.jpg", "ara");
scene.addZone(0, 0, 60, 480, "dock", Cursor.E_RESIZE_CURSOR);
scene.addZone(580, 0, 60, 480, "dock", Cursor.E_RESIZE_CURSOR);
scene.addZone(200, 210, 100, 70, "intro", Cursor.CROSSHAIR_CURSOR);
scenes.addElement(scene);
//dockup
scene = new Scene ("dockup", "dockup.jpg", "ara");
scene.addZone(0, 430, 640, 50, "dock", Cursor.S_RESIZE_CURSOR);
scenes.addElement(scene);
//doornbox
scene = new Scene ("doornbox", "doornbox.jpg");
scene.addZone(0, 430, 640, 50, "dock", Cursor.S_RESIZE_CURSOR);
scenes.addElement(scene);
//Put sprites in here
RippleA ripplea = new RippleA("ripplea", "intro");
sprites.addElement(ripplea);
GullA gulla = new GullA("gulla", "intro", 480, 40, 0);
sprites.addElement(gulla);
GullA gullb = new GullA("gullb", "intro", 370, 200, 78);
sprites.addElement(gullb);
GullA gullc = new GullA("gullc", "dockback", 80, 50, 12);
sprites.addElement(gullc);
GullA gulld = new GullA("gulld", "dockback", 150, 200, 78);
sprites.addElement(gulld);
// Put carryables here
GoldKey goldkey = new GoldKey("goldkey", "dock");
sprites.addElement(goldkey);
Zilch dockkeyhole = new Zilch("dockkeyhole", "doornbox");
dockkeyhole.setPosition(200, 240);
URL addr = owner.getCurrentUrl("sounds/rooster.au");
dockkeyhole.addAudio(0, owner.getAudioClip(addr));
sprites.addElement(dockkeyhole);
}
public boolean collide(String spritename, String hitter)
{
if (hitter == null)
{
return true;
}
else
{
if (spritename.equals("dockkeyhole") && (hitter.equals("goldkey")))
{
Sprite found = getSpriteByName(spritename);
if (found != null)
{
AudioClip auclip = found.getAudio(0);
auclip.play();
return true;
}
}
}
return false;
}

// Above here are game-specific functions
// Below here are general purpose functions
public Scene fetchScene(String a)
{
int ii;
if (scenes == null)
{
return null;
}
for(ii = 0; ii < scenes.size(); ii++)
{
scene = (Scene)scenes.elementAt(ii);
if (a.equals(scene.name))
{
return scene;
}
}
return null;
}
public Scene fetchCurrentScene()
{
return (fetchScene(currscene));
}
public void setCurrentScene(String name)
{
currscene = new String(name);
}
public String getCurrentScene()
{
return(currscene);
}
public Vector getSceneSprites(String sceneName)
{
Vector local = new Vector();
Sprite temp = null;
for (int ii = 0; ii < sprites.size(); ii++)
{
temp = (Sprite)sprites.elementAt(ii);
if (temp.scenename.equals(sceneName))
{
local.addElement(temp);
}
}
return local;
}
public Vector getCurrentSprites()
{
return getSceneSprites(currscene);
}
public Vector getAllSprites()
{
return sprites;
}
public Sprite getSpriteByName(String name)
{
Sprite temp;
for (int ii = 0; ii < sprites.size(); ii++)
{
temp = (Sprite)sprites.elementAt(ii);
if (temp.getName().equals(name))
{
return(temp);
}
}
return null;
}
}

No comments: