Example #1
0
  public void onAnalog(String name, float value, float tpf) {
    if ((Boolean) player.getUserData("alive")) {
      if (name.equals("mousePick")) {
        // shoot Bullet
        if (System.currentTimeMillis() - bulletCooldown > 83f) {
          bulletCooldown = System.currentTimeMillis();

          Vector3f aim = getAimDirection();
          Vector3f offset = new Vector3f(aim.y / 3, -aim.x / 3, 0);

          //                    init bullet 1
          Spatial bullet = getSpatial("Bullet");
          Vector3f finalOffset = aim.add(offset).mult(30);
          Vector3f trans = player.getLocalTranslation().add(finalOffset);
          bullet.setLocalTranslation(trans);
          bullet.addControl(
              new BulletControl(
                  aim, settings.getWidth(), settings.getHeight(), particleManager, grid));
          bulletNode.attachChild(bullet);

          //                    init bullet 2
          Spatial bullet2 = getSpatial("Bullet");
          finalOffset = aim.add(offset.negate()).mult(30);
          trans = player.getLocalTranslation().add(finalOffset);
          bullet2.setLocalTranslation(trans);
          bullet2.addControl(
              new BulletControl(
                  aim, settings.getWidth(), settings.getHeight(), particleManager, grid));
          bulletNode.attachChild(bullet2);

          sound.shoot();
        }
      }
    }
  }
Example #2
0
 public void registerAllAndroidDevicesInScenario() {
   List<Spatial> devices = SpatialUtils.getSpatialsByRole(app.getRootNode(), "AndroidDevice");
   for (Spatial device : devices) {
     String id = device.getUserData("ID");
     if (id != null) {
       addDevice(id, (Node) device);
     }
   }
 }
Example #3
0
 @Override
 public void simpleUpdate(float tpf) {
   if ((Boolean) player.getUserData("alive")) {
     spawnEnemies();
     spawnBlackHoles();
     handleCollisions();
     handleGravity(tpf);
   } else if (System.currentTimeMillis() - (Long) player.getUserData("dieTime") > 4000f
       && !gameOver) {
     // spawn player
     player.setLocalTranslation(500, 500, 0);
     guiNode.attachChild(player);
     player.setUserData("alive", true);
     sound.spawn();
     grid.applyDirectedForce(new Vector3f(0, 0, 5000), player.getLocalTranslation(), 100);
   }
   grid.update(tpf);
   hud.update();
 }
  public void simpleInitApp() {
    Node scene = (Node) assetManager.loadModel("Scenes/DotScene/DotScene.scene");
    System.out.println("Scene: " + scene);

    Spatial testNode = scene.getChild("TestNode");
    System.out.println("TestNode: " + testNode);

    for (String key : testNode.getUserDataKeys()) {
      System.out.println("Property " + key + " = " + testNode.getUserData(key));
    }
  }
