📄 worldmovement.java
字号:
/*
* BoundingSphere.java
*/
package org.java3dgamesdk.samples.IV;
import javax.media.j3d.*;
import javax.vecmath.*;
import org.java3dgamesdk.core.*;
import org.java3dgamesdk.core.util.*;
import org.java3dgamesdk.graphics.*;
import org.java3dgamesdk.graphics.util.*;
import org.java3dgamesdk.graphics.MS3D.*;
/**
*
* @author Norbert Nopper
*/
public class WorldMovement extends GameFrame {
private MS3DModel[] building;
/**
* Variable to remember the last time.
*/
private long lastTime;
/**
* Time delat.
*/
private double delta;
private double playerSpeed = 8.0;
private double playerRotation = 0.2;
private double playerOrientation;
private double playerLook;
private double sinus;
private double cosinus;
public static void main(String[] argv) {
// create the object
WorldMovement application = new WorldMovement();
// create the object and pass the game frame to it ...
RenderSelection renderSelection = new RenderSelection(application);
// ... rest is done by the dialog
renderSelection.show();
}
/**
* The game mouse is enabled here and also the cursor is changed as well.
*/
protected void initGame() {
// enable the game mouse
gameMouse = true;
// make an invisible cursor
canvas3d.setCursor(GameMouse.CURSOR_TRANSPARENT);
MS3DLoader loader = new MS3DLoader();
building = new MS3DModel[3];
building[0] = loader.loadModel("HosShop", "hosshop.ms3d");
building[0].worldZ = -40;
building[0].scale = 0.1;
building[1] = loader.loadModel("Shop", "shop.ms3d");
building[1].worldX = -10;
building[1].worldZ = -35;
building[1].rotY = 0.78;
building[1].scale = 0.1;
building[2] = loader.loadModel("WeapShop", "weapshop.ms3d");
building[2].worldX = 10;
building[2].worldZ = -30;
building[2].rotY = -1.04;
building[2].scale = 0.1;
// add light to the scene
gc3D.addLight(new AmbientLight());
//gc3D.addLight(new DirectionalLight());
}
/**
* For this sample we do not need the method right now.
*
*@param currentTime the current game time in miliseconds, starting with 0
*/
protected void updateGame(long currentTime) {
if(gameKeys[27])
exit();
// calculate the rotation depending on the time
delta = (double)(currentTime-lastTime)/1000.0;
// rotate if the mouse was moved horizontal
playerOrientation += delta*(double)deltaX*playerRotation;
playerLook += delta*(double)deltaY*playerRotation;
sinus = Math.sin(playerOrientation)*playerSpeed*delta;
cosinus = Math.cos(playerOrientation)*playerSpeed*delta;
for(int i = 0; i < 3; i++) {
// Look Up Down
building[i].worldRotX = playerLook;
// Rotate left right
building[i].worldRotY = playerOrientation;
// Move Up
if(gameKeys[33])
building[i].worldY -= playerSpeed*delta;
// Move Down
if(gameKeys[34])
building[i].worldY += playerSpeed*delta;
// Strafe Left
if(gameKeys[37]) {
building[i].worldX += cosinus;
building[i].worldZ += sinus;
}
// Forward
if(gameKeys[38]) {
building[i].worldX -= sinus;
building[i].worldZ += cosinus;
}
/// Strafe Right
if(gameKeys[39]) {
building[i].worldX -= cosinus;
building[i].worldZ -= sinus;
}
// Backward
if(gameKeys[40]) {
building[i].worldX += sinus;
building[i].worldZ -= cosinus;
}
building[i].update(currentTime);
}
// remember the last time
lastTime = currentTime;
}
protected void renderGame() {
gc3D.clear();
for(int i = 0; i < 3; i++)
building[i].draw(gc3D);
}
/**
* Set up the old cursor.
*/
protected void exitGame() {
// go back to the default cursor
canvas3d.setCursor(GameMouse.CURSOR_DEFAULT);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -