Пример #1
0
  public void collision(PhysicsCollisionEvent event) {
    String nodeA = event.getNodeA().getName();
    String nodeB = event.getNodeB().getName();
    if (nodeA.equals("Spaceship") || nodeB.equals("Spaceship")) {
      lives = -1;

      /*if(nodeB.equals("Sun") || nodeA.equals("Sun")) {
          lives -= 3;
      } else {
          lives--;
      }*/
      if (lives >= 0) {
        // guiNode.detachChild(livespic[lives]);
        Vector3f movement = new Vector3f(0, 0, 0);
        spaceship.getWorldRotation().mult(new Vector3f(0, 0, 10), movement);
        spaceship.getControl().setLinearVelocity(movement);
      } else {
        speed = 0.2f;
        generateExplosion(spaceship.getWorldTranslation());
        generateDebris(spaceship.getWorldTranslation());
        spaceship.getSound("Accelerate").stop();
        bap.getPhysicsSpace().remove(spaceship.getControl());
        spaceship.removeFromParent();
        lives = 0;
        guiNode.detachAllChildren();
        guiNode.detachChild(showText);
        showText.setText("GAME OVER");
        guiNode.attachChild(showText);
        guiNode.attachChild(scoreText);
        inputManager.removeListener(actionListener);
      }
    } else if (nodeA.equals("Laser") || nodeB.equals("Laser")) {
      if (nodeB.equals("Asteroid") || nodeA.equals("Asteroid")) {
        final Asteroid asteroid;
        if (nodeA.equals("Asteroid")) {
          asteroid = (Asteroid) event.getNodeA();
        } else {
          asteroid = (Asteroid) event.getNodeB();
        }
        if (asteroid.isComet()) {
          score += 500;
        } else {
          score += 100;
        }
        generateDebris(asteroid.getWorldTranslation());
        bap.getPhysicsSpace().remove(asteroid.getControl());
        asteroid.removeFromParent();
      } else {
        score -= 50;
      }
      Laser laser;
      if (nodeA.equals("Laser")) {
        laser = (Laser) event.getNodeA();
      } else {
        laser = (Laser) event.getNodeB();
      }
      generateExplosion(laser.getWorldTranslation());
      bap.getPhysicsSpace().remove(laser.getControl());
      laser.removeFromParent();
    }
  }
Пример #2
0
  @Override
  public void simpleUpdate(float tpf) {
    for (Planet planet : planets) {
      planet.getGeom().rotate(0, 0, planet.getRotationSpeed() * tpf);
      planet.getPivot().rotate(0, planet.getTranslationSpeed() * tpf, 0);
    }

    for (Spatial spatial : explosions.getChildren()) {
      ParticleEmitter explosion = (ParticleEmitter) spatial;
      if (explosion.getNumVisibleParticles() < 1) {
        explosion.removeFromParent();
      }
    }

    Vector3f rotation = new Vector3f(0, 0, 0);
    if (left) {
      rotation.addLocal(0, 1, 0);
    }
    if (right) {
      rotation.addLocal(0, -1, 0);
    }
    if (up) {
      rotation.addLocal(1, 0, 0);
    }
    if (down) {
      rotation.addLocal(-1, 0, 0);
    }
    if (leftSide) {
      rotation.addLocal(0, 0, 1);
    }
    if (rightSide) {
      rotation.addLocal(0, 0, -1);
    }

    Vector3f transformed = new Vector3f(0, 0, 0);
    spaceship.getLocalRotation().mult(rotation, transformed);
    spaceship.getControl().setAngularVelocity(transformed);

    Vector3f movement = new Vector3f(0, 0, 0);
    if (moving) {
      spaceship.getWorldRotation().mult(new Vector3f(0, 0, -6), movement);
    }
    spaceship.getControl().setLinearVelocity(movement);

    bloom.setBloomIntensity(bloom.getBloomIntensity() + (bloomDirection * tpf / 8));
    if (bloom.getBloomIntensity() > 4) {
      bloomDirection = -1;
    }
    if (bloom.getBloomIntensity() < 2) {
      bloomDirection = 1;
    }

    Vector3f direction = spaceship.getRear().getWorldTranslation().subtract(cam.getLocation());
    float magnitude = direction.length();
    if (magnitude > 0) {
      cam.setLocation(
          cam.getLocation().add(direction.normalize().mult(tpf * magnitude * magnitude / 2)));
    }
    cam.lookAt(spaceship.getFront().getWorldTranslation(), Vector3f.UNIT_Y);

    for (Spatial spatial : asteroids.getChildren()) {
      if (spatial.getWorldTranslation().subtract(Vector3f.ZERO).length() > 200) {
        spatial.removeFromParent();
      }
    }

    if (Math.random() < 0.01 && asteroids.getChildren().size() < MAX_ASTEROIDS) {
      generateRandomAsteroid();
    }

    for (Spatial spatial : lasers.getChildren()) {
      Laser laser = (Laser) spatial;
      if (laser.getWorldTranslation().subtract(Vector3f.ZERO).length() > 200) {
        bap.getPhysicsSpace().remove(laser.getControl());
        laser.removeFromParent();
        continue;
      }
      laser.move(laser.getDirection().mult(laser.getSpeed()));
    }

    scoreText.setText("Score: " + score);
  }