Browse Source

Car test and natives exporting

Added simple car physics.
Moved natives exporting to its own class.
Tankernn 8 years ago
parent
commit
122b068eb1

+ 16 - 9
.classpath

@@ -1,24 +1,31 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <classpath>
-	<classpathentry kind="src" path="res"/>
-	<classpathentry kind="src" path="src/main/java">
+	<classpathentry excluding="**" kind="src" output="target/classes" path="res">
 		<attributes>
-			<attribute name="optional" value="true"/>
 			<attribute name="maven.pomderived" value="true"/>
 		</attributes>
 	</classpathentry>
-	<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/LWJGL">
+	<classpathentry including="**/*.java" kind="src" output="target/classes" path="src/main/java">
 		<attributes>
-			<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="Tankernn game engine/lib/lwjgl-2.9.3/native"/>
+			<attribute name="optional" value="true"/>
+			<attribute name="maven.pomderived" value="true"/>
 		</attributes>
 	</classpathentry>
-	<classpathentry kind="src" path="src/test/java">
+	<classpathentry kind="src" output="target/test-classes" path="src/test/java">
 		<attributes>
 			<attribute name="optional" value="true"/>
 			<attribute name="maven.pomderived" value="true"/>
 		</attributes>
 	</classpathentry>
-	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
-	<classpathentry kind="lib" path="lib/PNGDecoder.jar"/>
-	<classpathentry kind="output" path="bin"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
+		<attributes>
+			<attribute name="maven.pomderived" value="true"/>
+		</attributes>
+	</classpathentry>
+	<classpathentry kind="output" path="target/classes"/>
 </classpath>

+ 6 - 0
.project

@@ -10,8 +10,14 @@
 			<arguments>
 			</arguments>
 		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.m2e.core.maven2Builder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
 	</buildSpec>
 	<natures>
+		<nature>org.eclipse.m2e.core.maven2Nature</nature>
 		<nature>org.eclipse.jdt.core.javanature</nature>
 	</natures>
 </projectDescription>

+ 2 - 2
pom.xml

@@ -5,7 +5,7 @@
 
 	<groupId>eu.tankernn.gameEngine</groupId>
 	<artifactId>tankernn-game-engine</artifactId>
-	<version>1.0-SNAPSHOT</version>
+	<version>1.0.1-SNAPSHOT</version>
 	<packaging>jar</packaging>
 
 	<url>http://tankernn.eu</url>
@@ -71,7 +71,7 @@
 				<executions>
 					<execution>
 						<id>unpacknatives</id>
-						<phase>generate-resources</phase>
+						<phase>package</phase>
 						<goals>
 							<goal>copy</goal>
 						</goals>

+ 81 - 0
src/main/java/eu/tankernn/gameEngine/entities/Car.java

