Various and sundry modifications to clean up orientation handling and add

support for 16 orientations in situations where it is merited.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1548 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2002-06-26 23:53:07 +00:00
parent d1b428158a
commit e75ae89f22
11 changed files with 338 additions and 32 deletions
@@ -1,5 +1,5 @@
//
// $Id: ActionFrames.java,v 1.4 2002/06/19 23:31:57 mdb Exp $
// $Id: ActionFrames.java,v 1.5 2002/06/26 23:53:06 mdb Exp $
package com.threerings.cast;
@@ -17,6 +17,12 @@ import com.threerings.util.DirectionCodes;
*/
public interface ActionFrames
{
/**
* Returns the number of orientations available in this set of action
* frames.
*/
public int getOrientationCount ();
/**
* Returns the multi-frame image that comprises the frames for the
* specified orientation.
@@ -1,5 +1,5 @@
//
// $Id: ActionSequence.java,v 1.3 2002/03/27 21:51:33 mdb Exp $
// $Id: ActionSequence.java,v 1.4 2002/06/26 23:53:06 mdb Exp $
package com.threerings.cast;
@@ -24,12 +24,17 @@ public class ActionSequence implements Serializable
/** The position of the character's base for this sequence. */
public Point origin = new Point();
/** Orientation codes for the orientations available for this
* action. */
public int[] orients;
/**
* Returns a string representation of this action sequence.
*/
public String toString ()
{
return "[name=" + name + ", framesPerSecond=" + framesPerSecond +
", origin=" + origin + "]";
", origin=" + origin +
", orients=" + (orients == null ? 0 : orients.length) + "]";
}
}
@@ -1,5 +1,5 @@
//
// $Id: CompositedActionFrames.java,v 1.6 2002/06/20 18:31:03 mdb Exp $
// $Id: CompositedActionFrames.java,v 1.7 2002/06/26 23:53:06 mdb Exp $
package com.threerings.cast;
@@ -37,12 +37,19 @@ public class CompositedActionFrames
}
_sources = sources;
// the sources must all have the same frame count, and each
// the sources must all have the same orientation count, and each
// orientation must also have the same frame count, so we just use
// the count from the first source and first orientation
// the counts from the first source and orientation
_orientCount = _sources[0].getOrientationCount();
_frameCount = _sources[0].getFrames(NORTH).getFrameCount();
_images = new Image[DIRECTION_COUNT][_frameCount];
_bounds = new Rectangle[DIRECTION_COUNT][_frameCount];
_images = new Image[_orientCount][_frameCount];
_bounds = new Rectangle[_orientCount][_frameCount];
}
// documentation inherited from interface
public int getOrientationCount ()
{
return _orientCount;
}
// documentation inherited from interface
@@ -198,6 +205,9 @@ public class CompositedActionFrames
return dest;
}
/** The number of orientations. */
protected int _orientCount;
/** The number of frames in each orientation. */
protected int _frameCount;
@@ -1,5 +1,5 @@
//
// $Id: BundledComponentRepository.java,v 1.14 2002/06/19 23:31:57 mdb Exp $
// $Id: BundledComponentRepository.java,v 1.15 2002/06/26 23:53:06 mdb Exp $
package com.threerings.cast.bundle;
@@ -19,6 +19,7 @@ import java.util.Iterator;
import com.samskivert.io.NestableIOException;
import com.samskivert.util.HashIntMap;
import com.samskivert.util.IntIntMap;
import com.samskivert.util.Tuple;
import org.apache.commons.collections.FilterIterator;
@@ -307,6 +308,22 @@ public class BundledComponentRepository
{
_set = set;
_actseq = actseq;
// compute these now to avoid pointless recomputation later
_ocount = actseq.orients.length;
_fcount = set.getTileCount() / _ocount;
// create our mapping from orientation to animation sequence
// index
for (int ii = 0; ii < _ocount; ii++) {
_orients.put(actseq.orients[ii], ii);
}
}
// documentation inherited from interface
public int getOrientationCount ()
{
return _ocount;
}
// documentation inherited from interface
@@ -316,7 +333,7 @@ public class BundledComponentRepository
// documentation inherited
public int getFrameCount ()
{
return _set.getTileCount() / DIRECTION_COUNT;
return _fcount;
}
// documentation inherited from interface
@@ -388,8 +405,7 @@ public class BundledComponentRepository
*/
protected Tile getTile (int orient, int index)
{
int fcount = _set.getTileCount() / DIRECTION_COUNT;
int tileIndex = orient * fcount + index;
int tileIndex = _orients.get(orient) * _fcount + index;
try {
return _set.getTile(tileIndex);
} catch (NoSuchTileException nste) {
@@ -404,6 +420,13 @@ public class BundledComponentRepository
/** The action sequence for which we're providing frame images. */
protected ActionSequence _actseq;
/** Frame and orientation counts. */
protected int _fcount, _ocount;
/** A mapping from orientation code to animation sequence
* index. */
protected IntIntMap _orients = new IntIntMap();
}
/** We use the image manager to decode and cache images. */
@@ -1,5 +1,5 @@
//
// $Id: MetadataBundlerTask.java,v 1.2 2002/02/05 20:29:09 mdb Exp $
// $Id: MetadataBundlerTask.java,v 1.3 2002/06/26 23:53:07 mdb Exp $
package com.threerings.cast.bundle.tools;
@@ -169,6 +169,14 @@ public class MetadataBundlerTask extends Task
for (int i = 0; i < setlist.size(); i++) {
TileSet set = (TileSet)setlist.get(i);
ActionSequence act = (ActionSequence)actlist.get(i);
// make sure nothing was missing in the action sequence
// definition parsed from XML
String errmsg = ActionRuleSet.validate(act);
if (errmsg != null) {
errmsg = "Action sequence invalid [seq=" + act +
", error=" + errmsg + "].";
throw new BuildException(errmsg);
}
actmap.put(act.name, act);
setmap.put(act.name, set);
}
@@ -1,5 +1,5 @@
//
// $Id: ActionRuleSet.java,v 1.1 2001/11/27 08:09:35 mdb Exp $
// $Id: ActionRuleSet.java,v 1.2 2002/06/26 23:53:07 mdb Exp $
package com.threerings.cast.tools.xml;
@@ -12,6 +12,9 @@ import com.samskivert.xml.CallMethodSpecialRule;
import com.samskivert.xml.SetFieldRule;
import com.samskivert.xml.SetPropertyFieldsRule;
import com.threerings.util.DirectionCodes;
import com.threerings.util.DirectionUtil;
import com.threerings.cast.ActionSequence;
/**
@@ -78,6 +81,47 @@ public class ActionRuleSet extends RuleSetBase
}
};
digester.addRule(_prefix + ACTION_PATH + "/origin", origin);
CallMethodSpecialRule orient = new CallMethodSpecialRule(digester) {
public void parseAndSet (String bodyText, Object target)
throws Exception {
ActionSequence seq = ((ActionSequence)target);
String[] ostrs = StringUtil.parseStringArray(bodyText);
seq.orients = new int[ostrs.length];
for (int ii = 0; ii < ostrs.length; ii++) {
int orient = DirectionUtil.fromShortString(ostrs[ii]);
if (orient != DirectionCodes.NONE) {
seq.orients[ii] = orient;
} else {
String errmsg = "Invalid orientation specification " +
"[index=" + ii + ", orient=" + ostrs[ii] + "].";
throw new Exception(errmsg);
}
}
}
};
digester.addRule(_prefix + ACTION_PATH + "/orients", orient);
}
/**
* Validates that all necessary fields have been parsed and set in
* this action sequence object and are valid.
*
* @return null if the sequence is valid, a string explaining the
* invalidity if it is not.
*/
public static String validate (ActionSequence seq)
{
if (StringUtil.blank(seq.name)) {
return "Missing 'name' definition.";
}
if (seq.framesPerSecond == 0) {
return "Missing 'framesPerSecond' definition.";
}
if (seq.orients == null) {
return "Missing 'orients' definition.";
}
return null;
}
/** The prefix at which me match our actions. */
@@ -1,5 +1,5 @@
//
// $Id: IsoUtil.java,v 1.34 2002/06/21 18:52:36 mdb Exp $
// $Id: IsoUtil.java,v 1.35 2002/06/26 23:53:07 mdb Exp $
package com.threerings.miso.scene.util;
@@ -14,6 +14,7 @@ import com.threerings.media.tile.ObjectTile;
import com.threerings.media.util.MathUtil;
import com.threerings.util.DirectionCodes;
import com.threerings.util.DirectionUtil;
import com.threerings.miso.Log;
import com.threerings.miso.scene.IsoSceneViewModel;
@@ -228,7 +229,9 @@ public class IsoUtil
// compare tile coordinates to determine direction
int dir = getIsoDirection(tax, tay, tbx, tby);
if (dir != DirectionCodes.NONE) return dir;
if (dir != DirectionCodes.NONE) {
return dir;
}
// destination point is in the same tile as the
// origination point, so consider fine coordinates
@@ -248,9 +251,11 @@ public class IsoUtil
}
/**
* Given two points in an isometric coordinate system, return the
* compass direction that point B lies in from point A. This method
* is used to determine direction for both tile coordinates and fine
* Given two points in an isometric coordinate system (in which {@link
* NORTH} is in the direction of the negative x-axis and {@link WEST}
* in the direction of the negative y-axis), return the compass
* direction that point B lies in from point A. This method is used
* to determine direction for both tile coordinates and fine
* coordinates within a tile, since the coordinate systems are the
* same.
*
@@ -291,9 +296,8 @@ public class IsoUtil
}
/**
* Given two points in screen (cartesian) coordinates, return the
* isometrically projected compass direction that point B lies in from
* point A.
* Given two points in screen coordinates, return the isometrically
* projected compass direction that point B lies in from point A.
*
* @param ax the x-position of point A.
* @param ay the y-position of point A.
@@ -306,10 +310,20 @@ public class IsoUtil
*/
public static int getProjectedIsoDirection (int ax, int ay, int bx, int by)
{
int dir = getIsoDirection(ax, ay, bx, by);
return toIsoDirection(DirectionUtil.getDirection(ax, ay, bx, by));
}
/**
* Converts a non-isometric orientation (where north points toward the
* top of the screen) to an isometric orientation where north points
* toward the upper-left corner of the screen.
*/
public static int toIsoDirection (int dir)
{
if (dir != DirectionCodes.NONE) {
// offset the direction by -1, ie change SOUTHWEST to SOUTH
dir = (dir + 7) % 8;
// rotate the direction clockwise (ie. change SOUTHEAST to
// SOUTH)
dir = DirectionUtil.rotateCW(dir, 2);
}
return dir;
}
@@ -1,5 +1,5 @@
//
// $Id: DirectionUtil.java,v 1.5 2002/06/26 02:54:56 mdb Exp $
// $Id: DirectionUtil.java,v 1.6 2002/06/26 23:53:07 mdb Exp $
package com.threerings.util;
@@ -37,6 +37,36 @@ public class DirectionUtil implements DirectionCodes
SHORT_DIR_STRINGS[direction] : "?";
}
/**
* Returns the direction code that corresponds to the supplied string
* or {@link NONE} if the string does not correspond to a known
* direction code.
*/
public static int fromString (String dirstr)
{
for (int ii = 0; ii < FINE_DIRECTION_COUNT; ii++) {
if (DIR_STRINGS[ii].equals(dirstr)) {
return ii;
}
}
return NONE;
}
/**
* Returns the direction code that corresponds to the supplied short
* string or {@link NONE} if the string does not correspond to a known
* direction code.
*/
public static int fromShortString (String dirstr)
{
for (int ii = 0; ii < FINE_DIRECTION_COUNT; ii++) {
if (SHORT_DIR_STRINGS[ii].equals(dirstr)) {
return ii;
}
}
return NONE;
}
/**
* Returns a string representation of an array of direction codes. The
* directions are represented by the abbreviated names.
@@ -100,7 +130,19 @@ public class DirectionUtil implements DirectionCodes
*/
public static int getDirection (int ax, int ay, int bx, int by)
{
double theta = Math.atan2(by-ay, bx-ax);
return getDirection(Math.atan2(by-ay, bx-ax));
}
/**
* Returns which of the eight compass directions is associated with
* the specified angle theta. <em>Note:</em> that the angle supplied
* is assumed to increase clockwise around the origin (which screen
* angles do) rather than counter-clockwise around the origin (which
* cartesian angles do) and <code>NORTH</code> is considered to point
* toward the top of the screen.
*/
public static int getDirection (double theta)
{
theta = ((theta + Math.PI) * 4) / Math.PI;
return (int)(Math.round(theta) + WEST) % 8;
}
@@ -128,7 +170,19 @@ public class DirectionUtil implements DirectionCodes
*/
public static int getFineDirection (int ax, int ay, int bx, int by)
{
double theta = Math.atan2(by-ay, bx-ax);
return getFineDirection(Math.atan2(by-ay, bx-ax));
}
/**
* Returns which of the sixteen compass directions is associated with
* the specified angle theta. <em>Note:</em> that the angle supplied
* is assumed to increase clockwise around the origin (which screen
* angles do) rather than counter-clockwise around the origin (which
* cartesian angles do) and <code>NORTH</code> is considered to point
* toward the top of the screen.
*/
public static int getFineDirection (double theta)
{
theta = ((theta + Math.PI) * 8) / Math.PI;
return ANGLE_MAP[(int)Math.round(theta) % FINE_DIRECTION_COUNT];
}