public void showCar() {
    for (int i = 0; i < 4; i++) {
      cars[i] = factory.getCar(car_names[i], assetManager);
      car_con[i] = cars[i].getControl(VehicleControl.class);
    }
    Camera camera = cam.clone();

    camera.setViewPort(.25f, .9f, .1f, .75f);
    carView = this.app.getRenderManager().createPostView("carview", camera);

    space.add(car_con[index]);
    dl = new DirectionalLight();
    localRootNode.addLight(dl);
    ai = new AmbientLight();
    localRootNode.addLight(ai);
    car_con[index].setPhysicsLocation(new Vector3f(0, 1, 0));
    localRootNode.attachChild(cars[index]);
    floor = assetManager.loadModel("Models/garage/garage.mesh.j3o");
    control = new RigidBodyControl(0);
    floor.addControl(control);
    control.setPhysicsLocation(Vector3f.ZERO);
    space.add(control);
    localRootNode.attachChild(floor);

    camera.setLocation(car_con[index].getPhysicsLocation().add(new Vector3f(3, 1f, 0)));
    camera.lookAt(car_con[index].getPhysicsLocation().add(new Vector3f(0, -1, 0)), Vector3f.UNIT_Y);
    dl.setDirection(camera.getDirection());

    carView.attachScene(localRootNode);
  }
 public void nextCar() {
   if (index == 3) {
     index = 0;
     localRootNode.detachChild(cars[3]);
     space.remove(car_con[3]);
     localRootNode.attachChild(cars[index]);
     space.add(car_con[index]);
   } else if (index != 3) {
     localRootNode.detachChild(cars[index]);
     space.remove(car_con[index]);
     ++index;
     localRootNode.attachChild(cars[index]);
     space.add(car_con[index]);
   }
 }
 protected void removeFromPhysicsSpace() {
   if (space == null) {
     return;
   }
   if (baseRigidBody != null) {
     space.remove(baseRigidBody);
   }
   for (Iterator<PhysicsBoneLink> it = boneLinks.values().iterator(); it.hasNext(); ) {
     PhysicsBoneLink physicsBoneLink = it.next();
     if (physicsBoneLink.joint != null) {
       space.remove(physicsBoneLink.joint);
       if (physicsBoneLink.rigidBody != null) {
         space.remove(physicsBoneLink.rigidBody);
       }
     }
   }
   added = false;
 }
 /**
  * For internal use only specific render for the ragdoll(if debugging)
  *
  * @param rm
  * @param vp
  */
 public void render(RenderManager rm, ViewPort vp) {
   if (enabled && space != null && space.getDebugManager() != null) {
     if (!debug) {
       attachDebugShape(space.getDebugManager());
     }
     for (Iterator<PhysicsBoneLink> it = boneLinks.values().iterator(); it.hasNext(); ) {
       PhysicsBoneLink physicsBoneLink = it.next();
       Spatial debugShape = physicsBoneLink.rigidBody.debugShape();
       if (debugShape != null) {
         debugShape.setLocalTranslation(
             physicsBoneLink.rigidBody.getMotionState().getWorldLocation());
         debugShape.setLocalRotation(
             physicsBoneLink.rigidBody.getMotionState().getWorldRotationQuat());
         debugShape.updateGeometricState();
         rm.renderScene(debugShape, vp);
       }
     }
   }
 }
 private void addToPhysicsSpace() {
   if (space == null) {
     return;
   }
   if (baseRigidBody != null) {
     space.add(baseRigidBody);
     added = true;
   }
   for (Iterator<PhysicsBoneLink> it = boneLinks.values().iterator(); it.hasNext(); ) {
     PhysicsBoneLink physicsBoneLink = it.next();
     if (physicsBoneLink.rigidBody != null) {
       space.add(physicsBoneLink.rigidBody);
       if (physicsBoneLink.joint != null) {
         space.add(physicsBoneLink.joint);
       }
       added = true;
     }
   }
 }
 public void startGame() {
   space.remove(car_con[index]);
   localRootNode.detachChild(cars[index]);
   localRootNode.detachChild(floor);
   carView.detachScene(localRootNode);
   stateManager.detach(this);
   gameState.setName(playerName);
   gameState.setCar(car_names[index]);
   gameState.setLevel(1, false);
   gameState.setDefeatedCars(0);
   stateManager.attach(gameState);
 }
  private void resetWallPhysics(PhysicsSpace space) {
    List<Spatial> children = ((Node) getTerrainNode().getChild("Walls")).getChildren();
    for (Spatial wallNode : children) {
      Spatial wall = ((Node) wallNode).getChild("Wall");
      wall.scale(6f);

      space.removeAll(wallNode);

      CollisionShape meshShape = CollisionShapeFactory.createMeshShape(wall);

      wall.scale(1f / 6f);
      RigidBodyControl wallPhysics = new RigidBodyControl(meshShape, 0);
      wallPhysics.setCollideWithGroups(CollisionGroups.NONE);

      wallPhysics.setFriction(0.5f);
      wall.addControl(wallPhysics);
      wall.getControl(RigidBodyControl.class).setCollisionGroup(CollisionGroups.WALLS);

      space.addAll(wall);
    }
  }
 /** Builds/rebuilds the phyiscs body when parameters have changed */
 protected void rebuildRigidBody() {
   boolean removed = false;
   if (collisionShape instanceof MeshCollisionShape && mass != 0) {
     throw new IllegalStateException("Dynamic rigidbody can not have mesh collision shape!");
   }
   if (objectId != 0) {
     if (isInWorld(objectId)) {
       PhysicsSpace.getPhysicsSpace().remove(this);
       removed = true;
     }
     Logger.getLogger(this.getClass().getName())
         .log(Level.FINE, "Clearing RigidBody {0}", Long.toHexString(objectId));
     finalizeNative(objectId);
   }
   preRebuild();
   objectId = createRigidBody(mass, motionState.getObjectId(), collisionShape.getObjectId());
   Logger.getLogger(this.getClass().getName())
       .log(Level.FINE, "Created RigidBody {0}", Long.toHexString(objectId));
   postRebuild();
   if (removed) {
     PhysicsSpace.getPhysicsSpace().add(this);
   }
 }
  public void nextScreen(String name) {
    if (name.equals("controls")) {
      nifty.gotoScreen(name);
    }
    if (name.equals("name")) {
      nifty.gotoScreen(name);
    }
    if (name.equals("start")) {
      nifty.gotoScreen(name);
      space.remove(car_con[index]);
      localRootNode.detachChild(cars[index]);
      localRootNode.removeLight(ai);
      localRootNode.removeLight(dl);
      localRootNode.detachChild(floor);
    }
    if (name.equals("settings")) {
      nifty.gotoScreen(name);
    }
    if (name.equals("credits")) {
      nifty.gotoScreen(name);
    }
    if (name.equals("startMenu")) {
      nifty.gotoScreen("start");
    }
    if (name.equals("carSelect")) {
      playerName = textfield.getText();

      if (!"".equals(playerName)) {
        nifty.gotoScreen(name);
        showCar();
      }
    }
    if (name.equals("startSettings")) {
      int res = settingsList.getFocusItemIndex();
      boolean fullScreen = true;
      boolean vSync = true;
      AppSettings settings = new AppSettings(true);
      settings.setFullscreen(fullScreen);
      settings.setVSync(vSync);
      settings.setResolution(width[res], height[res]);
      app.setSettings(settings);
      nifty.gotoScreen("start");
    }
  }
  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();
  }