@@ -0,0 +1,81 @@
+package eu.tankernn.gameEngine.entities;
+
+import org.lwjgl.input.Keyboard;
+import org.lwjgl.util.vector.Vector3f;
+
+import eu.tankernn.gameEngine.models.TexturedModel;
+import eu.tankernn.gameEngine.renderEngine.DisplayManager;
+import eu.tankernn.gameEngine.settings.Physics;
+import eu.tankernn.gameEngine.terrains.Terrain;
+import eu.tankernn.gameEngine.terrains.TerrainPack;
+
+public class Car extends Player {
+
+	private static final float MAX_SPEED = 100.0f, ACCELERATION = 20.0f, DECELERATION = 10.0f, BRAKE = 40.0f,
+			TURN_FORCE = 160.0f;
+
+	public Car(TexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, float scale,
+			TerrainPack terrainPack) {
+		super(model, position, rotX, rotY, rotZ, scale, terrainPack);
+	}
+
+	@Override
+	public void move(TerrainPack terrainPack) {
+		checkInputs();
+		super.increaseRotation(0, currentTurnSpeed * DisplayManager.getFrameTimeSeconds(), 0);
+		float distance = currentSpeed * DisplayManager.getFrameTimeSeconds();
+		float dx = (float) (distance * Math.sin(Math.toRadians(super.getRotY())));
+		float dz = (float) (distance * Math.cos(Math.toRadians(super.getRotY())));
+		super.increasePosition(dx, 0, dz);
+
+		upwardsSpeed += Physics.GRAVITY * DisplayManager.getFrameTimeSeconds();
+		super.increasePosition(0, upwardsSpeed * DisplayManager.getFrameTimeSeconds(), 0);
+
+		Terrain currentTerrain = terrainPack.getTerrainByWorldPos(this.getPosition().x, this.getPosition().z);
+
+		float terrainHeight = 0;
+		if (currentTerrain != null) {
+			terrainHeight = currentTerrain.getHeightOfTerrain(super.getPosition().x, super.getPosition().z);
+		}
+
+		if (super.getPosition().getY() < terrainHeight) {
+			super.getPosition().y = terrainHeight;
+		}
+	}
+
+	@Override
+	protected void checkInputs() {
+		if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
+			this.currentSpeed += ACCELERATION * DisplayManager.getFrameTimeSeconds();
+			this.currentSpeed = Math.min(currentSpeed, MAX_SPEED);
+		} else if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
+			this.currentSpeed -= BRAKE * DisplayManager.getFrameTimeSeconds();
+			this.currentSpeed = Math.max(currentSpeed, 0);
+		} else {
+			this.currentSpeed -= DECELERATION * DisplayManager.getFrameTimeSeconds();
+			this.currentSpeed = Math.max(currentSpeed, 0);
+		}
+
+		if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
+			this.currentTurnSpeed += TURN_FORCE * DisplayManager.getFrameTimeSeconds();
+		} else if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
+			this.currentTurnSpeed -= TURN_FORCE * DisplayManager.getFrameTimeSeconds();
+		} else {
+			if (this.currentTurnSpeed > 0) {
+				this.currentTurnSpeed -= TURN_FORCE;
+				if (currentTurnSpeed < 0)
+					this.currentTurnSpeed = 0;
+			} else if (this.currentTurnSpeed < 0) {
+				this.currentTurnSpeed += TURN_FORCE;
+				if (currentTurnSpeed > 0)
+					this.currentTurnSpeed = 0;
+			}
+		}
+		
+		this.currentTurnSpeed = Math.min(currentTurnSpeed, TURN_MAX);
+		this.currentTurnSpeed = Math.max(currentTurnSpeed, -TURN_MAX);
+
+		// TODO Nitro
+	}
+
+}

+ 7 - 7
src/main/java/eu/tankernn/gameEngine/entities/Player.java

@@ -13,14 +13,14 @@ import eu.tankernn.gameEngine.settings.Physics;
 public class Player extends Entity {
 	
 	private static final float RUN_SPEED = 20;
-	private static final float TURN_SPEED = 160;
+	protected static final float TURN_MAX = 160;
 	private static final float JUMP_POWER = 30;
 	
 	private TerrainPack terrainPack;
 	
-	private float currentSpeed = 0;
-	private float currentTurnSpeed = 0;
-	private float upwardsSpeed = 0;
+	protected float currentSpeed = 0;
+	protected float currentTurnSpeed = 0;
+	protected float upwardsSpeed = 0;
 	private boolean isInAir = false;
 	
 	private float height = 2.0f;
@@ -61,7 +61,7 @@ public class Player extends Entity {
 		}
 	}
 	
-	private void checkInputs() {
+	protected void checkInputs() {
 		if (Keyboard.isKeyDown(Keyboard.KEY_W) || (Mouse.isButtonDown(0) && Mouse.isButtonDown(1))) {
 			this.currentSpeed = RUN_SPEED;
 		} else if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
@@ -71,9 +71,9 @@ public class Player extends Entity {
 		}
 		
 		if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
-			this.currentTurnSpeed = TURN_SPEED;
+			this.currentTurnSpeed = TURN_MAX;
 		} else if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
-			this.currentTurnSpeed = -TURN_SPEED;
+			this.currentTurnSpeed = -TURN_MAX;
 		} else {
 			this.currentTurnSpeed = 0;
 		}

+ 4 - 80
src/main/java/eu/tankernn/gameEngine/tester/MainLoop.java

