diff --git a/src/java/com/threerings/media/sprite/MobileSprite.java b/src/java/com/threerings/media/sprite/MobileSprite.java new file mode 100644 index 000000000..1b0b1386c --- /dev/null +++ b/src/java/com/threerings/media/sprite/MobileSprite.java @@ -0,0 +1,82 @@ +// +// $Id: MobileSprite.java,v 1.1 2001/07/30 15:38:52 shaper Exp $ + +package com.threerings.miso.sprite; + +import com.threerings.miso.tile.Tile; +import com.threerings.miso.tile.TileManager; + +/** + * A MobileSprite is a sprite that can face in one of eight compass + * directions and that can be animated moving from one location to + * another (e.g., a human's legs move and arms swing.) + */ +public class MobileSprite extends Sprite +{ + /** + * Construct a MobileSprite object, loading the tiles used to + * display the sprite from specified tileset via the given tile + * manager. + * + * @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. + */ + public MobileSprite (int x, int y, TileManager tilemgr, int tsid) + { + super(x, y); + _charTiles = getTiles(tilemgr, tsid); + _dir = DIR_SOUTH; + setTiles(_charTiles[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 NUM_DIRECTIONS rows of tiles, with each + * row containing NUM_DIR_FRAMES 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[NUM_DIRECTIONS][NUM_DIR_FRAMES]; + + for (int ii = 0; ii < 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; + } + + /** The number of distinct directions the character may face. */ + protected static final int NUM_DIRECTIONS = 8; + + // Direction constants + protected static final int DIR_SOUTH = 0; + protected static final int DIR_SOUTHWEST = 1; + protected static final int DIR_WEST = 2; + protected static final int DIR_NORTHWEST = 3; + protected static final int DIR_NORTH = 4; + protected static final int DIR_NORTHEAST = 5; + protected static final int DIR_EAST = 6; + protected static final int DIR_SOUTHEAST = 7; + + /** 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[][] _charTiles; + + /** The direction the sprite is currently facing. */ + protected int _dir; +} diff --git a/src/java/com/threerings/media/sprite/Sprite.java b/src/java/com/threerings/media/sprite/Sprite.java index fd96ac46c..61d24abd1 100644 --- a/src/java/com/threerings/media/sprite/Sprite.java +++ b/src/java/com/threerings/media/sprite/Sprite.java @@ -1,8 +1,10 @@ // -// $Id: Sprite.java,v 1.1 2001/07/28 01:50:07 shaper Exp $ +// $Id: Sprite.java,v 1.2 2001/07/30 15:38:52 shaper Exp $ package com.threerings.miso.sprite; +import java.awt.Graphics2D; + import com.threerings.miso.tile.Tile; /** @@ -19,20 +21,84 @@ public class Sprite public int y; /** - * Construct and initialize a Sprite object. + * Construct a Sprite object. + * + * @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. */ public Sprite (int x, int y, Tile[] tiles) + { + init(x, y, tiles); + } + + /** + * 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 setTiles(). + * + * @param x the sprite x-position in pixels. + * @param y the sprite y-position in pixels. + */ + public Sprite (int x, int y) + { + init(x, y, null); + } + + /** + * Initialize the sprite object with the specified parameters. + */ + protected void init (int x, int y, Tile[] tiles) { this.x = x; this.y = y; _tiles = tiles; _curframe = 0; + } + + /** + * Paint the sprite to the specified graphics context. + */ + public void paint (Graphics2D gfx) + { + Tile tile = _tiles[_curframe]; + int xpos = x - (tile.width / 2); + int ypos = y - tile.height; + gfx.drawImage(tile.img, xpos, ypos, null); } - /** The tiles that comprise the sprite. */ + /** + * Returns whether the sprite is inside the given rectangle in + * pixel coordinates. + * + * @param x the rectangle x coordinate. + * @param y the rectangle y coordinate. + * @param width the rectangle width. + * @param height the rectangle height. + * + * @return true if the sprite is inside the rectangle, false if not. + */ + public boolean inside (int x, int y, int width, int height) + { + // treat the sprite as having a width and height of 1 pixel for now + return (this.x >= x && this.x <= (x + width) && + this.y >= y && this.y <= (y + height)); + } + + /** + * Set the tile array used to render the sprite. + * + * @param tiles the sprite tiles. + */ + public void setTiles (Tile[] tiles) + { + _tiles = tiles; + } + + /** The tiles used to render the sprite. */ protected Tile[] _tiles; - /** The current tile frame to render. */ + /** The current tile index to render. */ protected int _curframe; } diff --git a/src/java/com/threerings/media/sprite/SpriteManager.java b/src/java/com/threerings/media/sprite/SpriteManager.java index fcce89e90..ca4a1c9d0 100644 --- a/src/java/com/threerings/media/sprite/SpriteManager.java +++ b/src/java/com/threerings/media/sprite/SpriteManager.java @@ -1,8 +1,11 @@ // -// $Id: SpriteManager.java,v 1.1 2001/07/28 01:50:07 shaper Exp $ +// $Id: SpriteManager.java,v 1.2 2001/07/30 15:38:52 shaper Exp $ package com.threerings.miso.sprite; +import java.awt.Graphics2D; +import java.util.ArrayList; + /** * The SpriteManager manages the sprites running about in the game. */ @@ -13,5 +16,45 @@ public class SpriteManager */ public SpriteManager () { + _sprites = new ArrayList(); } + + /** + * Add a sprite to the set of sprites managed by this SpriteManager. + * + * @param sprite the sprite to add. + */ + public void addSprite (Sprite sprite) + { + _sprites.add(sprite); + } + + /** + * Render the sprites residing within the specified pixel bounds + * to the given graphics context. + * + * @param gfx the graphics context. + * @param x the bounds x-position. + * @param y the bounds y-position. + * @param width the bounds width. + * @param height the bounds height. + */ + public void renderSprites ( + Graphics2D gfx, int x, int y, int width, int height) + { + // TODO: optimize to store sprites based on quadrants they're + // in (or somesuch), and sorted, so that we can more quickly + // determine which sprites to draw. + + int size = _sprites.size(); + for (int ii = 0; ii < size; ii++) { + Sprite sprite = (Sprite)_sprites.get(ii); + if (sprite.inside(x, y, width, height)) { + sprite.paint(gfx); + } + } + } + + /** The sprite objects we're managing. */ + protected ArrayList _sprites; } diff --git a/src/java/com/threerings/miso/client/IsoSceneView.java b/src/java/com/threerings/miso/client/IsoSceneView.java index c8516fa17..cf8f91253 100644 --- a/src/java/com/threerings/miso/client/IsoSceneView.java +++ b/src/java/com/threerings/miso/client/IsoSceneView.java @@ -1,9 +1,10 @@ // -// $Id: IsoSceneView.java,v 1.17 2001/07/28 01:31:51 shaper Exp $ +// $Id: IsoSceneView.java,v 1.18 2001/07/30 15:38:52 shaper Exp $ package com.threerings.miso.scene; import com.threerings.miso.Log; +import com.threerings.miso.sprite.SpriteManager; import com.threerings.miso.tile.Tile; import com.threerings.miso.tile.TileManager; import com.threerings.miso.util.MathUtil; @@ -21,11 +22,12 @@ public class IsoSceneView implements EditableSceneView * Construct an IsoSceneView object and initialize it with the * given tile manager. * - * @param tmgr the tile manager. + * @param tilemgr the tile manager. */ - public IsoSceneView (TileManager tmgr) + public IsoSceneView (TileManager tilemgr, SpriteManager spritemgr) { - _tmgr = tmgr; + _tilemgr = tilemgr; + _spritemgr = spritemgr; _bounds = new Dimension(DEF_BOUNDS_WIDTH, DEF_BOUNDS_HEIGHT); @@ -111,7 +113,12 @@ public class IsoSceneView implements EditableSceneView // draw the tile image at the appropriate screen position gfx.drawImage(tile.img, screenX, ypos, null); - } + } + + // draw all sprites residing in the current line of tiles + _spritemgr.renderSprites( + gfx, screenX, screenY, (length * ISO_TILE_WIDTH), + ISO_TILE_HEIGHT); // draw tile coordinates in each tile if (_showCoords) paintCoords(gfx, tx, ty, screenX, screenY); @@ -360,6 +367,9 @@ public class IsoSceneView implements EditableSceneView /** The scene object to be displayed. */ protected Scene _scene; + /** The sprite manager. */ + protected SpriteManager _spritemgr; + /** The tile manager. */ - protected TileManager _tmgr; + protected TileManager _tilemgr; } diff --git a/tests/src/java/com/threerings/miso/viewer/SceneViewPanel.java b/tests/src/java/com/threerings/miso/viewer/SceneViewPanel.java index 09871cfbf..3654d14fa 100644 --- a/tests/src/java/com/threerings/miso/viewer/SceneViewPanel.java +++ b/tests/src/java/com/threerings/miso/viewer/SceneViewPanel.java @@ -1,5 +1,5 @@ // -// $Id: SceneViewPanel.java,v 1.2 2001/07/28 01:31:51 shaper Exp $ +// $Id: SceneViewPanel.java,v 1.3 2001/07/30 15:38:52 shaper Exp $ package com.threerings.miso.viewer; @@ -13,6 +13,7 @@ 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.SpriteManager; /** * The SceneViewPanel class is responsible for managing a SceneView, @@ -24,12 +25,12 @@ public class SceneViewPanel extends JPanel /** * Construct the panel and initialize it with a context. */ - public SceneViewPanel (ViewerContext ctx) + public SceneViewPanel (ViewerContext ctx, SpriteManager spritemgr) { _ctx = ctx; // construct the view object - _view = new IsoSceneView(_ctx.getTileManager()); + _view = new IsoSceneView(_ctx.getTileManager(), spritemgr); // listen to the desired events addMouseListener(this); diff --git a/tests/src/java/com/threerings/miso/viewer/ViewerFrame.java b/tests/src/java/com/threerings/miso/viewer/ViewerFrame.java index 7e3c55be1..4fc1f6712 100644 --- a/tests/src/java/com/threerings/miso/viewer/ViewerFrame.java +++ b/tests/src/java/com/threerings/miso/viewer/ViewerFrame.java @@ -1,16 +1,17 @@ // -// $Id: ViewerFrame.java,v 1.2 2001/07/28 01:31:51 shaper Exp $ +// $Id: ViewerFrame.java,v 1.3 2001/07/30 15:38:52 shaper 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.AnimationManager; -import com.threerings.miso.sprite.SpriteManager; +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.*; @@ -19,11 +20,13 @@ import javax.swing.*; * contains the application menu bar and panels and responds to menu * events. */ -class ViewerFrame extends JFrame +class ViewerFrame extends JFrame implements WindowListener { public ViewerFrame () { super("Scene Viewer"); + + addWindowListener(this); } /** @@ -33,18 +36,40 @@ class ViewerFrame extends JFrame { _ctx = ctx; - // set up the scene view panel with a default scene - SceneViewPanel svpanel = new SceneViewPanel(_ctx); - - // create the animation manager for the panel + // get a reference on our various manager objects SpriteManager spritemgr = new SpriteManager(); - + TileManager tilemgr = _ctx.getTileManager(); + + // set up the scene view panel with a default scene + SceneViewPanel svpanel = new SceneViewPanel(_ctx, spritemgr); + + // add the test character sprite to the sprite manager + MobileSprite ms = new MobileSprite(300, 300, tilemgr, TSID_CHAR); + spritemgr.addSprite(ms); + + // create the animation manager for this panel AnimationManager animmgr = new AnimationManager(spritemgr, svpanel); // add the scene view panel getContentPane().add(svpanel, BorderLayout.CENTER); } + /** WindowListener interface methods */ + + public void windowClosing (WindowEvent e) { + System.exit(0); + } + + public void windowOpened (WindowEvent e) { } + public void windowClosed (WindowEvent e) { } + public void windowIconified (WindowEvent e) { } + public void windowDeiconified (WindowEvent e) { } + public void windowActivated (WindowEvent e) { } + public void windowDeactivated (WindowEvent e) { } + + /** The tileset id for the character tiles. */ + protected static final int TSID_CHAR = 1003; + /** The panel displaying the scene. */ SceneViewPanel _svpanel;