public void actionPerformed(ActionEvent e) {
    Object o = e.getSource();

    for (JRadioButton u : ub) {
      if (o == u) {
        mapTM.setUnitType(u.getText());
        System.out.println("Set unit type - " + u.getText());
        return;
      }
    }

    if (o == timer) {

      /*
      mmLabel.setText("Main Menu ("+(cdTime--)+")");

      if(cdTime == 0){
      	timer.stop();
      	gsButton.setText("Game Start");
      	mmLabel.setText("Main Menu");

      	ArrayList<Building> bArr = new ArrayList<Building>();

      	String temp = "Elixir Collector-24,8-960|Elixir Collector-31,8-960|Gold Mine-17,10-960|Elixir Collector-25,21-960|Elixir Collector-11,22-960";
      	String[] bs = temp.split("\\|");
      	for(String b : bs){
      		String[] bParts = b.split("-");
      		String[] cParts = bParts[1].split(",");
      		int x = Integer.parseInt(cParts[0].trim());
      		int y = Integer.parseInt(cParts[1].trim());

      		Building tb = new Building(bParts[0], new Coordinate(x,y));
      		tb.setHp(Integer.parseInt(bParts[2].trim()));

      		bArr.add(tb);
      	}



      	mapTM.setBuildings(bArr);
      	switchCards("TM");
      }
      */

      return;
    }

    if (o == gsButton) {

      if (timer.isRunning()) {
        // timer.stop();
        gsButton.setText("Game Start");
        mmLabel.setText("Main Menu");
        // sends the leave command
        client.sendUDP("leave~" + unameUDP);
        return;
      }
      // sends the joinlobby command
      client.sendUDP("joinlobby~" + unameUDP);
      // gsButton.setText("Game Stop");
      String serverResp = client.receiveUDP();

      if (serverResp.trim().equals("false")) {

        // place false handler here

      } else {

        String[] enemies = serverResp.trim().split(",");
        ArrayList<Building> bArr = new ArrayList<Building>();
        String mapConfig = getBaseConfig(enemies[0]);
        String[] bs = mapConfig.split("\\|");
        for (String b : bs) {

          String[] bParts = b.split("-");
          String[] cParts = bParts[1].split(",");
          int x = Integer.parseInt(cParts[0].trim());
          int y = Integer.parseInt(cParts[1].trim());

          Building tb = new Building(bParts[0], new Coordinate(x, y));
          tb.setHp(Integer.parseInt(bParts[2].trim()));
          bArr.add(tb);
        }

        mapTM.setBuildings(bArr);
        switchCards("TM");
      }

      // System.out.println(serverResp);
      // cdTime = 10;
      // timer.start();
      return;
    }

    if (o == logout) {
      client.sendMessage(new ChatMessage(ChatMessage.LOGOUT, ""));
      chatArea.setText("");

      switchCards("Login");
      return;
    }

    if (o == cmButton) {
      String baseConfig = getBaseConfig();
      System.out.println("base config: " + baseConfig);

      for (int i = 0; i < mapSize; i++) {
        for (int j = 0; j < mapSize; j++) {
          tiles[i][j].setValue("");
        }
      }

      if (!baseConfig.equals("")) {

        String[] bs = baseConfig.split("\\|");
        for (String b : bs) {
          String[] bParts = b.split("-");
          String[] cParts = bParts[1].split(",");
          int x = Integer.parseInt(cParts[0].trim());
          int y = Integer.parseInt(cParts[1].trim());

          int index = 0;
          for (int i = 0; i < bb.size(); i++) {
            if (bb.get(i).getText().split("-")[0].trim().equals(bParts[0])) {
              index = i;
              break;
            }
          }
          insertBuilding(y, x, index);
        }
      }

      switchCards("CM");

      return;
    }

    if (o == tmButton) {

      ArrayList<Building> bArr = new ArrayList<Building>();

      for (int i = 0; i < 40; i++) {
        for (int j = 0; j < 40; j++) {
          if (tiles[i][j].getValue().equals("") || tiles[i][j].getValue().contains("-")) {
            continue;
          }
          // weird part here
          bArr.add(new Building(tiles[i][j].getValue(), new Coordinate(j, i)));
        }
      }

      mapTM.setBuildings(bArr);

      switchCards("TM");
      return;
    }

    // if it the who is in button
    if (o == whoIsIn) {
      client.sendMessage(new ChatMessage(ChatMessage.WHOISIN, ""));
      return;
    }

    if (o == cmBack) {
      ArrayList<Building> bArr = new ArrayList<Building>();
      for (int i = 0; i < 40; i++) {
        for (int j = 0; j < 40; j++) {
          if (tiles[i][j].getValue().equals("") || tiles[i][j].getValue().contains("-")) {
            continue;
          }
          // weird part here
          bArr.add(new Building(tiles[i][j].getValue(), new Coordinate(j, i)));
        }
      }

      String temp = "mapdata~" + unameUDP + "~";
      int tileCount = 40;
      int dim = 600;
      int tileDim = (int) (dim / tileCount);
      int counter = 0;
      for (Building b : bArr) {
        int x, y, hp;
        x = b.getPos().getX() / tileDim;
        y = b.getPos().getY() / tileDim;
        hp = b.getHp();
        temp += b.getName() + "-" + x + "," + y + "-" + hp + "|";
        counter += 1;
      }
      if (counter > 0) {
        temp = temp.substring(0, temp.length() - 1); // removes the last '|'
      } else {
        temp += "none";
      }

      client.sendUDP(temp); // allows saving of the current state of the map into the user's account

      switchCards("Menu");

      return;
    }
    if (o == tmBack) {
      switchCards("Menu");

      return;
    }

    for (int i = 0; i < 40; i++) {
      for (int j = 0; j < 40; j++) {
        if (o == tiles[i][j]) {
          for (int k = 0; k < bb.size(); k++) {
            if (bb.get(k).isSelected()) {
              if (bb.get(k).getText().equals("Remove Building")) {
                removeBuilding(i, j);
                return;
              }

              insertBuilding(i, j, k);
              // JOptionPane.showMessageDialog(null, bb.get(k).getText());
              return;
            }
          }

          // JOptionPane.showMessageDialog(null, "i-"+i+" j-"+j);
          return;
        }
      }
    }

    // ok it is coming from the JTextField
    if (connected) {
      // just have to send the message
      client.sendMessage(new ChatMessage(ChatMessage.MESSAGE, chatField.getText()));
      chatField.setText("");
      return;
    }

    if (o == login) {
      // ok it is a connection request
      String username = usernameField.getText().trim();
      String password = passwordField.getText().trim();
      // empty username ignore it
      if (username.length() == 0) return;
      // empty serverAddress ignore it
      String server = tfServer.getText().trim();
      if (server.length() == 0) return;
      // empty or invalid port numer, ignore it
      String portNumber = tfPort.getText().trim();
      if (portNumber.length() == 0) return;
      int port = 0;
      try {
        port = Integer.parseInt(portNumber);
      } catch (Exception en) {
        return; // nothing I can do if port number is not valid
      }

      // try creating a new Client with GUI
      client = new Client(server, port, username, password, this);
      // test if we can start the Client
      if (!client.start()) return;

      unameUDP = username;

      switchCards("Menu");
      // fetching of the base_config string from the database

      chatField.setText("");
      chatArea.setText("");
    }
  }
