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

📄 townmap.java

📁 Vyger offers a D & D and Rogue-like environment in a graphical online roleplay game.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   */
    public Building addNewBuilding() {
       Building myBuilding = new Building();
    
       if (buildings == null) {
           buildings = new Building[1];      
           myBuilding.setBuildingID(0);
           buildings[0] = myBuilding;
       } else {
           Building[] myBuildings = new Building[buildings.length+1];
           myBuilding.setBuildingID(buildings.length);
           System.arraycopy(buildings, 0, myBuildings, 0, buildings.length);
           myBuildings[buildings.length] = myBuilding; 
           buildings = myBuildings;      
       }

       return myBuilding;
    }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** Add a new MapExit object to the array {@link #mapExits mapExits}
   *
   * @return a new MapExit object
   */
    public MapExit addMapExit(ScreenRectangle r) {
      MapExit myMapExit = new MapExit(r);
    
      if (mapExits == null) {
         mapExits = new MapExit[1];
         myMapExit.setMapExitID(0);
         mapExits[0] = myMapExit;
      } else {
         MapExit[] myMapExits = new MapExit[mapExits.length+1];
         myMapExit.setMapExitID(mapExits.length);
         System.arraycopy(mapExits, 0, myMapExits, 0, mapExits.length);
         myMapExits[mapExits.length] = myMapExit;
         mapExits = myMapExits;
      }
      return myMapExit;
    }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** Add a new MapExit object to the array {@link #mapExits mapExits}
   *
   * @param me MapExit object
   */
    public void addMapExit( MapExit me ) {
      if (mapExits == null) {
         mapExits = new MapExit[1];
         mapExits[0] = me;
      } else {
         MapExit[] myMapExits = new MapExit[mapExits.length+1];
         System.arraycopy(mapExits, 0, myMapExits, 0, mapExits.length);
         myMapExits[mapExits.length] = me;
         mapExits = myMapExits;
      }
    }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To init this town ( it rebuilds shortcuts ). DON'T CALL this method directly, use
   *  the init() method of the associated world.
   *
   * @param myWorldMap our parent WorldMap.
   */
    public void init( WorldMap myWorldMap ) {

       this.myWorldMap = myWorldMap;

    // 1 - any data ?
       if(buildings==null) {
          Debug.signal(Debug.WARNING, this, "Town has no buildings ! "+this);
          return;
       }

    // 2 - we transmit the init() call
       for( int i=0; i<buildings.length; i++ )
            if( buildings[i]!=null )
                buildings[i].init( this );

    // 3 - MapExit inits
       if( mapExits==null ) return;
       
       WotlasLocation thisLocation = new WotlasLocation( myWorldMap.getWorldMapID(),townMapID );
       
       for( int i=0; i<mapExits.length; i++ )
            mapExits[i].setMapExitLocation(thisLocation);

    }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** To init this townmap for message routing. We create an appropriate message router
   *  for the town via the provided factory.
   *
   *  Don't call this method yourself it's called via the WorldManager !
   *
   * @param msgRouterFactory our router factory
   */
    public void initMessageRouting( MessageRouterFactory msgRouterFactory, WorldManager wManager ){
       // build/get our router
          messageRouter = msgRouterFactory.createMsgRouterForTownMap( this, wManager );

       // we transmit the call to other layers
          for( int i=0; i<buildings.length; i++ )
             if( buildings[i]!=null && buildings[i].getInteriorMaps()!=null) {
             	 InteriorMap imaps[] = buildings[i].getInteriorMaps();

                 for( int j=0; j<imaps.length; j++ )
                    if(imaps[j]!=null && imaps[j].getRooms()!=null) {

                       Room rooms[] = imaps[j].getRooms();
                       
                       for( int k=0; k<rooms.length; k++ )
                            rooms[k].initMessageRouting( msgRouterFactory, wManager );
                    }
             }
    }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** Returns the MapExit which is on the side given by the specified rectangle.
   *  It's an helper for you : if your player is on a WorldMap and wants to go inside
   *  a TownMap use this method to retrieve a valid MapExit and get an insertion point.
   *
   *  The MapExit is in fact a ScreenRectangle and the so called "insertion point"
   *  should be the center of this ScreenRectangle.
   * 
   * @param rCurrent rectangle containing the player's current position, width & height
   *        the rectangle position can be anything BUT it should represent in some
   *        way the direction by which the player hits this TownMap zone.
   * @return the appropriate MapExit, null if there are no MapExits.
   */
   public MapExit findTownMapExit( Rectangle fromPosition ) {

      if(mapExits==null) {
      	// Ok, this town has no MapExit, we suppose it's just a building
      	// Is there ONE building ?
      	   if(buildings==null || buildings.length==0 || buildings[0]==null
      	      || buildings[0].getBuildingExits()==null || buildings[0].getBuildingExits().length==0) {
              Debug.signal(Debug.ERROR,this,"Failed to find town map exit !");
              return null; // nope ! error
           }

           if(fromPosition==null)
              return null; // no position to analyze

        // We search on the first map exit
           MapExit bExits[] = buildings[0].getBuildingExits();

           for(int i=0; i<bExits.length; i++ ) {
             if( bExits[i].getMapExitSide()==MapExit.WEST && fromPosition.x <= x+width/2 )
                 return bExits[i];

             if( bExits[i].getMapExitSide()==MapExit.EAST && fromPosition.x >= x+width/2 )
                 return bExits[i];

             if( bExits[i].getMapExitSide()==MapExit.NORTH && fromPosition.y <= y+height/2 )
                 return bExits[i];

             if( bExits[i].getMapExitSide()==MapExit.SOUTH && fromPosition.y >= y+height/2 )
                 return bExits[i];
           }
   
          return bExits[0]; // default
      }

      if(mapExits.length==1)
         return mapExits[0];

      for(int i=0; i<mapExits.length; i++ ) {
         if( mapExits[i].getMapExitSide()==MapExit.WEST && fromPosition.x <= x+width/2 )
             return mapExits[i];

         if( mapExits[i].getMapExitSide()==MapExit.EAST && fromPosition.x >= x+width/2 )
             return mapExits[i];

         if( mapExits[i].getMapExitSide()==MapExit.NORTH && fromPosition.y <= y+height/2 )
             return mapExits[i];

         if( mapExits[i].getMapExitSide()==MapExit.SOUTH && fromPosition.y >= y+height/2 )
             return mapExits[i];
      }
   
      return mapExits[0]; // default
   }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** Tests if the given player rectangle has its x,y cordinates in a Building.
   *
   * @param destX destination x position of the player movement ( endPoint of path )
   * @param destY destination y position of the player movement ( endPoint of path )
   * @param rCurrent rectangle containing the player's current position, width & height
   * @return the Building the player is heading to (if he has reached it, or if there
   *         are any), null if none.
   */
     public Building isEnteringBuilding( int destX, int destY, Rectangle rCurrent ) {
        if(buildings==null)
           return null;

        for( int i=0; i<buildings.length; i++ ){
             Rectangle buildRect = buildings[i].toRectangle();

             if( buildRect.contains( destX, destY ) && buildRect.intersects( rCurrent ) )
                 return buildings[i]; // building reached
        }

        return null;
     }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** Returns the eventual MapExit the given player is intersecting.
   *
   * @param rCurrent rectangle containing the player's current position, width & height
   * @return the Building the player is heading to (if he has reached it, or if there
   *         are any), null if none.
   */
     public MapExit isIntersectingMapExit( int destX, int destY, Rectangle rCurrent ) {
        if(mapExits==null)
           return null;

        for( int i=0; i<mapExits.length; i++ )
             if( mapExits[i].toRectangle().contains(destX,destY)
                 && mapExits[i].toRectangle().intersects( rCurrent ) )
                 return mapExits[i]; // mapExits reached

        return null;
     }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

  /** String Info.
   */
    public String toString(){
         return "TownMap - "+fullName;
    }

 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/

}

⌨️ 快捷键说明

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