Beispiel #1
0
 private static float getBuildingSelectionHeight(Building building) {
   int render_level = building.getRenderLevel();
   switch (render_level) {
     case Building.RENDER_START:
       return building.getBuildingTemplate().getStartSelectionHeight();
     case Building.RENDER_HALFBUILT:
       return building.getBuildingTemplate().getHalfbuiltSelectionHeight();
     case Building.RENDER_BUILT:
       return building.getBuildingTemplate().getBuiltSelectionHeight();
     default:
       throw new RuntimeException();
   }
 }
Beispiel #2
0
 public final void visitBuilding(final Building building) {
   visitSelectable(
       building_visitor,
       building,
       building.getPositionZ(),
       getBuildingSelectionRadius(building),
       getBuildingSelectionHeight(building));
 }
  public void removeBuilding(int i, int j) {

    if (tiles[i][j].getValue().equals("")) {
      JOptionPane.showMessageDialog(null, "There is no building here");
      return;
    }

    if (tiles[i][j].value.contains("-")) {
      String tText = tiles[i][j].value;
      i = Integer.parseInt(tText.split("-")[0]);
      j = Integer.parseInt(tText.split("-")[1]);
    }

    int index;
    for (index = 0; index < bb.size(); index++) {
      if (bb.get(index).getText().split("-")[0].equals(tiles[i][j].value)) {
        break;
      }
    }

    String temp = bb.get(index).getText();
    String[] parts = temp.split("-");
    int newQ = Integer.parseInt(parts[1]) + 1;

    Building tB = bList.get(0);
    for (int b = 0; b < bList.size(); b++) {
      if (bList.get(b).getName().equals(tiles[i][j].value)) {
        tB = bList.get(b);
        break;
      }
    }

    for (int c = 0; c < tB.getQWidth(); c++) {
      for (int r = 0; r < tB.getQHeight(); r++) {

        if ((i + c + j + r) % 2 == 0) tiles[i + c][j + r].setBackground(new Color(102, 255, 51));
        else tiles[i + c][j + r].setBackground(new Color(51, 204, 51));

        tiles[i + c][j + r].setIcon(null);

        tiles[i + c][j + r].value = "";
        bb.get(index).setText(parts[0] + "-" + newQ);
      }
    }
  }
 /*
   Place all motionless objects (fire stations, police offices, ambulance teams and refuges)
   @param fire An array to be filled with fire stations
   @param police An array to be filled with police offices
   @param ambulance An array to be filled with ambulance centres
   @param refuge An array to be filled with refuges
   @param allBuildings All buildings in the map
   @return All ordinary buildings
 */
 public static Building[] placeMotionlessObjects(
     FireStation[] fire,
     PoliceOffice[] police,
     AmbulanceCenter[] ambulance,
     Refuge[] refuge,
     Building[] allBuildings) {
   List remaining = new ArrayList();
   for (int i = 0; i < allBuildings.length; ++i) remaining.add(allBuildings[i]);
   Collections.shuffle(remaining);
   System.out.println("Placing " + ambulance.length + " ambulance centers");
   Iterator it = remaining.iterator();
   for (int i = 0; i < ambulance.length; ++i) {
     Building location = (Building) it.next();
     it.remove();
     AmbulanceCenter a = new AmbulanceCenter(location);
     a.setID(location.getID());
     ambulance[i] = a;
     //			writeFixedObjectData(out,TYPE_AMBULANCE_CENTER,i,location);
   }
   System.out.println("Placing " + fire.length + " fire stations");
   for (int i = 0; i < fire.length; ++i) {
     Building location = (Building) it.next();
     it.remove();
     FireStation a = new FireStation(location);
     a.setID(location.getID());
     fire[i] = a;
     //			writeFixedObjectData(out,TYPE_FIRE_STATION,i,location);
     //			System.out.print(".");
   }
   System.out.println("Placing " + police.length + " police stations");
   for (int i = 0; i < police.length; ++i) {
     Building location = (Building) it.next();
     it.remove();
     PoliceOffice a = new PoliceOffice(location);
     a.setID(location.getID());
     police[i] = a;
     //			writeFixedObjectData(out,TYPE_POLICE_OFFICE,i,location);
     //			System.out.print(".");
   }
   System.out.println("Placing " + refuge.length + " refuges");
   for (int i = 0; i < refuge.length; ++i) {
     Building location = (Building) it.next();
     it.remove();
     Refuge a = new Refuge(location);
     a.setID(location.getID());
     refuge[i] = a;
     //			writeFixedObjectData(out,TYPE_REFUGE,i,location);
     //			System.out.print(".");
   }
   //		System.out.println();
   return (Building[]) remaining.toArray(new Building[0]);
 }
 public static Building[] placeBigFires(int num, Building[] b, Limits radius) {
   List remaining = new ArrayList();
   for (int i = 0; i < b.length; ++i) remaining.add(b[i]);
   Collections.shuffle(remaining);
   Collection fires = new HashSet();
   System.out.print("Placing " + num + " big fires");
   Iterator it = remaining.iterator();
   for (int i = 0; i < num; ++i) {
     Building center = (Building) it.next();
     fires.add(center);
     long r = radius.getNumber();
     long distanceSquared = r * r;
     // Check for close buildings
     for (int j = 0; j < b.length; ++j) {
       long dx = center.getX() - b[j].getX();
       long dy = center.getY() - b[j].getY();
       long distance = (dx * dx) + (dy * dy);
       if (distance <= distanceSquared) fires.add(b[j]);
     }
   }
   return (Building[]) fires.toArray(new Building[0]);
 }
  public void insertBuilding(int i, int j, int index) {

    if (!tiles[i][j].getValue().equals("")) {
      JOptionPane.showMessageDialog(null, "There is already a building here");
      return;
    }

    String temp = bb.get(index).getText();
    String[] parts = temp.split("-");

    int newQ = Integer.parseInt(parts[1]) - 1;
    if (newQ < 0) return;

    Building tB = bList.get(0);
    for (int b = 0; b < bList.size(); b++) {
      if (bList.get(b).getName().equals(parts[0])) {
        tB = bList.get(b);
        // System.out.println("here");
        break;
      }
    }

    Image image = null;
    BufferedImage buffered;
    try {
      image = ImageIO.read(new File("images/" + tB.getName().replace(" ", "_") + ".png"));
      System.out.println("Loaded image");
    } catch (IOException e) {
      System.out.println("Failed to load image");
    }
    buffered = (BufferedImage) image;

    for (int c = 0; c < tB.getQWidth(); c++) {
      for (int r = 0; r < tB.getQHeight(); r++) {
        if (i + c == 40 || j + r == 40) {
          JOptionPane.showMessageDialog(null, "Placing a building here would be out of bounds");
          return;
        }

        if (!tiles[i + c][j + r].getValue().equals("")) {
          JOptionPane.showMessageDialog(null, "Placing a building here will result to a overlap");
          return;
        }
      }
    }

    double w = buffered.getWidth() / tB.getQWidth();
    double h = buffered.getHeight() / tB.getQHeight();

    for (int c = 0; c < tB.getQWidth(); c++) {
      for (int r = 0; r < tB.getQHeight(); r++) {
        tiles[i + c][j + r].setBackground(new Color(51, 204, 51));
        tiles[i + c][j + r].setIcon(
            new ImageIcon(
                resize(
                    buffered.getSubimage((int) w * r, (int) h * c, (int) w, (int) h),
                    tiles[i + c][j + r].getWidth(),
                    tiles[i + c][j + r].getHeight())));

        String tValue = (c == 0 && r == 0) ? tB.getName() : i + "-" + j;
        // System.out.println(tValue);
        tiles[i + c][j + r].setValue(tValue);
      }
    }

    // tiles[i][j].setBackground(Color.BLUE);
    bb.get(index).setText(parts[0] + "-" + newQ);
  }
  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("");
    }
  }