@@ -1,20 +1,8 @@
 package eu.tankernn.gameEngine.tester;
 
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.net.URISyntaxException;
 import java.util.ArrayList;
-import java.util.Enumeration;
 import java.util.List;
 import java.util.Random;
-import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
-
-import javax.swing.JOptionPane;
-
 import org.lwjgl.input.Mouse;
 import org.lwjgl.opengl.Display;
 import org.lwjgl.util.vector.Vector2f;
@@ -22,6 +10,7 @@ import org.lwjgl.util.vector.Vector3f;
 import org.lwjgl.util.vector.Vector4f;
 
 import eu.tankernn.gameEngine.entities.Camera;
+import eu.tankernn.gameEngine.entities.Car;
 import eu.tankernn.gameEngine.entities.Entity;
 import eu.tankernn.gameEngine.entities.Light;
 import eu.tankernn.gameEngine.entities.Player;
@@ -52,6 +41,7 @@ import eu.tankernn.gameEngine.textures.ModelTexture;
 import eu.tankernn.gameEngine.textures.TerrainTexture;
 import eu.tankernn.gameEngine.textures.TerrainTexturePack;
 import eu.tankernn.gameEngine.util.MousePicker;
+import eu.tankernn.gameEngine.util.NativesExporter;
 import eu.tankernn.gameEngine.util.Sorter;
 import eu.tankernn.gameEngine.water.WaterMaster;
 import eu.tankernn.gameEngine.water.WaterTile;