Exemplo n.º 2
0
  public JavaGame(String[] args) {
    this.args = args;
    updater = new Updater(this);
    highscore = new Highscore(this);
    eventHandler = new EventHandler(this);
    eventHandler.registerTestEvents();

    setTitle("Survive-JavaGame"); // Fenstertitel setzen
    setSize(1200, 900); // Fenstergröße einstellen
    addWindowListener(new WindowListener());
    setLocationRelativeTo(null);

    try {
      arg = args[0];
    } catch (ArrayIndexOutOfBoundsException e) {
      arg = "nothing";
    }
    if (arg.equals("fullscreen")) {
      setUndecorated(true); // "Vollbild"
      setSize(
          (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth() - 200,
          (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth());
      setLocation(0, 0);
    }
    setVisible(true);
    setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));

    try {
      /*URI Path = URLDecoder.decode(getClass().getClassLoader().getResource("texture").toURI();//, "UTF-8"); //Pfad zu den Resourcen
      File F = new File(Path);
      basePath = F;
      System.out.println(basePath);
      */
      File File = new File((System.getenv("APPDATA")));
      basePath = new File(File, "/texture");
      backgroundTexture = new File(basePath, "/hintergrund.jpg");
    } catch (Exception ex) {
      ex.printStackTrace();
    }

    try {
      backgroundImage = ImageIO.read(backgroundTexture);
    } catch (IOException exeption) {

    }

    if (soundan) {
      currentVolume = 80;
    } // end of if

    dbImage = createImage(1920, 1080);
    // dbGraphics = dbImage.getGraphics();

    // Texturen Liste

    // Ebenen Liste

    ebenen[0][0] = 91;
    ebenen[0][1] = 991; // Main Ebene: Kann nicht durchschrittenwerden indem down gedrückt wird
    ebenen[0][2] = 563;

    ebenen[1][0] = 387; // x1
    ebenen[1][1] = 524; // x2
    ebenen[1][2] = 454; // y

    ebenen[2][0] = 525;
    ebenen[2][1] = 645;
    ebenen[2][2] = 350;

    ebenen[3][0] = 246;
    ebenen[3][1] = 365;
    ebenen[3][2] = 351;

    ebenen[4][0] = 760;
    ebenen[4][1] = 870;
    ebenen[4][2] = 294;

    ebenen[5][0] = 835;
    ebenen[5][1] = 969;
    ebenen[5][2] = 441;

    // Spieler

    // I'm in Space! SPACE!
    player[1] =
        new Player(
            (int) (Math.random() * (ebenen[0][1] - ebenen[0][0]) + ebenen[0][0]),
            0,
            false,
            67,
            100,
            texture[0],
            shottexture[0],
            KeyEvent.VK_A,
            KeyEvent.VK_D,
            KeyEvent.VK_W,
            KeyEvent.VK_S,
            KeyEvent.VK_Q,
            1,
            35,
            highscore.getName(1));
    player[2] =
        new Bot(
            (int) (Math.random() * (ebenen[0][1] - ebenen[0][0]) + ebenen[0][0]),
            0,
            false,
            67,
            100,
            texture[1],
            shottexture[1],
            KeyEvent.VK_J,
            KeyEvent.VK_L,
            KeyEvent.VK_I,
            KeyEvent.VK_K,
            KeyEvent.VK_U,
            2,
            35,
            highscore.getName(1));
    player[3] =
        new Bot(
            (int) (Math.random() * (ebenen[0][1] - ebenen[0][0]) + ebenen[0][0]),
            0,
            false,
            67,
            100,
            texture[2],
            shottexture[2],
            KeyEvent.VK_LEFT,
            KeyEvent.VK_RIGHT,
            KeyEvent.VK_UP,
            KeyEvent.VK_DOWN,
            KeyEvent.VK_ENTER,
            3,
            35,
            highscore.getName(1));

    player[1].laden(this);
    player[2].laden(this);
    player[3].laden(this);

    this.addKeyListener(player[1]);
    this.addKeyListener(player[2]);
    this.addKeyListener(player[3]);

    this.addKeyListener(this);

    int result;
    Object[] options = {"SinglePlayer", "MultiPlayer"};
    if (arg.equals("dedicated")) {
      Server server = new Server();
      this.server = true;
      setVisible(false);

    } else {
      if ((result =
              JOptionPane.showOptionDialog(
                  null,
                  "Treffen Sie eine Auswahl",
                  "Alternativen",
                  JOptionPane.DEFAULT_OPTION,
                  JOptionPane.INFORMATION_MESSAGE,
                  null,
                  options,
                  options[0]))
          == 1) {
        client = new Client(this);
        online = true;
        while ((onlinename =
                    JOptionPane.showInputDialog(
                        null,
                        "Geben Sie Ihren Namen ein",
                        "Eine Eingabeaufforderung",
                        JOptionPane.PLAIN_MESSAGE))
                .isEmpty()
            && onlinename != null) {}

        Object[] optionsmp = {"Host", "Client"};
        if ((result =
                JOptionPane.showOptionDialog(
                    null,
                    "Treffen Sie eine Auswahl",
                    "Alternativen",
                    JOptionPane.DEFAULT_OPTION,
                    JOptionPane.INFORMATION_MESSAGE,
                    null,
                    optionsmp,
                    optionsmp[0]))
            == 0) {
          Server server = new Server();
          this.server = true;
        } else if (online) {
          while ((serveradresse =
                      JOptionPane.showInputDialog(
                          null,
                          "Geben Sie die Serveradresse ein",
                          "Eine Eingabeaufforderung",
                          JOptionPane.PLAIN_MESSAGE))
                  .isEmpty()
              && serveradresse != null) {}
        }
      }
    }
    if (!arg.equals("dedicated")) {
      gamerunner = new GameRunner(player, this);
      DamageLogig = new damageLogig(gamerunner);
    }

    if (online) {
      try {
        client.initialise(serveradresse, 9876);
        client.start();
      } catch (SocketException e) {
        e.printStackTrace();
      } catch (UnknownHostException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  } // end of init