Ejemplo n.º 1
0
 @Override
 public void input(Window window, MouseInput mouseInput) {
   cameraInc.set(0, 0, 0);
   if (window.isKeyPressed(GLFW_KEY_W)) {
     cameraInc.z = -1;
   } else if (window.isKeyPressed(GLFW_KEY_S)) {
     cameraInc.z = 1;
   }
   if (window.isKeyPressed(GLFW_KEY_A)) {
     cameraInc.x = -1;
   } else if (window.isKeyPressed(GLFW_KEY_D)) {
     cameraInc.x = 1;
   }
   if (window.isKeyPressed(GLFW_KEY_Z)) {
     cameraInc.y = -1;
   } else if (window.isKeyPressed(GLFW_KEY_X)) {
     cameraInc.y = 1;
   }
   if (window.isKeyPressed(GLFW_KEY_LEFT)) {
     angleInc -= 0.05f;
     soundMgr.playSoundSource(Sounds.BEEP.toString());
   } else if (window.isKeyPressed(GLFW_KEY_RIGHT)) {
     angleInc += 0.05f;
     soundMgr.playSoundSource(Sounds.BEEP.toString());
   } else {
     angleInc = 0;
   }
 }
Ejemplo n.º 2
0
  private void setupSounds() throws Exception {
    SoundBuffer buffBack = new SoundBuffer("/sounds/background.ogg");
    soundMgr.addSoundBuffer(buffBack);
    SoundSource sourceBack = new SoundSource(true, true);
    sourceBack.setBuffer(buffBack.getBufferId());
    soundMgr.addSoundSource(Sounds.MUSIC.toString(), sourceBack);

    SoundBuffer buffBeep = new SoundBuffer("/sounds/beep.ogg");
    soundMgr.addSoundBuffer(buffBeep);
    SoundSource sourceBeep = new SoundSource(false, true);
    sourceBeep.setBuffer(buffBeep.getBufferId());
    soundMgr.addSoundSource(Sounds.BEEP.toString(), sourceBeep);

    SoundBuffer buffFire = new SoundBuffer("/sounds/fire.ogg");
    soundMgr.addSoundBuffer(buffFire);
    SoundSource sourceFire = new SoundSource(true, false);
    Vector3f pos = particleEmitter.getBaseParticle().getPosition();
    sourceFire.setPosition(pos);
    sourceFire.setBuffer(buffFire.getBufferId());
    soundMgr.addSoundSource(Sounds.FIRE.toString(), sourceFire);
    sourceFire.play();

    soundMgr.setListener(new SoundListener(new Vector3f(0, 0, 0)));

    sourceBack.play();
  }
Ejemplo n.º 3
0
  @Override
  public void cleanup() {
    renderer.cleanup();
    soundMgr.cleanup();

    scene.cleanup();
    if (hud != null) {
      hud.cleanup();
    }
  }
Ejemplo n.º 4
0
  @Override
  public void update(float interval, MouseInput mouseInput) {
    // Update camera based on mouse
    if (mouseInput.isRightButtonPressed()) {
      Vector2f rotVec = mouseInput.getDisplVec();
      camera.moveRotation(rotVec.x * MOUSE_SENSITIVITY, rotVec.y * MOUSE_SENSITIVITY, 0);
    }

    // Update camera position
    Vector3f prevPos = new Vector3f(camera.getPosition());
    camera.movePosition(
        cameraInc.x * CAMERA_POS_STEP,
        cameraInc.y * CAMERA_POS_STEP,
        cameraInc.z * CAMERA_POS_STEP);
    // Check if there has been a collision. If true, set the y position to
    // the maximum height
    float height = terrain != null ? terrain.getHeight(camera.getPosition()) : -Float.MAX_VALUE;
    if (camera.getPosition().y <= height) {
      camera.setPosition(prevPos.x, prevPos.y, prevPos.z);
    }

    lightAngle += angleInc;
    if (lightAngle < 0) {
      lightAngle = 0;
    } else if (lightAngle > 180) {
      lightAngle = 180;
    }
    float zValue = (float) Math.cos(Math.toRadians(lightAngle));
    float yValue = (float) Math.sin(Math.toRadians(lightAngle));
    Vector3f lightDirection = this.scene.getSceneLight().getDirectionalLight().getDirection();
    lightDirection.x = 0;
    lightDirection.y = yValue;
    lightDirection.z = zValue;
    lightDirection.normalize();

    particleEmitter.update((long) (interval * 1000));

    // Update sound listener position;
    soundMgr.updateListenerPosition(camera);
  }
