📄 controller.java
字号:
/*
* Created on 2005-2-12
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package tankgame;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.Properties;
import java.io.*;
import components.Tank;
/**
* @author AnSen
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Controller {
Tank tnkTarget=null;
ICamera camera=null;
Properties prpCtrl=new Properties();
//input key mask
int crrKey=0;
int iMov_up;
int iMov_down;
int iMov_left;
int iMov_right;
int iFire_key;
int iFire_mouse;
boolean[] aryKeyBorad=new boolean[256];
int gmcPointX=0;
int gmcPointY=0;
double dFireDriect=0;
/**
*
*/
public Controller() {
super();
try{
prpCtrl.load(new FileInputStream("configure"
+File.separator+"Control.properties"));
}catch(IOException e){
e.printStackTrace();
}
iMov_up=Integer.decode(prpCtrl.getProperty("MOV_UP")).intValue();
iMov_down=Integer.decode(prpCtrl.getProperty("MOV_DOWN")).intValue();
iMov_left=Integer.decode(prpCtrl.getProperty("MOV_LEFT")).intValue();
iMov_right=Integer.decode(prpCtrl.getProperty("MOV_RIGHT")).intValue();
iFire_key=Integer.decode(prpCtrl.getProperty("FIRE_KEY")).intValue();
iFire_mouse=Integer.parseInt(prpCtrl.getProperty("FIRE_MOUSE"));
for(int i=0;i<aryKeyBorad.length;i++){
aryKeyBorad[i]=false;
}
}
//key event
public void keyPressed(KeyEvent e) {
aryKeyBorad[e.getKeyCode()]=true;
}
// key event
public void keyReleased(KeyEvent e){
aryKeyBorad[e.getKeyCode()]=false;
}
// mouse event
public void mousePressed(MouseEvent e) {
if(tnkTarget==null){
return;
}
if(e.getButton()==iFire_mouse){
tnkTarget.fire();
}
}
// mouse event
public void mouseMoved(MouseEvent e) {
double dx,dy;
if(camera==null||tnkTarget==null){
return;
}
dx=e.getX()-camera.getGmcPointX();
dy=e.getY()-camera.getGmcPointY();
dFireDriect=Math.atan2(dy,dx);
//tnkTarget.setDFireDirect(dDirect);
}
//key input to tank ctrl echo
public void doKeyInput(){
if(tnkTarget==null){
return;
}
tnkTarget.setDFireDirect(dFireDriect);
if(aryKeyBorad[iMov_down]&&aryKeyBorad[iMov_right]){
tnkTarget.setDirect(Math.PI/4);
tnkTarget.setMoving(true);
return;
}
if(aryKeyBorad[iMov_down]&&aryKeyBorad[iMov_left]){
tnkTarget.setDirect(3*Math.PI/4);
tnkTarget.setMoving(true);
return;
}
if(aryKeyBorad[iMov_up]&&aryKeyBorad[iMov_right]){
tnkTarget.setDirect(-Math.PI/4);
tnkTarget.setMoving(true);
return;
}
if(aryKeyBorad[iMov_up]&&aryKeyBorad[iMov_left]){
tnkTarget.setDirect(-3*Math.PI/4);
tnkTarget.setMoving(true);
return;
}
if(aryKeyBorad[iMov_up]){
tnkTarget.setDirect(-Math.PI/2);
tnkTarget.setMoving(true);
return;
}
if(aryKeyBorad[iMov_down]){
tnkTarget.setDirect(Math.PI/2);
tnkTarget.setMoving(true);
return;
}
if(aryKeyBorad[iMov_left]){
tnkTarget.setDirect(Math.PI);
tnkTarget.setMoving(true);
return;
}
if(aryKeyBorad[iMov_right]){
tnkTarget.setDirect(0);
tnkTarget.setMoving(true);
return;
}
tnkTarget.setMoving(false);
if(aryKeyBorad[iFire_key]){
tnkTarget.fire();
}
}
/**
* @return Returns the camera.
*/
public ICamera getCamera() {
return camera;
}
/**
* @param camera The camera to set.
*/
public void setCamera(ICamera camera) {
this.camera = camera;
}
/**
* @return Returns the tnkTarget.
*/
public Tank getTnkTarget() {
return tnkTarget;
}
/**
* @param tnkTarget The tnkTarget to set.
*/
public void setTnkTarget(Tank tnkTarget) {
this.tnkTarget = tnkTarget;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -