[DEV] work

This commit is contained in:
2020-05-04 17:58:11 +02:00
parent a526e07335
commit dd6fb69230
410 changed files with 2517567 additions and 0 deletions

90
src/entities/Camera.java Normal file
View File

@@ -0,0 +1,90 @@
package entities;
import org.atriaSoft.etk.math.Vector3f;
import renderEngine.DisplayManager;
public class Camera {
private float distanceFromPlayer = 20;
private float angleAroundPlayer = 0;
private Vector3f position = new Vector3f(0,5,0);
private float pitch = 0;// (float) Math.toRadians(10);
private float yaw = 0;
private float roll = 0;
private Player player;
public Camera(Player player) {
this.player = player;
}
public void move() {
calculateZoom();
calculatePitch();
CalculateAngleAroundPlayer();
float horinzontalDistance = calculateHorizontalDistance();
float verticalDistance = calculateVerticalDistance();
calculateCameraPosition(horinzontalDistance, verticalDistance);
this.yaw = 3.141596f - player.getRotation().y - angleAroundPlayer ;
}
private void calculateCameraPosition (float horizontalDistance, float verticalDistance) {
float theta = 3.141596f + player.getRotation().y + angleAroundPlayer;
float offsetX = (float) (horizontalDistance * Math.sin(theta));
float offsetZ = (float) (horizontalDistance * Math.cos(theta));
position.x = player.getPosition().x + offsetX;
position.z = player.getPosition().z + offsetZ;
position.y = player.getPosition().y+5 + verticalDistance;
}
private float calculateHorizontalDistance() {
return (float) (distanceFromPlayer * Math.cos(pitch));
}
private float calculateVerticalDistance() {
return (float) (distanceFromPlayer * Math.sin(pitch));
}
public Vector3f getPosition() {
return position;
}
public float getPitch() {
return pitch;
}
public float getYaw() {
return yaw;
}
public float getRoll() {
return roll;
}
private void calculateZoom() {
float zoomLevel = DisplayManager.getDWheel() * 1;
distanceFromPlayer -= zoomLevel;
}
private void calculatePitch() {
if (DisplayManager.isButtonRightDown() ) {
float pitchChange = DisplayManager.getDY() * 0.01f;
pitch += pitchChange;
if (pitch < 0) {
pitch = 0;
} else if (pitch > 3.14159f/2f) {
pitch = 3.14159f/2f;
}
}
}
private void CalculateAngleAroundPlayer() {
if (DisplayManager.isButtonLeftDown()) {
float angleChange = DisplayManager.getDX() * 0.003f;
angleAroundPlayer -= angleChange;
}
}
}

76
src/entities/Entity.java Normal file
View File

@@ -0,0 +1,76 @@
package entities;
import org.atriaSoft.etk.math.Vector3f;
import models.TexturedModel;
public class Entity {
private TexturedModel model;
private Vector3f position;
private Vector3f rotation;
private float scale;
private int textureIndex = 0;
public Entity(TexturedModel model, Vector3f position, Vector3f rotation, float scale) {
this.model = model;
this.position = position;
this.rotation = rotation;
this.scale = scale;
}
public Entity(TexturedModel model, int textureIndex, Vector3f position, Vector3f rotation, float scale) {
this.model = model;
this.textureIndex = textureIndex;
this.position = position;
this.rotation = rotation;
this.scale = scale;
}
public float getTextureXOffset() {
int column = textureIndex%model.getTexture().getNumberOfRows();
return (float)column/(float)model.getTexture().getNumberOfRows();
}
public float getTextureYOffset() {
int row = textureIndex/model.getTexture().getNumberOfRows();
return (float)row/(float)model.getTexture().getNumberOfRows();
}
public void increasePosition(float dx, float dy, float dz) {
this.position = new Vector3f(position.x + dx, position.y + dy, position.z + dz);
}
public void increasePosition(Vector3f delta) {
this.position = new Vector3f(position.x + delta.x, position.y + delta.y, position.z + delta.z);
}
public void increaseRotation(float dx, float dy, float dz) {
this.rotation = new Vector3f(rotation.x + dx, rotation.y + dy, rotation.z + dz);
}
public void increaseRotation(Vector3f delta) {
this.rotation = new Vector3f(rotation.x + delta.x, rotation.y + delta.y, rotation.z + delta.z);
}
public TexturedModel getModel() {
return model;
}
public void setModel(TexturedModel model) {
this.model = model;
}
public Vector3f getPosition() {
return position;
}
public void setPosition(Vector3f position) {
this.position = position;
}
public Vector3f getRotation() {
return rotation;
}
public void setRotation(Vector3f rotation) {
this.rotation = rotation;
}
public float getScale() {
return scale;
}
public void setScale(float scale) {
this.scale = scale;
}
}

37
src/entities/Light.java Normal file
View File

@@ -0,0 +1,37 @@
package entities;
import org.atriaSoft.etk.math.Vector3f;
public class Light {
private Vector3f position;
private Vector3f colour;
private Vector3f attenuation = new Vector3f(1, 0, 0);
public Light(Vector3f position, Vector3f colour) {
this.position = position;
this.colour = colour;
}
public Light(Vector3f position, Vector3f colour, Vector3f attenuation) {
this.position = position;
this.colour = colour;
this.setAttenuation(attenuation);
}
public Vector3f getPosition() {
return position;
}
public void setPosition(Vector3f position) {
this.position = position;
}
public Vector3f getColour() {
return colour;
}
public void setColour(Vector3f colour) {
this.colour = colour;
}
public Vector3f getAttenuation() {
return attenuation;
}
public void setAttenuation(Vector3f attenuation) {
this.attenuation = attenuation;
}
}

96
src/entities/Player.java Normal file
View File

@@ -0,0 +1,96 @@
package entities;
import org.atriaSoft.etk.math.Vector3f;
import models.TexturedModel;
import renderEngine.DisplayManager;
import terrains.Terrain;
public class Player extends Entity {
private static final float RUN_SPEED = 35;
private static final float TRUN_SPEED = (float) Math.toRadians(120);
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(Terrain terrain) {
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);
float terrainHeight = terrain.getHeightOfTerrain(super.getPosition().x, super.getPosition().z);
if (super.getPosition().y < terrainHeight) {
upwardSpeed = 0;
super.getPosition().y = terrainHeight;
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;
}
*/
}
}