/*
* Copyright: Copyright (c) 2010 Pzhs2
* File Name : SofaRacingInterpreter.java
* Author : Suing
* Finish Date : 2010/12/15
* Maintainer : Suing
* Maintain Date :
* Maintain Reason:
* Current Version: 1.0
* Before Version :
*/
package cn.edu.nju.software.kernel.interpreter;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.TreeMap;
import cn.edu.nju.software.kernel.action.Action;
import cn.edu.nju.software.kernel.action.sofa.SofaAccelerateAction;
import cn.edu.nju.software.kernel.action.sofa.SofaBackwardAction;
import cn.edu.nju.software.kernel.action.sofa.SofaForwardAction;
import cn.edu.nju.software.kernel.action.sofa.SofaLeftwardAction;
import cn.edu.nju.software.kernel.action.sofa.SofaRightwardAction;
import cn.edu.nju.software.kernel.action.sofa.SofaStopAction;
/**
* This class implements am interpreter for racing game as an example.
*
* @author Suing
*
* @version 1.0, 2010/12/15
*
*/
public class SofaRacingInterpreter extends GameInterpreter {
/**
* The java robot used to control the keyboard/mouse.
*/
private Robot robot;
/**
* Mapping between action and key.
*/
private TreeMap<String, Integer> actionKeyMap;
public SofaRacingInterpreter(TreeMap<String, Integer> actionKeyMapin) {
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
actionKeyMap = actionKeyMapin;
Action[] actions = new Action[actionKeyMap.size()];
String[] actionNames = actionKeyMap.keySet().toArray(new String[0]);
for (int i = 0; i < actionNames.length; i++) {
if (actionNames[i].equals("SofaForwardAction"))
actions[i] = new RacingForwardAction();
else if (actionNames[i].equals("SofaBackwardAction"))
actions[i] = new RacingBackwardAction();
else if (actionNames[i].equals("SofaLeftwardAction"))
actions[i] = new RacingLeftwardAction();
else if (actionNames[i].equals("SofaRightwardAction"))
actions[i] = new RacingRightwardAction();
else if (actionNames[i].equals("SofaAccelerateAction"))
actions[i] = new RacingAccelerateAction();
else if (actionNames[i].equals("SofaStopAction"))
actions[i] = new RacingStopAction();
}
this.setActions(actions);
}
/**
* Control the forward action.
*
* @author suing
*
*/
private class RacingForwardAction extends SofaForwardAction {
private int key;
private boolean pressed = false;
public RacingForwardAction() {
key = actionKeyMap.get(this.getID());
if (KeyEvent.getKeyText(key).contains("Unknown keyCode"))
key = 0;
}
@Override
public void doAction() {
if(key==0)
return;
// forward needed
if ((getData() & Action.SOFA_FORWARD) > 0) {
if (!pressed) {
robot.keyPress(key);
pressed = true;
}
// forward needn't
} else if (pressed) {
robot.keyRelease(key);
pressed = false;
}
}
}
/**
* Control the back action.
*
* @author suing
*
*/
private class RacingBackwardAction extends SofaBackwardAction {
private int key;
private boolean pressed = false;
public RacingBackwardAction() {
key = actionKeyMap.get(this.getID());
if (KeyEvent.getKeyText(key).contains("Unknown keyCode"))
key = 0;
}
@Override
public void doAction() {
if(key==0)
return;
// backward needed
if ((getData() & Action.SOFA_BACKWARD) > 0) {
if (!pressed) {
robot.keyPress(key);
pressed = true;
}
// backward needn't
} else if (pressed) {
robot.keyRelease(key);
pressed = false;
}
}
}
/**
* Control the left action.
*
* @author suing
*
*/
private class RacingLeftwardAction extends SofaLeftwardAction {
private int key;
private boolean pressed = false;
private boolean presudden = false;
public RacingLeftwardAction() {
key = actionKeyMap.get(this.getID());
if (KeyEvent.getKeyText(key).contains("Unknown keyCode"))
key = 0;
}
@Override
public void doAction() {
if(key==0)
return;
// left needed
if ((getData() & Action.SOFA_LEFT) > 0) {
if ((getData() & Action.SOFA_SUDDEN) > 0) {
if (!pressed) {
robot.keyPress(key);
pressed = true;
}
presudden = true;
} else {
if (pressed) {
robot.keyRelease(key);
pressed = false;
} else if (!presudden) {
robot.keyPress(key);
try {
Thread.sleep(100);
} catch (Exception e) {
}
robot.keyRelease(key);
}
presudden = false;
}
// left needn't
} else if (pressed) {
robot.keyRelease(key);
pressed = false;
presudden = false;
}
}
}
/**
* Control the right action.
*
* @author suing
*
*/
private class RacingRightwardAction extends SofaRightwardAction {
private int key;
private boolean pressed = false;
private boolean presudden = false;
public RacingRightwardAction() {
key = actionKeyMap.get(this.getID());
if (KeyEvent.getKeyText(key).contains("Unknown keyCode"))
key = 0;
}
@Override
public void doAction() {
if(key==0)
return;
// right needed
if ((getData() & Action.SOFA_RIGHT) > 0) {
if ((getData() & Action.SOFA_SUDDEN) > 0) {
if (!pressed) {
robot.keyPress(key);
pressed = true;
}
presudden = true;
} else {
if (pressed) {
robot.keyRelease(key);
pressed = false;
} else if (!presudden) {
robot.keyPress(key);
try {
Thread.sleep(100);
} catch (Exception e) {
}
robot.keyRelease(key);
}
presudden = false;
}
// right needn't
} else if (pressed) {
robot.keyRelease(key);
pressed = false;
presudden = false;
}
}
}
/**
* Control the accelerate action.
*
* @author suing
*
*/
private class RacingAccelerateAction extends SofaAccelerateAction {
private int key;
private boolean pressed = false;
public RacingAccelerateAction() {
key = actionKeyMap.get(this.getID());
if (KeyEvent.getKeyText(key).contains("Unknown keyCode"))
key = 0;
}
@Override
public void doAction() {
if(key==0)
return;
// accelerate needed
if ((getData() & Action.SOFA_ACCELERATE) > 0) {
if (!pressed) {
robot.keyPress(key);
pressed = true;
}
// accelerate needn't
} else if (pressed) {
robot.keyRelease(key);
pressed = false;
}
}
}
/**
* Control the stop action.
*
* @author suing
*
*/
private class RacingStopAction extends SofaStopAction {
private int key;
private boolean pressed = false;
public RacingStopAction() {
key = actionKeyMap.get(this.getID());
if (KeyEvent.getKeyText(key).contains("Unknown keyCode"))
key = 0;
}
@Override
public void doAction() {
if(key==0)
return;
// stop needed
if (getData() == Action.SOFA_STOP) {
if (!pressed) {
robot.keyPress(key);
pressed = true;
}
// stop needn't
} else if (pressed) {
robot.keyRelease(key);
pressed = false;
}
}
}
/**
* Set the mapping between action and key.
*
* @param actionKeyMapin
*/
public void setActionKeyMap(TreeMap<String, Integer> actionKeyMapin) {
actionKeyMap = actionKeyMapin;
}
}
|