All pastes #814277 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-12-13 03:53 UTC
rendered paste body
import java.net.*;
import javax.swing.*;
import java.io.*;
import java.util.zip.*;
import java.awt.event.*;
import java.util.*;
import java.awt.*;

public class Client extends Thread implements KeyListener, MouseListener{
 final static int serverPort = 27055; 
 final long walklag = 250; 
 long lastwalk = 0; 
 long lastshot = 0;
 long lastheartbeat;
 final long heartbeatdelay = 3000;
 public HashMap<Player,Integer> scoreBoard;
 final InetAddress server;
 public Player p;
 DatagramSocket s;
 final InetAddress group;
 public int num;
 public GameMap map;
 private Carto c;
 private Controls con; 
 private Scores scor; 
 private StatsPanel stat; 
 private ItemPanel ipanel; 
 private WepPanel wpanel;
 private JFrame F;
 public Client(InetAddress serv, Player playa) throws SocketException, UnknownHostException, IOException, ClassNotFoundException {
  map = null;
  server = serv;
  group = InetAddress.getByName("228.5.6.7");
  s =  new DatagramSocket(27099);
  //s.joinGroup(group);
  p = playa;
  Thread t = new Thread(this);
  c = new Carto(map); 
  c.addMouseListener(this);
  con = new Controls(); 
  stat = new StatsPanel(this);
  ipanel = new ItemPanel(p,c);
  wpanel = new WepPanel(p,c);
  scor = new Scores(scoreBoard);
  F = new JFrame();
  F.addKeyListener(this);
  F.setSize(900,700);
  wpanel.set(); stat.set(); wpanel.repaint(); stat.repaint();
  F.getContentPane().add(BorderLayout.NORTH, stat);
  F.getContentPane().add(BorderLayout.NORTH, wpanel);
  F.getContentPane().add(BorderLayout.NORTH, ipanel);
  F.getContentPane().add(BorderLayout.WEST, scor);
  F.getContentPane().add(BorderLayout.SOUTH, con);
  F.getContentPane().add(c);
  F.setVisible(true);
  requestNum();
  typeHandler(); 
  lastheartbeat = System.currentTimeMillis();
  t.start();
 }

 public void run() { 
  while(true) {
   if(p.isDead()) {
    try { sendPlayer(); } catch (IOException e) { e.printStackTrace(System.err); }
    catch (ClassNotFoundException e) { e.printStackTrace(System.err); };
    p.x=1337;
   }
   if(System.currentTimeMillis() > (lastheartbeat + heartbeatdelay)) { 
    try { sendHeartbeat(); } catch (IOException e) { e.printStackTrace(System.err); }
       catch (ClassNotFoundException e) { e.printStackTrace(System.err); }
    lastheartbeat = System.currentTimeMillis();
   }
   try { typeHandler(); } 
   catch (IOException e) { e.printStackTrace(System.err); }
   catch (ClassNotFoundException e) { e.printStackTrace(System.err); }
   //try { sendChat(s,new Plaintext(new Player("bar"),"foo")); } 
   //catch (IOException e) { System.out.println("here"); return; }
   //catch (ClassNotFoundException e) { return; }
  }
 }

 private void requestNum() throws IOException,ClassNotFoundException {
  ByteArrayOutputStream byteArr = new ByteArrayOutputStream();
  ObjectOutputStream out = new ObjectOutputStream(byteArr);
  out.writeObject("GETMAPNUM");
  DatagramPacket query = new DatagramPacket(byteArr.toByteArray(), byteArr.toByteArray().length, server, serverPort);
  s.send(query);
  //System.out.println("SKEET SKEET2");
 }

 private void sendHeartbeat() throws IOException,ClassNotFoundException {
  ByteArrayOutputStream byteArr = new ByteArrayOutputStream();
  ObjectOutputStream out = new ObjectOutputStream(byteArr);
  out.writeObject("HELO");
  DatagramPacket query = new DatagramPacket(byteArr.toByteArray(), byteArr.toByteArray().length, server, serverPort);
  s.send(query);
  //System.out.println("SKEET SKEET2");
 } 

 private void sendPlayer() throws IOException,ClassNotFoundException {
  ByteArrayOutputStream byteArr = new ByteArrayOutputStream();
  ObjectOutputStream out = new ObjectOutputStream(byteArr);
  out.writeObject(p);
  DatagramPacket query = new DatagramPacket(byteArr.toByteArray(), byteArr.toByteArray().length, server, serverPort);
  s.send(query); 
  //System.out.println("SKEET SKEET2");
 }

