Beispiel #1
0
  /**
   * Add a new server, if given parameters are corrects.
   *
   * @param p_name
   * @param p_ip
   * @param p_port
   */
  private void addServer(String p_name, String p_ip, String p_port) {
    // 1: Controls that everything is correct
    int error = 2;
    try {
      if (p_name.equals(UIText.getMenuText("m3.defaultName"))) {
        throw new Exception();
      }
      error = 0;
      int intPort = Integer.valueOf(p_port);
      error = 1;
      new TransferObject(p_ip, intPort);

      // Everything's ok if we got here
      saveServerInfos(new ServerInfo(p_name, p_ip, intPort));

      // And go back to the menu
      client.handleMenu(new JoinGameMenu(loadServersInfos(), previousMenu.previousMenu));

    } catch (Exception e) {
      String message;
      switch (error) {
        case 0:
          message = "m3.error.port";
          break;
        case 2:
          message = "m3.error.name";
          break;
        default:
        case 1:
          message = "m3.error.unreachable";
          break;
      }

      message = UIText.getMenuText(message);
      client.handleMenu(new InfoMenu("Impossible. " + message, currentMenu));
    }
  }
  /**
   * @param p_action
   * @return boolean
   */
  public boolean render(ActionElement p_action) {
    boolean achieved = false;
    if (p_action.waiting) {
      waitForEndAction(p_action);
      achieved = p_action.done;
    } else {
      Perso perso = EngineZildo.persoManagement.getNamedPerso(p_action.who);
      if (perso != null) {
        scriptExec.involved.add(perso); // Note that this perso is concerned
      }
      Point location = p_action.location;
      if (p_action.delta && location != null) {
        Point currentPos = null;
        if (perso != null) {
          // Given position is a delta with current one (work ONLY with perso, not with camera)
          currentPos = new Point(perso.x, perso.y);
        } else if ("camera".equals(p_action.what)) {
          currentPos = ClientEngineZildo.mapDisplay.getCamera();
        } else {
          Element elem = EngineZildo.spriteManagement.getNamedElement(p_action.what);
          currentPos = new Point(elem.x, elem.y);
        }
        if (currentPos == null) {
          throw new RuntimeException("We need valid 'who' or 'what' attribute");
        }
        location = location.translate(currentPos.x, currentPos.y);
      }
      String text = p_action.text;
      switch (p_action.kind) {
        case pos:
          if (perso != null) {
            perso.x = location.x;
            perso.y = location.y;
          } else if ("camera".equals(p_action.what)) {
            ClientEngineZildo.mapDisplay.setCamera(location);
            ClientEngineZildo.mapDisplay.setFocusedEntity(null);
          } else {
            Element elem = EngineZildo.spriteManagement.getNamedElement(p_action.what);
            elem.x = location.x;
            elem.y = location.y;
            // TODO:the 'pushable' attribute shouldn't be set by this default way
            elem.setPushable(false);
          }
          achieved = true;
          break;
        case moveTo:
          if (perso != null) {
            perso.setGhost(true);
            perso.setTarget(location);
            perso.setForward(p_action.backward);
            perso.setSpeed(p_action.speed);
            perso.setOpen(p_action.open);
            perso.setUnstoppable(p_action.unstoppable);
          } else if ("camera".equals(p_action.what)) {
            ClientEngineZildo.mapDisplay.setTargetCamera(location);
            ClientEngineZildo.mapDisplay.setFocusedEntity(null);
          }
          break;
        case speak:
          String sentence = UIText.getGameText(text);
          EngineZildo.dialogManagement.launchDialog(
              SinglePlayer.getClientState(), null, new ScriptAction(sentence));
          scriptExec.userEndedAction = false;
          break;
        case script:
          MouvementPerso script = MouvementPerso.fromInt(p_action.val);
          perso.setQuel_deplacement(script, true);
          String param = p_action.fx;
          if (param != null) {
            switch (script) {
              case ZONE:
                perso.setZone_deplacement(
                    EngineZildo.mapManagement.range(
                        perso.getX() - 16 * 5,
                        perso.getY() - 16 * 5,
                        perso.getX() + 16 * 5,
                        perso.getY() + 16 * 5));
                break;
              case OBSERVE:
                Perso persoToObserve = EngineZildo.persoManagement.getNamedPerso(param);
                perso.setFollowing(persoToObserve);
                break;
            }
          }
          achieved = true;
          break;
        case angle:
          if (perso.getTarget() != null) {
            return false;
          }
          perso.setAngle(Angle.fromInt(p_action.val));
          achieved = true;
          break;
        case wait:
          count = p_action.val;
          break;
        case fadeIn:
          EngineZildo.askEvent(
              new ClientEvent(ClientEventNature.FADE_IN, FilterEffect.fromInt(p_action.val)));
          break;
        case fadeOut:
          EngineZildo.askEvent(
              new ClientEvent(ClientEventNature.FADE_OUT, FilterEffect.fromInt(p_action.val)));
          break;
        case clear:
          EngineZildo.askEvent(new ClientEvent(ClientEventNature.CLEAR));
          achieved = true;
          break;
        case map: // Change current map
          EngineZildo.mapManagement.loadMap(p_action.text, false);
          ClientEngineZildo.mapDisplay.setCurrentMap(EngineZildo.mapManagement.getCurrentMap());
          achieved = true;
          break;
        case focus: // Camera focus on given character
          Element toFocus = perso;
          if (p_action.what != null) {
            toFocus = EngineZildo.spriteManagement.getNamedElement(p_action.what);
          }
          if (toFocus == null) {
            ClientEngineZildo.mapDisplay.setFocusedEntity(null);
          }
          if (p_action.delta) {
            Point cameraLoc =
                new Point(toFocus.x - MapDisplay.CENTER_X, toFocus.y - MapDisplay.CENTER_Y);
            ClientEngineZildo.mapDisplay.setTargetCamera(cameraLoc);
          }
          ClientEngineZildo.mapDisplay.setFocusedEntity(toFocus);
          // If delta, we go smoothly to the target, except if it's explicitly asked to be
          // unblocking
          achieved = !p_action.delta || p_action.unblock;
          break;
        case spawn: // Spawn a new character
          if (p_action.who != null) {
            PersoDescription desc = PersoDescription.valueOf(p_action.text);
            Perso newOne =
                EngineZildo.persoManagement.createPerso(
                    desc, location.x, location.y, 0, p_action.who, p_action.val);
            newOne.setSpeed(p_action.speed);
            newOne.setEffect(p_action.fx);
            newOne.initPersoFX();
            EngineZildo.spriteManagement.spawnPerso(newOne);
          } else { // Spawn a new element
            if (EngineZildo.spriteManagement.getNamedElement(p_action.what) == null) {
              // Spawn only if doesn't exist yet
              SpriteDescription desc = SpriteDescription.Locator.findNamedSpr(p_action.text);
              Reverse rev = Reverse.fromInt(p_action.reverse);
              Rotation rot = Rotation.fromInt(p_action.rotation);
              Element elem =
                  EngineZildo.spriteManagement.spawnElement(
                      desc, location.x, location.y, 0, rev, rot);
              elem.setName(p_action.what);
            }
          }
          achieved = true;
          break;
        case impact:
          ImpactKind impactKind = ImpactKind.valueOf(p_action.text);
          EngineZildo.spriteManagement.spawnSprite(
              new ElementImpact(location.x, location.y, impactKind, null));
          achieved = true;
          break;
        case animation:
          SpriteAnimation anim = SpriteAnimation.valueOf(p_action.text);
          Element animElem =
              EngineZildo.spriteManagement.spawnSpriteGeneric(
                  anim, location.x, location.y, 0, null, null);
          if (p_action.what != null) {
            animElem.setName(p_action.what);
          }
          achieved = true;
          break;
        case take: // Someone takes an item
          if (p_action.who == null || "zildo".equalsIgnoreCase(p_action.who)) {
            // This is Zildo
            PersoZildo zildo = EngineZildo.persoManagement.getZildo();
            if (p_action.val != 0) {
              zildo.pickGoodies(null, p_action.val);
            } else {
              zildo.pickItem(ItemKind.fromString(text), null);
            }
          } else if (perso != null) {
            // This is somebody else
            Element elem = EngineZildo.spriteManagement.getNamedElement(p_action.what);
            perso.addPersoSprites(elem);
          }
          achieved = true;
          break;
        case putDown: // Zildo loses an item
          PersoZildo zildo = EngineZildo.persoManagement.getZildo();
          zildo.removeItem(ItemKind.fromString(text));
          achieved = true;
          break;
        case mapReplace:
          EngineZildo.scriptManagement.addReplacedMapName(p_action.what, text);
          achieved = true;
          break;
        case exec:
          // Note : we can sequence scripts in an action tag.
          EngineZildo.scriptManagement.execute(text);
          break;
        case music:
          if (text == null) { // Stop music ?
            EngineZildo.soundManagement.broadcastSound((BankMusic) null, (Point) null);
          } else {
            BankMusic musicSnd = BankMusic.valueOf(text);
            EngineZildo.soundManagement.broadcastSound(musicSnd, (Point) null);
          }
          EngineZildo.soundManagement.setForceMusic(true);
          achieved = true;
          break;
        case sound:
          BankSound snd = BankSound.valueOf(text);
          EngineZildo.soundManagement.playSound(snd, null);
          achieved = true;
          break;
        case remove:
          Element toRemove;
          if (p_action.what != null) {
            toRemove = EngineZildo.spriteManagement.getNamedElement(p_action.what);
          } else {
            toRemove = perso;
            EngineZildo.persoManagement.removePerso((Perso) toRemove);
          }
          EngineZildo.spriteManagement.deleteSprite(toRemove);
          achieved = true;
          break;
        case markQuest:
          if (p_action.val == 1) {
            EngineZildo.scriptManagement.accomplishQuest(p_action.text, true);
          } else {
            EngineZildo.scriptManagement.resetQuest(p_action.text);
          }
          achieved = true;
          break;
        case attack:
          if (p_action.text != null) {
            Item weapon = new Item(ItemKind.fromString(text));
            perso.setWeapon(weapon);
            perso.attack();
            achieved = true;
          }
          break;
        case activate:
          Element toActivate = EngineZildo.spriteManagement.getNamedElement(p_action.what);
          ElementGear gearToActivate = (ElementGear) toActivate;
          gearToActivate.activate(p_action.activate);
          break;
        case tile:
          // Change tile on map
          Area area = EngineZildo.mapManagement.getCurrentMap();
          Case c = area.get_mapcase(location.x, location.y + 4);
          if (p_action.back != -1) {
            c.setBackTile(new Tile(p_action.back, c));
          }
          if (p_action.back2 != -1) {
            c.setBackTile2(new Tile(p_action.back2, c));
          }
          if (p_action.fore != -1) {
            c.setForeTile(new Tile(p_action.fore, c));
          }
          EngineZildo.mapManagement.getCurrentMap().set_mapcase(location.x, location.y + 4, c);
          achieved = true;
          break;
        case filter:
          switch (p_action.val) {
            case 0: // REGULAR
              ClientEngineZildo.ortho.setFilteredColor(new Vector3f(1, 1, 1));
              break;
            case 1: // NIGHT
              ClientEngineZildo.ortho.setFilteredColor(Ortho.NIGHT_FILTER);
              break;
            case 2: // SEMI_NIGHT
              ClientEngineZildo.ortho.setFilteredColor(Ortho.SEMI_NIGHT_FILTER);
              break;
          }
          achieved = true;
          break;
        case end:
          new GameOverAction().launchAction(null);
      }

      p_action.done = achieved;
      p_action.waiting = !achieved;
    }
    return achieved;
  }
