Пример #1
0
    public void run() {
      adjustToScreenSize =
          (float)
              Math.min(
                  jframe.getWidth(),
                  jframe.getHeight()); // used here, since you can change the screen-Size

      Matrix4f newTranslation = new Matrix4f();
      newTranslation.setIdentity();

      Matrix4f oldcTranslation = new Matrix4f();
      oldcTranslation = camera.getCameraMatrix();

      // world z-Axis-turn
      if (mouseWorldTurn != null) {
        newTranslation.mul(mouseWorldTurn);
        mouseWorldTurn.setIdentity();
      }

      // camera x-Axis-turn
      if (mouseTurn != null) {
        newTranslation.mul(mouseTurn);
        mouseTurn.setIdentity();
      }

      // camera movement
      if (keyMove != null) {
        newTranslation.mul(keyMove);
        keyMove.setIdentity();
      }

      newTranslation.mul(oldcTranslation);

      camera.setCameraMatrix(newTranslation);
      // something still appears to be wrong while turning

      // Trigger redrawing of the render window
      renderPanel.getCanvas().repaint();
    }
Пример #2
0
  /**
   * The main function opens a 3D rendering window, constructs a simple 3D scene, and starts a timer
   * task to generate an animation.
   *
   * @throws IOException
   */
  public static void main(String[] args) throws IOException {

    sceneManager = new SimpleSceneManager();

    Landscape landscape = new Landscape(7, 50, 20, 10, 5);
    // Set up the vertex data and integrate
    VertexData vLandscape = landscape.getVertexData();
    sLandscape = new Shape(vLandscape);
    sceneManager.addShape(sLandscape);

    // adjust the camera
    camera = sceneManager.getCamera();

    Vector3f centerOfProjection = new Vector3f(0, 0, 40);
    Vector3f lookAtPoint = new Vector3f(0, 0, 0);
    Vector3f upVector = new Vector3f(0, 1, 0);
    camera.setCameraMatrix(centerOfProjection, lookAtPoint, upVector);

    // adjust the frustum
    Frustum frustum = sceneManager.getFrustum();

    float nearPlane = 1;
    float farPlane = 100;
    float aspectRatio = 1;
    float vFieldOfView = 60;
    frustum.setFrustum(nearPlane, farPlane, aspectRatio, vFieldOfView);

    // Make a render panel. The init function of the renderPanel
    // (see above) will be called back for initialization.
    renderPanel = new SimpleRenderPanel();

    // Make the main window of this application and add the renderer to it
    // JFrame
    jframe = new JFrame("simple");
    jframe.setSize(750, 750);
    jframe.setLocationRelativeTo(null); // center of screen
    jframe.getContentPane().add(renderPanel.getCanvas()); // put the canvas into a JFrame window

    // Add a mouse listener
    SimpleMouseListener mouse = new SimpleMouseListener();
    renderPanel.getCanvas().addMouseListener(mouse); // change proposed in forum
    renderPanel.getCanvas().addMouseMotionListener(mouse);
    renderPanel.getCanvas().addKeyListener(new MyKeyListener());

    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jframe.setVisible(true); // show window
  }
Пример #3
0
  public void step() {
    Camera cam = cameras.get(0);
    cam.activate();

    cam.setColor(Color.black);
    tree.display(50, 90, cam, spread, levelHeight);
    // -------------------------------------------------

    cam = cameras.get(1);
    cam.activate();

    if (state.equals("add")) {
      cam.setColor(Color.black);
      cam.drawText(">>> " + stringToAdd, 5, 2);
    }
  }
Пример #4
0
  public void keyPressed(KeyEvent e) {
    int code = e.getKeyCode();

    Camera cam = cameras.get(0);

    if (state.equals("regular")) {
      if (code == KeyEvent.VK_L) {
        cam.shiftRegion(0.25, 0);
      } else if (code == KeyEvent.VK_R) {
        cam.shiftRegion(-0.25, 0);
      } else if (code == KeyEvent.VK_U) {
        cam.shiftRegion(0, -0.25);
      } else if (code == KeyEvent.VK_D) {
        cam.shiftRegion(0, 0.25);
      } else if (code == KeyEvent.VK_S) {
        cam.scaleRegion(1.1, 1.1);
      } else if (code == KeyEvent.VK_B) {
        cam.scaleRegion(1 / 1.1, 1 / 1.1);
      } else if (code == KeyEvent.VK_W) {
        spread *= 1.1;
      } else if (code == KeyEvent.VK_N) {
        spread /= 1.1;
      } else if (code == KeyEvent.VK_H) {
        cam.setRegion(0, 100, 0, 100);
      }

    } // regular state
    else if (state.equals("add")) {
      if (code == KeyEvent.VK_ENTER) {
        state = "regular";
        if (!stringToAdd.equals("")) tree.add(stringToAdd);
      } else if (code == KeyEvent.VK_DELETE || code == KeyEvent.VK_BACK_SPACE) {
        if (stringToAdd.length() > 0)
          stringToAdd = stringToAdd.substring(0, stringToAdd.length() - 1);
      }
    } // add state
  }