 private void typeHandler() throws IOException, SocketException, ClassNotFoundException {
  byte[] buffer = new byte[102400];
  DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
  s.receive(reply);
  ObjectInputStream inputBuffer = new ObjectInputStream(new GZIPInputStream(new ByteArrayInputStream(reply.getData())));
  Object object = inputBuffer.readObject();
  //System.out.println(object.getClass());
  if(object instanceof GameMap) {
   map = (GameMap) object;
   //System.out.println("map gotten");
   c.setMap(map);
   c.repaint();
  }
  else if(object instanceof String) {
   System.out.println(object);
  }
  else if(object instanceof HashMap) {
 scoreBoard = (HashMap<Player,Integer>)object; scor.set(); scor.repaint();
  }
  else if(object.getClass().isArray()) {
   int[] tmp = (int[]) object;
   p.setLocation(tmp[0],tmp[1]);
   System.out.println("sup"+tmp[0]+tmp[1]);
  }
  else if(object instanceof Integer) {
   //System.out.println("SKEET SKEET");
   //System.out.println(object);
   num = (Integer) object;
   p.setMapNum(num);
   //s.joinGroup(group);
  }
  else if( object instanceof Ammunition) {
   p.healthDrain((Ammunition) object); stat.set();
  }
 }

 public void sendChat(MulticastSocket s, String text) throws IOException, SocketException {
  ByteArrayOutputStream byteArr = new ByteArrayOutputStream();
  ObjectOutputStream o = new ObjectOutputStream(byteArr);
  o.writeObject(text);
  DatagramPacket packet = new DatagramPacket(byteArr.toByteArray(), byteArr.toByteArray().length, group, 31337);
  try { s.send(packet); } catch (IOException e) { return; }
 }

 public void keyPressed(KeyEvent e) 
 {
      String in = e.getKeyChar() + ""; 
      if ("wasd".indexOf(in)!=-1)
      {
           long now = System.currentTimeMillis();
           if (now-lastwalk>=walklag)
           {
                int x = p.getx(); int y  = p.gety();
                if (in.equals("w"))
                     x-=1;
                if (in.equals("a"))
                     y-=1;
                if (in.equals("s"))
                     x+=1;
                if (in.equals("d"))
                     y+=1;
                if (map.isValid(x,y)&&map.isEmpty(x,y))
                     try {lastwalk = now; System.out.println("sending...?");sendMove(x,y); } catch (IOException ioe) {System.out.println("NOT SENDING");};
           }
      }
 }
 
 public void mouseClicked(MouseEvent e) {
  if(e.BUTTON1==e.getButton()) { System.out.println("gotclick");
  long now = System.currentTimeMillis();
  if(now - lastshot > p.currentWep().cooldown()) {
   int dx = e.getX() - (p.gety() * 20 + 10);
   int dy = (p.getx() * 20 + 10) - e.getY();
   double theta = Math.atan(1.0*dy/dx); 
   if (theta < 0) {theta += Math.PI; if (dx > 0) theta += Math.PI;}
   else {if (dx < 0) theta += Math.PI;}
   Ammunition a = p.currentWep().ammo(); a.set(p.getx()*20+10,p.gety()*20+10,theta,p);
   try { 
    lastshot = now;
    requestAmmo(a);
    p.currentWep().use();
   }
   catch (IOException ioe) {};
  }
 }
 }
 
 public void sendMove(int x, int y) throws IOException, SocketException
 {
      ByteArrayOutputStream byteArr = new ByteArrayOutputStream();
      ObjectOutputStream o = new ObjectOutputStream(byteArr);
      o.writeObject(new int[]{x,y});
      DatagramPacket packet = new DatagramPacket(byteArr.toByteArray(), byteArr.toByteArray().length, server, serverPort);
      s.send(packet);
 }
 
 public void requestAmmo(Ammunition a) throws IOException, SocketException { System.out.println("shooting");
      ByteArrayOutputStream byteArr = new ByteArrayOutputStream();
      ObjectOutputStream o = new ObjectOutputStream(byteArr);
      o.writeObject(a);
      DatagramPacket packet = new DatagramPacket(byteArr.toByteArray(), byteArr.toByteArray().length, server, serverPort);
      s.send(packet);
 }      
 
 public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} 
 public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {}
 public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {}
 
 public static void main(String args[]) throws IOException, ClassNotFoundException, SocketException, FileNotFoundException, InterruptedException {
  if(args.length > 0) { Client me = new Client(InetAddress.getByName(args[0]), new Player(args[1])); }
  else {
   BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
   System.out.println("Enter your desired name: ");
   String name = in.readLine();
   System.out.println("Enter the server you want to connect to (localhost for your own computer): ");
   String addr = in.readLine();
   Client me = new Client(InetAddress.getByName(addr), new Player(name));
  }
 }