public client() {
super();
Common.info(7, "new client()");
initComponents();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
premuti = new KeyFlag((short) 0);
bufferedImages = new HashMap<String, BufferedImage>();
clientusage=Common.product + " v" + Common.version + "\n\n"+
" to RUN use ARROWS or A,D keys\n"+
" to JUMP use UP key or SPACE or W\n"+
" to BEND use DOWN or S key\n"+
" to use the SPECIAL SKILLS use ALT or ALT GR key\n"+
" to AIM'n'FIRE use the MOUSE\n"+
" to change GUN use NUMBER keys\n"+
" to CHAT use C key\n";
this.addMouseListener(new java.awt.event.MouseListener() {
public void mouseClicked(MouseEvent e) {
requestFocus();
}
public void mouseEntered(MouseEvent e) {
requestFocus();
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
double x = e.getX();
double y = c.resolution.y - e.getY();
x -= c.resolution.x / 2;
y -= c.resolution.y / 2 ;
double mod = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
x /= mod;
y /= mod;
// x/=c.resolution.x/2;
// y/=c.resolution.y/2;
if (!premuti.check(KeyFlag.FIRE)) {
premuti.add(KeyFlag.FIRE);
}
try {
c.setDirection(x, -y);
} catch (IOException ex) {
Logger.getLogger(client.class.getName()).log(Level.SEVERE,
null, ex);
}
}
public void mouseReleased(MouseEvent e) {
}
});
this.addKeyListener(new java.awt.event.KeyListener() {
public void keyPressed(KeyEvent e) {
if ((e.getKeyCode() == KeyEvent.VK_C)) {
String str = Common.ask("chat: ");
try {
c.chat(str);
} catch (IOException ex) {
ex.printStackTrace();
}
}
// premuti.add("special");
if (e.getKeyCode() == KeyEvent.VK_F1)
Common.message(clientusage,false,false);
if (e.getKeyCode() == KeyEvent.VK_1
&& !premuti.check(KeyFlag.k1))
premuti.add(KeyFlag.k1);
if (e.getKeyCode() == KeyEvent.VK_2
&& !premuti.check(KeyFlag.k2))
premuti.add(KeyFlag.k2);
if (e.getKeyCode() == KeyEvent.VK_3
&& !premuti.check(KeyFlag.k3))
premuti.add(KeyFlag.k3);
if ((e.getKeyCode() == KeyEvent.VK_SPACE
|| e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W))
premuti.add(KeyFlag.JUMP);
if ((e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D))
premuti.add(KeyFlag.RIGHT);
if ((e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S))
premuti.add(KeyFlag.DOWN);
if ((e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A))
premuti.add(KeyFlag.LEFT);
if ((e.getKeyCode() == KeyEvent.VK_ALT || e.getKeyCode() == KeyEvent.VK_ALT_GRAPH))
premuti.add(KeyFlag.SPECIAL);
}
public void keyReleased(KeyEvent e) {
// if (e.isAltDown())
// premuti.remove("special");
if (e.getKeyCode() == KeyEvent.VK_1)
premuti.del(KeyFlag.k1);
if (e.getKeyCode() == KeyEvent.VK_2)
premuti.del(KeyFlag.k2);
if (e.getKeyCode() == KeyEvent.VK_3)
premuti.del(KeyFlag.k3);
if (e.getKeyCode() == KeyEvent.VK_UP
|| e.getKeyCode() == KeyEvent.VK_SPACE
|| e.getKeyCode() == KeyEvent.VK_W)
premuti.del(KeyFlag.JUMP);
if (e.getKeyCode() == KeyEvent.VK_RIGHT
|| e.getKeyCode() == KeyEvent.VK_D)
premuti.del(KeyFlag.RIGHT);
if (e.getKeyCode() == KeyEvent.VK_LEFT
|| e.getKeyCode() == KeyEvent.VK_A)
premuti.del(KeyFlag.LEFT);
if (e.getKeyCode() == KeyEvent.VK_DOWN
|| e.getKeyCode() == KeyEvent.VK_S)
premuti.del(KeyFlag.DOWN);
if (e.getKeyCode() == KeyEvent.VK_ALT
|| e.getKeyCode() == KeyEvent.VK_ALT_GRAPH)
premuti.del(KeyFlag.SPECIAL);
}
public void keyTyped(KeyEvent e) {
}
});
class autoupdateGUI extends Thread {
public void run() {
try {
long a, b, delay;
while (c.isConnected) {
a = Calendar.getInstance().getTimeInMillis();
try {
viewOutput();
} catch (IOException ex) {
Logger.getLogger(client.class.getName()).log(
Level.SEVERE, null, ex);
}
b = Calendar.getInstance().getTimeInMillis();
delay = b - a;
if (delay < framerate)
Thread.sleep(framerate - delay);
delay = (int) delay;
repaint();
}
JOptionPane.showMessageDialog(null, "ERROR: Connection timeout");
System.exit(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
// System.exit(0);
}
}
class autoSEND extends Thread {
public void run() {
try {
KeyFlag lastPremuti = new KeyFlag();
while (true) {
// Common.info("lastPremuti:"+lastPremuti+" premuti:"+premuti.toString());
// if(premuti.size()>0 || (premuti.size()==0 &&
// !lastPremuti.equals(premuti.toString())))
try {