Exemple #11
0
  /**
   * Initialises and sets up the camera. This should be called as soon as possible
   *
   * @param root The node to attach the camera to
   * @param physics The physics of the physics engine
   * @param cam The camera that we are going to position here
   */
  public void loadCamera(Node root, PhysicsSpace physics, Camera cam) {
    // Create a node that is at the origin
    centralNode = new Node(nodeName);
    // Create a physics control, that we can move the node with
    centralNodeControl = new RigidBodyControl(1);
    // Add the control
    centralNode.addControl(centralNodeControl);

    // Set up camera
    CameraNode camNode = new CameraNode("cam", cam);
    // The node moves the camera, instead of the camera moving the node
    camNode.setControlDir(CameraControl.ControlDirection.SpatialToCamera);
    // Move it back and up a bit
    camNode.setLocalTranslation(new Vector3f(0, -10, 20));
    // camNode.setLocalTranslation(new Vector3f(0, -10, 50));
    // Look at the origin
    camNode.lookAt(centralNode.getLocalTranslation(), Vector3f.UNIT_Z);

    // Attach the camera to the node at the origin, so that the camera
    // follows the node
    centralNode.attachChild(camNode);

    // Plane that stays at the top of the screen to stop the ball rolling
    // out of sight
    Plane upperPlane = new Plane(Vector3f.UNIT_Y.negate(), -11f);
    RigidBodyControl upperCollision = new RigidBodyControl(new PlaneCollisionShape(upperPlane), 1f);
    Node upperPlaneNode = new Node("uplane");
    upperPlaneNode.addControl(upperCollision);
    // And another plane for bottom of the screen
    Plane lowerPlane = new Plane(Vector3f.UNIT_Y, -19f);

    RigidBodyControl lowerCollision = new RigidBodyControl(new PlaneCollisionShape(lowerPlane), 1f);
    Node lowerPlaneNode = new Node("lplane");
    lowerPlaneNode.addControl(lowerCollision);
    // Put the planes into their own group so that boxes do not collide with them
    upperCollision.setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_02);
    lowerCollision.setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_02);
    // Makes the planes un-affected by forces
    upperCollision.setKinematic(true);
    lowerCollision.setKinematic(true);

    // Attach the planes to the central point that the camera looks at
    // That way we can move them both by moving the central point.
    centralNode.attachChild(upperPlaneNode);
    centralNode.attachChild(lowerPlaneNode);

    // Add some lighting
    PointLight light = new PointLight();
    light.setColor(ColorRGBA.White);
    // light.setRadius(30);
    LightControl lightControl = new LightControl(light);
    // Needs to be added to the camera, as a point in space
    camNode.addControl(lightControl);
    // And added to the root, to have an effect on everything
    root.addLight(light);

    // Finally, add the centre point to the root node
    root.attachChild(centralNode);

    // And add the planes and the centre point to the physics space
    physics.add(upperPlaneNode);
    physics.add(lowerPlaneNode);
    physics.add(centralNode);

    centralNodeControl.setGravity(Vector3f.ZERO); // No gravity
    centralNodeControl.setLinearVelocity(Vector3f.UNIT_Y); // Moves forward
  }