Example #5
0
 public void onAction(String name, boolean isPressed, float tpf) {
   if ((Boolean) player.getUserData("alive")) {
     if (name.equals("up")) {
       player.getControl(PlayerControl.class).up = isPressed;
     } else if (name.equals("down")) {
       player.getControl(PlayerControl.class).down = isPressed;
     } else if (name.equals("left")) {
       player.getControl(PlayerControl.class).left = isPressed;
     } else if (name.equals("right")) {
       player.getControl(PlayerControl.class).right = isPressed;
     }
   }
 }
  /**
   * The method loads library of a given ID from linked blender file.
   *
   * @param id the ID of the linked feature (it contains its name and blender path)
   * @return loaded feature or null if none was found
   * @throws BlenderFileException and exception is throw when problems with reading a blend file
   *     occur
   */
  @SuppressWarnings("unchecked")
  protected Object loadLibrary(Structure id) throws BlenderFileException {
    Pointer pLib = (Pointer) id.getFieldValue("lib");
    if (pLib.isNotNull()) {
      String fullName = id.getFieldValue("name").toString(); // we need full name with the prefix
      String nameOfFeatureToLoad = id.getName();
      Structure library = pLib.fetchData().get(0);
      String path = library.getFieldValue("filepath").toString();

      if (!blenderContext.getLinkedFeatures().keySet().contains(path)) {
        File file = new File(path);
        List<String> pathsToCheck = new ArrayList<String>();
        String currentPath = file.getName();
        do {
          pathsToCheck.add(currentPath);
          file = file.getParentFile();
          if (file != null) {
            currentPath = file.getName() + '/' + currentPath;
          }
        } while (file != null);

        Spatial loadedAsset = null;
        BlenderKey blenderKey = null;
        for (String p : pathsToCheck) {
          blenderKey = new BlenderKey(p);
          blenderKey.setLoadUnlinkedAssets(true);
          try {
            loadedAsset = blenderContext.getAssetManager().loadAsset(blenderKey);
            break; // break if no exception was thrown
          } catch (AssetNotFoundException e) {
            LOGGER.log(Level.FINEST, "Cannot locate linked resource at path: {0}.", p);
          }
        }

        if (loadedAsset != null) {
          Map<String, Map<String, Object>> linkedData = loadedAsset.getUserData("linkedData");
          for (Entry<String, Map<String, Object>> entry : linkedData.entrySet()) {
            String linkedDataFilePath = "this".equals(entry.getKey()) ? path : entry.getKey();

            List<Node> scenes = (List<Node>) entry.getValue().get("scenes");
            for (Node scene : scenes) {
              blenderContext.addLinkedFeature(linkedDataFilePath, "SC" + scene.getName(), scene);
            }
            List<Node> objects = (List<Node>) entry.getValue().get("objects");
            for (Node object : objects) {
              blenderContext.addLinkedFeature(linkedDataFilePath, "OB" + object.getName(), object);
            }
            List<TemporalMesh> meshes = (List<TemporalMesh>) entry.getValue().get("meshes");
            for (TemporalMesh mesh : meshes) {
              blenderContext.addLinkedFeature(linkedDataFilePath, "ME" + mesh.getName(), mesh);
            }
            List<MaterialContext> materials =
                (List<MaterialContext>) entry.getValue().get("materials");
            for (MaterialContext materialContext : materials) {
              blenderContext.addLinkedFeature(
                  linkedDataFilePath, "MA" + materialContext.getName(), materialContext);
            }
            List<Texture> textures = (List<Texture>) entry.getValue().get("textures");
            for (Texture texture : textures) {
              blenderContext.addLinkedFeature(
                  linkedDataFilePath, "TE" + texture.getName(), texture);
            }
            List<Texture> images = (List<Texture>) entry.getValue().get("images");
            for (Texture image : images) {
              blenderContext.addLinkedFeature(linkedDataFilePath, "IM" + image.getName(), image);
            }
            List<Animation> animations = (List<Animation>) entry.getValue().get("animations");
            for (Animation animation : animations) {
              blenderContext.addLinkedFeature(
                  linkedDataFilePath, "AC" + animation.getName(), animation);
            }
            List<Camera> cameras = (List<Camera>) entry.getValue().get("cameras");
            for (Camera camera : cameras) {
              blenderContext.addLinkedFeature(linkedDataFilePath, "CA" + camera.getName(), camera);
            }
            List<Light> lights = (List<Light>) entry.getValue().get("lights");
            for (Light light : lights) {
              blenderContext.addLinkedFeature(linkedDataFilePath, "LA" + light.getName(), light);
            }
            Spatial sky = (Spatial) entry.getValue().get("sky");
            if (sky != null) {
              blenderContext.addLinkedFeature(linkedDataFilePath, sky.getName(), sky);
            }
            List<Filter> filters = (List<Filter>) entry.getValue().get("filters");
            for (Filter filter : filters) {
              blenderContext.addLinkedFeature(linkedDataFilePath, filter.getName(), filter);
            }
          }
        } else {
          LOGGER.log(Level.WARNING, "No features loaded from path: {0}.", path);
        }
      }

      Object result = blenderContext.getLinkedFeature(path, fullName);
      if (result == null) {
        LOGGER.log(
            Level.WARNING,
            "Could NOT find asset named {0} in the library of path: {1}.",
            new Object[] {nameOfFeatureToLoad, path});
      } else {
        blenderContext.addLoadedFeatures(id.getOldMemoryAddress(), LoadedDataType.STRUCTURE, id);
        blenderContext.addLoadedFeatures(id.getOldMemoryAddress(), LoadedDataType.FEATURE, result);
      }
      return result;
    } else {
      LOGGER.warning("Library link points to nothing!");
    }
    return null;
  }
  private void changeEntityProperty(EntityPropertyChangeEvent event) {
    Spatial entity = event.getEntity();

    final Player player = entity.getUserData(EntityConstants.PLAYER);

    if (player != null) {
      String propertyName = event.getPropertyName();

      if (player.isLocalPlayer()) {
        if (propertyName.equals(EntityConstants.SHIELDS)) {
          long maxShields = entity.getUserData(EntityConstants.MAX_SHIELDS);
          Long currentShields = (Long) event.getPropertyValue();

          final float percentage = currentShields / (float) maxShields;

          enqueueHelper.enqueue(
              new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                  panShipStatus.setShieldsPercentage(percentage);
                  return null;
                }
              });
        } else if (propertyName.equals(EntityConstants.HIT_POINTS)) {
          long maxHitPoints = entity.getUserData(EntityConstants.MAX_HIT_POINTS);
          Long currentHitPoints = (Long) event.getPropertyValue();

          final float percentage = currentHitPoints / (float) maxHitPoints;

          enqueueHelper.enqueue(
              new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                  panShipStatus.setHitPointsPercentage(percentage);
                  return null;
                }
              });
        }
      } else if (propertyName.equals(EntityConstants.HIT_POINTS)) {
        Long currentHitPoints = (Long) event.getPropertyValue();

        if (currentHitPoints <= 0l) {
          final Node ship = player.getShip();

          enqueueHelper.enqueue(
              new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                  HUDMarkerControl markerControl = getMarkerControl(ship, player);
                  markerControl.setEnabled(false);

                  HUDArrowControl arrowControl = getArrowControl(ship, player);
                  arrowControl.setEnabled(false);

                  return null;
                }
              });
        }
      }
    }
  }
