예제 #1
0
  private void initEventBus() {
    eventBus.addHandler(
        UsernameSelectedEvent.TYPE,
        new UsernameSelectedHandler() {
          @Override
          public void onUsernameSelected(UsernameSelectedEvent event) {
            try {
              ClientConfig.setUsername(event.getUsername());
            } catch (IOException e) {
              // TODO
              e.printStackTrace();
            }

            stateManager.detach(usernameInputState);
            menuState.setEnabled(true);
          }
        });

    eventBus.addHandler(
        QuitEvent.TYPE,
        new QuitHandler() {
          @Override
          public void onQuit(QuitEvent e) {
            stopClient();
          }
        });

    eventBus.addHandler(
        CloseGameEvent.TYPE,
        new CloseGameHandler() {
          @Override
          public void onCloseGame(CloseGameEvent event) {
            closeGame(false);
          }
        });

    eventBus.addHandler(
        LoadGameEvent.TYPE,
        new LoadGameHandler() {
          @Override
          public void onLoadGame(final LoadGameEvent event) {
            enqueueHelper.enqueue(
                new Callable<Void>() {
                  @Override
                  public Void call() throws Exception {
                    loadGame(event.getData());
                    return null;
                  }
                });
          }
        });

    eventBus.addHandler(
        GameStateResponseEvent.TYPE,
        new GameStateResponseHandler() {
          @Override
          public void onGameStateResponse(GameStateResponseEvent event) {
            processGameState(event.getMessage());
          }
        });

    eventBus.addHandler(
        ToggleMainMenuEvent.TYPE,
        new ToggleMainMenuHandler() {
          @Override
          public void onChange(ToggleMainMenuEvent event) {
            if (localPlayer != null) {
              if (menuState.isEnabled()) {
                hudState.setEnabled(hudStateWasEnabled);
                spawnMenuState.setEnabled(!hudStateWasEnabled);
                menuState.setEnabled(false);
                ingameState.setEnabled(true);
              } else {
                hudStateWasEnabled = hudState.isEnabled();

                hudState.setEnabled(false);
                spawnMenuState.setEnabled(false);
                menuState.setEnabled(true);
                ingameState.setEnabled(false);
              }
            }
          }
        });

    eventBus.addHandler(
        ToggleConsoleEvent.TYPE,
        new ToggleConsoleHandler() {
          @Override
          public void onToggleConsole(ToggleConsoleEvent event) {
            consoleState.setEnabled(!consoleState.isEnabled());
          }
        });

    eventBus.addHandler(
        ToggleSpawnMenuEvent.TYPE,
        new ToggleSpawnMenuHandler() {
          @Override
          public void onChange(ToggleSpawnMenuEvent event) {
            if (ingameState.isEnabled() && endState == null) {
              hudState.setEnabled(!hudState.isEnabled());
              spawnMenuState.setEnabled(!spawnMenuState.isEnabled());
            }
          }
        });

    eventBus.addHandler(
        RespawnPlayerEvent.TYPE,
        new RespawnPlayerHandler() {
          @Override
          public void onRespawnPlayer(RespawnPlayerEvent event) {
            event.getPlayer().setRespawned();
          }
        });

    eventBus.addHandler(
        WinConditionReachedEvent.TYPE,
        new WinConditionReachedHandler() {
          @Override
          public void onWinConditionReached(WinConditionReachedEvent event) {
            endState = new EndStateImpl(screen, rootNode, guiNode);
            endState.setEnabled(true);
            stateManager.attach(endState);
            hudState.setEnabled(false);
            spawnMenuState.setEnabled(false);
          }
        });

    eventBus.addHandler(
        ToggleScoreBoardEvent.TYPE,
        new ToggleScoreBoardHandler() {
          @Override
          public void onToggleScoreBoard(ToggleScoreBoardEvent event) {
            if (endState == null && !consoleState.isEnabled()) {
              scoreBoardState.setEnabled(event.isShow());
            }
          }
        });

    eventBus.addHandler(
        RestartGameEvent.TYPE,
        new RestartGameHandler() {
          @Override
          public void onRestartGame(RestartGameEvent event) {
            closeGame(true);
          }
        });

    eventBus.addHandler(
        ClientConfigChangedEvent.TYPE,
        new ClientConfigChangedHandler() {
          @Override
          public void onChanged(ClientConfigChangedEvent event) {
            onUpdateClientConfig(event);
          }
        });

    eventBus.addHandler(
        UpdateDebugOptionEvent.TYPE,
        new UpdateDebugOptionHandler() {
          @Override
          public void onUpdate(UpdateDebugOptionEvent event) {
            if (enableDebug) {
              clientState.sendMessage(
                  new DebugConfigChangeRequestMessage(event.getDebugCommandKey()));
            }
          }
        });
  }
예제 #2
0
  public void init(boolean clientConfigExisted) throws Exception {
    stateManager.detach(stateManager.getState(FlyCamAppState.class));
    stateManager.detach(stateManager.getState(StatsAppState.class));

    guiNode.detachAllChildren();

    guiNode.addControl(screen);

    consoleState = new ConsoleStateImpl(screen, rootNode, guiNode);
    stateManager.attach(consoleState);
    consoleState.setEnabled(false);

    menuState = new MenuStateImpl(screen, rootNode, guiNode);
    stateManager.attach(menuState);

    if (clientConfigExisted) {
      menuState.setEnabled(true);
    } else {
      usernameInputState = new UsernameInputStateImpl(screen, rootNode, guiNode);
      stateManager.attach(usernameInputState);
      usernameInputState.setEnabled(true);
      menuState.setEnabled(false);
    }

    playersContainer = new ClientPlayersContainer();
    entityIdProvider = new ClientEntityIdProvider();
    entityFactory = new ClientEntityFactory(assetManager, entityIdProvider);

    final ClientFactory clientFactory =
        new ClientFactory() {
          @Override
          public NetworkClient createClient() {
            return Network.createClient();
          }
        };

    clientState = new ClientStateImpl(clientFactory);
    syncState = new ClientSyncState(enqueueHelper, clientState, playersContainer);

    FPSState fpsState = new FPSStateImpl(screen, rootNode, guiNode);
    stateManager.attach(fpsState);
    fpsState.setEnabled(true);

    ingameState = new ClientIngameStateImpl(rootNode, guiNode);
    stateManager.attach(ingameState);
    ingameState.setEnabled(false);
    stateManager.attach(syncState);
    syncState.setEnabled(false);

    bulletAppState = new BulletAppState();
    bulletAppState.setThreadingType(ThreadingType.SEQUENTIAL);
    stateManager.attach(bulletAppState);
    bulletAppState.setEnabled(false);

    PhysicsSpace physicsSpace = bulletAppState.getPhysicsSpace();
    physicsSpace.setGravity(new Vector3f());

    physicsWrapper = new PhysicsWrapperImpl();
    physicsWrapper.setPhysicsSpace(physicsSpace);

    entityAttacher = new EntityAttacher(ingameState.getRootNode(), syncState, physicsWrapper);

    teamsContainer = new TeamsContainer();

    dataContainer =
        new ClientDataContainer(
            entityFactory,
            teamsContainer,
            enqueueHelper,
            entityAttacher,
            clientState,
            playersContainer,
            syncState);
    clientMessageListener = new ClientMessageListener(dataContainer);
    setupNetwork();
    stateManager.attach(clientState);

    respawnLocalPlayerHandler = new RespawnLocalPlayerHandler(cam);
    eventBus.addHandler(RespawnPlayerEvent.TYPE, respawnLocalPlayerHandler);

    initInput();

    initEventBus();
  }