private void tickChat() { if (chat.isOpen()) { keys.release(); } if (keys.chat.wasReleased()) { chat.open(); } chat.tick(); String msg = chat.getWaitingMessage(); if (msg != null) { synchronizer.addCommand(new ChatCommand(texts.playerName(player.localTeam) + ": " + msg)); } }
@Override public void run() { long lastTime = System.nanoTime(); double unprocessed = 0; int frames = 0; long lastTimer1 = System.currentTimeMillis(); try { init(); } catch (Exception e) { e.printStackTrace(); return; } // if (!isMultiplayer) { // createLevel(); // } int toTick = 0; long lastRenderTime = System.nanoTime(); int min = 999999999; int max = 0; while (running) { if (!this.hasFocus()) { keys.release(); } double nsPerTick = 1000000000.0 / framerate; boolean shouldRender = false; while (unprocessed >= 1) { toTick++; unprocessed -= 1; } int tickCount = toTick; if (toTick > 0 && toTick < 3) { tickCount = 1; } if (toTick > 20) { toTick = 20; } for (int i = 0; i < tickCount; i++) { toTick--; // long before = System.nanoTime(); tick(); // long after = System.nanoTime(); // System.out.println("Tick time took " + (after - before) * // 100.0 / nsPerTick + "% of the max time"); shouldRender = true; } // shouldRender = true; BufferStrategy bs = getBufferStrategy(); if (bs == null) { createBufferStrategy(3); continue; } if (shouldRender) { frames++; Graphics g = bs.getDrawGraphics(); Random lastRandom = TurnSynchronizer.synchedRandom; TurnSynchronizer.synchedRandom = null; render(g); TurnSynchronizer.synchedRandom = lastRandom; long renderTime = System.nanoTime(); int timePassed = (int) (renderTime - lastRenderTime); if (timePassed < min) { min = timePassed; } if (timePassed > max) { max = timePassed; } lastRenderTime = renderTime; } long now = System.nanoTime(); unprocessed += (now - lastTime) / nsPerTick; lastTime = now; try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } if (shouldRender) { if (bs != null) { bs.show(); } } if (System.currentTimeMillis() - lastTimer1 > 1000) { lastTimer1 += 1000; fps = frames; frames = 0; } } }
private void tick() { // Not-In-Focus-Pause if (this.isFocusOwner() && level != null) { paused2 = false; } if (!this.isFocusOwner() && level != null) { keys.release(); mouseButtons.releaseAll(); if (!paused && !paused2) { PauseCommand pauseCommand = new PauseCommand(true); synchronizer.addCommand(pauseCommand); paused2 = true; } } if (isMultiplayer) { tickChat(); } if (requestToggleFullscreen || keys.fullscreen.wasPressed()) { requestToggleFullscreen = false; setFullscreen(!fullscreen); } if (level != null && level.victoryConditions != null) { if (level.victoryConditions.isVictoryConditionAchieved()) { addMenu(new WinMenu(GAME_WIDTH, GAME_HEIGHT, level.victoryConditions.playerVictorious())); level = null; return; } } if (packetLink != null) { packetLink.tick(); } mouseButtons.setPosition(getMousePosition()); if (!menuStack.isEmpty()) { menuStack.peek().tick(mouseButtons); } if (mouseMoved) { mouseMoved = false; mouseHideTime = 0; if (mouseButtons.mouseHidden) { mouseButtons.mouseHidden = false; } } if (mouseHideTime < 60) { mouseHideTime++; if (mouseHideTime == 60) { mouseButtons.mouseHidden = true; } } if (level == null) { mouseButtons.tick(); } else if (level != null) { if (synchronizer.preTurn()) { synchronizer.postTurn(); for (int index = 0; index < mouseButtons.currentState.length; index++) { boolean nextState = mouseButtons.nextState[index]; if (mouseButtons.isDown(index) != nextState) { synchronizer.addCommand(new ChangeMouseButtonCommand(index, nextState)); } } synchronizer.addCommand( new ChangeMouseCoordinateCommand( mouseButtons.getX(), mouseButtons.getY(), mouseButtons.mouseHidden)); mouseButtons.tick(); for (MouseButtons sMouseButtons : synchedMouseButtons) { sMouseButtons.tick(); } if (!paused) { for (int index = 0; index < keys.getAll().size(); index++) { Keys.Key key = keys.getAll().get(index); boolean nextState = key.nextState; if (key.isDown != nextState) { synchronizer.addCommand(new ChangeKeyCommand(index, nextState)); } } keys.tick(); for (Keys skeys : synchedKeys) { skeys.tick(); } if (keys.pause.wasPressed()) { keys.release(); mouseButtons.releaseAll(); synchronizer.addCommand(new PauseCommand(true)); } level.tick(); } // every 4 minutes, start new background music :) if (System.currentTimeMillis() / 1000 > nextMusicInterval) { nextMusicInterval = (System.currentTimeMillis() / 1000) + 4 * 60; soundPlayer.startBackgroundMusic(); } if (keys.screenShot.isDown) { takeScreenShot(); } } } if (createServerState == 1) { createServerState = 2; synchronizer = new TurnSynchronizer(MojamComponent.this, packetLink, localId, 2); clearMenus(); createLevel(TitleMenu.level, TitleMenu.defaultGameMode); synchronizer.setStarted(true); if (TitleMenu.level.vanilla) { packetLink.sendPacket( new StartGamePacket( TurnSynchronizer.synchedSeed, TitleMenu.level.getUniversalPath(), DifficultyList.getDifficultyID(TitleMenu.difficulty))); } else { packetLink.sendPacket( new StartGamePacketCustom( TurnSynchronizer.synchedSeed, level, DifficultyList.getDifficultyID(TitleMenu.difficulty))); } packetLink.setPacketListener(MojamComponent.this); } }
private void tick() { if (packetLink != null) { packetLink.tick(); } mouseButtons.setPosition(getMousePosition()); if (!menuStack.isEmpty()) { menuStack.peek().tick(mouseButtons); } if (mouseMoved) { mouseMoved = false; mouseHideTime = 0; if (mouseHidden) { mouseHidden = false; } } if (mouseHideTime < 60) { mouseHideTime++; if (mouseHideTime == 60) { mouseHidden = true; } } mouseButtons.tick(); if (level != null) { if (synchronizer.preTurn()) { synchronizer.postTurn(); if (!paused) { for (int index = 0; index < keys.getAll().size(); index++) { Keys.Key key = keys.getAll().get(index); boolean nextState = key.nextState; if (key.isDown != nextState) { synchronizer.addCommand(new ChangeKeyCommand(index, nextState)); } } keys.tick(); for (Keys skeys : synchedKeys) { skeys.tick(); } if (keys.pause.wasPressed()) { keys.release(); synchronizer.addCommand(new PauseCommand(true)); } if (keys.fullscreen.wasPressed()) { setFullscreen(!fullscreen); } // if mouse is in use, update player orientation before level tick if (!mouseHidden) { // update player mouse, in world pixels relative to // player player.setAimByMouse( ((mouseButtons.getX() / SCALE) - (screen.w / 2)), (((mouseButtons.getY() / SCALE) + 24) - (screen.h / 2))); } else { player.setAimByKeyboard(); } level.tick(); } // every 4 minutes, start new background music :) if (System.currentTimeMillis() / 1000 > nextMusicInterval) { nextMusicInterval = (System.currentTimeMillis() / 1000) + 4 * 60; soundPlayer.startBackgroundMusic(); } if (keys.screenShot.isDown) { takeScreenShot(); } } } if (createServerState == 1) { createServerState = 2; synchronizer = new TurnSynchronizer(MojamComponent.this, packetLink, localId, 2); clearMenus(); createLevel(TitleMenu.level); synchronizer.setStarted(true); if (TitleMenu.level.vanilla) { packetLink.sendPacket( new StartGamePacket( TurnSynchronizer.synchedSeed, TitleMenu.level.getUniversalPath(), DifficultyList.getDifficultyID(TitleMenu.difficulty))); } else { packetLink.sendPacket( new StartGamePacketCustom( TurnSynchronizer.synchedSeed, level, DifficultyList.getDifficultyID(TitleMenu.difficulty))); } packetLink.setPacketListener(MojamComponent.this); } }