Abstracted character-sprite-related activity to the cast package in
preparation for character component compositing and other character-related antics. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@575 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
//
|
||||
// $Id: CharacterComponent.java,v 1.1 2001/10/26 01:17:21 shaper Exp $
|
||||
|
||||
package com.threerings.cast;
|
||||
|
||||
import com.threerings.media.sprite.MultiFrameImage;
|
||||
|
||||
/**
|
||||
* The character component represents a single component that can be
|
||||
* composited with other character components to comprise an image
|
||||
* representing a single monolithic character displayable in any of
|
||||
* the eight cardinal compass directions as detailed in the {@link
|
||||
* com.threerings.media.sprite.Sprite} class's direction constants.
|
||||
*/
|
||||
public class CharacterComponent
|
||||
{
|
||||
/**
|
||||
* Constructs a character component.
|
||||
*/
|
||||
public CharacterComponent (ComponentType type, int cid,
|
||||
ComponentFrames frames)
|
||||
{
|
||||
_type = type;
|
||||
_cid = cid;
|
||||
_frames = frames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the unique component identifier.
|
||||
*/
|
||||
public int getId ()
|
||||
{
|
||||
return _cid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the display frames used to display this component.
|
||||
*/
|
||||
public ComponentFrames getFrames ()
|
||||
{
|
||||
return _frames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link ComponentType} object describing the base
|
||||
* component type information associated with this component.
|
||||
*/
|
||||
public ComponentType getType ()
|
||||
{
|
||||
return _type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of this character component.
|
||||
*/
|
||||
public String toString ()
|
||||
{
|
||||
return "[cid=" + _cid + ", type=" + _type + "]";
|
||||
}
|
||||
|
||||
/**
|
||||
* A class to hold the standing and walking frames of animation
|
||||
* that comprise a {@link CharacterComponent} object's various
|
||||
* display images.
|
||||
*/
|
||||
public static class ComponentFrames
|
||||
{
|
||||
/** The standing animations in each orientation. */
|
||||
public MultiFrameImage stand[];
|
||||
|
||||
/** The walking animations in each orientation. */
|
||||
public MultiFrameImage walk[];
|
||||
}
|
||||
|
||||
/** The unique character component identifier. */
|
||||
protected int _cid;
|
||||
|
||||
/** The animation frames. */
|
||||
protected ComponentFrames _frames;
|
||||
|
||||
/** The character component type. */
|
||||
protected ComponentType _type;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
//
|
||||
// $Id: CharacterDescriptor.java,v 1.1 2001/10/26 01:17:21 shaper Exp $
|
||||
|
||||
package com.threerings.cast;
|
||||
|
||||
import com.samskivert.util.StringUtil;
|
||||
|
||||
/**
|
||||
* The character descriptor object contains all necessary information
|
||||
* on the character components that are composited together to create
|
||||
* a character image.
|
||||
*/
|
||||
public class CharacterDescriptor
|
||||
{
|
||||
/**
|
||||
* Constructs the character descriptor.
|
||||
*/
|
||||
public CharacterDescriptor (int components[])
|
||||
{
|
||||
_components = components;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of the component identifiers comprising the
|
||||
* character described by this descriptor.
|
||||
*/
|
||||
public int[] getComponents ()
|
||||
{
|
||||
return _components;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of this character descriptor.
|
||||
*/
|
||||
public String toString ()
|
||||
{
|
||||
StringBuffer buf = new StringBuffer();
|
||||
buf.append("[").append(StringUtil.toString(_components));
|
||||
return buf.append("]").toString();
|
||||
}
|
||||
|
||||
/** The array of component identifiers. */
|
||||
protected int _components[];
|
||||
}
|
||||
@@ -1,22 +1,15 @@
|
||||
//
|
||||
// $Id: CharacterManager.java,v 1.4 2001/10/25 18:06:17 shaper Exp $
|
||||
// $Id: CharacterManager.java,v 1.5 2001/10/26 01:17:21 shaper Exp $
|
||||
|
||||
package com.threerings.miso.scene;
|
||||
package com.threerings.cast;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.samskivert.util.Config;
|
||||
import com.samskivert.util.HashIntMap;
|
||||
|
||||
import com.threerings.media.sprite.MultiFrameImage;
|
||||
import com.threerings.media.tile.TileManager;
|
||||
|
||||
import com.threerings.miso.Log;
|
||||
import com.threerings.miso.scene.AmbulatorySprite.CharacterImages;
|
||||
import com.threerings.miso.scene.xml.XMLCharacterParser;
|
||||
import com.threerings.miso.tile.TileUtil;
|
||||
import com.threerings.miso.util.MisoUtil;
|
||||
import com.threerings.cast.Log;
|
||||
import com.threerings.cast.TileUtil;
|
||||
|
||||
/**
|
||||
* The character manager provides facilities for constructing sprites
|
||||
@@ -25,82 +18,93 @@ import com.threerings.miso.util.MisoUtil;
|
||||
public class CharacterManager
|
||||
{
|
||||
/**
|
||||
* Construct the character manager.
|
||||
* Constructs the character manager.
|
||||
*/
|
||||
public CharacterManager (
|
||||
Config config, TileManager tilemgr, IsoSceneViewModel model)
|
||||
public CharacterManager (TileManager tilemgr, ComponentRepository repo)
|
||||
{
|
||||
// save off our objects
|
||||
_tilemgr = tilemgr;
|
||||
_model = model;
|
||||
|
||||
// load character data descriptions
|
||||
String file = config.getValue(CHARFILE_KEY, DEFAULT_CHARFILE);
|
||||
try {
|
||||
new XMLCharacterParser().loadCharacters(file, _characters);
|
||||
} catch (IOException ioe) {
|
||||
Log.warning("Exception loading character descriptions " +
|
||||
"[ioe=" + ioe + "].");
|
||||
}
|
||||
_repo = repo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sprite representing the character described by the
|
||||
* given tile set id.
|
||||
* Returns a {@link CharacterSprite} representing the character
|
||||
* described by the given {@link CharacterDescriptor}, or
|
||||
* <code>null</code> if an error occurs.
|
||||
*
|
||||
* @param the character tile set id.
|
||||
* @param desc the character descriptor.
|
||||
*/
|
||||
public AmbulatorySprite getCharacter (int tsid)
|
||||
public CharacterSprite getCharacter (CharacterDescriptor desc)
|
||||
{
|
||||
CharacterInfo info = (CharacterInfo)_characters.get(tsid);
|
||||
if (info == null) {
|
||||
// no such character
|
||||
Log.warning("Unknown character requested [tsid=" + tsid + "].");
|
||||
// get the list of component ids
|
||||
int components[] = desc.getComponents();
|
||||
|
||||
// TODO: here is where we'd iterate through all components
|
||||
// building up the full composite image of the sprite in each
|
||||
// orientation, standing and walking. we punt for now, but
|
||||
// we'll revisit this soon enough.
|
||||
if (components.length == 0 || components.length > 1) {
|
||||
Log.warning("Invalid number of components " +
|
||||
" [size=" + components.length + "].");
|
||||
return null;
|
||||
}
|
||||
|
||||
CharacterImages imgs = TileUtil.getCharacterImages(
|
||||
_tilemgr, tsid, info.frameCount);
|
||||
CharacterComponent comp;
|
||||
try {
|
||||
// as noted above, no compositing for now. note that when
|
||||
// we do composite, we probably will want to make sure all
|
||||
// components share a compatible component type.
|
||||
comp = _repo.getComponent(components[0]);
|
||||
} catch (Exception e) {
|
||||
Log.warning("Exception retrieving character component " +
|
||||
"[e=" + e + "].");
|
||||
return null;
|
||||
}
|
||||
|
||||
AmbulatorySprite sprite = new AmbulatorySprite(_model, 0, 0, imgs);
|
||||
sprite.setFrameRate(info.fps);
|
||||
sprite.setOrigin(info.origin.x, info.origin.y);
|
||||
// instantiate the character sprite
|
||||
CharacterSprite sprite;
|
||||
try {
|
||||
sprite = (CharacterSprite)_charClass.newInstance();
|
||||
} catch (Exception e) {
|
||||
Log.warning("Failed to instantiate character sprite " +
|
||||
"[e=" + e + "].");
|
||||
Log.logStackTrace(e);
|
||||
return null;
|
||||
}
|
||||
|
||||
// populate the character sprite with its attributes
|
||||
ComponentType ctype = comp.getType();
|
||||
sprite.setFrames(comp.getFrames());
|
||||
sprite.setFrameRate(ctype.fps);
|
||||
sprite.setOrigin(ctype.origin.x, ctype.origin.y);
|
||||
|
||||
return sprite;
|
||||
}
|
||||
|
||||
/** The config key for the character description file. */
|
||||
protected static final String CHARFILE_KEY =
|
||||
MisoUtil.CONFIG_KEY + ".characters";
|
||||
/**
|
||||
* Instructs the character manager to construct instances of this
|
||||
* derived class of <code>CharacterSprite</code>.
|
||||
*/
|
||||
public void setCharacterClass (Class charClass)
|
||||
{
|
||||
// sanity check
|
||||
if (!CharacterSprite.class.isAssignableFrom(charClass)) {
|
||||
Log.warning("Requested to use character class that does not " +
|
||||
"derive from CharacterSprite " +
|
||||
"[class=" + charClass.getName() + "].");
|
||||
return;
|
||||
}
|
||||
|
||||
/** The default character description file. */
|
||||
protected static final String DEFAULT_CHARFILE =
|
||||
"rsrc/config/miso/characters.xml";
|
||||
|
||||
/** The hashtable of character info objects. */
|
||||
protected HashIntMap _characters = new HashIntMap();
|
||||
// make a note of it
|
||||
_charClass = charClass;
|
||||
}
|
||||
|
||||
/** The tile manager. */
|
||||
protected TileManager _tilemgr;
|
||||
|
||||
/** The iso scene view model. */
|
||||
protected IsoSceneViewModel _model;
|
||||
/** The component repository. */
|
||||
protected ComponentRepository _repo;
|
||||
|
||||
/**
|
||||
* A class that contains character description information for a
|
||||
* single character for use when constructing the character's
|
||||
* sprite.
|
||||
*/
|
||||
public static class CharacterInfo
|
||||
{
|
||||
public int tsid;
|
||||
public int frameCount;
|
||||
public int fps;
|
||||
public Point origin = new Point();
|
||||
|
||||
public String toString ()
|
||||
{
|
||||
return "[tsid=" + tsid + ", frameCount=" + frameCount +
|
||||
", fps=" + fps + ", origin=" + origin + "]";
|
||||
}
|
||||
}
|
||||
/** The character class to be created. */
|
||||
protected Class _charClass = CharacterSprite.class;
|
||||
}
|
||||
|
||||
@@ -1,44 +1,38 @@
|
||||
//
|
||||
// $Id: CharacterSprite.java,v 1.15 2001/10/25 18:06:17 shaper Exp $
|
||||
// $Id: CharacterSprite.java,v 1.16 2001/10/26 01:17:21 shaper Exp $
|
||||
|
||||
package com.threerings.miso.scene;
|
||||
package com.threerings.cast;
|
||||
|
||||
import java.awt.Point;
|
||||
|
||||
import com.threerings.media.sprite.*;
|
||||
|
||||
import com.threerings.miso.Log;
|
||||
import com.threerings.miso.tile.MisoTile;
|
||||
import com.threerings.miso.scene.util.IsoUtil;
|
||||
import com.threerings.cast.Log;
|
||||
import com.threerings.cast.CharacterComponent.ComponentFrames;
|
||||
|
||||
/**
|
||||
* An ambulatory sprite is a sprite that animates itself while walking
|
||||
* A character sprite is a sprite that animates itself while walking
|
||||
* about in a scene.
|
||||
*/
|
||||
public class AmbulatorySprite extends Sprite implements Traverser
|
||||
public class CharacterSprite extends Sprite
|
||||
{
|
||||
/**
|
||||
* Construct an <code>AmbulatorySprite</code>, with a multi-frame
|
||||
* image associated with each of the eight compass directions. The
|
||||
* array should be in the order defined by the <code>Sprite</code>
|
||||
* direction constants (SW, W, NW, N, NE, E, SE, S).
|
||||
*
|
||||
* @param x the sprite x-position in pixels.
|
||||
* @param y the sprite y-position in pixels.
|
||||
* @param images the images used to display the sprite when
|
||||
* standing or walking about.
|
||||
* Constructs a character sprite.
|
||||
*/
|
||||
public AmbulatorySprite (
|
||||
IsoSceneViewModel model, int x, int y, CharacterImages images)
|
||||
public CharacterSprite ()
|
||||
{
|
||||
super(x, y);
|
||||
// assign an arbitrary starting orientation
|
||||
_orient = DIR_NORTH;
|
||||
}
|
||||
|
||||
// keep track of these
|
||||
_model = model;
|
||||
_images = images;
|
||||
|
||||
// give ourselves an initial orientation
|
||||
setOrientation(DIR_NORTH);
|
||||
/**
|
||||
* Sets the walking and standing frames of animation used to
|
||||
* display this character.
|
||||
*/
|
||||
public void setFrames (ComponentFrames frames)
|
||||
{
|
||||
_anims = frames;
|
||||
setFrames(_anims.walk[_orient]);
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
@@ -48,9 +42,9 @@ public class AmbulatorySprite extends Sprite implements Traverser
|
||||
|
||||
// update the sprite frames to reflect the direction
|
||||
if (_path == null) {
|
||||
setFrames(_images.standing[_orient]);
|
||||
setFrames(_anims.stand[_orient]);
|
||||
} else {
|
||||
setFrames(_images.walking[_orient]);
|
||||
setFrames(_anims.walk[_orient]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,137 +104,15 @@ public class AmbulatorySprite extends Sprite implements Traverser
|
||||
protected void halt ()
|
||||
{
|
||||
// come to a halt looking settled and at peace
|
||||
setFrames(_images.standing[_orient]);
|
||||
setFrames(_anims.stand[_orient]);
|
||||
|
||||
// disable walking animation
|
||||
setAnimationMode(NO_ANIMATION);
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public boolean canTraverse (MisoTile tile)
|
||||
{
|
||||
// by default, passability is solely the province of the tile
|
||||
return tile.passable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sprite's location on the x-axis in tile coordinates.
|
||||
*/
|
||||
public int getTileX ()
|
||||
{
|
||||
return _tilex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sprite's location on the y-axis in tile coordinates.
|
||||
*/
|
||||
public int getTileY ()
|
||||
{
|
||||
return _tiley;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sprite's location on the x-axis within its current
|
||||
* tile in fine coordinates.
|
||||
*/
|
||||
public int getFineX ()
|
||||
{
|
||||
return _finex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sprite's location on the y-axis within its current
|
||||
* tile in fine coordinates.
|
||||
*/
|
||||
public int getFineY ()
|
||||
{
|
||||
return _finey;
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public void setLocation (int x, int y)
|
||||
{
|
||||
super.setLocation(x, y);
|
||||
|
||||
if (_path == null) {
|
||||
// we only calculate the sprite's tile and fine
|
||||
// coordinates if we have no path, since paths that move
|
||||
// us about are responsible for keeping our scene
|
||||
// coordinates up to date since only they can know where
|
||||
// we really are while in transition from one place to
|
||||
// another.
|
||||
|
||||
// get the sprite's position in full coordinates
|
||||
Point fpos = new Point();
|
||||
IsoUtil.screenToFull(_model, _x, _y, fpos);
|
||||
|
||||
// save off the sprite's tile and fine coordinates
|
||||
_tilex = IsoUtil.fullToTile(fpos.x);
|
||||
_tiley = IsoUtil.fullToTile(fpos.y);
|
||||
_finex = IsoUtil.fullToFine(fpos.x);
|
||||
_finey = IsoUtil.fullToFine(fpos.y);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the sprite's location in tile coordinates; the sprite is
|
||||
* not actually moved in any way. This method is only intended
|
||||
* for use in updating the sprite's stored position which is made
|
||||
* accessible to others that may care to review it.
|
||||
*/
|
||||
public void setTileLocation (int x, int y)
|
||||
{
|
||||
_tilex = x;
|
||||
_tiley = y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the sprite's location in fine coordinates; the sprite is
|
||||
* not actually moved in any way. This method is only intended
|
||||
* for use in updating the sprite's stored position which is made
|
||||
* accessible to others that may care to review it.
|
||||
*/
|
||||
public void setFineLocation (int x, int y)
|
||||
{
|
||||
_finex = x;
|
||||
_finey = y;
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
protected void toString (StringBuffer buf)
|
||||
{
|
||||
super.toString(buf);
|
||||
buf.append(", tilex=").append(_tilex);
|
||||
buf.append(", tiley=").append(_tiley);
|
||||
buf.append(", finex=").append(_finex);
|
||||
buf.append(", finey=").append(_finey);
|
||||
}
|
||||
|
||||
/**
|
||||
* A class to hold the images that are used to display the sprite
|
||||
* while walking about.
|
||||
*/
|
||||
public static class CharacterImages
|
||||
{
|
||||
/** The images of the sprite standing at rest in each orientation. */
|
||||
public MultiFrameImage standing[];
|
||||
|
||||
/** The images of the sprite walking in each orientation. */
|
||||
public MultiFrameImage walking[];
|
||||
}
|
||||
|
||||
/** The animation frames for the sprite facing each direction. */
|
||||
protected CharacterImages _images;
|
||||
|
||||
/** The iso scene view model. */
|
||||
protected IsoSceneViewModel _model;
|
||||
/** The standing and walking animations for the sprite. */
|
||||
protected ComponentFrames _anims;
|
||||
|
||||
/** The origin of the sprite. */
|
||||
protected int _xorigin, _yorigin;
|
||||
|
||||
/** The sprite location in tile coordinates. */
|
||||
protected int _tilex, _tiley;
|
||||
|
||||
/** The sprite location in fine coordinates. */
|
||||
protected int _finex, _finey;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// $Id: ComponentRepository.java,v 1.1 2001/10/26 01:17:21 shaper Exp $
|
||||
|
||||
package com.threerings.cast;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* The component repository interface is intended to be implemented by
|
||||
* classes that provide access to {@link CharacterComponent} objects
|
||||
* keyed on their unique component identifier.
|
||||
*/
|
||||
public interface ComponentRepository
|
||||
{
|
||||
/**
|
||||
* Returns the {@link CharacterComponent} object for the given
|
||||
* unique component identifier.
|
||||
*/
|
||||
public CharacterComponent getComponent (int cid)
|
||||
throws NoSuchComponentException, NoSuchComponentTypeException;
|
||||
|
||||
/**
|
||||
* Returns an iterator over the <code>Integer</code> objects
|
||||
* representing all available character component identifiers for
|
||||
* the given character component type identifier.
|
||||
*/
|
||||
public Iterator enumerateComponents (int ctid)
|
||||
throws NoSuchComponentTypeException;
|
||||
|
||||
/**
|
||||
* Returns an iterator over the <code>Integer</code> objects
|
||||
* representing all available character component type
|
||||
* identifiers.
|
||||
*/
|
||||
public Iterator enumerateComponentTypes ();
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// $Id: ComponentType.java,v 1.1 2001/10/26 01:17:21 shaper Exp $
|
||||
|
||||
package com.threerings.cast;
|
||||
|
||||
import java.awt.Point;
|
||||
|
||||
/**
|
||||
* The component type class contains the base information necessary
|
||||
* for a variety of {@link CharacterComponent} objects to be usefully
|
||||
* composited into animation sequences representing a character.
|
||||
*/
|
||||
public class ComponentType
|
||||
{
|
||||
/** The unique component type identifier. */
|
||||
public int ctid;
|
||||
|
||||
/** The number of animation frames. */
|
||||
public int frameCount;
|
||||
|
||||
/** The number of frames per second to show when animating. */
|
||||
public int fps;
|
||||
|
||||
/** The origin of the component type's base. */
|
||||
public Point origin = new Point();
|
||||
|
||||
/**
|
||||
* Returns a string representation of this component type.
|
||||
*/
|
||||
public String toString ()
|
||||
{
|
||||
return "[ctid=" + ctid + ", frameCount=" + frameCount +
|
||||
", fps=" + fps + ", origin=" + origin + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// $Id: Log.java,v 1.1 2001/10/26 01:17:21 shaper Exp $
|
||||
|
||||
package com.threerings.cast;
|
||||
|
||||
/**
|
||||
* A placeholder class that contains a reference to the log object used by
|
||||
* the cast package.
|
||||
*/
|
||||
public class Log
|
||||
{
|
||||
public static com.samskivert.util.Log log =
|
||||
new com.samskivert.util.Log("cast");
|
||||
|
||||
/** Convenience function. */
|
||||
public static void debug (String message)
|
||||
{
|
||||
log.debug(message);
|
||||
}
|
||||
|
||||
/** Convenience function. */
|
||||
public static void info (String message)
|
||||
{
|
||||
log.info(message);
|
||||
}
|
||||
|
||||
/** Convenience function. */
|
||||
public static void warning (String message)
|
||||
{
|
||||
log.warning(message);
|
||||
}
|
||||
|
||||
/** Convenience function. */
|
||||
public static void logStackTrace (Throwable t)
|
||||
{
|
||||
log.logStackTrace(com.samskivert.util.Log.WARNING, t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// $Id: NoSuchComponentException.java,v 1.1 2001/10/26 01:17:21 shaper Exp $
|
||||
|
||||
package com.threerings.cast;
|
||||
|
||||
/**
|
||||
* Thrown when an attempt is made to retrieve a non-existent character
|
||||
* component from the component repository.
|
||||
*/
|
||||
public class NoSuchComponentException extends Exception
|
||||
{
|
||||
public NoSuchComponentException (int cid)
|
||||
{
|
||||
super("No such component [cid=" + cid + "]");
|
||||
_cid = cid;
|
||||
}
|
||||
|
||||
public int getId ()
|
||||
{
|
||||
return _cid;
|
||||
}
|
||||
|
||||
protected int _cid;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// $Id: NoSuchComponentTypeException.java,v 1.1 2001/10/26 01:17:21 shaper Exp $
|
||||
|
||||
package com.threerings.cast;
|
||||
|
||||
/**
|
||||
* Thrown when an attempt is made to reference a non-existent
|
||||
* component type in the component repository.
|
||||
*/
|
||||
public class NoSuchComponentTypeException extends Exception
|
||||
{
|
||||
public NoSuchComponentTypeException (int ctid)
|
||||
{
|
||||
super("No such component type [ctid=" + ctid + "]");
|
||||
_ctid = ctid;
|
||||
}
|
||||
|
||||
public int getTypeId ()
|
||||
{
|
||||
return _ctid;
|
||||
}
|
||||
|
||||
protected int _ctid;
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
//
|
||||
// $Id: TileUtil.java,v 1.1 2001/10/26 01:17:21 shaper Exp $
|
||||
|
||||
package com.threerings.cast;
|
||||
|
||||
import java.awt.Image;
|
||||
|
||||
import com.threerings.media.sprite.*;
|
||||
import com.threerings.media.tile.*;
|
||||
|
||||
import com.threerings.cast.Log;
|
||||
import com.threerings.cast.CharacterComponent.ComponentFrames;
|
||||
|
||||
/**
|
||||
* Miscellaneous tile-related utility functions.
|
||||
*/
|
||||
public class TileUtil
|
||||
{
|
||||
/**
|
||||
* Returns a {@link ComponentFrames} object containing the frames
|
||||
* of animation used to render the sprite while standing or
|
||||
* walking in each of the directions it may face. The tileset id
|
||||
* referenced must contain <code>Sprite.NUM_DIRECTIONS</code> rows
|
||||
* of tiles, with each row containing first the single standing
|
||||
* tile, followed by <code>frameCount</code> tiles describing the
|
||||
* walking animation.
|
||||
*
|
||||
* @param tilemgr the tile manager to retrieve tiles from.
|
||||
* @param tsid the tileset id containing the sprite tiles.
|
||||
* @param frameCount the number of walking frames of animation.
|
||||
*/
|
||||
public static ComponentFrames getComponentFrames (
|
||||
TileManager tilemgr, int tsid, int frameCount)
|
||||
{
|
||||
ComponentFrames frames = new ComponentFrames();
|
||||
frames.walk = new MultiFrameImage[Sprite.NUM_DIRECTIONS];
|
||||
frames.stand = new MultiFrameImage[Sprite.NUM_DIRECTIONS];
|
||||
|
||||
try {
|
||||
for (int ii = 0; ii < Sprite.NUM_DIRECTIONS; ii++) {
|
||||
|
||||
Image walkimgs[] = new Image[frameCount];
|
||||
int rowcount = frameCount + 1;
|
||||
for (int jj = 0; jj < rowcount; jj++) {
|
||||
int idx = (ii * rowcount) + jj;
|
||||
|
||||
Image img = tilemgr.getTile(tsid, idx).img;
|
||||
if (jj == 0) {
|
||||
frames.stand[ii] = new SingleFrameImageImpl(img);
|
||||
} else {
|
||||
walkimgs[jj - 1] = img;
|
||||
}
|
||||
}
|
||||
|
||||
frames.walk[ii] = new MultiFrameImageImpl(walkimgs);
|
||||
}
|
||||
|
||||
} catch (TileException te) {
|
||||
Log.warning("Exception retrieving character images " +
|
||||
"[te=" + te + "].");
|
||||
return null;
|
||||
}
|
||||
|
||||
return frames;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
//
|
||||
// $Id: XMLComponentParser.java,v 1.1 2001/10/26 01:17:21 shaper Exp $
|
||||
|
||||
package com.threerings.cast;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.xml.sax.*;
|
||||
|
||||
import com.samskivert.util.HashIntMap;
|
||||
import com.samskivert.util.StringUtil;
|
||||
import com.samskivert.xml.SimpleParser;
|
||||
|
||||
public class XMLComponentParser extends SimpleParser
|
||||
{
|
||||
// documentation inherited
|
||||
public void startElement (String uri, String localName,
|
||||
String qName, Attributes attributes)
|
||||
{
|
||||
if (qName.equals("type")) {
|
||||
// construct the component type object
|
||||
ComponentType ct = new ComponentType();
|
||||
|
||||
// retrieve character attributes
|
||||
ct.ctid = parseInt(attributes.getValue("ctid"));
|
||||
ct.frameCount = parseInt(attributes.getValue("frames"));
|
||||
ct.fps = parseInt(attributes.getValue("fps"));
|
||||
parsePoint(attributes.getValue("origin"), ct.origin);
|
||||
|
||||
Log.info("Parsed component type [ct=" + ct + "].");
|
||||
|
||||
// save the component type
|
||||
_types.put(ct.ctid, ct);
|
||||
|
||||
} else if (qName.equals("component")) {
|
||||
// save off the component info
|
||||
int cid = parseInt(attributes.getValue("cid"));
|
||||
int ctid = parseInt(attributes.getValue("ctid"));
|
||||
_components.put(cid, new Integer(ctid));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the component descriptions in the given file into {@link
|
||||
* ComponentType} and {@link CharacterComponent} objects in the
|
||||
* given hashtables, respectively, keyed on the type and component
|
||||
* unique id, also respectively.
|
||||
*/
|
||||
public void loadComponents (
|
||||
String file, HashIntMap types, HashIntMap components)
|
||||
throws IOException
|
||||
{
|
||||
// save off hashtables for reference while parsing
|
||||
_types = types;
|
||||
_components = components;
|
||||
|
||||
parseFile(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a string containing values as (x, y) into the
|
||||
* corresponding integer values and populates the given point
|
||||
* object.
|
||||
*
|
||||
* @param str the point values in string format.
|
||||
* @param point the point object to populate.
|
||||
*/
|
||||
protected void parsePoint (String str, Point point)
|
||||
{
|
||||
int vals[] = StringUtil.parseIntArray(str);
|
||||
point.setLocation(vals[0], vals[1]);
|
||||
}
|
||||
|
||||
/** The hashtable of component types gathered while parsing. */
|
||||
protected HashIntMap _types;
|
||||
|
||||
/** The hashtable of character components gathered while parsing. */
|
||||
protected HashIntMap _components;
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
//
|
||||
// $Id: XMLComponentRepository.java,v 1.1 2001/10/26 01:17:22 shaper Exp $
|
||||
|
||||
package com.threerings.miso.scene.xml;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
|
||||
import com.samskivert.util.Config;
|
||||
import com.samskivert.util.HashIntMap;
|
||||
|
||||
import com.threerings.cast.*;
|
||||
import com.threerings.cast.CharacterComponent.ComponentFrames;
|
||||
|
||||
import com.threerings.media.tile.TileManager;
|
||||
|
||||
import com.threerings.miso.Log;
|
||||
import com.threerings.miso.util.MisoUtil;
|
||||
|
||||
/**
|
||||
* The xml file component repository provides access to character
|
||||
* components obtained from component descriptions stored in an XML
|
||||
* file.
|
||||
*/
|
||||
public class XMLFileComponentRepository implements ComponentRepository
|
||||
{
|
||||
/**
|
||||
* Constructs an xml file component repository.
|
||||
*/
|
||||
public XMLFileComponentRepository (Config config, TileManager tilemgr)
|
||||
{
|
||||
// save off our objects
|
||||
_tilemgr = tilemgr;
|
||||
|
||||
// load component types and components
|
||||
String file = config.getValue(COMPFILE_KEY, DEFAULT_COMPFILE);
|
||||
try {
|
||||
new XMLComponentParser().loadComponents(
|
||||
file, _types, _components);
|
||||
} catch (IOException ioe) {
|
||||
Log.warning("Exception loading component descriptions " +
|
||||
"[ioe=" + ioe + "].");
|
||||
}
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public CharacterComponent getComponent (int cid)
|
||||
throws NoSuchComponentException, NoSuchComponentTypeException
|
||||
{
|
||||
// get the component type id
|
||||
Integer ctid = (Integer)_components.get(cid);
|
||||
if (ctid == null) {
|
||||
throw new NoSuchComponentException(cid);
|
||||
}
|
||||
|
||||
// get the component type
|
||||
ComponentType type = (ComponentType)_types.get(ctid.intValue());
|
||||
if (type == null) {
|
||||
throw new NoSuchComponentTypeException(ctid.intValue());
|
||||
}
|
||||
|
||||
// get the character animation images
|
||||
ComponentFrames frames = TileUtil.getComponentFrames(
|
||||
_tilemgr, cid, type.frameCount);
|
||||
|
||||
// create the component
|
||||
return new CharacterComponent(type, cid, frames);
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public Iterator enumerateComponents (int ctid)
|
||||
throws NoSuchComponentTypeException
|
||||
{
|
||||
return new ComponentTypeIterator(ctid);
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public Iterator enumerateComponentTypes ()
|
||||
{
|
||||
return _types.keys();
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterates over all components of a specified component type in
|
||||
* the component hashtable.
|
||||
*/
|
||||
protected class ComponentTypeIterator implements Iterator
|
||||
{
|
||||
public ComponentTypeIterator (int ctid)
|
||||
throws NoSuchComponentTypeException
|
||||
{
|
||||
_ctid = ctid;
|
||||
_iter = _components.keys();
|
||||
advance();
|
||||
|
||||
// make sure we have at least one component of the
|
||||
// specified type
|
||||
if (_next == null) {
|
||||
throw new NoSuchComponentTypeException(ctid);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasNext ()
|
||||
{
|
||||
return (_next != null);
|
||||
}
|
||||
|
||||
public Object next ()
|
||||
{
|
||||
Object next = _next;
|
||||
advance();
|
||||
return next;
|
||||
}
|
||||
|
||||
public void remove ()
|
||||
{
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
protected void advance ()
|
||||
{
|
||||
while (_iter.hasNext()) {
|
||||
CharacterComponent c = (CharacterComponent)_iter.next();
|
||||
if (c.getType().ctid == _ctid) {
|
||||
_next = c;
|
||||
return;
|
||||
}
|
||||
}
|
||||
_next = null;
|
||||
}
|
||||
|
||||
/** The component type id we're enumerating over. */
|
||||
protected int _ctid;
|
||||
|
||||
/** The next character component of the component type id
|
||||
* associated with this iterator, or null if no more exist. */
|
||||
protected Object _next;
|
||||
|
||||
/** Iterator over the components in the repository. */
|
||||
protected Iterator _iter;
|
||||
}
|
||||
|
||||
/** The config key for the character description file. */
|
||||
protected static final String COMPFILE_KEY =
|
||||
MisoUtil.CONFIG_KEY + ".components";
|
||||
|
||||
/** The default character description file. */
|
||||
protected static final String DEFAULT_COMPFILE =
|
||||
"rsrc/config/miso/components.xml";
|
||||
|
||||
/** The hashtable of component types. */
|
||||
protected HashIntMap _types = new HashIntMap();
|
||||
|
||||
/** The hashtable of character components. */
|
||||
protected HashIntMap _components = new HashIntMap();
|
||||
|
||||
/** The tile manager. */
|
||||
protected TileManager _tilemgr;
|
||||
}
|
||||
Reference in New Issue
Block a user