Beispiel #3
0
/**
 * A menu providing creation of a new server.
 *
 * <p>At the moment of adding it to the server file configuration, controls that it's a valid one.
 *
 * <p>After that, we go back the previous menu, assuming it was a "Join game" menu, reloading the
 * server configuration.
 *
 * @author tchegito
 */
@Deprecated
public class AddServerMenu extends Menu {

  StringBuilder name = new StringBuilder(UIText.getMenuText("m3.defaultName"));
  StringBuilder ip = new StringBuilder("<IP>");
  StringBuilder port = new StringBuilder("<port>");

  public AddServerMenu(Menu p_previousMenu) {
    super("m3.title");

    previousMenu = p_previousMenu;

    ItemMenu[] itms = new ItemMenu[5];
    itms[0] =
        new EditableItemMenu(name) {
          @Override
          public void run() {
            client.handleMenu(currentMenu);
          }
        };
    itms[1] =
        new EditableItemMenu(ip) {
          @Override
          public void run() {
            client.handleMenu(currentMenu);
          }
        };
    itms[2] =
        new EditableItemMenu(port) {
          @Override
          public void run() {
            client.handleMenu(currentMenu);
          }
        };
    itms[3] =
        new ItemMenu("m3.add") {
          @Override
          public void run() {
            addServer(name.toString(), ip.toString(), port.toString());
          }
        };
    itms[4] =
        new ItemMenu("global.back") {
          @Override
          public void run() {
            ClientEngineZildo.getClientForMenu().handleMenu(previousMenu);
          }
        };

    setMenu(itms);
  }

