Behold, Nenya, Ring of Water and repository for our media and animation related

goodies, both Java 2D and LWJGL/JME 3D.


git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@1 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Michael Bayne
2006-06-23 18:07:28 +00:00
commit c2117ee86d
570 changed files with 61913 additions and 0 deletions
@@ -0,0 +1,89 @@
//
// $Id: ScrollingFrame.java 3099 2004-08-27 02:21:06Z mdb $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.miso.client;
import java.awt.Color;
import java.awt.Component;
import java.awt.GraphicsConfiguration;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import com.samskivert.swing.VGroupLayout;
import com.threerings.media.SafeScrollPane;
/**
* The main application window.
*/
public class ScrollingFrame extends JFrame
{
/**
* Creates a frame in which the scrolling test app can operate.
*/
public ScrollingFrame (GraphicsConfiguration gc)
{
super(gc);
// set up the frame options
setTitle("Scene scrolling test");
// setUndecorated(true);
setIgnoreRepaint(true);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// set the frame and content panel background to black
setBackground(Color.black);
getContentPane().setBackground(Color.black);
// create some interface elements to go with our scrolling panel
VGroupLayout vgl = new VGroupLayout(VGroupLayout.STRETCH);
vgl.setOffAxisPolicy(VGroupLayout.STRETCH);
getContentPane().setLayout(vgl);
vgl = new VGroupLayout(VGroupLayout.NONE);
vgl.setOffAxisPolicy(VGroupLayout.STRETCH);
JPanel stuff = new JPanel(vgl);
for (int i = 0; i < 10; i++) {
stuff.add(new JButton("Button " + i));
}
getContentPane().add(new SafeScrollPane(stuff));
}
/**
* Sets the panel displayed by this frame.
*/
public void setPanel (Component panel)
{
// if we had an old panel, remove it
if (_panel != null) {
getContentPane().remove(_panel);
}
// now add the new one
_panel = panel;
getContentPane().add(_panel, 0);
}
protected Component _panel;
}
@@ -0,0 +1,124 @@
//
// $Id: ScrollingScene.java 3355 2005-02-17 01:54:54Z mdb $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.miso.client;
import java.awt.Rectangle;
import java.util.Iterator;
import java.util.Random;
import com.samskivert.io.PersistenceException;
import com.threerings.media.tile.NoSuchTileSetException;
import com.threerings.media.tile.TileSet;
import com.threerings.media.tile.TileSetRepository;
import com.threerings.miso.data.MisoSceneModel;
import com.threerings.miso.tile.BaseTile;
import com.threerings.miso.tile.BaseTileSet;
import com.threerings.miso.util.MisoContext;
import com.threerings.miso.util.ObjectSet;
import com.threerings.miso.data.ObjectInfo;
/**
* Provides an infinite array of tiles in which to scroll.
*/
public class ScrollingScene extends MisoSceneModel
{
public ScrollingScene (MisoContext ctx)
throws NoSuchTileSetException, PersistenceException
{
// locate the water tileset
TileSetRepository tsrepo = ctx.getTileManager().getTileSetRepository();
Iterator iter = tsrepo.enumerateTileSets();
String tsname = null;
while (iter.hasNext()) {
TileSet tset = (TileSet)iter.next();
// yay for built-in regex support!
if (tset.getName().matches(".*[Ww]ater.*") &&
tset instanceof BaseTileSet) {
tsname = tset.getName();
break;
}
}
if (tsname == null) {
throw new RuntimeException("Unable to locate water tileset.");
}
// now we look the tileset up by name so that it is properly
// initialized and all that business
TileSet wtset = ctx.getTileManager().getTileSet(tsname);
// grab our four repeating tiles
_tiles = new BaseTile[wtset.getTileCount()];
for (int ii = 0; ii < wtset.getTileCount(); ii++) {
_tiles[ii] = (BaseTile)wtset.getTile(ii);
}
}
public int getBaseTileId (int x, int y)
{
return -1;
}
public boolean setBaseTile (int fqTileId, int x, int y)
{
return false;
}
public boolean addObject (ObjectInfo info)
{
return true;
}
public void getObjects (Rectangle region, ObjectSet set)
{
}
public void updateObject (ObjectInfo info)
{
}
public boolean removeObject (ObjectInfo info)
{
return false;
}
// documentation inherited from interface
public BaseTile getBaseTile (int x, int y)
{
long seed = ((x^y) ^ multiplier) & mask;
long hash = (seed * multiplier + addend) & mask;
int tidx = (int)((hash >> 10) % _tiles.length);
return _tiles[tidx];
}
protected BaseTile[] _tiles;
protected Random _rand = new Random();
protected final static long multiplier = 0x5DEECE66DL;
protected final static long addend = 0xBL;
protected final static long mask = (1L << 48) - 1;
protected static final int WATER_TILESET_ID = 8;
}
@@ -0,0 +1,234 @@
//
// $Id: ScrollingTestApp.java 4181 2006-06-07 21:54:12Z mdb $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.miso.client;
import java.awt.DisplayMode;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.io.IOException;
import com.samskivert.swing.util.SwingUtil;
import com.samskivert.util.Config;
import com.threerings.resource.ResourceManager;
import com.threerings.media.FrameManager;
import com.threerings.media.image.ImageManager;
import com.threerings.media.util.LinePath;
import com.threerings.media.util.Path;
import com.threerings.media.sprite.PathAdapter;
import com.threerings.media.sprite.Sprite;
import com.threerings.media.tile.bundle.BundledTileSetRepository;
import com.threerings.cast.CharacterComponent;
import com.threerings.cast.CharacterDescriptor;
import com.threerings.cast.CharacterManager;
import com.threerings.cast.CharacterSprite;
import com.threerings.cast.NoSuchComponentException;
import com.threerings.cast.bundle.BundledComponentRepository;
import com.threerings.miso.Log;
import com.threerings.miso.MisoConfig;
import com.threerings.miso.tile.MisoTileManager;
import com.threerings.miso.util.MisoContext;
/**
* Tests the scrolling capabilities of the IsoSceneView.
*/
public class ScrollingTestApp
{
/**
* Construct and initialize the scrolling test app.
*/
public ScrollingTestApp (String[] args)
throws IOException
{
// get the graphics environment
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
// get the target graphics device
GraphicsDevice gd = env.getDefaultScreenDevice();
Log.info("Graphics device [dev=" + gd +
", mem=" + gd.getAvailableAcceleratedMemory() +
", displayChange=" + gd.isDisplayChangeSupported() +
", fullScreen=" + gd.isFullScreenSupported() + "].");
// get the graphics configuration and display mode information
GraphicsConfiguration gc = gd.getDefaultConfiguration();
DisplayMode dm = gd.getDisplayMode();
Log.info("Display mode [bits=" + dm.getBitDepth() +
", wid=" + dm.getWidth() + ", hei=" + dm.getHeight() +
", refresh=" + dm.getRefreshRate() + "].");
// create the window
_frame = new ScrollingFrame(gc);
// set up our frame manager
_framemgr = FrameManager.newInstance(_frame, _frame);
// we don't need to configure anything
ResourceManager rmgr = new ResourceManager("rsrc");
rmgr.initBundles(
null, "config/resource/manager.properties", null);
ImageManager imgr = new ImageManager(rmgr, _frame);
_tilemgr = new MisoTileManager(rmgr, imgr);
_tilemgr.setTileSetRepository(
new BundledTileSetRepository(rmgr, imgr, "tilesets"));
// hack in some different MisoProperties
MisoConfig.config = new Config("rsrc/config/miso/scrolling");
// create the context object
MisoContext ctx = new ContextImpl();
// create the various managers
BundledComponentRepository crepo =
new BundledComponentRepository(rmgr, imgr, "components");
CharacterManager charmgr = new CharacterManager(imgr, crepo);
// create our scene view panel
_panel = new MisoScenePanel(ctx, MisoConfig.getSceneMetrics());
_frame.setPanel(_panel);
// create our "ship" sprite
String scclass = "navsail", scname = "smsloop";
try {
CharacterComponent ccomp = crepo.getComponent(scclass, scname);
CharacterDescriptor desc = new CharacterDescriptor(
new int[] { ccomp.componentId }, null);
// now create the actual sprite and stick 'em in the scene
_ship = charmgr.getCharacter(desc);
if (_ship != null) {
_ship.setFollowingPathAction("sailing");
_ship.setRestingAction("sailing");
_ship.setActionSequence("sailing");
_ship.setLocation(0, 0);
_panel.addSprite(_ship);
}
} catch (NoSuchComponentException nsce) {
Log.warning("Can't locate ship component [class=" + scclass +
", name=" + scname + "].");
}
_ship.addSpriteObserver(new PathAdapter() {
public void pathCompleted (Sprite sprite, Path path, long when) {
// keep scrolling for a spell
if (++_sidx < DX.length) {
int x = _ship.getX(), y = _ship.getY();
LinePath lpath = new LinePath(
x, y, x + DX[_sidx], y + DY[_sidx], 30000l);
_ship.move(lpath);
}
}
protected int _sidx = -1;
protected final int[] DX = { 1620, 0, 1000, -1000, 1000, 2000 };
protected final int[] DY = { 1400, 1000, 0, 1000, -1000, 1000 };
});
// make the panel follow the ship around
_panel.setFollowsPathable(_ship, MisoScenePanel.CENTER_ON_PATHABLE);
int x = _ship.getX(), y = _ship.getY();
_ship.move(new LinePath(x, y, x, y + 1000, 3000l));
// size and position the window, entering full-screen exclusive
// mode if available
if (gd.isFullScreenSupported()) {
Log.info("Entering full-screen exclusive mode.");
gd.setFullScreenWindow(_frame);
_frame.setUndecorated(true);
} else {
Log.warning("Full-screen exclusive mode not available.");
// _frame.pack();
_frame.setSize(200, 300);
SwingUtil.centerWindow(_frame);
}
try {
_panel.setSceneModel(new ScrollingScene(ctx));
} catch (Exception e) {
Log.warning("Error creating scene: " + e);
Log.logStackTrace(e);
}
}
/**
* The implementation of the MisoContext interface that provides
* handles to the manager objects that offer commonly used services.
*/
protected class ContextImpl implements MisoContext
{
public MisoTileManager getTileManager () {
return _tilemgr;
}
public FrameManager getFrameManager () {
return _framemgr;
}
}
/**
* Run the application.
*/
public void run ()
{
// show the window
_frame.show();
_framemgr.start();
}
/**
* Instantiate the application object and start it running.
*/
public static void main (String[] args)
{
try {
ScrollingTestApp app = new ScrollingTestApp(args);
app.run();
} catch (IOException ioe) {
System.err.println("Error initializing scrolling app.");
ioe.printStackTrace();
}
}
/** The tile manager object. */
protected MisoTileManager _tilemgr;
/** The frame manager. */
protected FrameManager _framemgr;
/** The main application window. */
protected ScrollingFrame _frame;
/** The main panel. */
protected MisoScenePanel _panel;
/** The ship in the center of our screen. */
protected CharacterSprite _ship;
}
@@ -0,0 +1,191 @@
//
// $Id: ViewerApp.java 4181 2006-06-07 21:54:12Z mdb $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.miso.viewer;
import java.awt.DisplayMode;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.io.IOException;
import com.samskivert.swing.util.SwingUtil;
import com.threerings.resource.ResourceManager;
import com.threerings.media.FrameManager;
import com.threerings.media.image.ImageManager;
import com.threerings.media.tile.bundle.BundledTileSetRepository;
import com.threerings.cast.CharacterManager;
import com.threerings.cast.bundle.BundledComponentRepository;
import com.threerings.miso.Log;
import com.threerings.miso.data.SimpleMisoSceneModel;
import com.threerings.miso.tile.MisoTileManager;
import com.threerings.miso.tools.xml.SimpleMisoSceneParser;
import com.threerings.miso.util.MisoContext;
/**
* The ViewerApp is a scene viewing application that allows for trying
* out game scenes in a pseudo-runtime environment.
*/
public class ViewerApp
{
/**
* Construct and initialize the ViewerApp object.
*/
public ViewerApp (String[] args)
throws IOException
{
if (args.length < 1) {
System.err.println("Usage: ViewerApp scene_file.xml");
System.exit(-1);
}
// get the graphics environment
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
// get the target graphics device
GraphicsDevice gd = env.getDefaultScreenDevice();
Log.info("Graphics device [dev=" + gd +
", mem=" + gd.getAvailableAcceleratedMemory() +
", displayChange=" + gd.isDisplayChangeSupported() +
", fullScreen=" + gd.isFullScreenSupported() + "].");
// get the graphics configuration and display mode information
GraphicsConfiguration gc = gd.getDefaultConfiguration();
DisplayMode dm = gd.getDisplayMode();
Log.info("Display mode [bits=" + dm.getBitDepth() +
", wid=" + dm.getWidth() + ", hei=" + dm.getHeight() +
", refresh=" + dm.getRefreshRate() + "].");
// create the window
_frame = new ViewerFrame(gc);
_framemgr = FrameManager.newInstance(_frame, _frame);
// we don't need to configure anything
ResourceManager rmgr = new ResourceManager("rsrc");
rmgr.initBundles(
null, "config/resource/manager.properties", null);
ImageManager imgr = new ImageManager(rmgr, _frame);
_tilemgr = new MisoTileManager(rmgr, imgr);
_tilemgr.setTileSetRepository(
new BundledTileSetRepository(rmgr, imgr, "tilesets"));
// create the context object
MisoContext ctx = new ContextImpl();
// create the various managers
BundledComponentRepository crepo =
new BundledComponentRepository(rmgr, imgr, "components");
CharacterManager charmgr = new CharacterManager(imgr, crepo);
// create our scene view panel
_panel = new ViewerSceneViewPanel(ctx, charmgr, crepo);
_frame.setPanel(_panel);
// load up the scene specified by the user
try {
SimpleMisoSceneParser parser = new SimpleMisoSceneParser("");
SimpleMisoSceneModel model = parser.parseScene(args[0]);
if (model == null) {
Log.warning("No miso scene found in scene file " +
"[path=" + args[0] + "].");
System.exit(-1);
}
_panel.setSceneModel(model);
} catch (Exception e) {
Log.warning("Unable to parse scene [path=" + args[0] + "].");
Log.logStackTrace(e);
System.exit(-1);
}
// size and position the window, entering full-screen exclusive
// mode if available
if (gd.isFullScreenSupported()) {
Log.info("Entering full-screen exclusive mode.");
gd.setFullScreenWindow(_frame);
} else {
Log.warning("Full-screen exclusive mode not available.");
// _frame.pack();
_frame.setSize(600, 400);
SwingUtil.centerWindow(_frame);
}
}
/**
* The implementation of the MisoContext interface that provides
* handles to the config and manager objects that offer commonly used
* services.
*/
protected class ContextImpl implements MisoContext
{
public MisoTileManager getTileManager ()
{
return _tilemgr;
}
public FrameManager getFrameManager ()
{
return _framemgr;
}
}
/**
* Run the application.
*/
public void run ()
{
// show the window
_frame.show();
_framemgr.start();
}
/**
* Instantiate the application object and start it running.
*/
public static void main (String[] args)
{
try {
ViewerApp app = new ViewerApp(args);
app.run();
} catch (IOException ioe) {
System.err.println("Error initializing viewer app.");
ioe.printStackTrace();
}
}
/** The tile manager object. */
protected MisoTileManager _tilemgr;
/** The frame manager. */
protected FrameManager _framemgr;
/** The main application window. */
protected ViewerFrame _frame;
/** The main panel. */
protected ViewerSceneViewPanel _panel;
}
@@ -0,0 +1,94 @@
//
// $Id: ViewerFrame.java 3099 2004-08-27 02:21:06Z mdb $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.miso.viewer;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GraphicsConfiguration;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import com.samskivert.swing.util.MenuUtil;
/**
* The viewer frame is the main application window.
*/
public class ViewerFrame extends JFrame
{
/**
* Creates a frame in which the viewer application can operate.
*/
public ViewerFrame (GraphicsConfiguration gc)
{
super(gc);
// set up the frame options
setTitle("Scene Viewer");
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// set the frame and content panel background to black
setBackground(Color.black);
getContentPane().setBackground(Color.black);
// create the "Settings" menu
JMenu menuSettings = new JMenu("Settings");
MenuUtil.addMenuItem(
menuSettings, "Preferences", this, "handlePreferences");
// create the menu bar
JMenuBar bar = new JMenuBar();
bar.add(menuSettings);
// add the menu bar to the frame
setJMenuBar(bar);
}
/**
* Sets the panel displayed by this frame.
*/
public void setPanel (Component panel)
{
// if we had an old panel, remove it
if (_panel != null) {
getContentPane().remove(_panel);
}
// now add the new one
_panel = panel;
getContentPane().add(_panel, BorderLayout.CENTER);
}
/**
* Dummy callback method.
*/
public void handlePreferences (ActionEvent event)
{
System.err.println("Nothing doing!");
}
protected Component _panel;
}
@@ -0,0 +1,237 @@
//
// $Id: ViewerSceneViewPanel.java 4188 2006-06-13 18:03:48Z mdb $
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2004 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.miso.viewer;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import com.samskivert.util.RandomUtil;
import com.threerings.cast.CharacterDescriptor;
import com.threerings.cast.CharacterManager;
import com.threerings.cast.CharacterSprite;
import com.threerings.cast.ComponentRepository;
import com.threerings.cast.util.CastUtil;
import com.threerings.media.sprite.PathObserver;
import com.threerings.media.sprite.Sprite;
import com.threerings.media.sprite.SpriteManager;
import com.threerings.media.util.LineSegmentPath;
import com.threerings.media.util.Path;
import com.threerings.media.util.PerformanceMonitor;
import com.threerings.media.util.PerformanceObserver;
import com.threerings.miso.Log;
import com.threerings.miso.MisoConfig;
import com.threerings.miso.client.MisoScenePanel;
import com.threerings.miso.data.MisoSceneModel;
import com.threerings.miso.util.MisoContext;
public class ViewerSceneViewPanel extends MisoScenePanel
implements PerformanceObserver, PathObserver
{
/**
* Construct the panel and initialize it with a context.
*/
public ViewerSceneViewPanel (MisoContext ctx,
CharacterManager charmgr,
ComponentRepository crepo)
{
super(ctx, MisoConfig.getSceneMetrics());
// create the character descriptors
_descUser = CastUtil.getRandomDescriptor("female", crepo);
_descDecoy = CastUtil.getRandomDescriptor("male", crepo);
// create the manipulable sprite
_sprite = createSprite(_spritemgr, charmgr, _descUser);
setFollowsPathable(_sprite, CENTER_ON_PATHABLE);
// create the decoy sprites
createDecoys(_spritemgr, charmgr);
PerformanceMonitor.register(this, "paint", 1000);
}
// documentation inherited
public void setSceneModel (MisoSceneModel model)
{
super.setSceneModel(model);
Log.info("Using " + model + ".");
}
// documentation inherited
public void doLayout ()
{
super.doLayout();
// now that we have a scene, we can create valid paths for our
// decoy sprites
createDecoyPaths();
}
/**
* Creates a new sprite.
*/
protected CharacterSprite createSprite (
SpriteManager spritemgr, CharacterManager charmgr,
CharacterDescriptor desc)
{
CharacterSprite s = charmgr.getCharacter(desc);
if (s != null) {
// start 'em out standing
s.setActionSequence(CharacterSprite.STANDING);
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 CharacterSprite[NUM_DECOYS];
for (int ii = 0; ii < NUM_DECOYS; ii++) {
_decoys[ii] = createSprite(spritemgr, charmgr, _descDecoy);
}
}
/**
* Creates paths for the decoy sprites.
*/
protected void createDecoyPaths ()
{
for (int ii = 0; ii < NUM_DECOYS; ii++) {
if (_decoys[ii] != null) {
createRandomPath(_decoys[ii]);
}
}
}
// documentation inherited
public void paint (Graphics g)
{
super.paint(g);
PerformanceMonitor.tick(this, "paint");
}
// documentation inherited
public void checkpoint (String name, int ticks)
{
Log.info(name + " [ticks=" + ticks + "].");
}
// documentation inherited
public void mousePressed (MouseEvent e)
{
super.mousePressed(e);
int x = e.getX(), y = e.getY();
Log.info("Mouse pressed +" + x + "+" + y);
switch (e.getModifiers()) {
case MouseEvent.BUTTON1_MASK:
createPath(_sprite, x, y);
break;
case MouseEvent.BUTTON2_MASK:
for (int ii = 0; ii < NUM_DECOYS; ii++) {
createPath(_decoys[ii], x, y);
}
break;
}
}
/**
* Assigns the sprite a path leading to the given destination
* screen coordinates. Returns whether a path was successfully
* assigned.
*/
protected boolean createPath (CharacterSprite s, int x, int y)
{
// get the path from here to there
LineSegmentPath path = (LineSegmentPath)getPath(s, x, y, true);
if (path == null) {
s.cancelMove();
return false;
}
// start the sprite moving along the path
path.setVelocity(100f/1000f);
s.move(path);
return true;
}
/**
* Assigns a new random path to the given sprite.
*/
protected void createRandomPath (CharacterSprite s)
{
Dimension d = _vbounds.getSize();
if (d.width <= 0 || d.height <= 0) {
return;
}
int x, y;
do {
x = RandomUtil.getInt(d.width);
y = RandomUtil.getInt(d.height);
} while (!createPath(s, x, y));
}
// documentation inherited
public void pathCompleted (Sprite sprite, Path path, long when)
{
CharacterSprite s = (CharacterSprite)sprite;
if (s != _sprite) {
// move the sprite to a new random location
createRandomPath(s);
}
}
// documentation inherited
public void pathCancelled (Sprite sprite, Path path)
{
// nothing doing
}
/** The number of decoy characters milling about. */
protected static final int NUM_DECOYS = 5;
/** The character descriptor for the user character. */
protected CharacterDescriptor _descUser;
/** The character descriptor for the decoy characters. */
protected CharacterDescriptor _descDecoy;
/** The sprite we're manipulating within the view. */
protected CharacterSprite _sprite;
/** The test sprites that meander about aimlessly. */
protected CharacterSprite _decoys[];
}