Пример #5
0
  public static void loadLevel(File levelFile) {

    // clean up old loads:
    loadedLevel.clean();

    if (levelFile != null) {
      GameObject[] go = new GameObject[0];

      try {
        loadedLevel = new Level(levelFile.getPath());
        camera = loadedLevel.getCamera();
        go = loadedLevel.getGameObjects();
      } catch (Exception e) {
        System.out.println(e);
      }

      // Reset numberOf ...
      numberOfBoxes = 0;
      numberOfSprites = 0;
      numberOfTiles = 0;

      for (int i = 0; i < actor.length; i++) {
        actor[i] = null;
      }

      backgroundImage = new Image[2];

      try {
        tileSheet = ImageIO.read(new File(loadedLevel.levelName + "/tilesheet.png"));
        backgroundImage[0] = ImageIO.read(new File(loadedLevel.levelName + "/bg0.png"));
        backgroundImage[1] = ImageIO.read(new File(loadedLevel.levelName + "/bg1.png"));
      } catch (Exception e) {
        System.out.println("ERROR loading images: " + e);
      }

      int MapWidth = loadedLevel.getWidth();
      int MapHeight = loadedLevel.getHeight();

      for (int y = 0; y < MapHeight; y++) {
        for (int x = 0; x < MapWidth; x++) {
          // Number entered in the position represents tileNumber;
          // position of the sprite x*16, y*16

          // get char at position X/Y in the levelLoaded string
          char CharAtXY =
              loadedLevel.level.substring(MapWidth * y, loadedLevel.level.length()).charAt(x);

          // Load objects into the engine/game
          for (int i = 0; i < go.length; i++) {
            if (CharAtXY == go[i].objectChar) {
              try {
                invoke(
                    "game.objects." + go[i].name,
                    "new" + go[i].name,
                    new Class[] {Point.class},
                    new Object[] {new Point(x * 16, y * 16)});
              } catch (Exception e) {
                System.out.println("ERROR trying to invoke method: " + e);
              }
            }
          }

          // Load tiles into engine/game
          // 48 = '0' , 57 = '9'
          if ((int) CharAtXY >= 48 && (int) CharAtXY <= 57) {
            tileObject[gameMain.numberOfTiles] = new WorldTile(Integer.parseInt(CharAtXY + ""));
            tileObject[gameMain.numberOfTiles - 1].sprite.setPosition(x * 16, y * 16);
          }
        }
      }

      // clean up:
      loadedLevel.clean();

      // additional game-specific loading options:
      camera.forceSetPosition(new Point(mario.spawn.x, camera.prefHeight));
      pCoin = new PopupCoin(new Point(-80, -80));

      levelLoaded = true;
    } else {
      System.out.println("Loading cancelled...");
    }
  }
Пример #6
0
  // -- Main Loop
  public void run() {

    // called only once:
    initialize();

    // create me a timer
    Timer t = new Timer();

    // start main loop:
    while (true) {

      if (levelLoaded == true) {

        camera.follow(mario.sprite);

        // act() all actors that are actable:
        int a = 0;
        while (actor[a] != null && actor[a] instanceof Actable) {
          Actable actable = (Actable) actor[a];
          actable.act();
          a++;
        }

        pCoin.fly();

        for (int i = 0; i < numberOfBoxes; i++) {
          try {
            box[i].open();
          } catch (Exception e) {
            System.out.println("ERROR: " + e);
          }
        }
        for (int i = 0; i < numberOfCoins; i++) {
          try {
            coin[i].collect();
          } catch (Exception e) {
            System.out.println("ERROR: " + e);
          }
        }

        // reset mario if fallen off from screen:
        if (mario.sprite.posy > loadedLevel.getHeight() * 16) {
          camera.position = new Point(width / 2, camera.prefHeight + camera.tolerance);
          mario.sprite.setPosition(new Point(gameMain.mario.spawn.x, gameMain.mario.spawn.y));
        }
      }

      try {

        // Draw to panel if not Fullscreen
        if (fullscreen == false) {

          t.start();

          render();
          repaint();

          System.out.println("FPS: " + (int) (((100 / (double) t.stop())) * 2));

        } else {

          t.start();

          long sleeptime = 5 - t.stop();

          // calculate sleep time (max fps)
          if (sleeptime < 0) {
            sleeptime = 0;
          }

          main.sleep(1L + sleeptime);

          fps = (int) (((100 / (double) t.stop())) * 2);

          System.out.println("FPS: " + fps);
        }
      } catch (Exception e) {

      }
    }
  }
Пример #7
0
 public void actionPerformed(ActionEvent e) {
   if (e.getSource() == updateButton) {
     camera.zoom = Integer.parseInt(zoomField.getText());
   }
 }