示例#1
0
  /**
   * @param 0 <= x < dungeon.width
   * @param 0 <= y < dungeon.height
   */
  public Chara(Dungeon dungeon) {
    this.dungeon = dungeon;

    // if no room then re-generate x, y
    do {
      x = PSUDO_RANDOM.nextInt(dungeon.width());
      y = PSUDO_RANDOM.nextInt(dungeon.height());

    } while (!dungeon.isRoom(x, y));
  }
示例#2
0
  /**
   * move the chara to the specified direction.
   *
   * @return <code>true</code> if the chara was successfully moved. <code>false</code> otherwise.
   */
  public boolean moveTo(Direction direction) {

    int newx = getNewLocationX(direction);
    int newy = getNewLocationY(direction);

    boolean newxOk = isInRange(newx, 0, dungeon.width());
    boolean newyOk = isInRange(newy, 0, dungeon.height());

    // check if the new location is a room.
    boolean allOk = newxOk && newyOk && dungeon.isRoom(newx, newy);
    if (allOk) {
      x = newx;
      y = newy;
    }

    return allOk;
  }
  public void submit(boolean win) {

    load();

    Record rec = new Record();

    rec.info = Dungeon.resultDescription;
    rec.win = win;
    rec.heroClass = Dungeon.hero.heroClass;
    rec.armorTier = Dungeon.hero.tier();
    rec.herolevel = Dungeon.hero.lvl;
    rec.depth = Dungeon.depth;
    rec.score = score(win);

    String gameFile = Utils.format(DETAILS_FILE, SystemTime.now);
    try {
      Dungeon.saveGame(gameFile);
      rec.gameFile = gameFile;
    } catch (IOException e) {
      rec.gameFile = "";
    }

    records.add(rec);

    Collections.sort(records, scoreComparator);

    lastRecord = records.indexOf(rec);
    int size = records.size();
    while (size > TABLE_SIZE) {

      Record removedGame;
      if (lastRecord == size - 1) {
        removedGame = records.remove(size - 2);
        lastRecord--;
      } else {
        removedGame = records.remove(size - 1);
      }

      if (removedGame.gameFile.length() > 0) {
        Game.instance.deleteFile(removedGame.gameFile);
      }

      size = records.size();
    }

    totalNumber++;
    if (win) {
      wonNumber++;
    }

    Badges.validateGamesPlayed();

    save();
  }
示例#4
0
  public Dungeon getDungeonForLocation(Location loc) {

    String[] keys = getDungeonList();

    for (int i = 0; i < keys.length; i++) {

      String name = keys[i];

      Dungeon dungeon = this.dungeons.get(name);

      if (dungeon == null) {
        continue;
      }

      if (dungeon.containsLocation(loc)) {
        return dungeon;
      }
    }

    return null;
  }
  public void nextLevel() {
    //		if (!firstStage)
    this.getInfo().writeText("You moved to the next stage!");
    //		else
    //			firstStage = false;
    //		boolean check = false;
    Dungeon d;
    floorLevel++;
    if (floorLevel == 6)
      JOptionPane.showMessageDialog(
          null, "Great Job! You beat the game (as it is right now). If you want you can continue.");
    d = new Dungeon(60, 60); // creates a dungeon
    ArrayList<Room> roo = d.getRooms();
    boolean reDo = d.checkRooms();
    while (reDo == true) {
      d = new Dungeon(50, 50);
      reDo = d.checkRooms();
    }

    land = d.getDungeon(); // / change to land
    openSpaces = openLocations(); // gets locations of the rooms/corridors
    hero.setLand(this.land);
    grid = new BoundedGrid<>(land.length, land[1].length);
    world = new ActorWorld(grid);

    int counter = 0;
    for (int i = 0; i < numEnemies * floorLevel; i++) {
      addEnemy();
      counter++;
    }
    for (int i = 0; i < numItems + floorLevel; i++) {
      addItem();
    }

    Location l = (openSpaces.get((int) (Math.random() * openSpaces.size())));
    world.add(openSpaces.get((int) (Math.random() * openSpaces.size())), hero.main);
    hero.setGrid(grid);
    hero.setPanel(this);

    addStairs();

    ArrayList<ArrayList<Location>> a = d.getCorridors();
    for (ArrayList<Location> lo : a) {
      for (int i = 0; i < lo.size(); i++) {
        openSpaces.add(lo.get(i));
      }
    }
    repaint();
  }
  public void startGame() {
    JOptionPane.showMessageDialog(
        null, "Welcome to Pokemon Mystery Dungeon! Reach the 6th stage to beat the game!");
    //			if (!firstStage)
    //				this.getInfo().writeText("You moved to the next stage!");
    //			else
    //				firstStage = false;
    //			boolean check = false;
    Dungeon d;

    d = new Dungeon(60, 60); // creates a dungeon
    ArrayList<Room> roo = d.getRooms();
    boolean reDo = d.checkRooms();
    while (reDo == true) {
      d = new Dungeon(50, 50);
      reDo = d.checkRooms();
    }

    land = d.getDungeon(); // / change to land
    openSpaces = openLocations(); // gets locations of the rooms/corridors
    grid = new BoundedGrid<>(land.length, land[1].length);
    world = new ActorWorld(grid);

    int counter = 0;
    for (int i = 0; i < numEnemies * floorLevel; i++) {
      addEnemy();
      counter++;
    }
    for (int i = 0; i < numItems + floorLevel; i++) {
      addItem();
    }
    PersonalityTest t = new PersonalityTest(land);
    Pokemon z = t.chooseCharacter(this);
    hero = new Hero(z, land, this);
    Location l = (openSpaces.get((int) (Math.random() * openSpaces.size())));
    world.add(openSpaces.get((int) (Math.random() * openSpaces.size())), hero.main);
    hero.setGrid(grid);
    hero.setPanel(this);

    addStairs();

    ArrayList<ArrayList<Location>> a = d.getCorridors();
    for (ArrayList<Location> lo : a) {
      for (int i = 0; i < lo.size(); i++) {
        openSpaces.add(lo.get(i));
      }
    }
    repaint();
  }
