Added facilities for a simple robot player to optionally run rampant in

the sword game.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@756 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Walter Korman
2001-12-12 18:09:20 +00:00
parent 0432219e99
commit d31ebd2b54
3 changed files with 66 additions and 13 deletions
@@ -1,8 +1,10 @@
//
// $Id: KeyTranslator.java,v 1.1 2001/12/12 16:55:06 shaper Exp $
// $Id: KeyTranslator.java,v 1.2 2001/12/12 18:09:20 shaper Exp $
package com.threerings.yohoho.puzzle.util;
import java.util.Iterator;
/**
* The key translator interface provides a means whereby the keyboard
* manager can map a key code to the logical {@link
@@ -12,9 +14,9 @@ public interface KeyTranslator
{
/**
* Returns whether there is an action command for the key
* corresponding to the given keycode. The translator may have a
* mapping for an action command for a key press or a key release of
* the key, or both.
* corresponding to the given keycode. The translator may have an
* action command for either a key press or a key release of the key,
* or both.
*/
public boolean hasCommand (int keyCode);
@@ -31,4 +33,16 @@ public interface KeyTranslator
* if there is no associated command.
*/
public String getReleaseCommand (int keyCode);
/**
* Returns an iterator that iterates over the available press
* commands.
*/
public Iterator enumeratePressCommands ();
/**
* Returns an iterator that iterates over the available release
* commands.
*/
public Iterator enumerateReleaseCommands ();
}
@@ -1,8 +1,10 @@
//
// $Id: KeyTranslatorImpl.java,v 1.1 2001/12/12 16:55:06 shaper Exp $
// $Id: KeyTranslatorImpl.java,v 1.2 2001/12/12 18:09:20 shaper Exp $
package com.threerings.yohoho.puzzle.util;
import java.util.Iterator;
import com.samskivert.util.HashIntMap;
/**
@@ -46,7 +48,19 @@ public class KeyTranslatorImpl implements KeyTranslator
{
return (String)_release.get(keyCode);
}
// documentation inherited
public Iterator enumeratePressCommands ()
{
return _press.values().iterator();
}
// documentation inherited
public Iterator enumerateReleaseCommands ()
{
return _release.values().iterator();
}
/** The mapping for key presses from key codes to action commands. */
protected HashIntMap _press = new HashIntMap();
@@ -1,5 +1,5 @@
//
// $Id: KeyboardManager.java,v 1.1 2001/12/12 16:55:06 shaper Exp $
// $Id: KeyboardManager.java,v 1.2 2001/12/12 18:09:20 shaper Exp $
package com.threerings.yohoho.puzzle.util;
@@ -51,6 +51,18 @@ public class KeyboardManager implements KeyListener
});
}
/**
* Sets whether the keyboard manager accepts keyboard input.
*/
public void setEnabled (boolean enabled)
{
// release all keys if we were enabled and are soon to not be
if (!enabled && _enabled) {
releaseAllKeys();
}
_enabled = enabled;
}
/**
* Sets the expected delay in milliseconds between each key
* press/release event the keyboard manager should expect to receive
@@ -85,7 +97,12 @@ public class KeyboardManager implements KeyListener
// documentation inherited
public void keyPressed (KeyEvent e)
{
// logKey("keyPressed", e);
logKey("keyPressed", e);
// bail if we're not accepting input
if (!_enabled) {
return;
}
// get the action command associated with this key
int keyCode = e.getKeyCode();
@@ -105,7 +122,12 @@ public class KeyboardManager implements KeyListener
// documentation inherited
public void keyReleased (KeyEvent e)
{
// logKey("keyReleased", e);
logKey("keyReleased", e);
// bail if we're not accepting input
if (!_enabled) {
return;
}
// get the info object for this key
KeyInfo info = (KeyInfo)_keys.get(e.getKeyCode());
@@ -208,10 +230,10 @@ public class KeyboardManager implements KeyListener
long now = System.currentTimeMillis();
long deltaRelease = now - _lastRelease;
// Log.info("intervalExpired [id=" + id +
// ", key=" + _keyText +
// ", deltaPress=" + (now - _lastPress) +
// ", deltaRelease=" + deltaRelease + "].");
Log.info("intervalExpired [id=" + id +
", key=" + _keyText +
", deltaPress=" + (now - _lastPress) +
", deltaRelease=" + deltaRelease + "].");
if (id == _iid) {
// handle a normal interval where we either (a) create a
@@ -309,6 +331,9 @@ public class KeyboardManager implements KeyListener
/** A hashtable mapping key codes to {@link KeyInfo} objects. */
protected HashIntMap _keys = new HashIntMap();
/** Whether the keyboard manager is accepting keyboard input. */
protected boolean _enabled = true;
/** The component that receives keyboard events and that we associate
* with posted controller commands. */
protected Component _target;