Example #8
0
 private boolean checkCollision(Spatial a, Spatial b) {
   float distance = a.getLocalTranslation().distance(b.getLocalTranslation());
   float maxDistance = (Float) a.getUserData("radius") + (Float) b.getUserData("radius");
   return distance <= maxDistance;
 }
Example #9
0
  private void handleCollisions() {
    // should the player die?
    for (int i = 0; i < enemyNode.getQuantity(); i++) {
      if ((Boolean) enemyNode.getChild(i).getUserData("active")) {
        if (checkCollision(player, enemyNode.getChild(i))) {
          killPlayer();
        }
      }
    }

    // should an enemy die?
    int i = 0;
    while (i < enemyNode.getQuantity()) {
      int j = 0;
      while (j < bulletNode.getQuantity()) {
        if (checkCollision(enemyNode.getChild(i), bulletNode.getChild(j))) {
          // add points depending on the type of enemy
          if (enemyNode.getChild(i).getName().equals("Seeker")) {
            hud.addPoints(2);
            hud.addEmp();

          } else if (enemyNode.getChild(i).getName().equals("Wanderer")) {
            hud.addPoints(1);
            hud.addEmp();
          }
          particleManager.enemyExplosion(enemyNode.getChild(i).getLocalTranslation());
          enemyNode.detachChildAt(i);
          bulletNode.detachChildAt(j);
          sound.explosion();
          break;
        }
        j++;
      }
      i++;
    }

    // is something colliding with a black hole?
    for (i = 0; i < blackHoleNode.getQuantity(); i++) {
      Spatial blackHole = blackHoleNode.getChild(i);
      if ((Boolean) blackHole.getUserData("active")) {
        // player
        if (checkCollision(player, blackHole)) {
          killPlayer();
        }

        // enemies
        int j = 0;
        while (j < enemyNode.getQuantity()) {
          if (checkCollision(enemyNode.getChild(j), blackHole)) {
            particleManager.enemyExplosion(enemyNode.getChild(j).getLocalTranslation());
            enemyNode.detachChildAt(j);
          }
          j++;
        }

        // bullets
        j = 0;
        while (j < bulletNode.getQuantity()) {
          if (checkCollision(bulletNode.getChild(j), blackHole)) {
            bulletNode.detachChildAt(j);
            blackHole.getControl(BlackHoleControl.class).wasShot();
            particleManager.blackHoleExplosion(blackHole.getLocalTranslation());
            if (blackHole.getControl(BlackHoleControl.class).isDead()) {
              blackHoleNode.detachChild(blackHole);
              sound.explosion();
            }
          }
          j++;
        }
      }
    }
  }