📄 datamanager.java
字号:
changeMapData();
resumeTickThread();
pMonitor.setProgress("Starting Game...",95);
clientScreen.show();
pMonitor.setProgress("Done...",100);
pMonitor.close();
// 5 - Welcome message
sendMessage(new WelcomeMessage());
}
/*------------------------------------------------------------------------------------*/
/** Main loop to tick the graphics director every 50ms.
*/
public void run() {
long now;
int deltaT;
int delay;
String os = System.getProperty( "os.name" );
String arch = System.getProperty( "os.arch" );
String vers = System.getProperty( "os.version" );
Debug.signal( Debug.NOTICE, this, "OS INFO :\n\nOS NAME : <"+os+">\nOS ARCH: <"+arch+">\nOS VERSION: <"+vers+">\n" );
delay = 50;
//if ( os.equals("Windows 2000") || os.equals("Windows XP") )
// delay = 40;
pauseTickThread = false;
while( true ) {
now = System.currentTimeMillis();
// Pause Thread ?
synchronized( pauseTickThreadLock ) {
if(pauseTickThread)
try{
pauseTickThreadLock.wait();
}catch(Exception e){}
}
// Tick
tick();
deltaT = (int) (System.currentTimeMillis()-now);
if (deltaT<delay)
Tools.waitTime(delay-deltaT);
}
}
/*------------------------------------------------------------------------------------*/
/** Tick Action. We propagate the tick on the players & GraphicsDirector.
*/
public void tick() {
// I - Update myPlayer's location
myMapData.locationUpdate(myPlayer);
// II - Update players drawings
synchronized(players) {
Iterator it = players.values().iterator();
while( it.hasNext() )
( (PlayerImpl) it.next() ).tick();
}
ScreenObject item = null;
synchronized(screenObjects) {
Iterator it = screenObjects.values().iterator();
while( it.hasNext() )
item = (ScreenObject) it.next();
if( item instanceof PlayerOnTheScreen
|| item instanceof ItemOnTheScreen )
item.tick();
}
if( circle!=null )
circle.tick();
// III - Graphics Director update & redraw
if( clientScreen.getState()==Frame.ICONIFIED )
Tools.waitTime(400); // we reduce our tick rate... and don't refresh the screen
else
gDirector.tick(); // game screen update
// IV - Sync Messages Execution
NetMessageBehaviour syncMessages[] = syncMessageQueue.pullMessages();
for( int i=0; i<syncMessages.length; i++)
syncMessages[i].doBehaviour( this );
}
/*------------------------------------------------------------------------------------*/
/** To pause the tick thread.
*/
private void pauseTickThread() {
synchronized( pauseTickThreadLock ) {
pauseTickThread=true;
}
}
/*------------------------------------------------------------------------------------*/
/** To resume the tick thread.
*/
private void resumeTickThread() {
synchronized( pauseTickThreadLock ) {
pauseTickThread=false;
pauseTickThreadLock.notify();
}
}
/*------------------------------------------------------------------------------------*/
/** To tell if the DataManager's tick thread is running.
* @return true if it's running, false otherwise
*/
public boolean isRunning() {
synchronized( pauseTickThreadLock ) {
if( !pauseTickThread && myPlayer!=null )
return true;
return false;
}
}
/*------------------------------------------------------------------------------------*/
/** To invoke the code of the specified message just after the current tick.
* This method can be called multiple times and is synchronized.
*/
public void invokeLater( NetMessageBehaviour msg ) {
syncMessageQueue.queueMessage( msg );
}
/*------------------------------------------------------------------------------------*/
/** To show a warning message
*/
public void showWarningMessage(String warningMsg) {
JOptionPane.showMessageDialog( clientScreen, warningMsg, "Warning message!", JOptionPane.WARNING_MESSAGE);
}
/** To ear a warning beep
*/
public void playerWarningBeep() {
SoundLibrary.getSoundPlayer().playSound("bell.wav");
}
/*------------------------------------------------------------------------------------*/
/** Called when user left-clic on JMapPanel
*/
public void onLeftClicJMapPanel(MouseEvent e) {
if(SHOW_DEBUG)
System.out.println("DataManager::onLeftClicJMapPanel");
if(updatingMapData)
return; // updating Map Location
// Menu clicked ?
if( menuManager.isVisible() )
if( !menuManager.mouseClicked( e ) )
menuManager.hide();
else
return;
// Object/Player selected ?
if( mouseSelect( e.getX(), e.getY(), true ) )
return;
// Clicked object is the game screen...
// We move the player to that location.
Rectangle screen = gDirector.getScreenRectangle();
synchronized( players ) { //!?!?!? why? : wahy syncr playERS to move playER ?
myPlayer.moveTo( new Point( e.getX() + (int)screen.getX(),
e.getY() + (int)screen.getY() ), worldManager );
}
if (SHOW_DEBUG)
System.out.println("END of DataManager::onLeftClicJMapPanel");
}
/*------------------------------------------------------------------------------------*/
/** Called when user right-clic on JMapPanel
*/
public void onRightClicJMapPanel(MouseEvent e) {
if (SHOW_DEBUG)
System.out.println("DataManager::onRightClicJMapPanel");
if( menuManager.isVisible() )
menuManager.hide();
else {
// Menu selection & display
mouseSelect( e.getX(), e.getY(), false );
if(selectedPlayer!=null)
menuManager.initContent( selectedPlayer );
// else if(selectedObject!=null)
// ADD menuManager initContent here
else
menuManager.initNoContent();
menuManager.show( new Point( e.getX(), e.getY() ) );
}
}
/*------------------------------------------------------------------------------------*/
/** Called when the mouse cursor is dragged with the left button.
* @param e mouse event
* @param dx delta x since mouse pressed
* @param dy delta y since mouse pressed
* @param finalMov movement type as describe in JMapPanel, INIT_MOUSE_MOVEMENT, etc...
*/
public void onLeftButtonDragged( MouseEvent e, int dx, int dy, byte movementType ) {
// if the player is moving we return
if(myPlayer.getMovementComposer().isMoving())
return;
double orientation = myPlayer.getMovementComposer().getOrientationAngle();
// init the rotation ?
if(movementType==JMapPanel.INIT_MOUSE_MOVEMENT) {
refOrientation = orientation;
ghostOrientation = orientation;
return;
}
if( Math.abs((double)dx/100)>3.4 )
return;
myPlayer.getMovementComposer().setOrientationAngle(refOrientation-(double)dx/100);
orientation = myPlayer.getMovementComposer().getOrientationAngle();
// send an update message ?
if( Math.abs(orientation-ghostOrientation) > 1.0
|| (movementType==JMapPanel.END_MOUSE_MOVEMENT
&& Math.abs(orientation-ghostOrientation) >= 0.05) ) {
myPlayer.getMovementComposer().rotateTo( orientation );
ghostOrientation = orientation;
}
}
/*------------------------------------------------------------------------------------*/
/** Called when the mouse cursor is moved.
* @param x mouse's x
* @param y mouse's y
*/
public void onLeftButtonMoved( int x, int y ) {
if( !menuManager.isVisible() )
return;
menuManager.mouseMoved( x, y );
}
/*------------------------------------------------------------------------------------*/
/** Called when the mouse cursor is dragged with the left button.
* @param dx delta x since mouse pressed
* @param dy delta y since mouse pressed
* @param startsNow tells if the drag movement is just about to start
*/
public void onRightButtonDragged( int dx, int dy, boolean startsNow ) {
if( !menuManager.isVisible() )
return;
menuManager.mouseDragged( dx, dy, startsNow );
}
/*------------------------------------------------------------------------------------*/
/** To select an object/player on screen via a mouse click
* @param screen game screen dimension
* @param x x position of the mouse
* @param y y position of the mouse
* @param isLeftClick true if the left button was clicked, false if it's the right.
* @return true if we processed the mouse event, false if it was not for us.
*/
public boolean mouseSelect( int x, int y, boolean isLeftClick ) {
// We search for the owner of the object
Object object = gDirector.findOwner( x, y );
// We take a look at the selected object the user clicked
// Is it a player ? a door ? an objecOnTheScreen
if( myPlayer.getLocation().isTileMap() ) {
Rectangle screen = gDirector.getScreenRectangle();
int tmpX = new Integer( (x+screen.x)/32 ).intValue();
int tmpY = new Integer( (y+screen.y)/32 ).intValue();
// ITS NOT GOOD FOR DOORS, HOWEVER I CAN USE IT FOR THE MOMENT
// THEN DOORS COMES, WE CAN GET ANOTHER.
try {
if( myPlayer.getMyTileMap().getManager().getMapMask()[tmpX][tmpY] == TileMap.TILE_NOT_FREE ) {
getClientScreen().getMapPanel().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
commandRequest = COMMAND_NOTHING;
return true;
}
} catch (Exception e) {
/*
System.out.println("Error got : look at this. The error should(?)"
+" comes out 'because empty space (out of map) was clicked(?).");
e.printStackTrace();
*/
getClientScreen().getMapPanel().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
commandRequest = COMMAND_NOTHING;
return true;
}
x = new Integer( tmpX*32 ).intValue();
y = new Integer( tmpY*32 ).intValue();
}
if( commandRequest != COMMAND_NOTHING ) {
// init vars
byte indexForMaskTarget = 0;
byte targetRange = 0;
WotlasLocation loc = new WotlasLocation(myPlayer.getLocation());
String targetKey = "";
// checking what's the target
if( object==null ) {
if (SHOW_DEBUG)
System.out.println("The ground has been clicked...");
indexForMaskTarget = UserAction.TARGET_TYPE_GROUND;
targetKey = "";
}
else if ( object instanceof ScreenObject ) {
if (SHOW_DEBUG)
System.out.println("A screenobject has been clicked...");
indexForMaskTarget = ((ScreenObject) object).getTargetType();
targetKey = ((ScreenObject) object).getPrimaryKey();
}
else {
commandRequest = COMMAND_NOTHING;
if (SHOW_DEBUG) {
System.out.println("Action aborting: wrong target class.....");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -