Refactored things so that the whirled.spot Location class is merely an
interface and specific games can define their own concept of a location. Portal and SceneLocation now both have a Location instance within them. All the whirled stuff is happy not knowing the details of the Location even though it manages portals and other such goodies. The 'stage' stuff does need to know, and yohoho uses the new StageLocation class that is essentially identical to the old 'Location'. SceneLocation previously extended Location, and there were a lot of places where they were used somewhat interchangeably, or a SceneLocation and a regular Location were compared. This shouldn't be done anymore and I've left in code that should dump stack if it occurs, so that we can spot these logic errors at runtime. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4140 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -1,112 +1,34 @@
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Narya library - tools for developing networked games
|
||||
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
|
||||
// http://www.threerings.net/code/narya/
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or modify it
|
||||
// under the terms of the GNU Lesser General Public License as published
|
||||
// by the Free Software Foundation; either version 2.1 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
package com.threerings.whirled.spot.data;
|
||||
|
||||
import com.threerings.io.SimpleStreamableObject;
|
||||
import com.threerings.util.DirectionCodes;
|
||||
import com.threerings.util.DirectionUtil;
|
||||
import com.threerings.io.Streamable;
|
||||
|
||||
/**
|
||||
* Contains information on a scene occupant's position and orientation.
|
||||
*/
|
||||
public class Location extends SimpleStreamableObject
|
||||
implements Cloneable
|
||||
public interface Location extends Streamable, Cloneable
|
||||
{
|
||||
/** The user's x position (interpreted by the display system). */
|
||||
public int x;
|
||||
|
||||
/** The user's y position (interpreted by the display system). */
|
||||
public int y;
|
||||
|
||||
/** The user's orientation (defined by {@link DirectionCodes}). */
|
||||
public byte orient;
|
||||
|
||||
/** {@link #toString} helper function. */
|
||||
public String orientToString ()
|
||||
{
|
||||
return DirectionUtil.toShortString(orient);
|
||||
}
|
||||
/**
|
||||
* Get a new Location instance that is equals() to this one but that
|
||||
* has an orientation facing the opposite direction.
|
||||
*/
|
||||
public Location getOpposite ();
|
||||
|
||||
/**
|
||||
* A zero-argument constructor used when unserializing instances.
|
||||
* Two locations are equivalent if they specify the same location
|
||||
* and orientation.
|
||||
*/
|
||||
public Location ()
|
||||
{
|
||||
}
|
||||
public boolean equivalent (Location other);
|
||||
|
||||
/**
|
||||
* Constructs a location with the specified coordinates and
|
||||
* orientation.
|
||||
* Two locations are equals if they specify the same coordinates, but
|
||||
* the orientation may be different.
|
||||
*/
|
||||
public Location (int x, int y, byte orient)
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.orient = orient;
|
||||
|
||||
if (orient == -1) {
|
||||
Thread.dumpStack();
|
||||
}
|
||||
}
|
||||
public boolean equals (Object other);
|
||||
|
||||
/**
|
||||
* Creates a clone of this instance.
|
||||
* The hashcode of a Location should be based only on its coordinates.
|
||||
*/
|
||||
public Object clone ()
|
||||
{
|
||||
try {
|
||||
return (Location)super.clone();
|
||||
} catch (CloneNotSupportedException cnse) {
|
||||
throw new RuntimeException("Location.clone() failed " + cnse);
|
||||
}
|
||||
}
|
||||
public int hashCode ();
|
||||
|
||||
/**
|
||||
* Location equality is determined by coordinates.
|
||||
* Locations are cloneable.
|
||||
*/
|
||||
public boolean equals (Object other)
|
||||
{
|
||||
if (other instanceof Location) {
|
||||
Location oloc = (Location)other;
|
||||
return (x == oloc.x) && (y == oloc.y);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Location equivalence means that the coordinates and orientation are
|
||||
* the same.
|
||||
*/
|
||||
public boolean equivalent (Location oloc)
|
||||
{
|
||||
return equals(oloc) && (orient == oloc.orient);
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes a reasonable hashcode for location instances.
|
||||
*/
|
||||
public int hashCode ()
|
||||
{
|
||||
return x ^ y;
|
||||
}
|
||||
public Object clone ();
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@
|
||||
package com.threerings.whirled.spot.data;
|
||||
|
||||
import com.threerings.io.SimpleStreamableObject;
|
||||
import com.threerings.util.DirectionUtil;
|
||||
|
||||
/**
|
||||
* Represents an exit to another scene. A body sprite would walk over to a
|
||||
@@ -39,9 +38,8 @@ public class Portal extends SimpleStreamableObject
|
||||
/** This portal's unique identifier. */
|
||||
public short portalId;
|
||||
|
||||
/** The location of the portal. Typically this is a base Location (2d)
|
||||
* class, but different games could use a different subclass of
|
||||
* Location. */
|
||||
/** The location of the portal.
|
||||
* This field is present on client and server, it is streamed specially. */
|
||||
public Location loc;
|
||||
|
||||
/** The scene identifier of the scene to which a body will exit when
|
||||
@@ -70,9 +68,7 @@ public class Portal extends SimpleStreamableObject
|
||||
*/
|
||||
public Location getOppLocation ()
|
||||
{
|
||||
Location oppLoc = getLocation();
|
||||
oppLoc.orient = (byte) DirectionUtil.getOpposite(oppLoc.orient);
|
||||
return oppLoc;
|
||||
return loc.getOpposite();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,12 +98,8 @@ public class Portal extends SimpleStreamableObject
|
||||
*/
|
||||
public boolean equals (Object other)
|
||||
{
|
||||
if (other instanceof Portal) {
|
||||
Portal oport = (Portal)other;
|
||||
return portalId == oport.portalId;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return (other instanceof Portal) &&
|
||||
((Portal) other).portalId == portalId;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,33 +21,29 @@
|
||||
|
||||
package com.threerings.whirled.spot.data;
|
||||
|
||||
import com.threerings.io.SimpleStreamableObject;
|
||||
|
||||
import com.threerings.presents.dobj.DSet;
|
||||
|
||||
/**
|
||||
* Extends {@link Location} with the data and functionality needed to
|
||||
* represent a particular user's location in a scene.
|
||||
*/
|
||||
public class SceneLocation extends Location
|
||||
public class SceneLocation extends SimpleStreamableObject
|
||||
implements DSet.Entry
|
||||
{
|
||||
/** The oid of the body that occupies this location. */
|
||||
public int bodyOid;
|
||||
|
||||
/**
|
||||
* Creates a scene location with the specified information.
|
||||
*/
|
||||
public SceneLocation (int x, int y, byte orient, int bodyOid)
|
||||
{
|
||||
super(x, y, orient);
|
||||
this.bodyOid = bodyOid;
|
||||
}
|
||||
/** The actual location, which is interpreted by the display system. */
|
||||
public Location loc;
|
||||
|
||||
/**
|
||||
* Creates a scene location with the specified information.
|
||||
*/
|
||||
public SceneLocation (Location loc, int bodyOid)
|
||||
{
|
||||
super(loc.x, loc.y, loc.orient);
|
||||
this.loc = loc;
|
||||
this.bodyOid = bodyOid;
|
||||
}
|
||||
|
||||
@@ -67,6 +63,26 @@ public class SceneLocation extends Location
|
||||
return _key;
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public boolean equals (Object other)
|
||||
{
|
||||
// TEMP
|
||||
if (other instanceof Location) {
|
||||
Thread.dumpStack(); // this will help us find logic errors,
|
||||
// as a SceneLocation and a Location shouldn't be compared
|
||||
}
|
||||
// END: temp
|
||||
|
||||
return (other instanceof SceneLocation) &&
|
||||
this.loc.equals(((SceneLocation) other).loc);
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public int hashCode ()
|
||||
{
|
||||
return loc.hashCode();
|
||||
}
|
||||
|
||||
/** Used for {@link #getKey}. */
|
||||
protected transient Integer _key;
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ import com.threerings.whirled.spot.tools.EditablePortal;
|
||||
/**
|
||||
* Used to parse a {@link SpotSceneModel} from XML.
|
||||
*/
|
||||
public class SpotSceneRuleSet implements NestableRuleSet
|
||||
public abstract class SpotSceneRuleSet implements NestableRuleSet
|
||||
{
|
||||
// documentation inherited from interface
|
||||
public String getOuterElement ()
|
||||
@@ -70,10 +70,7 @@ public class SpotSceneRuleSet implements NestableRuleSet
|
||||
* Create a new instance of the Location class that should be used
|
||||
* with Portals.
|
||||
*/
|
||||
protected Location createNewLocation ()
|
||||
{
|
||||
return new Location();
|
||||
}
|
||||
protected abstract Location createLocation ();
|
||||
|
||||
/**
|
||||
* A rule used to create the portal but also initialize the Location
|
||||
@@ -95,7 +92,7 @@ public class SpotSceneRuleSet implements NestableRuleSet
|
||||
|
||||
// create the empty Location in the Portal
|
||||
Portal p = (Portal) digester.peek();
|
||||
p.loc = _ruleset.createNewLocation();
|
||||
p.loc = _ruleset.createLocation();
|
||||
}
|
||||
|
||||
protected SpotSceneRuleSet _ruleset;
|
||||
|
||||
Reference in New Issue
Block a user