Skip to content

Commit

Permalink
Cleaned up code.
Browse files Browse the repository at this point in the history
Created Heightmap for better code separation.
Added filebrowser to gui for selecting custom heightmap
  • Loading branch information
usbalbin committed Apr 27, 2016
1 parent 96e7b48 commit d4d32ec
Show file tree
Hide file tree
Showing 27 changed files with 526 additions and 315 deletions.
2 changes: 1 addition & 1 deletion src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Manifest-Version: 1.0
Main-Class: se.liu.ida.albhe417.tddd78.menu.MenuFrame
Main-Class: se.liu.ida.albhe417.tddd78.gui.MenuFrame

40 changes: 24 additions & 16 deletions src/se/liu/ida/albhe417/tddd78/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWWindowSizeCallback;
import org.lwjgl.opengl.GL;
import se.liu.ida.albhe417.tddd78.game.GameObject.AbstractGameObject;
import se.liu.ida.albhe417.tddd78.game.GameObject.Misc.Target;
import se.liu.ida.albhe417.tddd78.game.GameObject.Vehicles.AbstractVehicle;
import se.liu.ida.albhe417.tddd78.game.GameObject.Vehicles.VehicleAirplane;
import se.liu.ida.albhe417.tddd78.game.GameObject.Vehicles.VehicleHelicopterBox;
import se.liu.ida.albhe417.tddd78.game.gameObject.AbstractGameObject;
import se.liu.ida.albhe417.tddd78.game.gameObject.misc.Target;
import se.liu.ida.albhe417.tddd78.game.gameObject.vehicles.AbstractVehicle;
import se.liu.ida.albhe417.tddd78.game.gameObject.vehicles.VehicleAirplane;
import se.liu.ida.albhe417.tddd78.game.gameObject.vehicles.VehicleAirplaneBox;
import se.liu.ida.albhe417.tddd78.game.gameObject.vehicles.VehicleHelicopterBox;
import se.liu.ida.albhe417.tddd78.math.Matrix4x4;
import se.liu.ida.albhe417.tddd78.math.Vector3;

Expand All @@ -43,8 +44,8 @@ public class Game implements Runnable
private final Settings settings;

private long window;//Reference to window
GLFWWindowSizeCallback windowSizeCallback;
GLFWErrorCallback errorCallback;
private GLFWWindowSizeCallback windowSizeCallback;
private GLFWErrorCallback errorCallback;

private static final String TITLE = "Simple Java Flight Simulator";

