⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 world.java

📁 j2me游戏编程光盘源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   }

   public final byte getTile(int x, int y)
   {
      int tx = getTileAtX(x);
      int ty = getTileAtY(y);
      if (tx < 0 || tx >= tilesWide || ty < 0 || ty >= tilesHigh) return -1;
      return tileMap[y / TILE_WIDTH][x / TILE_WIDTH];
   }


   public final void render(Graphics g)
   {
      try
      {
// Draw a star field scrolling 10 times slower than the view.
         if (StarAssault.getApp().isOptionStarField())
            Tools.drawStarField(g, viewX / 10, viewY / 10, viewWidth, viewHeight);

         // draw all the tiles
         int startX = (viewX / TILE_WIDTH) - 1;
         int startY = (viewY / TILE_HEIGHT) - 1;
         int endTileX = ((Math.abs(viewX) + viewWidth) / TILE_WIDTH) + 1;
         int endTileY = ((Math.abs(viewY) + viewHeight) / TILE_HEIGHT) + 1;
         //System.out.println("viewHeight=" + viewHeight + " viewY="+ viewY + " c=" + (viewY + viewHeight));

         if (endTileX > tilesWide) endTileX = tilesWide;
         if (endTileY > tilesHigh) endTileY = tilesHigh;

         byte tileType = 0;
         int xpos = 0;
         int ypos = 0;

// Loop through all the rows of the tile map that need to be drawn,
// then all the columns. This starts at startTileY and goes down till
// we get to the last viewable row at endTileY, then for each of these
// it goes across the map from startTileX to endTileX.
         for (int tileY = startY; tileY < endTileY; tileY++)
         {
            for (int tileX = startX; tileX < endTileX; tileX++)
            {
               if (tileY >= 0 && tileX >= 0)
               {
// Access the entry corresponding to this location. Since most
// tile maps contains empty space the code also does a quick
// check to see if it can just ignore this entry if the byte
// equals the default value 0 (NO_TILE).
                  tileType = tileMap[tileY][tileX];
                  if (tileType == NO_TILE)
                     continue; // quick abort if it's nothing

// Calculate the x and y position of this tile.
                  xpos = (tileX * TILE_WIDTH) - viewX;
                  ypos = (tileY * TILE_HEIGHT) - viewY;

// Check whether this tile position is in the view port.
                  if (xpos > 0 - TILE_WIDTH && xpos < viewWidth &&
                          ypos > 0 - TILE_HEIGHT && ypos < viewHeight)
                  {
                     if (tileType == GATEWAY_TILE)
                     {
// Draw the glowing gateway sprite.
                        gatewaySprite.draw(g, xpos, ypos);
                     }
                     else if (tileType >= START_REAL_TILE &&
                             tileType <= END_REAL_TILE)
                     {
// Based on the byte value this code draws an image
// from the ImageSet loaded in the constructor. To
// keep this simpler I抳e mapped the frames in the
// graphics file to the same byte values in the map.
// That way the code doesn抰 need to translate the
// values when drawing them [--] if the tile map byte
// is the number two the second frame from the loaded
// world images will be drawn (note that I take one
// away from the byte value to account for zero
// meaning no tile in the world).
                        tiles.draw(g, 0, tileType - 1, xpos, ypos);
                     }
                     else if (tileType >= START_ACTIVATOR_TILE &&
                             tileType <= END_ACTIVATOR_TILE)
                        activateTile(tileX, tileY);
                  }
               }
            }
         }

         // now draw all the actors that are in this sector
         Actor a = enemyShipPool.getFirstUsed();
         while (a != null)
         {
            //System.out.println("x=" + a.getX() + " y=" + a.getY() +
            //		  " x2=" + (viewX - TILE_WIDTH) + " y2=" + (viewY - TILE_HEIGHT) +
            //		  " w=" + (tw * TILE_WIDTH) +  " h=" + (th * TILE_HEIGHT) +
            //			" isInRect = " + Tools.isPointInRect(a.getX(), a.getY(), viewX - TILE_WIDTH,
            //															 viewY - TILE_HEIGHT, tw * TILE_WIDTH, th * TILE_HEIGHT)
            //);

            if (Tools.isPointInRect(a.getX(), a.getY(), viewX - TILE_WIDTH,
                                    viewY - TILE_HEIGHT, endTileX * TILE_WIDTH, endTileY * TILE_HEIGHT))
               a.render(g, viewX, viewY);

            a = a.getNextLinked();
         }

         // draw the player's ship
         playerShip.render(g, viewX, viewY);

         // render all the active bullets
         a = bulletPool.getFirstUsed();
         while (a != null)
         {
            if (Tools.isPointInRect(a.getX(), a.getY(), viewX - TILE_WIDTH,
                                    viewY - TILE_HEIGHT, endTileX * TILE_WIDTH, endTileY * TILE_HEIGHT))
               a.render(g, viewX, viewY);
            a = a.getNextLinked();
         }


      }
      catch (Exception e)
      {
         System.out.println("App exception: " + e);
         e.printStackTrace();
      }
   }

   private final void activateTile(int tileX, int tileY)
   {
      byte tileType = tileMap[tileY][tileX];
      int xpos = (tileX * TILE_WIDTH);
      int ypos = (tileY * TILE_HEIGHT);

      switch (tileType)
      {
         case DRONE_ACTIVATOR_TILE:
            getEnemyShipFromPool().init(Ship.ENEMY_DRONE, xpos, ypos);
            break;

         case TURRET_ACTIVATOR_TILE:
            getEnemyShipFromPool().init(Ship.ENEMY_TURRET, xpos, ypos);
            break;

         case FIGHTER_ACTIVATOR_TILE:
            getEnemyShipFromPool().init(Ship.ENEMY_FIGHTER, xpos, ypos);
            break;
      }
      // clear the activator tile
      tileMap[tileY][tileX] = NO_TILE;
   }


   /**
    * Save level to RMS (overwrites any existing level record)
    */
   public boolean saveLevel()
   {
      RecordStore store = null;
      try
      {
         try
         {
            RecordStore.deleteRecordStore("Level");
         }
         catch (RecordStoreNotFoundException rse)
         {
         }

         store = RecordStore.openRecordStore("Level", true);

         ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
         DataOutputStream dataOutputStream = new DataOutputStream(byteOutputStream);

         dataOutputStream.writeInt(levelNum);
         dataOutputStream.writeInt(GameScreen.getGameScreen().getLives());
         dataOutputStream.writeInt(startX);
         dataOutputStream.writeInt(startY);
         dataOutputStream.writeInt(tilesWide);
         dataOutputStream.writeInt(tilesHigh);

         for (int ty = 0; ty < tilesHigh; ty++)
            for (int tx = 0; tx < tilesWide; tx++)
               dataOutputStream.writeByte(tileMap[ty][tx]);

         dataOutputStream.flush();

         // delete the old one and add the new record
         byte[] recordOut = byteOutputStream.toByteArray();
         try
         {
            store.setRecord(1, recordOut, 0, recordOut.length);
         }
         catch (InvalidRecordIDException ir)
         {
            store.addRecord(recordOut, 0, recordOut.length);
         }
         dataOutputStream.close();
         byteOutputStream.close();

         return true;
      }

      catch (IOException io)
      {
         System.out.println("IOException: " + io);
         return false;
      }
      catch (RecordStoreException rse)
      {
         System.out.println("RSException: " + rse);
         return false;
      }

      finally
      {
         try
         {
            if (store != null) store.closeRecordStore();
         }

         catch (RecordStoreNotOpenException e)
         {
         }

         catch (RecordStoreException e)
         {
         }
      }

   }

   /**
    * returns the level number that was last saved (without loading the level)
    */
   public static int getSavedLevel()
   {
      RecordStore store = null;
      try
      {
         store = RecordStore.openRecordStore("Level", false);

         ByteArrayInputStream byteInputStream = new ByteArrayInputStream(store.getRecord(1));
         DataInputStream dataInputStream = new DataInputStream(byteInputStream);
         int level = dataInputStream.readInt();
         dataInputStream.close();
         byteInputStream.close();
         return level;
      }

      catch (IOException io)
      {
         System.out.println("IOException: " + io);
         return 0;
      }
      catch (RecordStoreNotOpenException rse)
      {
         return 0;
      }
      catch (RecordStoreException rse)
      {
         System.out.println("RSException: " + rse);
         return 0;
      }

      finally
      {
         try
         {
            if (store != null) store.closeRecordStore();
         }

         catch (RecordStoreNotOpenException e)
         {
         }

         catch (RecordStoreException e)
         {
         }
      }
   }


   /**
    * Called publicly by the GameScreen if the user is reloading the game
    * (they click on Resume Level, and the game is not loaded)
    * @return the levelNum loaded
    */
   public int loadLevel()
   {
      // load up the tilemap from RMS for the current level
      RecordStore store = null;
      try
      {
         store = RecordStore.openRecordStore("Level", true);

         ByteArrayInputStream byteInputStream = new ByteArrayInputStream(store.getRecord(1));
         DataInputStream dataInputStream = new DataInputStream(byteInputStream);

         levelNum = dataInputStream.readInt();
         int lives = dataInputStream.readInt();
         startX = dataInputStream.readInt();
         startY = dataInputStream.readInt();
         tilesWide = dataInputStream.readInt();
         tilesHigh = dataInputStream.readInt();

         tileMap = new byte[tilesHigh][tilesWide];
         for (int ty = 0; ty < tilesHigh; ty++)
            for (int tx = 0; tx < tilesWide; tx++)
               tileMap[ty][tx] = dataInputStream.readByte();

         playerShip.setStartingPos(startX * TILE_WIDTH, startY * TILE_HEIGHT);
         GameScreen.getGameScreen().setLives(lives);

         // now restart the world for this level
         restart();

         dataInputStream.close();
         byteInputStream.close();

         return levelNum;
      }

      catch (IOException io)
      {
         System.out.println("IOException: " + io);
         return 0;
      }
      catch (RecordStoreException rse)
      {
         System.out.println("RSException: " + rse);
         return 0;
      }

      finally
      {
         try
         {
            if (store != null) store.closeRecordStore();
         }

         catch (RecordStoreNotOpenException e)
         {
         }

         catch (RecordStoreException e)
         {
         }
      }
   }

   public void removeSavedGame()
   {
      try
      {
         RecordStore.deleteRecordStore("Level");
      }
      catch (RecordStoreNotFoundException rse)
      {
      }
      catch (RecordStoreException e)
      {
      }
   }


}














⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -