Esempio n. 1
0
 @Test
 public void timeAfterReloadAndWin() {
   // 0) Create an hypothetic zildo
   EngineZildo.spawnClient(ZildoOutfit.Zildo);
   // 1) Create a game, and save
   Game game = new Game("preintro", "unitTest");
   EngineZildo.setGame(game);
   EasyBuffering buffer = new EasyBuffering(5000);
   game.serialize(buffer);
   int savedTime = EngineZildo.game.getTimeSpent();
   // 2) Reload this game
   game = Game.deserialize(buffer, false);
   EngineZildo.setGame(game);
   // 3) Wait some seconds
   ZUtils.sleep(4000);
   // 4) Save again
   // game.serialize(buffer);
   // ==> check that time has been well updated
   int timeSpent = EngineZildo.game.getTimeSpent();
   Assert.assertTrue(
       "Time measured at the end ("
           + timeSpent
           + ") should have been greater than starting time ("
           + savedTime
           + ")",
       timeSpent > savedTime);
 }
  /** Sauvegarde la carte en cours dans le fichier sélectionné */
  public void save() {
    if (fileChooser.getSelectedFile() == null) {
      return;
    }

    String name = fileChooser.getSelectedFile().getName();
    EngineZildo.getMapManagement().getCurrentMap().setName(name);
    masterFrame.getManager().saveAs(name);
  }
  /**
   * @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;
  }
  void debug() {
    int fps = (int) openGLGestion.getFPS();

    ortho.drawText(1, 50, "fps=" + fps, new Vector3f(0.0f, 0.0f, 1.0f));

    SpriteEntity zildo = spriteDisplay.getZildo();
    if (zildo == null) {
      return;
    }
    ortho.drawText(1, 80, "zildo: " + zildo.x, new Vector3f(1.0f, 0.0f, 1.0f));
    ortho.drawText(43, 86, "" + zildo.y, new Vector3f(1.0f, 0.0f, 1.0f));

    // Debug collision
    Point camera = mapDisplay.getCamera();
    if (EngineZildo.collideManagement != null && Zildo.infoDebugCollision) {
      for (Collision c : EngineZildo.collideManagement.getTabColli()) {
        if (c != null) {
          int rayon = c.cr;
          int color = 15;
          Perso damager = c.perso;
          Vector4f alphaColor = new Vector4f(0.2f, 0.4f, 0.9f, 16.0f);
          if (damager != null && damager.getInfo() == PersoInfo.ENEMY) {
            color = 20;
          }
          if (c.size == null) {
            ortho.box(
                c.cx - rayon - camera.x,
                c.cy - rayon - camera.y,
                rayon * 2,
                rayon * 2,
                0,
                alphaColor);
          } else {
            Point center = new Point(c.cx - camera.x, c.cy - camera.y);
            Rectangle rect = new Rectangle(center, c.size);
            ortho.box(rect, color, null);
          }
        }
      }
      // -7, -10
      int x = (int) zildo.x - 4;
      int y = (int) zildo.y - 10;
      ortho.box(x - 3 - camera.x, y - camera.y, 16, 16, 12, null);
    }

    if (Zildo.infoDebugCase) {
      for (int y = 0; y < 20; y++) {
        for (int x = 0; x < 20; x++) {
          int cx = x + camera.x / 16;
          int cy = y + camera.y / 16;
          int px = x * 16 - camera.x % 16;
          int py = y * 16 - camera.y % 16;
          if (cy < 64 && cx < 64) {
            Case c = EngineZildo.getMapManagement().getCurrentMap().get_mapcase(cx, cy + 4);
            int onmap = EngineZildo.getMapManagement().getCurrentMap().readmap(cx, cy);
            ortho.drawText(px, py + 4, "" + c.getZ(), new Vector3f(0, 1, 0));
            ortho.drawText(px, py, "" + onmap, new Vector3f(1, 0, 0));
          }
        }
      }
    }
  }
  public ClientEvent renderEvent(ClientEvent p_event) {

    ClientEvent retEvent = p_event;

    boolean displayGUI = !p_event.script && !p_event.mapChange;

    if (p_event.wait != 0) {
      p_event.wait--;
    } else {
      switch (p_event.nature) {
        case CHANGINGMAP_ASKED:
          // Changing map : 1/3 we launch the fade out
          retEvent.effect = p_event.effect; // FilterEffect.BLEND;
        case FADE_OUT:
          retEvent.nature = ClientEventNature.FADING_OUT;
          guiDisplay.fadeOut(retEvent.effect);
          break;
        case FADING_OUT:
          if (guiDisplay.isFadeOver()) {
            retEvent.nature = ClientEventNature.FADEOUT_OVER;
          } else {
            circleOnZildo();
          }
          break;
        case CLEAR:
          // Reset fade, hide Zildo and kill the current map
          filterCommand.fadeEnd();
          EngineZildo.getMapManagement().deleteCurrentMap();
          EngineZildo.persoManagement.getZildo().setX(-100);
          ClientEngineZildo.tileEngine.cleanUp();
          break;
        case CHANGINGMAP_LOADED:
          // Changing map : 2/3 we load the new map and launch the
          // fade in

        case FADE_IN:
          retEvent.nature = ClientEventNature.FADING_IN;
          guiDisplay.fadeIn(retEvent.effect);

          break;
        case FADING_IN:
          if (guiDisplay.isFadeOver()) {
            // Changing map : 3/3 we unblock the player
            filterCommand.active(retEvent.effect.getFilterClass()[0], false, null);
            filterCommand.active(BilinearFilter.class, true, null);
            retEvent.nature = ClientEventNature.NOEVENT;
            retEvent.mapChange = false;
          } else {
            // Call Circle filter to focus on Zildo
            circleOnZildo();
          }
          break;
        case CHANGINGMAP_SCROLL_ASKED:
          retEvent.nature = ClientEventNature.CHANGINGMAP_SCROLL_WAIT_MAP;
          displayGUI = false;
          break;
        case CHANGINGMAP_SCROLL_START:
          if (mapDisplay.getTargetCamera() == null) {
            mapDisplay.centerCamera();
            mapDisplay.shiftForMapScroll(p_event.angle);

            retEvent.nature = ClientEventNature.CHANGINGMAP_WAITSCRIPT;
          }
          displayGUI = false;
          break;
        case CHANGINGMAP_SCROLL:
          displayGUI = false;
          if (!mapDisplay.isScrolling()) {
            retEvent.nature = ClientEventNature.CHANGINGMAP_SCROLLOVER;
            // Show GUI sprites back
            displayGUI = true;
            // mapDisplay.setPreviousMap(null);
          }
          break;
        case NOEVENT:
          if (askedEvent != null) {
            retEvent.nature = askedEvent.nature;
            askedEvent = null;
          }
          break;
        default:
          break;
      }
    }
    // Remove GUI when scripting
    guiDisplay.setToDisplay_generalGui(displayGUI);

    return retEvent;
  }