@@ -61,7 +51,7 @@ public class MainLoop {
 	private static final int SEED = 1235;
 	
 	public static void main(String[] args) {
-		exportNatives();
+		NativesExporter.exportNatives();
 		
 		List<Entity> entities = new ArrayList<Entity>();
 		List<Entity> normalMapEntities = new ArrayList<Entity>();
@@ -82,7 +72,7 @@ public class MainLoop {
 		Entity entity = new Entity(texturedMonkeyModel, new Vector3f(0, 0, 20), 0, 0, 0, 1);
 		entities.add(entity);
 		TexturedModel monkey = new TexturedModel(monkeyModel, new ModelTexture(loader.loadTexture("white")));
-		Player player = new Player(monkey, new Vector3f(10, 0, 50), 0, 0, 0, 1, terrainPack);
+		Player player = new Car(monkey, new Vector3f(10, 0, 50), 0, 0, 0, 1, terrainPack);
 		entities.add(player);
 		Camera camera = new PlayerCamera(player, terrainPack);
 		
@@ -239,70 +229,4 @@ public class MainLoop {
 		loader.cleanUp();
 		DisplayManager.closeDisplay();
 	}
-	
-	private static void exportNatives() {
-		File nativeDir = new File("natives");
-		if (!nativeDir.isDirectory() || nativeDir.list().length == 0) {
-			try {
-				nativeDir.mkdir();
-				
-				File jarFile = null;
-				try {
-					jarFile = new File(MainLoop.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
-				} catch (URISyntaxException e1) {
-					e1.printStackTrace();
-				}
-				
-				if (jarFile != null && jarFile.isFile()) { // Run with JAR file
-					final JarFile jar = new JarFile(jarFile);
-					final Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
-					while (entries.hasMoreElements()) {
-						final String name = entries.nextElement().getName();
-						if (name.endsWith(".dll") || name.endsWith(".so") || name.endsWith(".dylib") || name.endsWith(".jnilb")) { //filter according to the path
-							System.out.println(name);
-							try {
-								exportFile(name, "natives");
-							} catch (Exception e) {
-								e.printStackTrace();
-							}
-						}
-					}
-					jar.close();
-					System.setProperty("org.lwjgl.librarypath", nativeDir.getAbsolutePath());
-				} else { // Run with IDE
-				}
-			} catch (IOException e) {
-				JOptionPane.showMessageDialog(null, "Could not export natives. Execute in terminal to see full error output.", "Export natives", JOptionPane.ERROR_MESSAGE);
-				e.printStackTrace();
-			}
-		} else {
-			System.setProperty("org.lwjgl.librarypath", nativeDir.getAbsolutePath());
-		}
-	}
-	
-	private static String exportFile(String classPath, String targetDir) throws Exception {
-		InputStream stream = null;
-		OutputStream resStreamOut = null;
-		try {
-			stream = MainLoop.class.getResourceAsStream("/" + classPath);//note that each / is a directory down in the "jar tree" been the jar the root of the tree
-			if (stream == null) {
-				throw new Exception("Cannot get resource \"" + classPath + "\" from Jar file.");
-			}
-			
-			int readBytes;
-			byte[] buffer = new byte[4096];
-			File outFile = new File(targetDir + "/" + classPath);
-			outFile.getParentFile().mkdirs();
-			resStreamOut = new FileOutputStream(outFile);
-			while ((readBytes = stream.read(buffer)) > 0) {
-				resStreamOut.write(buffer, 0, readBytes);
-			}
-			stream.close();
-			resStreamOut.close();
-		} catch (Exception ex) {
-			throw ex;
-		}
-		
-		return classPath;
-	}
 }

+ 112 - 0
src/main/java/eu/tankernn/gameEngine/util/NativesExporter.java

@@ -0,0 +1,112 @@
+package eu.tankernn.gameEngine.util;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URISyntaxException;
+import java.util.Enumeration;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+
+import javax.swing.JOptionPane;
+
+import eu.tankernn.gameEngine.tester.MainLoop;
+
+public class NativesExporter {
+	public static void exportNatives() {
+		File nativeDir = new File("natives");
+		if (!nativeDir.isDirectory() || nativeDir.list().length == 0) {
+			try {
+				nativeDir.mkdir();
+
+				File jarFile = null;
+				try {
+					jarFile = new File(
+							MainLoop.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
+				} catch (URISyntaxException e1) {
+					e1.printStackTrace();
+				}
+
+				if (jarFile != null && jarFile.isFile()) { // Run with JAR file
+					final JarFile jar = new JarFile(jarFile);
+					final Enumeration<JarEntry> entries = jar.entries(); // gives
+																			// ALL
+																			// entries
+																			// in
+																			// jar
+					while (entries.hasMoreElements()) {
+						final String name = entries.nextElement().getName();
+						if (name.endsWith(".dll") || name.endsWith(".so") || name.endsWith(".dylib")
+								|| name.endsWith(".jnilb")) { // filter
+																// according to
+																// the path
+							System.out.println(name);
+							try {
+								exportFile(name, "natives");
+							} catch (Exception e) {
+								e.printStackTrace();
+							}
+						}
+					}
+					jar.close();
+					System.setProperty("org.lwjgl.librarypath", nativeDir.getAbsolutePath());
+				} else { // Run with IDE
+				}
+			} catch (IOException e) {
+				JOptionPane.showMessageDialog(null,
+						"Could not export natives. Execute in terminal to see full error output.", "Export natives",
+						JOptionPane.ERROR_MESSAGE);
+				e.printStackTrace();
+			}
+		} else {
+			System.setProperty("org.lwjgl.librarypath", nativeDir.getAbsolutePath());
+		}
+	}
+
+	private static String exportFile(String classPath, String targetDir) throws Exception {
+		InputStream stream = null;
+		OutputStream resStreamOut = null;
+		try {
+			stream = MainLoop.class.getResourceAsStream("/" + classPath);// note
+																			// that
+																			// each
+																			// /
+																			// is
+																			// a
+																			// directory
+																			// down
+																			// in
+																			// the
+																			// "jar
+																			// tree"
+																			// been
+																			// the
+																			// jar
+																			// the
+																			// root
+																			// of
+																			// the
+																			// tree
+			if (stream == null) {
+				throw new Exception("Cannot get resource \"" + classPath + "\" from Jar file.");
+			}
+
+			int readBytes;
+			byte[] buffer = new byte[4096];
+			File outFile = new File(targetDir + "/" + classPath);
+			outFile.getParentFile().mkdirs();
+			resStreamOut = new FileOutputStream(outFile);
+			while ((readBytes = stream.read(buffer)) > 0) {
+				resStreamOut.write(buffer, 0, readBytes);
+			}
+			stream.close();
+			resStreamOut.close();
+		} catch (Exception ex) {
+			throw ex;
+		}
+
+		return classPath;
+	}
+}