Package ponkOut

Source Code of ponkOut.GameState

/* Copyright 2010-2013 Christian Matt
*
* This file is part of PonkOut.
*
* PonkOut is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PonkOut is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PonkOut.  If not, see <http://www.gnu.org/licenses/>.
*/

package ponkOut;

import java.util.LinkedList;

import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector3f;

import ponkOut.Menu.MenuState;
import ponkOut.Menu.MenuState.FlyingDirection;
import ponkOut.graphics.Board;
import ponkOut.graphics.Camera;
import ponkOut.graphics.GameGraphicsObjectsManager;
import ponkOut.graphics.GraphicsObjectsManager;
import ponkOut.graphics.HUDText;
import ponkOut.graphics.Lighting;
import ponkOut.graphics.Satellite;
import ponkOut.input.InputManager;
import ponkOut.logic.EntityManager;

public class GameState extends State {
  private EntityManager entityManager;
  private GraphicsObjectsManager goManager;

  private LinkedList<Satellite> satellites;

  private boolean paused;
  private HUDText pauseText;
  private HUDText anyKeyText;

  private MenuState.FlyingDirection flying;
  private float posZ;
  private float destPosZ;

  public GameState(StateChangeListener listener, State previous) {
    super(listener, previous);

    entityManager = EntityManager.getInstance();
    goManager = new GameGraphicsObjectsManager();

    paused = false;
    pauseText = new HUDText("Pause", 50.0f, 0.8f, 0.8f, 1.0f, 0.5f, false, goManager);
    anyKeyText = new HUDText("(press Escape to quit or any other key to continue)", 12.0f, 0.8f, 0.8f, 1.0f, 1.0f,
        false, goManager);
    anyKeyText.translate(0.0f, -50.0f);

    new Map(goManager);
  }

  @Override
  public void init() {
    InputManager.getInstance().initInputDevices();

    Lighting.init();

    // create satellites after initialising lighting
    satellites = new LinkedList<Satellite>();
    satellites.add(new Satellite(5.0f, 1.0f, 0.9f, 0.7f, 200.0f, 0.85f, 4.0f, 50.0f, 0.0f, 0.2f, 0.979796f, 1.6f,
        goManager));
    satellites.add(new Satellite(1.0f, 0.4f, 0.5f, 1.0f, 60.0f, 0.4f, 0.0f, 10.0f, 0.8666f, 0.5f, 0.0f, 0.6f,
        goManager));

    flying = MenuState.FlyingDirection.IN;
    posZ = 25.5f + 1000.0f;
    destPosZ = 25.5f;

    Camera.init(new Vector3f(0.0f, 0.0f, posZ));
    Board.init();
  }

  @Override
  public void update(float elapsedTime) {
    if (!paused) {
      entityManager.applyInput(elapsedTime);
      entityManager.moveEntities(elapsedTime);

      // move satellites
      for (Satellite satellite : satellites)
        satellite.move(elapsedTime);

      switch (flying) {
      case IN:
        if (posZ > destPosZ)
          posZ -= MenuState.getFlySpeed(posZ - destPosZ, 1050.0f, true) * elapsedTime;
        if (posZ <= destPosZ) {
          posZ = destPosZ;
          flying = FlyingDirection.NONE;
        }
        Camera.setPosition(new Vector3f(0.0f, 0.0f, posZ));
        break;

      case OUT:
        if (posZ < destPosZ)
          posZ += MenuState.getFlySpeed(destPosZ - posZ + 50.0f, 1050.0f, true) * elapsedTime;
        if (posZ >= destPosZ) {
          posZ = destPosZ;
          flying = FlyingDirection.NONE;
          getListener().changeState(getPrevious());
          return;
        }

        Camera.setPosition(new Vector3f(0.0f, 0.0f, posZ));
        break;

      case NONE:
        break;
      }
    }
  }

  @Override
  public void render() {
    // clear screen
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);

    // update camera position
    GL11.glLoadIdentity();
    Camera.applyTransformation();

    // draw objects
    goManager.drawScene();
  }

  @Override
  public void cleanup() {
    entityManager.clear();
  }

  @Override
  public void keyDown(int key) {
    if (key == Keyboard.KEY_ESCAPE && flying == FlyingDirection.NONE) {
      if (paused) {
        paused = false;
        pauseText.setVisibility(false);
        anyKeyText.setVisibility(false);

        flying = FlyingDirection.OUT;
        destPosZ += 1000.0f;
      } else {
        paused = true;
        pauseText.setVisibility(true);
        anyKeyText.setVisibility(true);
      }
    } else {
      if (paused) {
        paused = false;
        pauseText.setVisibility(false);
        anyKeyText.setVisibility(false);
      }
    }
  }
}
TOP

Related Classes of ponkOut.GameState

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.