Allow whirled.spot.data.Location to be extended so that games

with 3d coordinate systems (or that track additional location information
besides orientation) can just drop in their new Location class.
Refactored Portal to contain a Location object rather than duplicate
the fields, so that Portals can work unchanged with a 3d coordinate system.

Note: I haven't yet updated SpotSceneRuleSet.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4072 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2006-04-28 01:34:02 +00:00
parent a3679b0bfd
commit d8f7e48cbf
6 changed files with 40 additions and 38 deletions
@@ -163,9 +163,9 @@ public class StageScenePanel extends MisoScenePanel
_portobjs.clear(); _portobjs.clear();
for (Iterator iter = _scene.getPortals(); iter.hasNext(); ) { for (Iterator iter = _scene.getPortals(); iter.hasNext(); ) {
Portal portal = (Portal) iter.next(); Portal portal = (Portal) iter.next();
Point p = getScreenCoords(portal.x, portal.y); Point p = getScreenCoords(portal.loc.x, portal.loc.y);
int tx = MisoUtil.fullToTile(portal.x); int tx = MisoUtil.fullToTile(portal.loc.x);
int ty = MisoUtil.fullToTile(portal.y); int ty = MisoUtil.fullToTile(portal.loc.y);
Point ts = MisoUtil.tileToScreen(_metrics, tx, ty, new Point()); Point ts = MisoUtil.tileToScreen(_metrics, tx, ty, new Point());
// Log.info("Added portal " + portal + // Log.info("Added portal " + portal +
@@ -180,7 +180,7 @@ public class StageScenePanel extends MisoScenePanel
ObjectTile tile = new PortalObjectTile( ObjectTile tile = new PortalObjectTile(
ts.x + _metrics.tilehwid - p.x + (PORTAL_ICON_WIDTH / 2), ts.x + _metrics.tilehwid - p.x + (PORTAL_ICON_WIDTH / 2),
ts.y + _metrics.tilehei - p.y + (PORTAL_ICON_HEIGHT / 2)); ts.y + _metrics.tilehei - p.y + (PORTAL_ICON_HEIGHT / 2));
tile.setImage(ots.getTileMirage(portal.orient)); tile.setImage(ots.getTileMirage(portal.loc.orient));
_portobjs.add(new SceneObject(this, info, tile) { _portobjs.add(new SceneObject(this, info, tile) {
public boolean setHovered (boolean hovered) { public boolean setHovered (boolean hovered) {
@@ -482,7 +482,7 @@ public class StageScenePanel extends MisoScenePanel
Iterator iter = _scene.getPortals(); Iterator iter = _scene.getPortals();
while (iter.hasNext()) { while (iter.hasNext()) {
Portal portal = (Portal)iter.next(); Portal portal = (Portal)iter.next();
if (portal.x == fullX && portal.y == fullY) { if (portal.loc.x == fullX && portal.loc.y == fullY) {
return portal; return portal;
} }
} }
@@ -554,7 +554,7 @@ public class StageScenePanel extends MisoScenePanel
Iterator iter = _scene.getPortals(); Iterator iter = _scene.getPortals();
while (iter.hasNext()) { while (iter.hasNext()) {
Portal p = (Portal)iter.next(); Portal p = (Portal)iter.next();
if (p.x == loc.x && p.y == loc.y) { if (loc.equals(p.loc)) {
return true; return true;
} }
} }
@@ -307,8 +307,8 @@ public class StageSceneManager extends SpotSceneManager
_plocs.clear(); _plocs.clear();
for (Iterator iter = _sscene.getPortals(); iter.hasNext(); ) { for (Iterator iter = _sscene.getPortals(); iter.hasNext(); ) {
Portal port = (Portal)iter.next(); Portal port = (Portal)iter.next();
_plocs.add(new Point(MisoUtil.fullToTile(port.x), _plocs.add(new Point(MisoUtil.fullToTile(port.loc.x),
MisoUtil.fullToTile(port.y))); MisoUtil.fullToTile(port.loc.y)));
} }
} }
@@ -889,14 +889,14 @@ public class EditorScenePanel extends StageScenePanel
{ {
// get the portal's center coordinate // get the portal's center coordinate
Point spos = new Point(); Point spos = new Point();
MisoUtil.fullToScreen(_metrics, port.x, port.y, spos); MisoUtil.fullToScreen(_metrics, port.loc.x, port.loc.y, spos);
int cx = spos.x, cy = spos.y; int cx = spos.x, cy = spos.y;
// translate the origin to center on the portal // translate the origin to center on the portal
gfx.translate(cx, cy); gfx.translate(cx, cy);
// rotate to reflect the portal orientation // rotate to reflect the portal orientation
double rot = (Math.PI / 4.0f) * port.orient; double rot = (Math.PI / 4.0f) * port.loc.orient;
gfx.rotate(rot); gfx.rotate(rot);
// draw the triangle // draw the triangle
@@ -14,6 +14,7 @@ import com.threerings.miso.util.MisoUtil;
import com.threerings.util.DirectionCodes; import com.threerings.util.DirectionCodes;
import com.threerings.util.DirectionUtil; import com.threerings.util.DirectionUtil;
import com.threerings.whirled.spot.data.Location;
import com.threerings.whirled.spot.tools.EditablePortal; import com.threerings.whirled.spot.tools.EditablePortal;
import com.threerings.stage.data.StageScene; import com.threerings.stage.data.StageScene;
@@ -36,8 +37,7 @@ public class PortalTool extends MouseInputAdapter
// configure the portal with these full coordinates // configure the portal with these full coordinates
_portal = new EditablePortal(); _portal = new EditablePortal();
_portal.x = p.x; _portal.loc = new Location(p.x, p.y, (byte)DirectionCodes.NORTH);
_portal.y = p.y;
// we want to listen to drags and clicks // we want to listen to drags and clicks
_panel.addMouseListener(this); _panel.addMouseListener(this);
@@ -99,8 +99,8 @@ public class PortalTool extends MouseInputAdapter
} }
// if our orientation changed, repaint // if our orientation changed, repaint
if (norient != _portal.orient) { if (norient != _portal.loc.orient) {
_portal.orient = (byte)norient; _portal.loc.orient = (byte)norient;
_panel.repaint(); _panel.repaint();
} }
} }
@@ -111,7 +111,8 @@ public class PortalTool extends MouseInputAdapter
protected void savePortal () protected void savePortal ()
{ {
// try to come up with a reasonable name // try to come up with a reasonable name
String dirname = DirectionUtil.toString(_portal.orient).toLowerCase(); String dirname =
DirectionUtil.toString(_portal.loc.orient).toLowerCase();
String name = dirname; String name = dirname;
for (int ii=1; portalNameExists(name); ii++) { for (int ii=1; portalNameExists(name); ii++) {
name = dirname + ii; name = dirname + ii;
@@ -39,14 +39,10 @@ public class Portal extends SimpleStreamableObject
/** This portal's unique identifier. */ /** This portal's unique identifier. */
public short portalId; public short portalId;
/** This portal's x coordinate (interpreted by the display system). */ /** The location of the portal. Typically this is a base Location (2d)
public int x; * class, but different games could use a different subclass of
* Location. */
/** This portal's y coordinate (interpreted by the display system). */ public Location loc;
public int y;
/** This portal's y orientation (interpreted by the display system). */
public byte orient;
/** The scene identifier of the scene to which a body will exit when /** The scene identifier of the scene to which a body will exit when
* they "use" this portal. */ * they "use" this portal. */
@@ -62,7 +58,7 @@ public class Portal extends SimpleStreamableObject
*/ */
public Location getLocation () public Location getLocation ()
{ {
return new Location(x, y, orient); return (Location) loc.clone();
} }
/** /**
@@ -74,7 +70,9 @@ public class Portal extends SimpleStreamableObject
*/ */
public Location getOppLocation () public Location getOppLocation ()
{ {
return new Location(x, y, (byte)DirectionUtil.getOpposite(orient)); Location oppLoc = getLocation();
oppLoc.orient = (byte) DirectionUtil.getOpposite(oppLoc.orient);
return oppLoc;
} }
/** /**
@@ -112,12 +110,6 @@ public class Portal extends SimpleStreamableObject
} }
} }
/** {@link #toString} helper function. */
public String orientToString ()
{
return DirectionUtil.toShortString(orient);
}
/** /**
* Computes a reasonable hashcode for portal instances. * Computes a reasonable hashcode for portal instances.
*/ */
@@ -1,5 +1,5 @@
// //
// $Id: SpotSceneWriter.java,v 1.9 2004/08/27 02:20:48 mdb Exp $ // $Id$
// //
// Narya library - tools for developing networked games // Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved // Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
@@ -27,6 +27,7 @@ import org.xml.sax.helpers.AttributesImpl;
import com.megginson.sax.DataWriter; import com.megginson.sax.DataWriter;
import com.threerings.tools.xml.NestableWriter; import com.threerings.tools.xml.NestableWriter;
import com.threerings.whirled.spot.data.Location;
import com.threerings.whirled.spot.data.SpotSceneModel; import com.threerings.whirled.spot.data.SpotSceneModel;
import com.threerings.whirled.spot.tools.EditablePortal; import com.threerings.whirled.spot.tools.EditablePortal;
@@ -56,7 +57,7 @@ public class SpotSceneWriter
{ {
if (model.defaultEntranceId != -1) { if (model.defaultEntranceId != -1) {
attrs.addAttribute("", "defaultEntranceId", "", "", attrs.addAttribute("", "defaultEntranceId", "", "",
Integer.toString(model.defaultEntranceId)); String.valueOf(model.defaultEntranceId));
} }
} }
@@ -68,11 +69,8 @@ public class SpotSceneWriter
EditablePortal port = (EditablePortal)model.portals[ii]; EditablePortal port = (EditablePortal)model.portals[ii];
AttributesImpl attrs = new AttributesImpl(); AttributesImpl attrs = new AttributesImpl();
attrs.addAttribute("", "portalId", "", "", attrs.addAttribute("", "portalId", "", "",
Integer.toString(port.portalId)); String.valueOf(port.portalId));
attrs.addAttribute("", "x", "", "", Integer.toString(port.x)); addPortalLocationAttributes(port.loc, attrs);
attrs.addAttribute("", "y", "", "", Integer.toString(port.y));
attrs.addAttribute("", "orient", "", "",
Integer.toString(port.orient));
maybeAddAttr(attrs, "name", port.name); maybeAddAttr(attrs, "name", port.name);
maybeAddAttr(attrs, "targetSceneName", port.targetSceneName); maybeAddAttr(attrs, "targetSceneName", port.targetSceneName);
maybeAddAttr(attrs, "targetPortalName", port.targetPortalName); maybeAddAttr(attrs, "targetPortalName", port.targetPortalName);
@@ -80,6 +78,17 @@ public class SpotSceneWriter
} }
} }
protected void addPortalLocationAttributes (
Location portalLoc, AttributesImpl attrs)
{
// we assume here that the Location is purely 2d. Subclasses
// may write out more information
attrs.addAttribute("", "x", "", "", String.valueOf(portalLoc.x));
attrs.addAttribute("", "y", "", "", String.valueOf(portalLoc.y));
attrs.addAttribute("", "orient", "", "",
String.valueOf(portalLoc.orient));
}
/** /**
* Adds the supplied attribute to the attributes object iff the value * Adds the supplied attribute to the attributes object iff the value
* is non-null. * is non-null.