Ejemplo n.º 5
0
  @Override
  public void init(Window window) throws Exception {
    renderer.init(window);
    soundMgr.init();

    scene = new Scene();

    float reflectance = 1f;

    float blockScale = 0.5f;
    float skyBoxScale = 100.0f;
    float extension = 2.0f;

    float startx = extension * (-skyBoxScale + blockScale);
    float startz = extension * (skyBoxScale - blockScale);
    float starty = -1.0f;
    float inc = blockScale * 2;

    float posx = startx;
    float posz = startz;
    float incy = 0.0f;

    BufferedImage heightMapImage =
        ImageIO.read(getClass().getResourceAsStream("/textures/heightmap.png"));
    int height = heightMapImage.getHeight();
    int width = heightMapImage.getWidth();
    int instances = height * width;
    Mesh mesh = OBJLoader.loadMesh("/models/cube.obj", instances);
    Texture texture = new Texture("/textures/terrain_textures.png", 2, 1);
    Material material = new Material(texture, reflectance);
    mesh.setMaterial(material);
    GameItem[] gameItems = new GameItem[instances];
    for (int i = 0; i < height; i++) {
      for (int j = 0; j < width; j++) {
        GameItem gameItem = new GameItem(mesh);
        gameItem.setScale(blockScale);
        int rgb = heightMapImage.getRGB(j, i);
        incy = rgb / (10 * 255 * 255);
        gameItem.setPosition(posx, starty + incy, posz);
        int textPos = Math.random() > 0.5f ? 0 : 1;
        gameItem.setTextPos(textPos);
        gameItems[i * width + j] = gameItem;

        posx += inc;
      }
      posx = startx;
      posz -= inc;
    }
    scene.setGameItems(gameItems);

    // Particles
    int maxParticles = 200;
    Vector3f particleSpeed = new Vector3f(0, 1, 0);
    particleSpeed.mul(2.5f);
    long ttl = 4000;
    long creationPeriodMillis = 300;
    float range = 0.2f;
    float scale = 0.2f;
    Mesh partMesh = OBJLoader.loadMesh("/models/particle.obj", maxParticles);
    Texture particleTexture = new Texture("/textures/particle_anim.png", 4, 4);
    Material partMaterial = new Material(particleTexture, reflectance);
    partMesh.setMaterial(partMaterial);
    Particle particle = new Particle(partMesh, particleSpeed, ttl, 100);
    particle.setScale(scale);
    particleEmitter = new FlowParticleEmitter(particle, maxParticles, creationPeriodMillis);
    particleEmitter.setActive(true);
    particleEmitter.setPositionRndRange(range);
    particleEmitter.setSpeedRndRange(range);
    particleEmitter.setAnimRange(10);
    this.scene.setParticleEmitters(new FlowParticleEmitter[] {particleEmitter});

    // Shadows
    scene.setRenderShadows(false);

    // Fog
    Vector3f fogColour = new Vector3f(0.5f, 0.5f, 0.5f);
    scene.setFog(new Fog(true, fogColour, 0.02f));

    // Setup  SkyBox
    SkyBox skyBox = new SkyBox("/models/skybox.obj", new Vector3f(0.65f, 0.65f, 0.65f));
    skyBox.setScale(skyBoxScale);
    scene.setSkyBox(skyBox);

    // Setup Lights
    setupLights();

    camera.getPosition().x = 0.25f;
    camera.getPosition().y = 6.5f;
    camera.getPosition().z = 6.5f;
    camera.getRotation().x = 25;
    camera.getRotation().y = -1;

    hud = new Hud("DEMO");

    // Sounds
    this.soundMgr.init();
    this.soundMgr.setAttenuationModel(AL11.AL_EXPONENT_DISTANCE);
    setupSounds();
  }