Expand Down Expand Up @@ -222,11 +223,9 @@ private void setupGameObjects(){
terrain = new TerrainLOD(new Vector3(0, 0, 0), HEIGHT_SCALE, settings, shaderProgram, physics, this);
//currentVehicle = new VehicleHelicopterBox(new Vector3(11, 6, 148.0f), -(float)Math.PI / 2.0f, terrain, shaderProgram, physics);

for(int y = 0; y < 4; y++) {
for(int x = -2; x < 2; x++) {
//gameObjects.add(new ProjectileMesh(new Vector3(x * 10, 2 + 5 * y, y * 4), new Vector3(), shaderProgram, physics, this));
gameObjects.add(new Target(new Vector3(x * 25, -200, y * 25), shaderProgram, physics, this, "Target at " + x + "; " + y));
//gameObjects.add(new VehicleHelicopterBox(new Vector3(x * 10 + 2, 1 + 5 * y, y * 4), -(float) Math.PI / 2.0f, shaderProgram, physics, this));
for(int y = -4; y < 4; y++) {
for(int x = -4; x < 4; x++) {
gameObjects.add(new Target(new Vector3(x * 250, 140, y * 250), shaderProgram, physics, this, "Target at " + x + "; " + y));
}
}
//gameObjects.add(terrain);
Expand All @@ -251,10 +250,19 @@ private void setupLight(){
}

private void respawn(){
final Vector3 spawnPos = new Vector3(0, -307, 0);//new Vector3(-225, /*-316.1f*/0, 20);
//currentVehicle = new VehicleHelicopterBox(spawnPos, -(float)Math.PI / 2, shaderProgram, physics, this, settings.getPlayerName());
//currentVehicle = new VehicleAirplaneBox(new Vector3(0, 0, 0), -(float)Math.PI / 2, shaderProgram, physics, this, settings.getPlayerName());
currentVehicle = new VehicleAirplane(new Vector3(0, 0, 0), physics, this, settings.getPlayerName(), shaderProgram);
final Vector3 spawnPos = new Vector3(0, 130, 0);//new Vector3(0, -307, 0);

switch (settings.getVehicleType()) {
case HelicopterBox:
currentVehicle = new VehicleHelicopterBox(spawnPos, -(float)Math.PI / 2, shaderProgram, physics, this, settings.getPlayerName());
break;
case AirplaneBox:
currentVehicle = new VehicleAirplaneBox(spawnPos, -(float)Math.PI / 2, shaderProgram, physics, this, settings.getPlayerName());
break;
case Airplane:
currentVehicle = new VehicleAirplane(spawnPos, physics, this, settings.getPlayerName(), shaderProgram);
break;
}
gameObjects.add(currentVehicle);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package se.liu.ida.albhe417.tddd78.game.GameObject;
package se.liu.ida.albhe417.tddd78.game.gameObject;

import com.bulletphysics.collision.narrowphase.ManifoldPoint;
import com.bulletphysics.dynamics.DynamicsWorld;
import com.bulletphysics.dynamics.constraintsolver.TypedConstraint;
import se.liu.ida.albhe417.tddd78.game.Game;
import se.liu.ida.albhe417.tddd78.game.GameObjectPart.GameObjectPart;
import se.liu.ida.albhe417.tddd78.game.gameObjectPart.GameObjectPart;
import se.liu.ida.albhe417.tddd78.math.Matrix4x4;
import se.liu.ida.albhe417.tddd78.math.Vector3;

Expand All @@ -23,10 +23,9 @@ public abstract class AbstractGameObject
//protected float yaw, pitch, roll;
protected Matrix4x4 modelMatrix;
private final DynamicsWorld physics;
protected final Game game;
private final float DAMAGE_RESISTANCE;
private float health;
public final AtomicInteger score;
protected final AtomicInteger score;
public final String playerName;
public AbstractGameObject killedBy;

Expand All @@ -37,7 +36,6 @@ protected AbstractGameObject(Vector3 position, DynamicsWorld physics, Game game,
modelMatrix = new Matrix4x4();
modelMatrix = modelMatrix.getTranslated(position);
this.physics = physics;
this.game = game;
this.health = maxHealth;
this.DAMAGE_RESISTANCE = maxHealth / 10;
this.score = new AtomicInteger(0);
Expand Down Expand Up @@ -84,4 +82,5 @@ public void update(){
public void addConnection(TypedConstraint constraint){
constraints.add(constraint);
}

}
7 changes: 3 additions & 4 deletions src/se/liu/ida/albhe417/tddd78/game/GameObject/Misc/Gun.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package se.liu.ida.albhe417.tddd78.game.GameObject.Misc;
package se.liu.ida.albhe417.tddd78.game.gameObject.misc;

import com.bulletphysics.collision.dispatch.CollisionFlags;
import com.bulletphysics.collision.shapes.SphereShape;
Expand All @@ -8,8 +8,7 @@
import com.bulletphysics.linearmath.MotionState;
import com.bulletphysics.linearmath.Transform;
import se.liu.ida.albhe417.tddd78.game.Game;
import se.liu.ida.albhe417.tddd78.game.GameObject.AbstractGameObject;
import se.liu.ida.albhe417.tddd78.game.GameObject.Vehicles.AbstractVehicle;
import se.liu.ida.albhe417.tddd78.game.gameObject.vehicles.AbstractVehicle;
import se.liu.ida.albhe417.tddd78.math.Matrix4x4;
import se.liu.ida.albhe417.tddd78.math.Vector3;

Expand All @@ -35,7 +34,7 @@ public class Gun extends Weapon {
private final ProjectileMesh projectile;

public Gun(Vector3 offsetPosition, AbstractVehicle owner, DynamicsWorld physics, int shaderProgram, Game game, String playerName){
super(offsetPosition, physics, game, Float.POSITIVE_INFINITY, playerName);
super(offsetPosition, physics, game, playerName);
this.offsetPosition = offsetPosition;
this.FIRE_RATE = 0.1f; //= 1/shots per sec
this.owner = owner;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package se.liu.ida.albhe417.tddd78.game.GameObject.Misc;
package se.liu.ida.albhe417.tddd78.game.gameObject.misc;

import com.bulletphysics.dynamics.DynamicsWorld;
import com.bulletphysics.dynamics.RigidBody;
import se.liu.ida.albhe417.tddd78.game.*;
import se.liu.ida.albhe417.tddd78.game.GameObject.AbstractGameObject;
import se.liu.ida.albhe417.tddd78.game.GameObjectPart.GameObjectPart;
import se.liu.ida.albhe417.tddd78.game.gameObject.AbstractGameObject;
import se.liu.ida.albhe417.tddd78.game.gameObjectPart.GameObjectPart;
import se.liu.ida.albhe417.tddd78.math.Vector3;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package se.liu.ida.albhe417.tddd78.game.GameObject.Misc;
package se.liu.ida.albhe417.tddd78.game.gameObject.misc;

import com.bulletphysics.collision.shapes.SphereShape;
import com.bulletphysics.dynamics.DynamicsWorld;
Expand All @@ -7,8 +7,8 @@
import com.bulletphysics.linearmath.MotionState;
import com.bulletphysics.linearmath.Transform;
import se.liu.ida.albhe417.tddd78.game.Game;
import se.liu.ida.albhe417.tddd78.game.GameObject.AbstractGameObject;
import se.liu.ida.albhe417.tddd78.game.GameObjectPart.GameObjectPart;
import se.liu.ida.albhe417.tddd78.game.gameObject.AbstractGameObject;
import se.liu.ida.albhe417.tddd78.game.gameObjectPart.GameObjectPart;
import se.liu.ida.albhe417.tddd78.game.Helpers;
import se.liu.ida.albhe417.tddd78.game.VertexPositionColorNormal;
import se.liu.ida.albhe417.tddd78.math.Matrix4x4;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package se.liu.ida.albhe417.tddd78.game.GameObject.Misc;
package se.liu.ida.albhe417.tddd78.game.gameObject.misc;

import com.bulletphysics.dynamics.DynamicsWorld;
import se.liu.ida.albhe417.tddd78.game.Game;
import se.liu.ida.albhe417.tddd78.game.GameObject.AbstractGameObject;
import se.liu.ida.albhe417.tddd78.game.gameObject.AbstractGameObject;
import se.liu.ida.albhe417.tddd78.math.Matrix4x4;
import se.liu.ida.albhe417.tddd78.math.Vector3;

Expand All @@ -13,8 +13,8 @@
*/
public abstract class Weapon extends AbstractGameObject{

Weapon(Vector3 position, DynamicsWorld physics, Game game, float maxHealth, String gunName){
super(position, physics, game, maxHealth, gunName);
Weapon(Vector3 position, DynamicsWorld physics, Game game, String gunName){
super(position, physics, game, Float.POSITIVE_INFINITY, gunName);
}

abstract public void fire(float currTimeSec);
Expand Down
12 changes: 12 additions & 0 deletions src/se/liu/ida/albhe417/tddd78/game/GameObject/VehicleType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package se.liu.ida.albhe417.tddd78.game.gameObject;

/**
* Project TDDD78
* <p>
* File created by Albin on 26/04/2016.
*/
public enum VehicleType {
AirplaneBox,
Airplane,
HelicopterBox
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package se.liu.ida.albhe417.tddd78.game.GameObject.Vehicles;
package se.liu.ida.albhe417.tddd78.game.gameObject.vehicles;

import javax.vecmath.Vector3f;

import com.bulletphysics.dynamics.DynamicsWorld;
import se.liu.ida.albhe417.tddd78.game.Game;
import se.liu.ida.albhe417.tddd78.game.GameObject.AbstractGameObject;
import se.liu.ida.albhe417.tddd78.game.GameObjectPart.GameObjectPart;
import se.liu.ida.albhe417.tddd78.game.gameObject.AbstractGameObject;
import se.liu.ida.albhe417.tddd78.game.gameObjectPart.GameObjectPart;
import se.liu.ida.albhe417.tddd78.math.Matrix4x4;
import se.liu.ida.albhe417.tddd78.math.Vector3;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package se.liu.ida.albhe417.tddd78.game.GameObject.Vehicles;
package se.liu.ida.albhe417.tddd78.game.gameObject.vehicles;

import com.bulletphysics.dynamics.DynamicsWorld;
import com.bulletphysics.dynamics.constraintsolver.TypedConstraint;
import se.liu.ida.albhe417.tddd78.game.Game;
import se.liu.ida.albhe417.tddd78.game.GameObjectPart.Wing;
import se.liu.ida.albhe417.tddd78.game.gameObjectPart.Wing;
import se.liu.ida.albhe417.tddd78.game.InputHandler;
import se.liu.ida.albhe417.tddd78.game.Thruster;
import se.liu.ida.albhe417.tddd78.math.Vector3;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_LEFT;

/**
* Project TDDD78
Expand All @@ -23,64 +22,92 @@ public VehicleAirplane(Vector3 position, DynamicsWorld physics, Game game, Strin
super(position, 1000, 1000, physics, game, 10000, playerName + "'s Test vehicle");



float wingThickness = 0.075f;
float bodyMass = 5;
Vector3 bodySize = new Vector3(0.5f, 1, 6);
float bodyMass = 100;
Vector3 bodySize = new Vector3(1, 1, 4);
float wingOffsetZ = bodySize.getZ()/2f;

Wing body = new Wing(Vector3.ZERO, bodySize, bodyMass, modelMatrix, shaderProgram, physics, this);


float tailMass = 2;

Vector3 tailSize = new Vector3(0.25f, 0.25f, 3);
Vector3 tailPosition = new Vector3(0, 0, bodySize.getZ() + tailSize.getZ());

Vector3 tailPoint = new Vector3(0, 0, -tailSize.getZ());
Vector3 bodyToLeftPoint = new Vector3(0, 0, bodySize.getZ());

Wing tail = new Wing(tailPosition, tailSize, tailMass, modelMatrix, shaderProgram, physics, this);
TypedConstraint tailConstraint = tail.attachToParentFixed(body, tailPoint, bodyToLeftPoint, this);


float mainWingMass = 1;
Vector3 mainWingSize = new Vector3(5, wingThickness, 2.5f);
Vector3 leftMainWingPos = new Vector3(+mainWingSize.getX() + bodySize.getX(), -bodySize.getY(), -bodySize.getZ()/100f);
Vector3 rightMainWingPos = new Vector3(-mainWingSize.getX() - bodySize.getX(), -bodySize.getY(), -bodySize.getZ()/100f);
Vector3 mainWingSize = new Vector3(6, wingThickness, 1.5f);
Vector3 leftMainWingPos = new Vector3(-mainWingSize.getX(), +bodySize.getY(), +wingOffsetZ);
Vector3 rightMainWingPos = new Vector3(+mainWingSize.getX(), +bodySize.getY(), +wingOffsetZ);

Vector3 leftMainWingPoint = new Vector3(+mainWingSize.getX(), 0, 0);
Vector3 rightMainWingPoint = new Vector3(-mainWingSize.getX(), 0, 0);

Vector3 bodyToLeftWingPoint = new Vector3(0, bodySize.getY() + 2 * wingThickness, +wingOffsetZ);
Vector3 bodyToRightWingPoint = new Vector3(0, bodySize.getY() + 2 * wingThickness, +wingOffsetZ);


Wing leftMainWing = new Wing(leftMainWingPos, mainWingSize, mainWingMass, modelMatrix, shaderProgram, physics, this);
TypedConstraint leftWingConstraint = leftMainWing.attachToParentFixed(body, Vector3.ZERO, leftMainWingPos, this);
TypedConstraint leftWingConstraint = leftMainWing.attachToParentFixed(body, leftMainWingPoint, bodyToLeftWingPoint, this);

Wing rightMainWing = new Wing(leftMainWingPos, mainWingSize, mainWingMass, modelMatrix, shaderProgram, physics, this);
TypedConstraint rightWingConstraint = rightMainWing.attachToParentFixed(body, Vector3.ZERO, rightMainWingPos, this);
Wing rightMainWing = new Wing(rightMainWingPos, mainWingSize, mainWingMass, modelMatrix, shaderProgram, physics, this);
TypedConstraint rightWingConstraint = rightMainWing.attachToParentFixed(body, rightMainWingPoint, bodyToRightWingPoint, this);


float elevatorMass = 1;
float elevatorMass = 0.5f;
Vector3 elevatorSize = new Vector3(2, wingThickness, 1);
Vector3 elevatorPos = new Vector3(0, -bodySize.getY() - wingThickness, -elevatorSize.getZ() - bodySize.getZ());
Vector3 elevatorPos = new Vector3(0, tailSize.getY(), elevatorSize.getZ() + tailSize.getZ()).add(tailPosition);

Vector3 elevatorPoint = new Vector3(0, 0, -elevatorSize.getZ());
Vector3 tailToElevatorPoint = new Vector3(0, tailSize.getY(), tailSize.getZ());

Wing elevator = new Wing(elevatorPos, elevatorSize, elevatorMass, modelMatrix, shaderProgram, physics, this);
TypedConstraint elevatorConstraint = elevator.attachToParentFixed(body, Vector3.ZERO, elevatorPos, this);
TypedConstraint elevatorConstraint = elevator.attachToParentFixed(tail, elevatorPoint, tailToElevatorPoint, this);



float rudderMass = 0.5f;
Vector3 rudderSize = new Vector3(wingThickness, 1, 1);
Vector3 rudderPos = new Vector3(0, tailSize.getY() + wingThickness, tailSize.getZ()).add(tailPosition);

//float rudderMass = 1;
//Vector3 rudderSize = new Vector3(wingThickness, 0.5f, 0.5f);
//Vector3 rudderPos = new Vector3(0, 2, 7);
Vector3 rudderPoint = new Vector3(0, 0, -rudderSize.getZ());
Vector3 tailToRudderPoint = new Vector3(0, tailSize.getY() + wingThickness, tailSize.getZ());

//Wing rudder = new Wing(rudderPos, rudderSize, rudderMass, modelMatrix, shaderProgram, physics, this);
//TypedConstraint rudderConstraint = rudder.attachToParentFixed(body, Vector3.ZERO, rudderPos, this);
Wing rudder = new Wing(rudderPos, rudderSize, rudderMass, modelMatrix, shaderProgram, physics, this);
TypedConstraint rudderConstraint = rudder.attachToParentFixed(tail, rudderPoint, tailToRudderPoint, this);


final float thrustFactor = 2000;
final Vector3 direction = new Vector3(0, 0, -1);
final Vector3 thrusterPosition = new Vector3(0, 0, -bodySize.getZ());
final float thrustFactor = 10000;
final Vector3 thrustDirection = new Vector3(0, 0, -1);
final Vector3 thrusterPosition = new Vector3(0, bodySize.getY(), -bodySize.getZ());

thruster = new Thruster(thrusterPosition, direction, thrustFactor, body);
thruster = new Thruster(thrusterPosition, thrustDirection, thrustFactor, body);

partBody = body;
parts.add(partBody);
parts.add(tail);
parts.add(leftMainWing);
parts.add(rightMainWing);
parts.add(elevator);
//parts.add(rudder);
parts.add(rudder);

constraints.add(tailConstraint);
constraints.add(leftWingConstraint);
constraints.add(rightWingConstraint);
constraints.add(elevatorConstraint);
//constraints.add(rudderConstraint);
constraints.add(rudderConstraint);

constraints.forEach(physics::addConstraint);
}


@Override
public void handleInput(float deltaTime) {
float deltaThrottle = 0.0f;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package se.liu.ida.albhe417.tddd78.game.GameObject.Vehicles;
package se.liu.ida.albhe417.tddd78.game.gameObject.vehicles;

import com.bulletphysics.collision.shapes.BoxShape;
import com.bulletphysics.collision.shapes.CollisionShape;
Expand All @@ -8,14 +8,13 @@
import com.bulletphysics.linearmath.MotionState;
import com.bulletphysics.linearmath.Transform;
import se.liu.ida.albhe417.tddd78.game.*;
import se.liu.ida.albhe417.tddd78.game.GameObject.Misc.Gun;
import se.liu.ida.albhe417.tddd78.game.GameObject.Misc.Weapon;
import se.liu.ida.albhe417.tddd78.game.GameObjectPart.GameObjectPart;
import se.liu.ida.albhe417.tddd78.game.gameObject.misc.Gun;
import se.liu.ida.albhe417.tddd78.game.gameObject.misc.Weapon;
import se.liu.ida.albhe417.tddd78.game.gameObjectPart.GameObjectPart;
import se.liu.ida.albhe417.tddd78.math.Matrix4x4;
import se.liu.ida.albhe417.tddd78.math.Vector3;

import javax.vecmath.Vector3f;
import java.util.ArrayList;

import static org.lwjgl.glfw.GLFW.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package se.liu.ida.albhe417.tddd78.game.GameObject.Vehicles;
package se.liu.ida.albhe417.tddd78.game.gameObject.vehicles;

import com.bulletphysics.collision.shapes.BoxShape;
import com.bulletphysics.collision.shapes.CollisionShape;
Expand All @@ -8,14 +8,13 @@
import com.bulletphysics.linearmath.MotionState;
import com.bulletphysics.linearmath.Transform;
import se.liu.ida.albhe417.tddd78.game.*;
import se.liu.ida.albhe417.tddd78.game.GameObject.Misc.Gun;
import se.liu.ida.albhe417.tddd78.game.GameObject.Misc.Weapon;
import se.liu.ida.albhe417.tddd78.game.GameObjectPart.GameObjectPart;
import se.liu.ida.albhe417.tddd78.game.gameObject.misc.Gun;
import se.liu.ida.albhe417.tddd78.game.gameObject.misc.Weapon;
import se.liu.ida.albhe417.tddd78.game.gameObjectPart.GameObjectPart;
import se.liu.ida.albhe417.tddd78.math.Matrix4x4;
import se.liu.ida.albhe417.tddd78.math.Vector3;

import javax.vecmath.Vector3f;
import java.util.ArrayList;

import static org.lwjgl.glfw.GLFW.*;

Expand Down
Loading

0 comments on commit d4d32ec

Please sign in to comment.