示例#7
0
  /** Start the game and interacts with the player for each dungeon */
  public void start() {
    for (int i = 0; i < dungeons.size(); i++) {
      currentDungeon = dungeons.get(i);
      System.out.println("Welcome to the dungeon number " + (i + 1));
      do {
        System.out.println("What do you want to do?");
        System.out.print("> ");
        currentDungeon.interpretCommand(scanner.nextLine().toLowerCase());

      } while (!gameIsFinished());
      if (gameIsWon()) {
        System.out.println("You win the dungeon number" + (i + 1) + " !");
        if (i == dungeons.size() - 1) {
          System.out.println("Congratulation, you've completed all the dungeons !");
        }
      } else {
        System.out.println("You loose!");
        break;
      }
    }
  }
示例#8
0
文件: Map.java 项目: AlanTR/solarus
  /**
   * Sets the world where this map is.
   *
   * @param world the world index: 0 if the map is outside, -1 if it is inside, 1 to 20 if it is in
   *     a dungeon
   * @throws MapException if the specified world is incorrect
   */
  public void setWorld(int world) throws MapException {

    if (world != this.world) {

      if (world > 20 || world < -1) {
        throw new MapException("Invalid world: " + world);
      }

      this.world = world;

      if (!isInDungeon()) { // no dungeon : no floor by default
        setFloor(-100);
      } else { // dungeon: first floor by default
        dungeon = new Dungeon(world);
        setFloor(dungeon.getDefaultFloor());
        setSmallKeysVariable(205 + 10 * (world - 1));
      }

      setChanged();
      notifyObservers();
    }
  }
示例#9
0
  public void addDungeon(Dungeon dungeon) {

    String name = dungeon.getName();
    this.dungeons.put(name, dungeon);
  }
示例#10
0
文件: Map.java 项目: AlanTR/solarus
  /**
   * Saves the map into its file.
   *
   * @throws ZSDXException if the file could not be written for various reasons
   */
  public void save() throws ZSDXException {

    // check that the map is valid
    checkValidity();

    try {

      // open the map file
      File mapFile = Project.getMapFile(mapId);
      PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(mapFile)));

      // print the map general info
      // syntax: width height world floor x y small_keys_variable tileset_id music_id
      out.print(size.width);
      out.print('\t');
      out.print(size.height);
      out.print('\t');
      out.print(world);
      out.print('\t');
      out.print(floor);
      out.print('\t');
      out.print(location.x);
      out.print('\t');
      out.print(location.y);
      out.print('\t');
      out.print(smallKeysVariable);
      out.print('\t');
      out.print(tilesetId);
      out.print('\t');
      out.print(musicId);
      out.println();

      for (Layer layer : Layer.values()) {

        MapEntities entities = allEntities[layer.getId()];

        // print the entities
        for (MapEntity entity : entities) {
          out.print(entity.toString());
          out.println();
        }
      }

      out.close();

      history.setSaved();

      // also update the map name in the global resource list
      Resource mapResource = Project.getResource(ResourceType.MAP);
      mapResource.setElementName(mapId, name);
      Project.getResourceDatabase().save();

      // upate the dungeon elements of this map
      if (isInDungeon()) {
        Dungeon.saveMapInfo(this);
      }

      // create a script for the map if necessary
      File scriptFile = Project.getMapScriptFile(mapId);
      if (!scriptFile.exists()) {
        scriptFile.createNewFile();
      }
    } catch (IOException ex) {
      throw new MapException(ex.getMessage());
    }
  }
