[DEV] tutorial 18 'player movement' implementation
This commit is contained in:
parent
6cd99d61cb
commit
c8548d0f78
BIN
res/white.png
BIN
res/white.png
Binary file not shown.
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 22 KiB |
@ -12,6 +12,7 @@ import org.lwjgl.openvr.Texture;
|
||||
import entities.Camera;
|
||||
import entities.Entity;
|
||||
import entities.Light;
|
||||
import entities.Player;
|
||||
import models.RawModel;
|
||||
import models.TexturedModel;
|
||||
import renderEngine.DisplayManager;
|
||||
@ -102,6 +103,11 @@ public class MainGameLoop {
|
||||
new Vector3f(0,0,0),1.5f));
|
||||
}
|
||||
|
||||
TexturedModel stanfordBunnyModel = new TexturedModel(OBJLoader.loadObjModel("bunny", loader),
|
||||
new ModelTexture(loader.loadTexture("white")));
|
||||
|
||||
Player player = new Player(stanfordBunnyModel, new Vector3f(0,0,-50), new Vector3f(0,0,0), 1);
|
||||
|
||||
MasterRenderer renderer = new MasterRenderer();
|
||||
manager.setDrawer(new DisplayManagerDraw() {
|
||||
@Override
|
||||
@ -110,8 +116,10 @@ public class MainGameLoop {
|
||||
//entity.increaseRotation(0, 0, 0.01f);
|
||||
//entity.increaseRotation(0.01f, 0.02f, 0.0f);
|
||||
camera.move();
|
||||
player.move();
|
||||
renderer.processTerrain(terrain);
|
||||
renderer.processTerrain(terrain2);
|
||||
renderer.processEntity(player);
|
||||
for (Entity entity : entities) {
|
||||
renderer.processEntity(entity);
|
||||
}
|
||||
|
@ -14,18 +14,6 @@ public class Camera {
|
||||
|
||||
}
|
||||
public void move() {
|
||||
if (DisplayManager.isKeyDown('w')) {
|
||||
position.z -= 0.8f;
|
||||
}
|
||||
if (DisplayManager.isKeyDown('s')) {
|
||||
position.z += 0.8f;
|
||||
}
|
||||
if (DisplayManager.isKeyDown('d')) {
|
||||
position.x += 0.8f;
|
||||
}
|
||||
if (DisplayManager.isKeyDown('a')) {
|
||||
position.x -= 0.8f;
|
||||
}
|
||||
if (DisplayManager.isKeyDown('q')) {
|
||||
position.y += 0.8f;
|
||||
}
|
||||
|
94
src/entities/Player.java
Normal file
94
src/entities/Player.java
Normal file
@ -0,0 +1,94 @@
|
||||
package entities;
|
||||
|
||||
import javax.vecmath.Vector3f;
|
||||
|
||||
import models.TexturedModel;
|
||||
import renderEngine.DisplayManager;
|
||||
|
||||
public class Player extends Entity {
|
||||
|
||||
private static final float RUN_SPEED = 20;
|
||||
private static final float TRUN_SPEED = (float) Math.toRadians(160);
|
||||
private static final float GRAVITY = -50;
|
||||
private static final float JUMP_POWER = 30;
|
||||
|
||||
private static final float TERRAIN_HEIGHT = 0;
|
||||
|
||||
private float currentSpeed = 0;
|
||||
private float currentTurnSpeed = 0;
|
||||
private float upwardSpeed = 0;
|
||||
|
||||
private boolean isInAir = false;
|
||||
|
||||
public Player(TexturedModel model, Vector3f position, Vector3f rotation, float scale) {
|
||||
super(model, position, rotation, scale);
|
||||
|
||||
}
|
||||
|
||||
public void move() {
|
||||
checkInputs();
|
||||
if (isInAir == false) {
|
||||
super.increaseRotation(0, this.currentTurnSpeed * DisplayManager.getFrameTimeSecconds(), 0);
|
||||
}
|
||||
float distance = currentSpeed * DisplayManager.getFrameTimeSecconds();
|
||||
float dx = (float) (distance * Math.sin(super.getRotation().y));
|
||||
float dz = (float) (distance * Math.cos(super.getRotation().y));
|
||||
super.increasePosition(dx, 0, dz);
|
||||
upwardSpeed += GRAVITY * DisplayManager.getFrameTimeSecconds();
|
||||
super.increasePosition(0, upwardSpeed * DisplayManager.getFrameTimeSecconds(), 0);
|
||||
if (super.getPosition().y < TERRAIN_HEIGHT) {
|
||||
upwardSpeed = 0;
|
||||
super.getPosition().y = TERRAIN_HEIGHT;
|
||||
isInAir = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void jump() {
|
||||
if (isInAir == true) {
|
||||
return;
|
||||
}
|
||||
this.upwardSpeed = JUMP_POWER;
|
||||
isInAir = true;
|
||||
}
|
||||
|
||||
public void checkInputs() {
|
||||
if (DisplayManager.isKeyDown('w') && DisplayManager.isKeyDown('s')) {
|
||||
this.currentSpeed = 0;
|
||||
} else if (DisplayManager.isKeyDown('w')) {
|
||||
this.currentSpeed = RUN_SPEED;
|
||||
} else if (DisplayManager.isKeyDown('s')) {
|
||||
this.currentSpeed = -RUN_SPEED;
|
||||
} else {
|
||||
this.currentSpeed = 0;
|
||||
}
|
||||
if (DisplayManager.isKeyDown('d') && DisplayManager.isKeyDown('a')) {
|
||||
this.currentTurnSpeed = 0;
|
||||
} else if (DisplayManager.isKeyDown('a')) {
|
||||
this.currentTurnSpeed = TRUN_SPEED;
|
||||
} else if (DisplayManager.isKeyDown('d')) {
|
||||
this.currentTurnSpeed = -TRUN_SPEED;
|
||||
} else {
|
||||
this.currentTurnSpeed = 0;
|
||||
}
|
||||
if (DisplayManager.isKeyDown(' ')) {
|
||||
this.jump();
|
||||
}
|
||||
//System.out.println("position = " + super.getPosition());
|
||||
/*
|
||||
if (DisplayManager.isKeyDown('d')) {
|
||||
position.x += 0.8f;
|
||||
}
|
||||
if (DisplayManager.isKeyDown('a')) {
|
||||
position.x -= 0.8f;
|
||||
}
|
||||
if (DisplayManager.isKeyDown('q')) {
|
||||
position.y += 0.8f;
|
||||
}
|
||||
if (DisplayManager.isKeyDown('z')) {
|
||||
position.y -= 0.8f;
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -26,48 +26,15 @@ public class DisplayManager {
|
||||
private static final int WIDTH = 1280;
|
||||
private static final int HEIGHT = 720;
|
||||
private static final int FPS_CAP = 60;
|
||||
private static final String TITLE = "Our First Display";
|
||||
private static final String TITLE = "Open GL tutorial";
|
||||
|
||||
private static long lastFrameTime;
|
||||
private static float delta;
|
||||
|
||||
private DisplayManagerDraw drawer = null;
|
||||
public void setDrawer(DisplayManagerDraw drawer) {
|
||||
this.drawer = drawer;
|
||||
}
|
||||
// /**
|
||||
// * Creates a display window on which we can render our game. The dimensions
|
||||
// * of the window are determined by setting the display mode. By using
|
||||
// * "glViewport" we tell OpenGL which part of the window we want to render
|
||||
// * our game onto. We indicated that we want to use the entire window.
|
||||
// */
|
||||
// public static void createDisplay() {
|
||||
// ContextAttribs attribs = new ContextAttribs(3, 2).withForwardCompatible(true).withProfileCore(true);
|
||||
// try {
|
||||
// Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
|
||||
// Display.create(new PixelFormat(), attribs);
|
||||
// Display.setTitle(TITLE);
|
||||
// } catch (LWJGLException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// GL11.glViewport(0, 0, WIDTH, HEIGHT);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * This method is used to update the display at the end of every frame. When
|
||||
// * we have set up a rendering process this method will display whatever
|
||||
// * we've been rendering onto the screen. The "sync" method is used here to
|
||||
// * cap the frame rate. Without this the computer would just try to run the
|
||||
// * game as fast as it possibly can, doing more work than it needs to.
|
||||
// */
|
||||
// public static void updateDisplay() {
|
||||
// Display.sync(FPS_CAP);
|
||||
// Display.update();
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * This closes the window when the game is closed.
|
||||
// */
|
||||
// public static void closeDisplay() {
|
||||
// Display.destroy();
|
||||
// }
|
||||
|
||||
// The window handle
|
||||
private static long window;
|
||||
|
||||
@ -106,6 +73,7 @@ public class DisplayManager {
|
||||
private static boolean valueD = false;
|
||||
private static boolean valueA = false;
|
||||
private static boolean valueW = false;
|
||||
private static boolean valueSPACE = false;
|
||||
public static boolean isKeyDown(char value) {
|
||||
if (value == 's') {
|
||||
return valueS;
|
||||
@ -125,6 +93,9 @@ public class DisplayManager {
|
||||
if (value == 'a') {
|
||||
return valueA;
|
||||
}
|
||||
if (value == ' ') {
|
||||
return valueSPACE;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -206,6 +177,15 @@ public class DisplayManager {
|
||||
valueW = false;
|
||||
}
|
||||
}
|
||||
if ( key == GLFW_KEY_SPACE ) {
|
||||
if (action == GLFW_PRESS ) {
|
||||
System.out.println("Key SPACE is pressed");
|
||||
valueSPACE = true;
|
||||
} else if (action == GLFW_RELEASE ) {
|
||||
System.out.println("Key SPACE is release");
|
||||
valueSPACE = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Get the thread stack and push a new frame
|
||||
@ -234,6 +214,9 @@ public class DisplayManager {
|
||||
|
||||
// Make the window visible
|
||||
glfwShowWindow(window);
|
||||
|
||||
lastFrameTime = getCurrentTime();
|
||||
|
||||
}
|
||||
|
||||
public void loop() {
|
||||
@ -241,6 +224,9 @@ public class DisplayManager {
|
||||
// Run the rendering loop until the user has attempted to close
|
||||
// the window or has pressed the ESCAPE key.
|
||||
while ( !glfwWindowShouldClose(window) ) {
|
||||
long currentFrameTime = getCurrentTime();
|
||||
delta = (currentFrameTime-lastFrameTime)/1000f;
|
||||
lastFrameTime = currentFrameTime;
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
|
||||
if (this.drawer != null) {
|
||||
this.drawer.draw();
|
||||
@ -252,5 +238,14 @@ public class DisplayManager {
|
||||
glfwPollEvents();
|
||||
}
|
||||
}
|
||||
|
||||
public static float getFrameTimeSecconds() {
|
||||
return delta;
|
||||
}
|
||||
|
||||
private static long getCurrentTime() {
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user