  /**
   * Add a new server, if given parameters are corrects.
   *
   * @param p_name
   * @param p_ip
   * @param p_port
   */
  private void addServer(String p_name, String p_ip, String p_port) {
    // 1: Controls that everything is correct
    int error = 2;
    try {
      if (p_name.equals(UIText.getMenuText("m3.defaultName"))) {
        throw new Exception();
      }
      error = 0;
      int intPort = Integer.valueOf(p_port);
      error = 1;
      new TransferObject(p_ip, intPort);

      // Everything's ok if we got here
      saveServerInfos(new ServerInfo(p_name, p_ip, intPort));

      // And go back to the menu
      client.handleMenu(new JoinGameMenu(loadServersInfos(), previousMenu.previousMenu));

    } catch (Exception e) {
      String message;
      switch (error) {
        case 0:
          message = "m3.error.port";
          break;
        case 2:
          message = "m3.error.name";
          break;
        default:
        case 1:
          message = "m3.error.unreachable";
          break;
      }

      message = UIText.getMenuText(message);
      client.handleMenu(new InfoMenu("Impossible. " + message, currentMenu));
    }
  }

  /**
   * Add given server to the server file {@link Constantes#SERVER_FILE}
   *
   * @param p_serverInfo
   */
  private void saveServerInfos(ServerInfo p_serverInfo) {
    // Read the file
    List<ServerInfo> infos = loadServersInfos();
    // Replace server with same name, if needed.
    int idx = infos.indexOf(p_serverInfo);
    if (idx != -1) {
      infos.remove(idx);
    }
    infos.add(p_serverInfo);
    // And save the file
    EasyBuffering buffer = new EasyBuffering();
    for (ServerInfo info : infos) {
      buffer.put(info.name);
      buffer.put(info.ip);
      buffer.put(info.port);
    }
    EasyWritingFile file = new EasyWritingFile(buffer);
    file.saveFile(Constantes.SERVER_FILE);
  }

  /**
   * Read the servers configuration file to return a list of {@link ServerInfo}.<br>
   * In any error case, return an empty list.
   *
   * @return List<ServerInfo>
   */
  public static List<ServerInfo> loadServersInfos() {
    List<ServerInfo> infos = new ArrayList<ServerInfo>();
    try {
      EasyBuffering file = Zildo.pdPlugin.openFile(Constantes.SERVER_FILE);
      while (!file.eof()) {
        String name = file.readString();
        String ip = file.readString();
        int port = file.readInt();
        infos.add(new ServerInfo(name, ip, port));
      }
      return infos;
    } catch (Exception e) {
      return infos;
    }
  }
}