示例#11
0
 public boolean gameIsWon() {
   return currentDungeon.gameIsWon();
 }
示例#12
0
 public boolean gameIsLost() {
   return currentDungeon.gameIsLost();
 }
示例#13
0
 @Test
 public void testInterpretCommand() {
   assertEquals(dungeon.rooms[0], dungeon.currentRoom);
   dungeon.interpretCommand("go north");
   assertEquals(dungeon.rooms[1], dungeon.currentRoom);
 }
示例#14
0
  /**
   * moveEntity: This method moves an entity and checks the potential move for collisions. Pre: The
   * game must have been initialized. The entity at the index must not be null. Post: The entity
   * will have been moved accordingly.
   */
  public static void moveEntity(int iEntityID) {

    boolean bXHandled = false;
    boolean bYHandled = false;
    boolean bCollidesWithWalls = handleEntity(iEntityID).collidesWithWalls();
    // X and Y displacements of the entity, provided that there will be no collisions.
    double dEntityXShift =
        Math.sin(handleEntity(iEntityID).getMovementDirection())
            * handleEntity(iEntityID).dMovementMagnitude;
    double dEntityYShift =
        (-1)
            * Math.cos(handleEntity(iEntityID).getMovementDirection())
            * handleEntity(iEntityID).dMovementMagnitude;

    // The pre-movement xposition, yposition, and circle radius.
    double dCurrentXPos = handleEntity(iEntityID).getXPos();
    double dCurrentYPos = handleEntity(iEntityID).getYPos();
    double dCurrentSize = handleEntity(iEntityID).getSize();

    // Hypothetical post-movement coordinates of the circle's bounding box
    double dNewXPosCenter = dCurrentXPos + dEntityXShift;
    double dNewXPosRight = dNewXPosCenter + dCurrentSize;
    double dNewXPosLeft = dNewXPosCenter - dCurrentSize;
    double dNewYPosCenter = dCurrentYPos + dEntityYShift;
    double dNewYPosTop = dNewYPosCenter - dCurrentSize;
    double dNewYPosBot = dNewYPosCenter + dCurrentSize;

    // Handling right-way movement
    if (dNewXPosCenter < dngCurrentDungeon.getXSize() - dCurrentSize) {
      if (dEntityXShift > 0 && !bXHandled && bCollidesWithWalls) { // Entity is moving right
        rightMovingCollisions:
        for (int iuP1 = (int) dNewXPosLeft; iuP1 <= (int) dNewXPosRight; iuP1 += 1) {
          for (int iuP2 = valueInBoundsY((int) (dCurrentYPos - dCurrentSize));
              iuP2 <= valueInBoundsY((int) (dCurrentYPos + dCurrentSize));
              iuP2 += 1) {
            if (!isWalkable(handleTile(iuP1, iuP2).getTileType())) {
              if (intersectsCircleMapTile(dNewXPosCenter, dCurrentYPos, dCurrentSize, iuP1, iuP2)) {
                dEntityXShift =
                    Math.max(
                        (iuP1 - dCurrentXPos) - (dCurrentSize + DISTANCE_TO_KEEP_FROM_WALL), 0);
                break rightMovingCollisions;
              }
            }
          }
        }
        bXHandled = true;
      }
    } else {
      dEntityXShift = 0;
    }

    // Handling left-way movement
    if (dNewXPosCenter > 0 + dCurrentSize) {
      if (dEntityXShift < 0 && !bXHandled && bCollidesWithWalls) { // Entity is moving left
        leftMovingCollisions:
        for (int iuP1 = (int) dNewXPosRight; iuP1 >= (int) dNewXPosLeft; iuP1 -= 1) {
          for (int iuP2 = valueInBoundsY((int) (dCurrentYPos - dCurrentSize));
              iuP2 <= valueInBoundsY((int) (dCurrentYPos + dCurrentSize));
              iuP2 += 1) {
            if (!isWalkable(handleTile(iuP1, iuP2).getTileType())) {
              if (intersectsCircleMapTile(dNewXPosCenter, dCurrentYPos, dCurrentSize, iuP1, iuP2)) {
                dEntityXShift =
                    Math.min(
                        (iuP1 + 1 - dCurrentXPos) + (dCurrentSize + DISTANCE_TO_KEEP_FROM_WALL), 0);
                break leftMovingCollisions;
              }
            }
          }
        }
        bXHandled = true;
      }
    } else {
      dEntityXShift = 0;
    }

    // Handling down-way movement
    if (dNewYPosCenter < dngCurrentDungeon.getYSize() - dCurrentSize) {
      if (dEntityYShift > 0 && !bYHandled && bCollidesWithWalls) { // Entity is moving down
        downMovingCollisions:
        for (int iuP1 = (int) dNewYPosTop; iuP1 <= (int) dNewYPosBot; iuP1 += 1) {
          for (int iuP2 = valueInBoundsX((int) (dCurrentXPos - dCurrentSize));
              iuP2 <= valueInBoundsX((int) (dCurrentXPos + dCurrentSize));
              iuP2 += 1) {
            if (!isWalkable(handleTile(iuP2, iuP1).getTileType())) {
              if (intersectsCircleMapTile(dCurrentXPos, dNewYPosCenter, dCurrentSize, iuP2, iuP1)) {
                dEntityYShift =
                    Math.max(
                        (iuP1 - dCurrentYPos) - (dCurrentSize + DISTANCE_TO_KEEP_FROM_WALL), 0);
                break downMovingCollisions;
              }
            }
          }
        }
        bYHandled = true;
      }
    } else {
      dEntityYShift = 0;
    }

    // Handling up-way movement
    if (dNewYPosCenter > 0 + dCurrentSize) {
      if (dEntityYShift < 0 && !bYHandled && bCollidesWithWalls) { // Entity is moving up
        upMovingCollisions:
        for (int iuP1 = (int) dNewYPosBot; iuP1 >= (int) dNewYPosTop; iuP1 -= 1) {
          for (int iuP2 = valueInBoundsX((int) (dCurrentXPos - dCurrentSize));
              iuP2 <= valueInBoundsX((int) (dCurrentXPos + dCurrentSize));
              iuP2 += 1) {
            if (!isWalkable(handleTile(iuP2, iuP1).getTileType())) {
              if (intersectsCircleMapTile(dCurrentXPos, dNewYPosCenter, dCurrentSize, iuP2, iuP1)) {
                dEntityYShift =
                    Math.min(
                        (iuP1 + 1 - dCurrentYPos) + (dCurrentSize + DISTANCE_TO_KEEP_FROM_WALL), 0);
                break upMovingCollisions;
              }
            }
          }
        }
        bYHandled = true;
      }
    } else {
      dEntityYShift = 0;
    }

    dNewXPosCenter = dCurrentXPos + dEntityXShift;
    dNewYPosCenter = dCurrentYPos + dEntityYShift;
    if (handleEntity(iEntityID).collidesWithEntities()) {
      for (int iuP1 = 0; iuP1 < entveCurrentEntities.size(); iuP1++) {
        if (iuP1 != iEntityID) {
          if (handleEntity(iuP1).collidesWithEntities()) {
            double distance =
                (dNewXPosCenter - handleEntity(iuP1).getXPos())
                        * (dNewXPosCenter - handleEntity(iuP1).getXPos())
                    + (dNewYPosCenter - handleEntity(iuP1).getYPos())
                        * (dNewYPosCenter - handleEntity(iuP1).getYPos());
            if (distance
                < (dCurrentSize + handleEntity(iuP1).getSize())
                    * (dCurrentSize + handleEntity(iuP1).getSize())) {
              dEntityXShift = 0;
              dEntityYShift = 0;
            }
          }
        }
      }
    }

    // Moves the character by shifting their X and Y coordinates.
    handleEntity(iEntityID).shiftXPos(dEntityXShift);
    handleEntity(iEntityID).shiftYPos(dEntityYShift);
  }