diff --git a/src/java/com/threerings/util/KeyTranslator.java b/src/java/com/threerings/util/KeyTranslator.java index 0f54968c3..0d8d8182b 100644 --- a/src/java/com/threerings/util/KeyTranslator.java +++ b/src/java/com/threerings/util/KeyTranslator.java @@ -1,7 +1,7 @@ // -// $Id: KeyTranslator.java,v 1.2 2001/12/12 18:09:20 shaper Exp $ +// $Id: KeyTranslator.java,v 1.3 2002/01/18 23:32:14 shaper Exp $ -package com.threerings.yohoho.puzzle.util; +package com.threerings.util; import java.util.Iterator; diff --git a/src/java/com/threerings/util/KeyTranslatorImpl.java b/src/java/com/threerings/util/KeyTranslatorImpl.java index 06d25f473..f707cc0fc 100644 --- a/src/java/com/threerings/util/KeyTranslatorImpl.java +++ b/src/java/com/threerings/util/KeyTranslatorImpl.java @@ -1,7 +1,7 @@ // -// $Id: KeyTranslatorImpl.java,v 1.2 2001/12/12 18:09:20 shaper Exp $ +// $Id: KeyTranslatorImpl.java,v 1.3 2002/01/18 23:32:14 shaper Exp $ -package com.threerings.yohoho.puzzle.util; +package com.threerings.util; import java.util.Iterator; diff --git a/src/java/com/threerings/util/KeyboardManager.java b/src/java/com/threerings/util/KeyboardManager.java index b8da63489..13a082521 100644 --- a/src/java/com/threerings/util/KeyboardManager.java +++ b/src/java/com/threerings/util/KeyboardManager.java @@ -1,7 +1,7 @@ // -// $Id: KeyboardManager.java,v 1.7 2002/01/18 23:12:09 shaper Exp $ +// $Id: KeyboardManager.java,v 1.8 2002/01/18 23:32:14 shaper Exp $ -package com.threerings.yohoho.puzzle.util; +package com.threerings.util; import java.awt.Component; import java.awt.KeyEventDispatcher; @@ -17,8 +17,6 @@ import com.samskivert.util.HashIntMap; import com.samskivert.util.Interval; import com.samskivert.util.IntervalManager; -import com.threerings.yohoho.Log; - /** * The keyboard manager observes keyboard actions on a particular * component and posts commands associated with the key presses to the diff --git a/src/java/com/threerings/util/RobotPlayer.java b/src/java/com/threerings/util/RobotPlayer.java new file mode 100644 index 000000000..1b2fd0ca7 --- /dev/null +++ b/src/java/com/threerings/util/RobotPlayer.java @@ -0,0 +1,111 @@ +// +// $Id: RobotPlayer.java,v 1.1 2002/01/18 23:32:14 shaper Exp $ + +package com.threerings.util; + +import java.awt.Component; +import java.util.ArrayList; + +import com.samskivert.swing.Controller; +import com.samskivert.util.CollectionUtil; +import com.samskivert.util.Interval; +import com.samskivert.util.IntervalManager; + +import com.threerings.media.util.RandomUtil; + +/** + * The robot player is a computer player with truly rudimentary artificial + * intelligence that periodically posts random commands selected from the + * available key press and release commands to the target component. + * + * Note that {@link java.awt.Robot} could have been used to post key + * events to the target component rather than commands, but not all key + * events can be simulated in that fashion (e.g., a right shift key + * press), and this seemed somehow more proper in any case. + */ +public class RobotPlayer implements Interval +{ + /** + * Constructs a robot player. + */ + public RobotPlayer (Component target, KeyTranslator xlate) + { + // save off references + _target = target; + _xlate = xlate; + + // build the list of available commands + CollectionUtil.addAll(_press, _xlate.enumeratePressCommands()); + CollectionUtil.addAll(_release, _xlate.enumerateReleaseCommands()); + } + + /** + * Sets whether the robot player is actively posting action commands. + */ + public void setActive (boolean active) + { + if (active && !isActive()) { + // start the robot player + _iid = IntervalManager.register(this, _robotDelay, null, true); + + } else if (!active && isActive()) { + // stop the robot player + IntervalManager.remove(_iid); + _iid = -1; + } + } + + /** + * Sets the delay in milliseconds between posting each action command. + */ + public void setRobotDelay (long delay) + { + _robotDelay = delay; + + // if the robot is active, reset it with the new delay time + if (isActive()) { + setActive(false); + setActive(true); + } + } + + /** + * Returns whether the robot is currently active and periodically + * posting action commands. + */ + public boolean isActive () + { + return (_iid != -1); + } + + // documentation inherited + public void intervalExpired (int id, Object arg) + { + // post a random key press command + int idx = RandomUtil.getInt(_press.size()); + String command = (String)_press.get(idx); + // Log.info("Posting artificial command [cmd=" + command + "]."); + Controller.postAction(_target, command); + } + + /** The default robot delay. */ + protected static final long DEFAULT_ROBOT_DELAY = 500L; + + /** The unique robot interval identifier. */ + protected int _iid = -1; + + /** The milliseconds between posting each action command. */ + protected long _robotDelay = DEFAULT_ROBOT_DELAY; + + /** The list of available key press action commands. */ + protected ArrayList _press = new ArrayList(); + + /** The list of available key release action commands. */ + protected ArrayList _release = new ArrayList(); + + /** The key translator that describes available keys and commands. */ + protected KeyTranslator _xlate; + + /** The target component associated with game action commands. */ + protected Component _target; +} diff --git a/tests/src/java/com/threerings/util/KeyTimerApp.java b/tests/src/java/com/threerings/util/KeyTimerApp.java index 6b9b8c8cb..0c386249b 100644 --- a/tests/src/java/com/threerings/util/KeyTimerApp.java +++ b/tests/src/java/com/threerings/util/KeyTimerApp.java @@ -1,13 +1,13 @@ // -// $Id: KeyTimerApp.java,v 1.1 2002/01/12 02:31:16 shaper Exp $ +// $Id: KeyTimerApp.java,v 1.2 2002/01/18 23:32:15 shaper Exp $ -package com.threerings.yohoho.puzzle.util; +package com.threerings.util; import java.awt.Frame; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; -import com.threerings.yohoho.Log; +import com.threerings.util.Log; public class KeyTimerApp { diff --git a/tests/src/java/com/threerings/util/KeyboardManagerApp.java b/tests/src/java/com/threerings/util/KeyboardManagerApp.java index 44bffe2ab..5d1565363 100644 --- a/tests/src/java/com/threerings/util/KeyboardManagerApp.java +++ b/tests/src/java/com/threerings/util/KeyboardManagerApp.java @@ -1,7 +1,7 @@ // -// $Id: KeyboardManagerApp.java,v 1.1 2002/01/12 02:31:16 shaper Exp $ +// $Id: KeyboardManagerApp.java,v 1.2 2002/01/18 23:32:15 shaper Exp $ -package com.threerings.yohoho.puzzle.util; +package com.threerings.util; import java.awt.Frame; import java.awt.event.ActionEvent; @@ -11,7 +11,7 @@ import java.awt.event.KeyListener; import com.samskivert.swing.Controller; import com.samskivert.swing.ControllerProvider; -import com.threerings.yohoho.Log; +import com.threerings.util.Log; public class KeyboardManagerApp {