Player.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package eu.tankernn.gameEngine.entities;
  2. import org.lwjgl.input.Keyboard;
  3. import org.lwjgl.input.Mouse;
  4. import org.lwjgl.util.vector.Vector3f;
  5. import eu.tankernn.gameEngine.animation.animatedModel.AnimatedModel;
  6. import eu.tankernn.gameEngine.loader.models.AABB;
  7. import eu.tankernn.gameEngine.loader.models.TexturedModel;
  8. import eu.tankernn.gameEngine.renderEngine.DisplayManager;
  9. import eu.tankernn.gameEngine.settings.Physics;
  10. import eu.tankernn.gameEngine.terrains.Terrain;
  11. import eu.tankernn.gameEngine.terrains.TerrainPack;
  12. public class Player extends Entity3D {
  13. private static final float RUN_SPEED = 20;
  14. protected static final float TURN_MAX = 160;
  15. private static final float JUMP_POWER = 30;
  16. protected TerrainPack terrainPack;
  17. protected float currentSpeed = 0;
  18. protected float currentTurnSpeed = 0;
  19. protected float upwardsSpeed = 0;
  20. private boolean isInAir = false;
  21. private float height = 2.0f;
  22. public Player(TexturedModel model, Vector3f position, Vector3f rotation, float scale, AABB boundingBox, TerrainPack terrainPack) {
  23. super(model, position, rotation, scale, boundingBox);
  24. this.terrainPack = terrainPack;
  25. }
  26. public void move() {
  27. checkInputs();
  28. super.increaseRotation(new Vector3f(0, currentTurnSpeed * DisplayManager.getFrameTimeSeconds(), 0));
  29. float distance = currentSpeed * DisplayManager.getFrameTimeSeconds();
  30. float dx = (float) (distance * Math.sin(Math.toRadians(super.getRotation().y)));
  31. float dz = (float) (distance * Math.cos(Math.toRadians(super.getRotation().y)));
  32. super.increasePosition(dx, 0, dz);
  33. upwardsSpeed += Physics.GRAVITY * DisplayManager.getFrameTimeSeconds();
  34. super.increasePosition(0, upwardsSpeed * DisplayManager.getFrameTimeSeconds(), 0);
  35. Terrain currentTerrain = terrainPack.getTerrainByWorldPos(this.getPosition().x, this.getPosition().z);
  36. float terrainHeight = 0;
  37. if (currentTerrain != null) {
  38. terrainHeight = currentTerrain.getHeightOfTerrain(super.getPosition().x, super.getPosition().z);
  39. }
  40. if (super.getPosition().getY() < terrainHeight) {
  41. upwardsSpeed = 0;
  42. this.isInAir = false;
  43. super.getPosition().y = terrainHeight;
  44. } else {
  45. this.isInAir = true;
  46. }
  47. }
  48. private void jump() {
  49. if (!this.isInAir) {
  50. this.upwardsSpeed = JUMP_POWER;
  51. this.isInAir = true;
  52. }
  53. }
  54. protected void checkInputs() {
  55. if (Keyboard.isKeyDown(Keyboard.KEY_W) || (Mouse.isButtonDown(0) && Mouse.isButtonDown(1))) {
  56. if (this.getModel() instanceof AnimatedModel)
  57. ((AnimatedModel) getModel()).doAnimation(0);
  58. this.currentSpeed = RUN_SPEED;
  59. } else if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
  60. this.currentSpeed = -RUN_SPEED;
  61. } else {
  62. this.currentSpeed = 0;
  63. if (this.getModel() instanceof AnimatedModel)
  64. ((AnimatedModel) getModel()).doAnimation(null);
  65. }
  66. if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
  67. this.currentTurnSpeed = TURN_MAX;
  68. } else if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
  69. this.currentTurnSpeed = -TURN_MAX;
  70. } else {
  71. this.currentTurnSpeed = 0;
  72. }
  73. if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
  74. jump();
  75. }
  76. if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) && Keyboard.isKeyDown(Keyboard.KEY_W)) {
  77. this.currentSpeed = 5 * RUN_SPEED;
  78. }
  79. }
  80. public float getHeight() {
  81. return height;
  82. }
  83. public void setHeight(float height) {
  84. this.height = height;
  85. }
  86. public Terrain getCurrentTerrain() {
  87. return terrainPack.getTerrainByWorldPos(this.getPosition().x, this.getPosition().z);
  88. }
  89. }