Advertisement
Guest User

client main

a guest
May 27th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. package ch.epfl.xblast.client;
  2.  
  3. import java.util.List;
  4. import java.awt.event.KeyEvent;
  5. import java.io.IOException;
  6. import java.lang.reflect.InvocationTargetException;
  7. import java.net.InetSocketAddress;
  8. import java.net.SocketAddress;
  9. import java.net.StandardProtocolFamily;
  10. import java.nio.ByteBuffer;
  11. import java.nio.channels.DatagramChannel;
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. import java.util.Objects;
  16. import java.util.function.Consumer;
  17.  
  18. import javax.swing.JFrame;
  19. import javax.swing.SwingUtilities;
  20.  
  21. import ch.epfl.xblast.PlayerID;
  22. import ch.epfl.xblast.Time;
  23. import ch.epfl.xblast.client.GameStateDeserializer;
  24. import ch.epfl.xblast.server.Level;
  25. import ch.epfl.xblast.PlayerAction;
  26. import ch.epfl.xblast.server.graphics.BoardPainter;
  27.  
  28. /**
  29. *
  30. * @author Amine Chaouachi (260709) / Alban Favre (260025)
  31. *
  32. */
  33. public class Main {
  34.  
  35. private static DatagramChannel CHANNEL;
  36. private static SocketAddress ADDRESS;
  37. private static int MAXIMUM_SERIALISED_BYTE = 420;
  38.  
  39. private static BoardPainter bp = Level.DEFAULT_LEVEL.boardPainter();
  40. private static PlayerAction thisPlayerAction = null;
  41.  
  42. /**
  43. * Used to display
  44. *
  45. * @param xbc
  46. * xblastcomponent
  47. */
  48. public static void createUI(XBlastComponent xbc) {
  49.  
  50. Map<Integer, PlayerAction> keyboardKeys = new HashMap<>();
  51. keyboardKeys.put(KeyEvent.VK_UP, PlayerAction.MOVE_N);
  52. keyboardKeys.put(KeyEvent.VK_DOWN, PlayerAction.MOVE_S);
  53. keyboardKeys.put(KeyEvent.VK_LEFT, PlayerAction.MOVE_W);
  54. keyboardKeys.put(KeyEvent.VK_RIGHT, PlayerAction.MOVE_E);
  55. keyboardKeys.put(KeyEvent.VK_SPACE, PlayerAction.DROP_BOMB);
  56. keyboardKeys.put(KeyEvent.VK_SHIFT, PlayerAction.STOP);
  57.  
  58. Consumer<PlayerAction> thisPlayerActionCons = x -> {
  59.  
  60. thisPlayerAction = x;
  61. System.out.println(thisPlayerAction);
  62. if(!Objects.isNull(thisPlayerAction)){
  63. ByteBuffer b = ByteBuffer.allocate(1);
  64. b.put((byte)thisPlayerAction.ordinal());
  65. b.flip();
  66.  
  67. try {
  68. CHANNEL.send(b, ADDRESS);
  69. System.out.println("action sent "+thisPlayerAction.ordinal());
  70. }
  71.  
  72. catch (IOException e) {
  73. e.printStackTrace();
  74. }
  75.  
  76. thisPlayerAction=null;
  77. }
  78.  
  79.  
  80.  
  81.  
  82. };
  83. xbc.addKeyListener(
  84. new KeyboardEventHandler(keyboardKeys, thisPlayerActionCons));
  85.  
  86. xbc.setSize(xbc.getPreferredSize());
  87. xbc.setFocusable(true);
  88. xbc.requestFocusInWindow();
  89. JFrame jfr = new JFrame();
  90. jfr.setTitle("Xblast");
  91. jfr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  92. jfr.setResizable(true);
  93. jfr.add(xbc);
  94. jfr.pack();
  95. jfr.setVisible(true);
  96.  
  97.  
  98. }
  99.  
  100. /**
  101. * main method, useful for the program's correct functioning
  102. *
  103. * @param args
  104. * the server IP
  105. * @throws InterruptedException
  106. * an exception
  107. * @throws IOException
  108. * an exception
  109. * @throws InvocationTargetException
  110. */
  111. public static void main(String args[]) throws InterruptedException,
  112. IOException, InvocationTargetException {
  113.  
  114. XBlastComponent xbc = new XBlastComponent();
  115. SwingUtilities.invokeAndWait(() -> createUI(xbc));
  116. GameState gs = null;
  117.  
  118. CHANNEL = DatagramChannel.open(StandardProtocolFamily.INET);
  119. ADDRESS = new InetSocketAddress(
  120. args.length == 0 ? "localhost" : args[0], 2016);
  121. ByteBuffer sendBuffer = ByteBuffer.allocate(1);
  122. ByteBuffer receiveBuffer = ByteBuffer.allocate(MAXIMUM_SERIALISED_BYTE);
  123.  
  124.  
  125. CHANNEL.configureBlocking(false);
  126.  
  127. while(Objects.isNull(CHANNEL.receive(receiveBuffer))){
  128. System.out.println("isNull");
  129. sendBuffer.clear();
  130. sendBuffer.put((byte) PlayerAction.JOIN_GAME.ordinal());
  131. sendBuffer.flip();
  132. CHANNEL.send(sendBuffer, ADDRESS);
  133. Thread.sleep(Time.CLIENT_WAIT_TIME);
  134. }
  135.  
  136.  
  137. receiveBuffer.flip();
  138.  
  139. List<Byte> gsList = new ArrayList<>();
  140.  
  141. while (receiveBuffer.hasRemaining()){
  142.  
  143. gsList.add(receiveBuffer.get());
  144. }
  145.  
  146.  
  147. PlayerID myid = PlayerID.values()[gsList.get(0)];// here -1 to switch back to 0,1,2,3
  148.  
  149.  
  150. gs = GameStateDeserializer.deserializeGameState(gsList.subList(1, gsList.size()));
  151. xbc.setGameState(gs, myid);
  152. gsList.clear();
  153.  
  154. CHANNEL.configureBlocking(true);
  155. while (true) {
  156.  
  157. receiveBuffer.clear();
  158.  
  159. CHANNEL.receive(receiveBuffer);
  160.  
  161. receiveBuffer.flip();
  162.  
  163. while (receiveBuffer.hasRemaining())
  164. gsList.add(receiveBuffer.get());
  165.  
  166. System.out.println(gsList);
  167. gs = GameStateDeserializer.deserializeGameState(gsList.subList(1, gsList.size()));
  168. xbc.setGameState(gs, myid);
  169. gsList.clear();
  170.  
  171. }
  172.  
  173. }
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement