Added facilities for creating a veritable slew of dummy sprites that

walk around at random.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@525 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Walter Korman
2001-10-23 02:03:49 +00:00
parent 53426bc5c4
commit cb7dd5a521
2 changed files with 102 additions and 37 deletions
@@ -1,20 +1,15 @@
//
// $Id: ViewerFrame.java,v 1.22 2001/10/17 22:16:04 shaper Exp $
// $Id: ViewerFrame.java,v 1.23 2001/10/23 02:03:49 shaper Exp $
package com.threerings.miso.viewer;
import java.awt.*;
import javax.swing.*;
import javax.swing.JFrame;
import com.samskivert.swing.*;
import com.threerings.media.sprite.*;
import com.threerings.media.sprite.SpriteManager;
import com.threerings.media.tile.TileManager;
import com.threerings.miso.Log;
import com.threerings.miso.scene.AmbulatorySprite;
import com.threerings.miso.scene.CharacterManager;
import com.threerings.miso.tile.TileUtil;
import com.threerings.miso.viewer.util.ViewerContext;
/**
@@ -36,38 +31,19 @@ public class ViewerFrame extends JFrame
*/
public void init (ViewerContext ctx)
{
_ctx = ctx;
// get a reference on our various manager objects
SpriteManager spritemgr = new SpriteManager();
TileManager tilemgr = _ctx.getTileManager();
TileManager tilemgr = ctx.getTileManager();
// construct the character manager from which we obtain our sprite
CharacterManager charmgr = new CharacterManager(
ctx.getConfig(), tilemgr);
// add the test character sprite to the sprite manager
AmbulatorySprite sprite = charmgr.getCharacter(TSID_CHAR);
if (sprite != null) {
sprite.setLocation(300, 300);
spritemgr.addSprite(sprite);
Log.info("Created sprite [sprite=" + sprite + "].");
}
// set up the scene view panel with a default scene
ViewerSceneViewPanel svpanel =
new ViewerSceneViewPanel(_ctx, spritemgr, sprite);
new ViewerSceneViewPanel(ctx, spritemgr, charmgr);
// add the main panel to the frame
getContentPane().add(svpanel);
}
/** The tileset id for the character tiles. */
protected static final int TSID_CHAR = 1011;
/** The panel displaying the scene. */
ViewerSceneViewPanel _svpanel;
/** The context object. */
protected ViewerContext _ctx;
}
@@ -1,5 +1,5 @@
//
// $Id: ViewerSceneViewPanel.java,v 1.19 2001/10/22 18:15:57 shaper Exp $
// $Id: ViewerSceneViewPanel.java,v 1.20 2001/10/23 02:03:49 shaper Exp $
package com.threerings.miso.viewer;
@@ -9,7 +9,9 @@ import java.io.IOException;
import javax.swing.JPanel;
import com.samskivert.util.Config;
import com.threerings.media.sprite.*;
import com.threerings.media.util.RandomUtil;
import com.threerings.miso.Log;
import com.threerings.miso.scene.*;
@@ -18,18 +20,17 @@ import com.threerings.miso.util.*;
import com.threerings.miso.viewer.util.ViewerContext;
public class ViewerSceneViewPanel extends SceneViewPanel
implements PerformanceObserver
implements PerformanceObserver, SpriteObserver
{
/**
* Construct the panel and initialize it with a context.
*/
public ViewerSceneViewPanel (
ViewerContext ctx, SpriteManager spritemgr, AmbulatorySprite sprite)
ViewerContext ctx, SpriteManager spritemgr, CharacterManager charmgr)
{
super(ctx.getConfig(), spritemgr);
_ctx = ctx;
_sprite = sprite;
// create an animation manager for this panel
_animmgr = new AnimationManager(spritemgr, this);
@@ -44,9 +45,45 @@ public class ViewerSceneViewPanel extends SceneViewPanel
// load up the initial scene
prepareStartingScene();
// create the manipulable sprite
_sprite = createSprite(spritemgr, charmgr);
// create the decoy sprites
createDecoys(spritemgr, charmgr);
PerformanceMonitor.register(this, "paint", 1000);
}
/**
* Creates a new sprite.
*/
protected AmbulatorySprite createSprite (
SpriteManager spritemgr, CharacterManager charmgr)
{
AmbulatorySprite s = charmgr.getCharacter(TSID_CHAR);
if (s != null) {
s.setLocation(300, 300);
s.addSpriteObserver(this);
spritemgr.addSprite(s);
}
return s;
}
/**
* Creates the decoy sprites.
*/
protected void createDecoys (
SpriteManager spritemgr, CharacterManager charmgr)
{
_decoys = new AmbulatorySprite[NUM_DECOYS];
for (int ii = 0; ii < NUM_DECOYS; ii++) {
if ((_decoys[ii] = createSprite(spritemgr, charmgr)) != null) {
createRandomPath(_decoys[ii]);
}
}
}
/**
* Load and set up the starting scene for display.
*/
@@ -63,12 +100,14 @@ public class ViewerSceneViewPanel extends SceneViewPanel
}
}
// documentation inherited
public void paintComponent (Graphics g)
{
super.paintComponent(g);
PerformanceMonitor.tick(this, "paint");
}
// documentation inherited
public void checkpoint (String name, int ticks)
{
Log.info(name + " [ticks=" + ticks + "].");
@@ -80,24 +119,74 @@ public class ViewerSceneViewPanel extends SceneViewPanel
{
int x = e.getX(), y = e.getY();
Log.info("mousePressed [x=" + x + ", y=" + y + "].");
createPath(_sprite, x, y);
}
/**
* Assigns the sprite a path leading to the given destination
* screen coordinates. Returns whether a path was successfully
* assigned.
*/
protected boolean createPath (AmbulatorySprite s, int x, int y)
{
// get the path from here to there
Path path = _view.getPath(_sprite, x, y);
Path path = _view.getPath(s, x, y);
if (path == null) {
_sprite.cancelMove();
return;
s.cancelMove();
return false;
}
// start the sprite moving along the path
((LineSegmentPath)path).setVelocity(100f/1000f);
_sprite.move(path);
s.move(path);
return true;
}
/**
* Assigns a new random path to the given sprite.
*/
protected void createRandomPath (AmbulatorySprite s)
{
Dimension d = _scenemodel.bounds.getSize();
int x, y;
do {
x = RandomUtil.getInt(d.width);
y = RandomUtil.getInt(d.height);
// Log.info("Moving sprite [s=" + s + ", x=" + x +
// ", y=" + y + "].");
} while (!createPath(s, x, y));
}
// documentation inherited
public void handleEvent (SpriteEvent event)
{
if (event instanceof PathCompletedEvent) {
AmbulatorySprite s = (AmbulatorySprite)event.getSprite();
// Log.info("Path completed [sprite=" + s + "].");
if (s != _sprite) {
// move the sprite to a new random location
createRandomPath(s);
}
}
}
/** The number of decoy characters milling about. */
protected static final int NUM_DECOYS = 10;
/** The tileset id for the character tiles. */
protected static final int TSID_CHAR = 1011;
/** The animation manager. */
AnimationManager _animmgr;
/** The sprite we're manipulating within the view. */
protected AmbulatorySprite _sprite;
/** The test sprites that meander about aimlessly. */
protected AmbulatorySprite _decoys[];
/** The context object. */
protected ViewerContext _ctx;
}