/* 
	JCavernApplet.java

	Title:			JCavern And Glen
	Author:			Bill Walker
	Description:	
*/

package jcavern;

import java.awt.event.*;
import java.awt.*;

public class KeyboardCommandListener extends KeyAdapter
{
	/** * A model of the game world */
	private World				mWorld;
	
	/** * The representation of the player */
	private Player				mPlayer;
	
	private TextArea			mLogView;
	
	private int					mCurrentMode;
	
	/** * In normal command mode (movement, etc) */
	private static final int	NORMAL_MODE = 1;
	
	/** * In normal command mode (movement, etc) */
	private static final int	SWORD_MODE = 2;
	
	/** * In normal command mode (movement, etc) */
	private static final int	RANGED_ATTACK_MODE = 3;
	
	/** * In normal command mode (movement, etc) */
	private static final int	CASTLE_MODE = 4;


	public KeyboardCommandListener(World aWorld, Player aPlayer, TextArea logView)
	{
		mWorld = aWorld;
		mPlayer = aPlayer;
		mLogView = logView;
		mCurrentMode = NORMAL_MODE;
	}
	
	private int parseDirectionKey(KeyEvent e)
	{
	    switch (e.getKeyChar())
		{
			case 'q' : return Location.NORTHWEST;
			case 'w' : return Location.NORTH;
			case 'e' : return Location.NORTHEAST;
			case 'a' : return Location.WEST;
			case 'd' : return Location.EAST;
			case 'z' : return Location.SOUTHWEST;
			case 'x' : return Location.SOUTH;
			case 'c' : return Location.SOUTHEAST;
		}
		
		throw new IllegalArgumentException("not a movement key!");
	}

	
	/**
	 * Handles keyboard commands.
	 */
	public void keyTyped(KeyEvent e)
	{
	    try
		{
			switch (mCurrentMode)
			{
				case NORMAL_MODE: switch (e.getKeyChar())
						{
							// movement commands
							case 'q' :
							case 'w' :
							case 'e' :
							case 'a' :
							case 'd' :
							case 'z' :
							case 'x' :
							case 'c' : mWorld.move(mPlayer, parseDirectionKey(e)); break;
							case 's' : mCurrentMode = SWORD_MODE; mLogView.append("Sword attack, direction?\n"); break;
						} break;
				case SWORD_MODE: switch (e.getKeyChar())
						{
							// movement commands
							case 'q' :
							case 'w' :
							case 'e' :
							case 'a' :
							case 'd' :
							case 'z' :
							case 'x' :
							case 'c' : String result = mWorld.attack(mPlayer, parseDirectionKey(e)); mLogView.append(result + "\n"); mCurrentMode = NORMAL_MODE; break;
						} break;
			}
		}
		catch(ThingCollisionException tce)
		{
			mLogView.append("Collided with tree\n"); 
			mPlayer.collideWithTree();
		}
		catch(IllegalLocationException ile)
		{
			mLogView.append("Tried to move off the edge of the world\n"); 
		}
		catch(NoSuchThingException nste)
		{
			mLogView.append("Nothing to attack!\n");
			mCurrentMode = NORMAL_MODE;
		}
	}
}
@@ -148,16 +148,46 @@
 a.awt.*;
+%0Aimport java.util.Enumeration;
 %0A%0Apublic
@@ -379,40 +379,8 @@
 ;%0A%09%0A
-%09private TextArea%09%09%09mLogView;%0A%09%0A
 %09pri
@@ -854,26 +854,8 @@
 ayer
-, TextArea logView
 )%0A%09%7B
@@ -899,30 +899,8 @@
 er;%0A
-%09%09mLogView = logView;%0A
 %09%09mC
@@ -1391,18 +1391,16 @@
 %22);%0A%09%7D%0A%0A
-%09%0A
 %09/**%0A%09 *
@@ -1834,31 +1834,33 @@
 D_MODE; 
-mLogView.append
+JCavernApplet.log
 (%22Sword 
@@ -1877,18 +1877,16 @@
 rection?
-%5Cn
 %22); brea
@@ -2131,24 +2131,8 @@
 c' :
- String result =
  mWo
@@ -2177,40 +2177,8 @@
 e));
- mLogView.append(result + %22%5Cn%22);
  mCu
@@ -2281,46 +2281,59 @@
 %0A%09%09%09
-mLogView.append(%22Collided with tree%5Cn%22
+JCavernApplet.log(%22Collided with %22 + tce.getMovee()
 ); %0A
@@ -2411,31 +2411,33 @@
 %0A%09%09%7B%0A%09%09%09
-mLogView.append
+JCavernApplet.log
 (%22Tried 
@@ -2469,18 +2469,16 @@
 he world
-%5Cn
 %22); %0A%09%09%7D
@@ -2524,23 +2524,25 @@
 %0A%09%09%09
-mLogView.append
+JCavernApplet.log
 (%22No
@@ -2561,10 +2561,8 @@
 ack!
-%5Cn
 %22);%0A
@@ -2596,12 +2596,116 @@
 DE;%0A%09%09%7D%0A
+%09%09%0A%09%09// and now, the monsters get a turn%0A%09%09if (mCurrentMode == NORMAL_MODE)%0A%09%09%7B%0A%09%09%09mWorld.doTurn();%0A%09%09%7D%0A
 %09%7D%0A%7D

use new global logging API; use new doTurn() mechanism