Moved sprite stuff into media package, removed coupling between miso stuff

and sprite stuff by added some interfaces in the sprite stuff and
implementing them with the miso stuff.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@245 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2001-08-14 23:35:22 +00:00
parent 0d0cb7f8ec
commit 3eea4dcaad
13 changed files with 221 additions and 134 deletions
@@ -1,64 +1,37 @@
//
// $Id: CharacterSprite.java,v 1.3 2001/08/14 22:54:45 mdb Exp $
// $Id: CharacterSprite.java,v 1.4 2001/08/14 23:35:22 mdb Exp $
package com.threerings.media.sprite;
import com.threerings.media.Log;
import com.threerings.miso.tile.Tile;
import com.threerings.miso.tile.TileManager;
/**
* An <code>AmbulatorySprite</code> is a sprite that can face in one
* of the various compass directions and that can animate itself
* walking along some chosen path.
* An <code>AmbulatorySprite</code> is a sprite that can face in one of
* the various compass directions and that can animate itself walking
* along some chosen path.
*/
public class AmbulatorySprite extends Sprite
{
/**
* Construct an <code>AmbulatorySprite</code>, loading the tiles
* used to display the sprite from the given tileset.
* 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>Path</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 tilemgr the tile manager to retrieve tiles from.
* @param tsid the tileset id containing the sprite tiles.
* @param anims the set of multi-frame images to use when animating
* the sprite in each of the compass directions.
*/
public AmbulatorySprite (SpriteManager spritemgr, int x, int y,
TileManager tilemgr, int tsid)
MultiFrameImage[] anims)
{
super(spritemgr, x, y);
_dirTiles = getTiles(tilemgr, tsid);
_anims = anims;
_dir = Path.DIR_SOUTH;
setTiles(_dirTiles[0]);
}
/**
* Returns a two-dimensional array of tiles corresponding to the
* frames of animation used to render the mobile sprite in each of
* the directions it may face. The tileset id referenced must
* contain <code>Path.NUM_DIRECTIONS</code> rows of tiles, with each
* row containing <code>NUM_DIR_FRAMES</code> tiles.
*
* @param tilemgr the tile manager to retrieve tiles from.
* @param tsid the tileset id containing the sprite tiles.
*
* @return the two-dimensional array of sprite tiles.
*/
protected Tile[][] getTiles (TileManager tilemgr, int tsid)
{
Tile[][] tiles =
new Tile[Path.NUM_DIRECTIONS][NUM_DIR_FRAMES];
for (int ii = 0; ii < Path.NUM_DIRECTIONS; ii++) {
for (int jj = 0; jj < NUM_DIR_FRAMES; jj++) {
int idx = (ii * NUM_DIR_FRAMES) + jj;
tiles[ii][jj] = tilemgr.getTile(tsid, idx);
}
}
return tiles;
setFrames(_anims[Path.DIR_NORTH]);
}
/**
@@ -81,18 +54,15 @@ public class AmbulatorySprite extends Sprite
return;
}
// update the sprite tiles to reflect the direction
setTiles(_dirTiles[_dir = _dest.dir]);
// update the sprite frames to reflect the direction
setFrames(_anims[_dir = _dest.dir]);
// start tile animation to show movement
setAnimationDelay(0);
}
/** The number of frames of animation for each direction. */
protected static final int NUM_DIR_FRAMES = 8;
/** The animation frames for the sprite facing each direction. */
protected Tile[][] _dirTiles;
protected MultiFrameImage[] _anims;
/** The direction the sprite is currently facing. */
protected int _dir;
@@ -0,0 +1,23 @@
//
// $Id: AnimatedView.java,v 1.1 2001/08/14 23:35:22 mdb Exp $
package com.threerings.media.sprite;
import java.util.List;
/**
* A view that wishes to interact with the animation manager needs to
* implement this interface to give the animation manager a means by which
* to communicate the regions of the view that need to be repainted
* because of the process of animating on top of the view.
*/
public interface AnimatedView
{
/**
* Invalidate a list of rectangles in screen pixel coordinates in the
* scene view for later repainting.
*
* @param rects the list of {@link java.awt.Rectangle} objects.
*/
public void invalidateRects (List rects);
}
@@ -1,5 +1,5 @@
//
// $Id: AnimationManager.java,v 1.11 2001/08/14 22:54:45 mdb Exp $
// $Id: AnimationManager.java,v 1.12 2001/08/14 23:35:22 mdb Exp $
package com.threerings.media.sprite;
@@ -12,7 +12,6 @@ import com.samskivert.util.Interval;
import com.samskivert.util.IntervalManager;
import com.threerings.media.Log;
import com.threerings.miso.scene.SceneView;
import com.threerings.miso.util.PerformanceMonitor;
import com.threerings.miso.util.PerformanceObserver;
@@ -28,7 +27,7 @@ public class AnimationManager implements Interval, PerformanceObserver
* manager and the panel that animations will take place within.
*/
public AnimationManager (SpriteManager spritemgr, JComponent target,
SceneView view)
AnimatedView view)
{
// save off references to the objects we care about
_spritemgr = spritemgr;
@@ -176,6 +175,6 @@ public class AnimationManager implements Interval, PerformanceObserver
/** The component to refresh. */
protected JComponent _target;
/** The scene view. */
protected SceneView _view;
/** The view on which we are animating. */
protected AnimatedView _view;
}
@@ -1,5 +1,5 @@
//
// $Id: Sprite.java,v 1.9 2001/08/14 22:54:45 mdb Exp $
// $Id: Sprite.java,v 1.10 2001/08/14 23:35:22 mdb Exp $
package com.threerings.media.sprite;
@@ -9,12 +9,10 @@ import java.util.Enumeration;
import com.threerings.media.Log;
import com.threerings.media.util.MathUtil;
import com.threerings.miso.tile.Tile;
/**
* The Sprite class represents a single moveable object within a
* scene. A sprite has a position within the scene, and a set of
* tiles used to render it (perhaps multiple frames for animation).
* The <code>Sprite</code> class represents a single moveable object
* within a scene. A sprite has a position within the scene, and a set of
* images used to render it (perhaps multiple frames for animation).
*/
public class Sprite
{
@@ -25,22 +23,23 @@ public class Sprite
public int y;
/**
* Construct a Sprite object.
* Construct a sprite object.
*
* @param spritemgr the sprite manager.
* @param x the sprite x-position in pixels.
* @param y the sprite y-position in pixels.
* @param tiles the tiles used to display the sprite.
* @param frames the multi-frame image used to display the sprite.
*/
public Sprite (SpriteManager spritemgr, int x, int y, Tile[] tiles)
public Sprite (SpriteManager spritemgr, int x, int y,
MultiFrameImage frames)
{
init(spritemgr, x, y, tiles);
init(spritemgr, x, y, frames);
}
/**
* Construct a Sprite object without any associated tiles. The
* sprite should be populated with a set of tiles used to display
* it via a subsequent call to <code>setTiles()</code>.
* Construct a sprite object without any associated frames. The sprite
* should be populated with a set of frames used to display it via a
* subsequent call to <code>setFrames()</code>.
*
* @param spritemgr the sprite manager.
* @param x the sprite x-position in pixels.
@@ -54,7 +53,8 @@ public class Sprite
/**
* Initialize the sprite object with its variegated parameters.
*/
protected void init (SpriteManager spritemgr, int x, int y, Tile[] tiles)
protected void init (
SpriteManager spritemgr, int x, int y, MultiFrameImage frames)
{
_spritemgr = spritemgr;
@@ -63,11 +63,11 @@ public class Sprite
updateDrawPosition();
_curFrame = 0;
_frameIdx = 0;
_animDelay = ANIM_NONE;
_numTicks = 0;
setTiles(_tiles);
setFrames(frames);
invalidate();
}
@@ -79,7 +79,7 @@ public class Sprite
*/
public void paint (Graphics2D gfx)
{
gfx.drawImage(_curTile.img, _drawx, _drawy, null);
gfx.drawImage(_frame, _drawx, _drawy, null);
// Log.info("Sprite painting image [sprite=" + this + "].");
}
@@ -97,8 +97,8 @@ public class Sprite
}
/**
* Set the number of ticks to wait before switching to the next
* tile in the array of tiles used to display the sprite.
* Set the number of ticks to wait before switching to the next image
* in the array of images used to display the sprite.
*
* @param ticks the number of ticks.
*/
@@ -108,16 +108,16 @@ public class Sprite
}
/**
* Set the tile array used to render the sprite.
* Set the image array used to render the sprite.
*
* @param tiles the sprite tiles.
* @param frames the sprite images.
*/
public void setTiles (Tile[] tiles)
public void setFrames (MultiFrameImage frames)
{
if (tiles == null) return;
if (frames == null) return;
_tiles = tiles;
_curTile = _tiles[_curFrame];
_frames = frames;
_frame = _frames.getFrame(_frameIdx);
updateDrawPosition();
invalidate();
}
@@ -183,10 +183,10 @@ public class Sprite
*/
public void invalidate ()
{
if (_curTile == null) return;
if (_frame == null) return;
Rectangle dirty = new Rectangle(
_drawx, _drawy, _curTile.width, _curTile.height);
_drawx, _drawy, _frame.getWidth(null), _frame.getHeight(null));
// Log.info("Sprite invalidate [x=" + x + ", y=" + y +
// ", dx=" + dirty.x + ", dy=" + dirty.y +
@@ -202,13 +202,13 @@ public class Sprite
*/
public void tick ()
{
// increment the display tile if performing tile animation
// increment the display image if performing image animation
if (_animDelay != ANIM_NONE && (_numTicks++ == _animDelay)) {
_numTicks = 0;
if (++_curFrame > _tiles.length - 1) _curFrame = 0;
_curTile = _tiles[_curFrame];
_frameIdx = (_frameIdx + 1) % _frames.getFrameCount();
_frame = _frames.getFrame(_frameIdx);
// dirty our rectangle since we've altered our display tile
// dirty our rectangle since we've altered our display image
invalidate();
}
@@ -256,14 +256,14 @@ public class Sprite
*/
protected void updateDrawPosition ()
{
if (_curTile == null) {
if (_frame == null) {
_drawx = x;
_drawy = y;
return;
}
_drawx = x - (_curTile.width / 2);
_drawy = y - _curTile.height;
} else {
_drawx = x - (_frame.getWidth(null) / 2);
_drawy = y - _frame.getHeight(null);
}
}
/**
@@ -274,23 +274,23 @@ public class Sprite
StringBuffer buf = new StringBuffer();
buf.append("[x=").append(x);
buf.append(", y=").append(y);
buf.append(", curframe=").append(_curFrame);
buf.append(", fidx=").append(_frameIdx);
return buf.append("]").toString();
}
/** Value used to denote that no tile animation is desired. */
/** Value used to denote that no image animation is desired. */
protected static final int ANIM_NONE = -1;
/** The tiles used to render the sprite. */
protected Tile[] _tiles;
/** The images used to render the sprite. */
protected MultiFrameImage _frames;
/** The current tile to render the sprite. */
protected Tile _curTile;
/** The current frame being rendered. */
protected Image _frame;
/** The current tile index to render. */
protected int _curFrame;
/** The current frame index to render. */
protected int _frameIdx;
/** The coordinates at which the tile image is drawn. */
/** The coordinates at which the frame image is drawn. */
protected int _drawx, _drawy;
/** The PathNode objects describing the path the sprite is following. */
@@ -305,10 +305,10 @@ public class Sprite
/** When moving, the distance to move per tick in fractional pixels. */
protected float _incx, _incy;
/** The number of ticks to wait before rendering with the next tile. */
/** The number of ticks to wait before rendering with the next image. */
protected int _animDelay;
/** The number of ticks since the last tile animation. */
/** The number of ticks since the last image animation. */
protected int _numTicks;
/** The sprite manager. */
@@ -0,0 +1,23 @@
//
// $Id: MultiFrameImage.java,v 1.1 2001/08/14 23:35:22 mdb Exp $
package com.threerings.media.sprite;
import java.awt.Image;
/**
* The multi-frame image interface provides encapsulated access to a set
* of images that are used to create a multi-frame animation.
*/
public interface MultiFrameImage
{
/**
* Returns the number of frames in this multi-frame image.
*/
public int getFrameCount ();
/**
* Returns the image for the specified frame index.
*/
public Image getFrame (int index);
}
@@ -1,15 +1,18 @@
//
// $Id: IsoSceneView.java,v 1.42 2001/08/14 21:29:40 shaper Exp $
// $Id: IsoSceneView.java,v 1.43 2001/08/14 23:35:22 mdb Exp $
package com.threerings.miso.scene;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.util.List;
import java.util.ArrayList;
import com.threerings.media.sprite.*;
import com.threerings.miso.Log;
import com.threerings.miso.sprite.*;
import com.threerings.miso.tile.Tile;
import com.threerings.miso.tile.TileManager;
import com.threerings.miso.scene.util.IsoUtil;
@@ -397,7 +400,7 @@ public class IsoSceneView implements EditableSceneView
*
* @param rects the list of Rectangle objects.
*/
public void invalidateRects (ArrayList rects)
public void invalidateRects (List rects)
{
int size = rects.size();
for (int ii = 0; ii < size; ii++) {
@@ -1,9 +1,9 @@
//
// $Id: Location.java,v 1.4 2001/08/11 00:00:13 shaper Exp $
// $Id: Location.java,v 1.5 2001/08/14 23:35:22 mdb Exp $
package com.threerings.miso.scene;
import com.threerings.miso.sprite.Path;
import com.threerings.media.sprite.Path;
/**
* The <code>Location</code> class represents a unique well-defined
@@ -1,14 +1,12 @@
//
// $Id: SceneView.java,v 1.9 2001/08/02 20:43:03 shaper Exp $
// $Id: SceneView.java,v 1.10 2001/08/14 23:35:22 mdb Exp $
package com.threerings.miso.scene;
import java.awt.Component;
import java.awt.Graphics;
import java.util.ArrayList;
import com.threerings.miso.sprite.Path;
import com.threerings.miso.sprite.Sprite;
import com.threerings.media.sprite.*;
import com.threerings.miso.tile.Tile;
/**
@@ -16,7 +14,7 @@ import com.threerings.miso.tile.Tile;
* classes that provide a view of a given scene by drawing the scene
* contents onto a particular GUI component.
*/
public interface SceneView
public interface SceneView extends AnimatedView
{
/**
* Render the scene to the given graphics context.
@@ -32,14 +30,6 @@ public interface SceneView
*/
public void setScene (Scene scene);
/**
* Invalidate a list of rectangles in screen pixel coordinates in
* the scene view for later repainting.
*
* @param rects the list of <code>java.awt.Rectangle</code> objects.
*/
public void invalidateRects (ArrayList rects);
/**
* Return a Path object detailing a valid path for the given
* sprite to take in the scene to get from its current position to
@@ -1,12 +1,12 @@
//
// $Id: SceneViewPanel.java,v 1.4 2001/08/08 03:19:38 shaper Exp $
// $Id: SceneViewPanel.java,v 1.5 2001/08/14 23:35:22 mdb Exp $
package com.threerings.miso.scene;
import java.awt.*;
import javax.swing.JPanel;
import com.threerings.miso.sprite.SpriteManager;
import com.threerings.media.sprite.SpriteManager;
import com.threerings.miso.tile.TileManager;
/**
@@ -1,14 +1,15 @@
//
// $Id: IsoUtil.java,v 1.1 2001/08/14 21:29:40 shaper Exp $
// $Id: IsoUtil.java,v 1.2 2001/08/14 23:35:22 mdb Exp $
package com.threerings.miso.scene.util;
import java.awt.Point;
import java.awt.Polygon;
import com.threerings.media.sprite.Path;
import com.threerings.media.util.MathUtil;
import com.threerings.miso.scene.*;
import com.threerings.miso.sprite.Path;
import com.threerings.miso.util.MathUtil;
/**
* The <code>IsoUtil</code> class is a holding place for miscellaneous
@@ -0,0 +1,72 @@
//
// $Id: TileUtil.java,v 1.1 2001/08/14 23:35:22 mdb Exp $
package com.threerings.miso.tile;
import java.awt.Image;
import com.threerings.media.sprite.AmbulatorySprite;
import com.threerings.media.sprite.MultiFrameImage;
import com.threerings.media.sprite.Path;
/**
* Tile-related utility functions.
*/
public class TileUtil
{
/**
* Returns an array of multi-frame images corresponding to the frames
* of animation used to render the mobile sprite in each of the
* directions it may face. The tileset id referenced must contain
* <code>Path.NUM_DIRECTIONS</code> rows of tiles, with each row
* containing <code>NUM_DIR_FRAMES</code> tiles.
*
* @param tilemgr the tile manager to retrieve tiles from.
* @param tsid the tileset id containing the sprite tiles.
*
* @return the array of multi-frame sprite images.
*/
public static MultiFrameImage[] getSpriteFrames (
TileManager tilemgr, int tsid)
{
MultiFrameImage[] anims = new MultiFrameImage[Path.NUM_DIRECTIONS];
for (int ii = 0; ii < Path.NUM_DIRECTIONS; ii++) {
Tile[] tiles = new Tile[NUM_DIR_FRAMES];
for (int jj = 0; jj < NUM_DIR_FRAMES; jj++) {
int idx = (ii * NUM_DIR_FRAMES) + jj;
tiles[jj] = tilemgr.getTile(tsid, idx);
}
anims[ii] = new MultiTileImage(tiles);
}
return anims;
}
/**
* A class that treats an array of tiles as source images for a
* multi-frame animation to be used by the sprite engine.
*/
protected static class MultiTileImage implements MultiFrameImage
{
public MultiTileImage (Tile[] tiles)
{
_tiles = tiles;
}
public int getFrameCount ()
{
return _tiles.length;
}
public Image getFrame (int index)
{
return _tiles[index].img;
}
protected Tile[] _tiles;
}
/** The number of frames of animation for each direction. */
protected static final int NUM_DIR_FRAMES = 8;
}
@@ -1,20 +1,23 @@
//
// $Id: ViewerFrame.java,v 1.9 2001/08/07 18:29:18 shaper Exp $
// $Id: ViewerFrame.java,v 1.10 2001/08/14 23:35:22 mdb Exp $
package com.threerings.miso.viewer;
import com.samskivert.swing.*;
import com.threerings.miso.Log;
import com.threerings.miso.scene.Scene;
import com.threerings.miso.sprite.*;
import com.threerings.miso.tile.TileManager;
import com.threerings.miso.viewer.util.ViewerContext;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import com.samskivert.swing.*;
import com.threerings.media.sprite.*;
import com.threerings.miso.Log;
import com.threerings.miso.scene.Scene;
import com.threerings.miso.tile.TileManager;
import com.threerings.miso.tile.TileUtil;
import com.threerings.miso.viewer.util.ViewerContext;
/**
* The ViewerFrame is the main application window that constructs and
* contains the application menu bar and panels and responds to menu
@@ -40,8 +43,10 @@ class ViewerFrame extends JFrame implements WindowListener
TileManager tilemgr = _ctx.getTileManager();
// add the test character sprite to the sprite manager
MultiFrameImage[] anims =
TileUtil.getSpriteFrames(tilemgr, TSID_CHAR);
AmbulatorySprite sprite =
new AmbulatorySprite(spritemgr, 300, 300, tilemgr, TSID_CHAR);
new AmbulatorySprite(spritemgr, 300, 300, anims);
spritemgr.addSprite(sprite);
// create a top-level panel to manage everything
@@ -1,5 +1,5 @@
//
// $Id: ViewerSceneViewPanel.java,v 1.6 2001/08/10 21:17:07 shaper Exp $
// $Id: ViewerSceneViewPanel.java,v 1.7 2001/08/14 23:35:22 mdb Exp $
package com.threerings.miso.viewer;
@@ -9,12 +9,13 @@ import java.io.IOException;
import javax.swing.JPanel;
import com.samskivert.util.Config;
import com.threerings.media.sprite.*;
import com.threerings.miso.Log;
import com.threerings.miso.viewer.util.ViewerContext;
import com.threerings.miso.scene.*;
import com.threerings.miso.scene.xml.XMLFileSceneRepository;
import com.threerings.miso.sprite.*;
import com.threerings.miso.util.*;
import com.threerings.miso.viewer.util.ViewerContext;
public class ViewerSceneViewPanel extends SceneViewPanel
implements MouseListener, MouseMotionListener, PerformanceObserver