public void onAction(String name, boolean keyPressed, float tpf) {
          if (name.equals("KEY_R") && !keyPressed) {}

          if (name.equals("KEY_F10") && !keyPressed) {
            showMainMenu();
          }

          if (name.equals("LMB") && !keyPressed) {
            CollisionResults results = new CollisionResults();

            Vector2f click2d = app.getInputManager().getCursorPosition();
            Vector3f click3d =
                app.getCamera().getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f).clone();
            Vector3f dir =
                app.getCamera()
                    .getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1f)
                    .subtractLocal(click3d);
            Ray ray = new Ray(click3d, dir);

            /* Collect intersections between Ray and nodes in results list */
            resourceNodes.collideWith(ray, results);
            farmNodes.collideWith(ray, results);
            locationNodes.collideWith(ray, results);

            if (results.size() > 0) {
              CollisionResult closest = results.getClosestCollision();

              Geometry geom = closest.getGeometry();
              String geomName = geom.getName();

              if (geom.getUserData(geomName) instanceof Farm) {
                Farm farm = (Farm) geom.getUserData(geomName);
                setMessageHUD("Clicked on Farm cost: " + farm.getCost());
              }

              if (geom.getUserData(geomName) instanceof Location) {
                Location loc = (Location) geom.getUserData(geomName);
                setMessageHUD("Clicked on Location: " + loc.getName());
              }

              if (geom.getUserData(geomName) instanceof Resource) {
                Resource res = (Resource) geom.getUserData(geomName);
                Integer cost = null;

                if (res.getType() == Resource.CHICK) {
                  cost = gm.getResourceManager().getCurrentPriceChicken();
                } else if (res.getType() == Resource.LAMB) {
                  cost = gm.getResourceManager().getCurrentPriceLamb();
                } else if (res.getType() == Resource.PIGLET) {
                  cost = gm.getResourceManager().getCurrentPricePiglet();
                } else if (res.getType() == Resource.CALF) {
                  cost = gm.getResourceManager().getCurrentPriceCalf();
                }

                setMessageHUD("Clicked on " + res.getName() + " cost: " + cost);
              }
            }
          }
        }
예제 #2
0
  private Mesh[] findTargets(Node node) {
    Mesh sharedMesh = null;
    ArrayList<Mesh> animatedMeshes = new ArrayList<Mesh>();

    for (Spatial child : node.getChildren()) {
      if (!(child instanceof Geometry)) {
        continue; // could be an attachment node, ignore.
      }

      Geometry geom = (Geometry) child;

      // is this geometry using a shared mesh?
      Mesh childSharedMesh = geom.getUserData(UserData.JME_SHAREDMESH);

      if (childSharedMesh != null) {
        // Don't bother with non-animated shared meshes
        if (isMeshAnimated(childSharedMesh)) {
          // child is using shared mesh,
          // so animate the shared mesh but ignore child
          if (sharedMesh == null) {
            sharedMesh = childSharedMesh;
          } else if (sharedMesh != childSharedMesh) {
            throw new IllegalStateException("Two conflicting shared meshes for " + node);
          }
        }
      } else {
        Mesh mesh = geom.getMesh();
        if (isMeshAnimated(mesh)) {
          animatedMeshes.add(mesh);
        }
      }
    }

    if (sharedMesh != null) {
      animatedMeshes.add(sharedMesh);
    }

    return animatedMeshes.toArray(new Mesh[animatedMeshes.size()]);
  }