📄 room.java
字号:
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 get the wotlas location associated to this Map.
* @return associated Wotlas Location
*/
public WotlasLocation getLocation() {
if(thisLocation==null) {
thisLocation = new WotlasLocation();
thisLocation.setRoomID( roomID );
thisLocation.setInteriorMapID( myInteriorMap.getInteriorMapID() );
thisLocation.setBuildingID( myInteriorMap.getMyBuilding().getBuildingID() );
thisLocation.setTownMapID( myInteriorMap.getMyBuilding().getMyTownMap().getTownMapID() );
thisLocation.setWorldMapID( myInteriorMap.getMyBuilding().getMyTownMap().getMyWorldMap().getWorldMapID() );
}
return thisLocation;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To init this room ( it rebuilds shortcuts ). DON'T CALL this method directly,
* use the init() method of the associated world.
*
* @param myInteriorMap our father InteriorMap
*/
public void init( InteriorMap myInteriorMap ){
// 1 - inits
this.myInteriorMap = myInteriorMap;
thisLocation = getLocation();
// 2 - We reconstruct MapExit links...
if( mapExits != null )
for( int i=0; i<mapExits.length; i++ )
mapExits[i].setMapExitLocation(thisLocation);
// 3 - We reconstruct RoomLinks links...
if(roomLinks==null)
return;
for( int i=0; i<roomLinks.length; i++ ) {
if(roomLinks[i].getRoom1()!=null)
continue; // already done
if(roomLinks[i].getDoor()!=null) {
roomLinks[i].getDoor().setMyRoomLinkID( roomLinks[i].getRoomLinkID() );
roomLinks[i].getDoor().setMyRoomID( roomID );
}
Room other = null;
if( roomLinks[i].getRoom1ID()==roomID ) {
roomLinks[i].setRoom1( this );
other = myInteriorMap.getRoomFromID( roomLinks[i].getRoom2ID() );
roomLinks[i].setRoom2( other );
}
else if( roomLinks[i].getRoom2ID()==roomID ) {
roomLinks[i].setRoom2( this );
other = myInteriorMap.getRoomFromID( roomLinks[i].getRoom1ID() );
roomLinks[i].setRoom1( other );
}
else
Debug.signal( Debug.ERROR, this, "BAD ROOMLINK DETECTED : "+thisLocation );
// RoomLink - double detection
RoomLink otherLinks[] = other.getRoomLinks();
if(otherLinks==null) {
Debug.signal( Debug.ERROR, this, "BAD ROOMLINK DETECTED : "+thisLocation );
continue;
}
for( int j=0; j<otherLinks.length; j++ )
if( roomLinks[i].equals(otherLinks[j]) && otherLinks[j].getRoom1()==null ) {
otherLinks[j] = roomLinks[i];
break;
}
}
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To init this room for message routing. We create an appropriate message router
* for the room via the provided factory.
*
* Don't call this method yourself it's called from the WorldManager !
*
* @param msgRouterFactory our router factory
*/
public void initMessageRouting( MessageRouterFactory msgRouterFactory, WorldManager wManager ){
// build/get our router
messageRouter = msgRouterFactory.createMsgRouterForRoom( this, wManager );
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Returns the eventual RoomLink 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 RoomLink isIntersectingRoomLink( Rectangle rCurrent ) {
if(roomLinks==null)
return null;
for( int i=0; i<roomLinks.length; i++ )
if( roomLinks[i].toRectangle().intersects( rCurrent ) )
return roomLinks[i]; // RoomLink 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().contains(destX+rCurrent.width/2,destY+rCurrent.height/2)
|| mapExits[i].toRectangle().contains(destX+rCurrent.width,destY+rCurrent.height) )
&& mapExits[i].toRectangle().intersects( rCurrent ) )
return mapExits[i]; // mapExits reached
return null;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** Returns the RoomID of the RoomLink's target Room if the given current player
* Rectangle is proved to be in this other room. We return -1 if we are still in our
* room.
*
* You should call this method
*
* @param rlink a RoomLink the player has intersected recently
* @param rCurrent current player Rectangle.
* @return -1 if the player is still in this Room, the other RoomID if he has moved to
* the other Room pointed by the given RoomLink.
*/
public int isInOtherRoom( RoomLink rlink, Rectangle rCurrent ) {
if( rlink.width < rlink.height ) {
if( rlink.getRoom1ID()==roomID ){
// ok, we are the west Room
if( rlink.x < rCurrent.x+rCurrent.width/2 )
return rlink.getRoom2ID(); // we are in the other room
}
else {
// ok, we are the east Room
if( rCurrent.x+rCurrent.width/2 < rlink.x + rlink.width )
return rlink.getRoom1ID(); // we are in the other room
}
}
else if( rlink.width > rlink.height ) {
if( rlink.getRoom1ID()==roomID ){
// ok, we are the north Room
if( rlink.y <= rCurrent.y )
return rlink.getRoom2ID(); // we are in the other room
}
else {
// ok, we are the south Room
if( rCurrent.y +rCurrent.height <= rlink.y + rlink.height)
return rlink.getRoom1ID(); // we are in the other room
}
}
return -1; // we are still in this room
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** To get a list of the doors of this room.
* @return array of Doors ( never returns null ).
*/
public Door[] getDoors(){
if(roomLinks==null)
return new Door[0];
// 1 - how many doors are there ?
int nb = 0;
for( int i=0; i<roomLinks.length; i++ )
if( roomLinks[i].getDoor()!=null ) nb++;
// 2 - Create our array
Door doors[] = new Door[nb];
if(nb==0) return doors;
nb=0;
for( int i=0; i<roomLinks.length; i++ )
if( roomLinks[i].getDoor()!=null ) {
doors[nb] = roomLinks[i].getDoor();
nb++;
}
return doors;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
/** String Info.
*/
public String toString(){
return "Room - "+fullName;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -