Advertisement
Guest User

Untitled

a guest
Mar 28th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.73 KB | None | 0 0
  1. package entities;
  2.  
  3. import static java.lang.Math.cos;
  4. import static java.lang.Math.sin;
  5. import static java.lang.Math.toRadians;
  6.  
  7. import java.util.List;
  8.  
  9. import models.TexturedModel;
  10.  
  11. import org.lwjgl.input.Keyboard;
  12. import org.lwjgl.input.Mouse;
  13. import org.lwjgl.opengl.Display;
  14. import org.lwjgl.util.vector.Vector3f;
  15.  
  16. import renderEngine.DisplayHandler;
  17. import renderEngine.FinalRenderer;
  18. import terrainGens.Terrain;
  19. import engineTester.MainGameLoop;
  20.  
  21. public class Player extends Entity {
  22.  
  23.     public Player(FinalRenderer renderer, TexturedModel model,
  24.             Vector3f position, float rotX, float rotY, float rotZ, float scale, int playerType, List<Player> players, boolean activePlayer, int playerID) {
  25.         super(model, position, rotX, rotY, rotZ, scale, true);
  26.         this.players = players;
  27.         this.renderer = renderer;
  28.         this.playerType = playerType;
  29.         this.activePlayer = activePlayer;
  30.         this.playerID = playerID;
  31.  
  32.     }
  33.  
  34.     // Booleans
  35.     boolean activePlayer;
  36.     public boolean paused = false;
  37.     boolean sprinting = false;
  38.     public boolean isThirdPerson = false;
  39.     public boolean isAirborne = false;
  40.     boolean controlPressed;
  41.     boolean turbo = false;
  42.  
  43.     // Numbers
  44.     public int playerID;
  45.     public int playerType;
  46.     float pitch = 0;
  47.     float yaw = 0;
  48.     float roll = 0;
  49.     private float halfWidth = Display.getWidth() / 2;
  50.     private float halfHeight = Display.getHeight() / 2;
  51.     float sensitivity = 0.5f;
  52.     float speed = 25f;
  53.     private int movementModeNumber = 0;
  54.     int dWheel = Mouse.getDWheel();
  55.     private float sprintMult = (float) 2.5;
  56.     int currentCamNum = 0;
  57.     public float thirdPersonDist = 50;
  58.     public float gravity = -100;
  59.     public float jumpStrength = 500;
  60.     public float upwardVelocity = 0;
  61.     float distance;
  62.     float dx;
  63.     float dz;
  64.     float sdx;
  65.     float sdz;
  66.  
  67.     //Vectors
  68.     private Vector3f velocity = new Vector3f();
  69.     private Vector3f deadVector = new Vector3f();
  70.  
  71.     // Strings / Objects
  72.     String cameraName;
  73.     String movementMode = "normal";
  74.     String[] movementModes = { "normal", "flyCam" };
  75.     Camera currentCamera;
  76.     public FinalRenderer renderer;
  77.     private Terrain currentTerrain;
  78.  
  79.     //Lists
  80.     List<Player> players;
  81.  
  82.     public static Player activePlayer(List<Player> players) {
  83.         Player currentPlayer = null;
  84.         for (Player player:players) {
  85.             if (player.activePlayer) {
  86.                 currentPlayer = player;
  87.             }
  88.         }
  89.         return currentPlayer;
  90.     }
  91.  
  92.     public boolean isPaused() {
  93.         return paused;
  94.     }
  95.  
  96.     public float maxLook() {
  97.  
  98.         if (movementMode == "flyCam" || playerType == 4) {
  99.             return 999999999;
  100.         } else
  101.             return 90;
  102.  
  103.     }
  104.  
  105.     public float tick() {
  106.         return DisplayHandler.getFrameTimeSeconds();
  107.     }
  108.  
  109.     public void setThirdPersonDist(int change) {
  110.         this.thirdPersonDist -= change / 10;
  111.     }
  112.  
  113.     public void jump(Float jumpMult) {
  114.         if (!isAirborne) {
  115.             this.velocity.y += jumpStrength * jumpMult;
  116.             isAirborne = !isAirborne;
  117.         }
  118.     }
  119.  
  120.     public float movementSpeed() {
  121.         if (turbo) {
  122.             return speed * 5;
  123.         } else return speed;
  124.     }
  125.  
  126.     public Player nextPlayer() {
  127.         Player nextPlayer;
  128.         if (this.playerID < players.size()) {
  129.             nextPlayer = players.get(this.playerID);
  130.         } else
  131.             nextPlayer = players.get(0);
  132.         return nextPlayer;
  133.     }
  134.  
  135.     public void increaseRotation(float pitchChange, float yawChange, float rollChange) {
  136.  
  137.         pitch += pitchChange;
  138.         yaw += yawChange;
  139.  
  140.     }
  141.  
  142.     private float terrainHeight(Terrain terrain) {
  143.  
  144.         if (terrain == null) {
  145.             return 0;
  146.         } else return terrain.getHeightOfTerrain(super.getPosition().x, super.getPosition().z);
  147.  
  148.     }
  149.    
  150.     public void checkCollision() {
  151.        
  152.         Vector3f.add(super.getPosition(), this.velocity, super.getPosition());
  153.         this.velocity.set(deadVector);
  154.        
  155.     }
  156.    
  157.     public void move(Terrain terrain) {
  158.  
  159.         this.currentTerrain = terrain;
  160.         this.getCollisionShell().generateCorners(this);
  161.         checkInputs();
  162.         checkCollision();
  163.        
  164.     }
  165.  
  166.     public float angleVers() {
  167.         float a = 0;
  168.         if (this.isThirdPerson == true) {
  169.             a = -(this.getRotY());
  170.         } else if (this.isThirdPerson == false) {
  171.             a = this.yaw;
  172.         }
  173.         return a;
  174.     }
  175.  
  176.     public float sAngleVers() {
  177.         float a = 0;
  178.         if (this.isThirdPerson == true) {
  179.             a = -(super.getRotY() - 90);
  180.         } else if (this.isThirdPerson == false) {
  181.             a = this.yaw + 90;
  182.         }
  183.         return a;
  184.     }
  185.  
  186.     public void getOtherKeys() {
  187.  
  188.         while (Keyboard.next()) {
  189.  
  190.             if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
  191.                 MainGameLoop.paused = !MainGameLoop.paused;
  192.                 Mouse.setCursorPosition((int) halfWidth, (int) halfHeight);
  193.             }
  194.             if (Keyboard.isKeyDown(Keyboard.KEY_DECIMAL)) {
  195.                 MainGameLoop.pausedTime = !MainGameLoop.pausedTime;
  196.             }
  197.             if (Keyboard.isKeyDown(Keyboard.KEY_M)) {
  198.                 if (this.playerType ==  3) {
  199.                     movementMode = movementModes[movementModeNumber];
  200.                     if (movementModeNumber + 1 >= movementModes.length) {
  201.                         movementModeNumber = 0;
  202.                     } else
  203.                         movementModeNumber += 1;
  204.                 }
  205.             }
  206.             if (Keyboard.isKeyDown(Keyboard.KEY_C)) {
  207.                 if (this.playerType == 2 || this.playerType == 3) {
  208.                     isThirdPerson = !isThirdPerson;
  209.                 }
  210.             }
  211.             if (Keyboard.isKeyDown(Keyboard.KEY_ADD)) {
  212.                 renderer.getSkyboxRenderer().time += 1000;
  213.             }
  214.             if (Keyboard.isKeyDown(Keyboard.KEY_SUBTRACT)) {
  215.                 renderer.getSkyboxRenderer().time -= 1000;
  216.             }
  217.             if (Keyboard.isKeyDown(Keyboard.KEY_7)) {
  218.                 activePlayer = !activePlayer;
  219.                 nextPlayer().activePlayer = true;
  220.             }
  221.             if (Keyboard.isKeyDown(Keyboard.KEY_LCONTROL)) {
  222.                 turbo = !turbo;
  223.             }
  224.             if (Keyboard.isKeyDown(Keyboard.KEY_F11)) {
  225.                 DisplayHandler.setDisplayMode(Display.getDesktopDisplayMode().getWidth(), Display.getDesktopDisplayMode().getHeight(), true);
  226.             }
  227.             if (Keyboard.isKeyDown(Keyboard.KEY_F12)) {
  228.                 DisplayHandler.setDisplayMode(Display.getDesktopDisplayMode().getWidth(), Display.getDesktopDisplayMode().getHeight(), false);
  229.             }
  230.             if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
  231.                 this.setScale(this.getScale() + 0.1f);
  232.             }
  233.             if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
  234.                 this.setScale(this.getScale() - 0.1f);
  235.             }
  236.             if (Keyboard.isKeyDown(Keyboard.KEY_T)) {
  237.                 this.setPosition(new Vector3f(0, 0, 0));
  238.             }
  239.             if (Keyboard.isKeyDown(Keyboard.KEY_H)) {
  240.                 System.out.println("X-Angle: " + this.pitch);
  241.                 System.out.println("Y-Angle: " + this.yaw);
  242.                 System.out.println("Z-Angle: " + this.roll);
  243.                 System.out.println("RotY: " + this.getRotY());
  244.                 System.out.println("RotX: " + this.getRotX());
  245.                 System.out.println("FOV: " + renderer.FOV);
  246.                 System.out.println("TPD: " + this.thirdPersonDist);
  247.                 System.out.println("Turbo: " + turbo);
  248.                 System.out.println("Fullscreen:" + DisplayHandler.fullscreen);
  249.                 System.out.println("Render Width:" + DisplayHandler.getRenderWidth());
  250.                 System.out.println("Render Height:" + DisplayHandler.getRenderHeight());
  251.                 System.out.println(activePlayer);
  252.                 System.out.println(this.playerID);
  253.                 System.out.println("X-Pos:" + super.getPosition().x);
  254.                 System.out.println("Y-Pos:" + super.getPosition().y);
  255.                 System.out.println("Z-Pos:" + super.getPosition().z);
  256.             }
  257.  
  258.         }
  259.  
  260.     }
  261.  
  262.     public void checkInputs() {
  263.        
  264.         getOtherKeys();
  265.        
  266.         this.distance = movementSpeed() * DisplayHandler.getFrameTimeSeconds();
  267.         this.dx = (float) (sin(toRadians(angleVers())) * this.distance);
  268.         this.dz = (float) (cos(toRadians(angleVers())) * this.distance);
  269.         this.sdx = (float) (sin(toRadians(sAngleVers())) * this.distance);
  270.         this.sdz = (float) (cos(toRadians(sAngleVers())) * this.distance);
  271.  
  272.         if (movementMode == "flyCam") {
  273.  
  274.             float dy = (float) -(cos(toRadians(pitch + 90)) * this.distance);
  275.  
  276.             // Ascend or Descend
  277.             if (Keyboard.isKeyDown(Keyboard.KEY_E)) {
  278.                 if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
  279.                     this.velocity.y += sprintMult * 2 * this.distance;
  280.                 } else {
  281.                     this.velocity.y += this.distance;
  282.                 }
  283.             } else if (Keyboard.isKeyDown(Keyboard.KEY_Q)) {
  284.                 if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
  285.                     this.velocity.y += sprintMult * 2 * -this.distance;
  286.                 } else {
  287.                     this.velocity.y += -this.distance;
  288.                 }
  289.             } else {
  290.                 this.velocity.y += 0;
  291.             }
  292.            
  293.             // Front and Back
  294.             if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
  295.                 if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
  296.                     this.velocity.z -= sprintMult * 2 * -dz;
  297.                     this.velocity.x -= sprintMult * 2 * dx;
  298.                     this.velocity.y -= sprintMult * 2 * dy;
  299.                 } else {
  300.                     this.velocity.z -= -dz;
  301.                     this.velocity.x -= dx;
  302.                     this.velocity.y -= dy;
  303.                 }
  304.             } else if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
  305.                 if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
  306.                     this.velocity.z -= sprintMult * 2 * dz;
  307.                     this.velocity.x -= sprintMult * 2 * -dx;
  308.                     this.velocity.y -= sprintMult * 2 * -dy;
  309.                 } else {
  310.                     this.velocity.z -= dz;
  311.                     this.velocity.x -= -dx;
  312.                     this.velocity.y -= -dy;
  313.                 }
  314.             } else {
  315.                 this.velocity.z -= 0;
  316.                 this.velocity.x -= 0;
  317.                 this.velocity.y -= 0;
  318.             }
  319.  
  320.         }
  321.  
  322.         //Jumping
  323.         if (movementMode != "flyMode") {
  324.             if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
  325.                 if (turbo) {
  326.                     jump(1.5f);
  327.                 } else {
  328.                     jump(1f);
  329.                 }
  330.             }
  331.         }
  332.  
  333.         // Front and Back
  334.         if (playerType != 4 && playerType != 5) {
  335.             if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
  336.                 if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
  337.                     this.velocity.z -= sprintMult * -dz;
  338.                     this.velocity.x -= sprintMult * dx;
  339.                 } else {
  340.                     this.velocity.z -= -dz;
  341.                     this.velocity.x -= dx;
  342.                 }
  343.             } else if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
  344.                 if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
  345.                     this.velocity.z -= sprintMult * dz;
  346.                     this.velocity.x -= sprintMult * -dx;
  347.                 } else {
  348.                     this.velocity.z -= dz;
  349.                     this.velocity.x -= -dx;
  350.                 }
  351.             } else {
  352.                 this.velocity.z -= 0;
  353.                 this.velocity.x -= 0;
  354.             }
  355.         }
  356.  
  357.         // Sideways
  358.         if (playerType != 4 && playerType != 5) {
  359.             if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
  360.                 this.velocity.z -= -sdz;
  361.                 this.velocity.x -= sdx;
  362.             }
  363.             else if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
  364.                 this.velocity.z -= sdz;
  365.                 this.velocity.x -= -sdx;
  366.             }
  367.         }
  368.  
  369.         Mouse.setGrabbed(!MainGameLoop.paused);
  370.  
  371.         if (!MainGameLoop.paused && playerType != 5) {
  372.  
  373.             if (isThirdPerson == false) {
  374.                 super.setRotY(-yaw);
  375.  
  376.                 float maxLookUp = maxLook();
  377.                 float maxLookDown = -maxLook();
  378.                 float mouseDX = (Mouse.getX() - halfWidth) * sensitivity;
  379.                 float mouseDY = (Mouse.getY() - halfHeight) * sensitivity;
  380.                 if (yaw + mouseDX >= 360) {
  381.                     yaw = yaw + mouseDX - 360;
  382.                 } else if (yaw + mouseDX < 0) {
  383.                     yaw = 360 - yaw + mouseDX;
  384.                 } else {
  385.                     yaw += mouseDX;
  386.                 }
  387.                 if (pitch - mouseDY >= maxLookDown
  388.                         && pitch - mouseDY <= maxLookUp) {
  389.                     pitch += -mouseDY;
  390.                 } else if (pitch - mouseDY < maxLookDown) {
  391.                     pitch = maxLookDown;
  392.                 } else if (pitch - mouseDY > maxLookUp) {
  393.                     pitch = maxLookUp;
  394.                 }
  395.  
  396.             }
  397.  
  398.             float terrainHeight = terrainHeight(this.currentTerrain);
  399.  
  400.             if (movementMode != "flyCam") {
  401.                 upwardVelocity += gravity * tick();
  402.                 this.velocity.y += upwardVelocity;
  403.                 //super.increasePosition(0, upwardVelocity * tick(), 0);
  404.             }
  405.  
  406.             if (super.getPosition().y + this.velocity.y < terrainHeight) {
  407.                 this.velocity.y = 0;
  408.                 super.getPosition().y = terrainHeight;
  409.                 isAirborne = false;
  410.             }
  411.  
  412.             if (isThirdPerson == true) {
  413.                 float mouseDX = (Mouse.getX() - halfWidth) * sensitivity;
  414.                 float mouseDY = (Mouse.getY() - halfHeight) * sensitivity;
  415.                 if (yaw + mouseDX >= 360) {
  416.                     yaw = yaw + mouseDX - 360;
  417.                 } else if (yaw + mouseDX < 0) {
  418.                     yaw = 360 - yaw + mouseDX;
  419.                 } else {
  420.                     yaw += mouseDX;
  421.                 }
  422.                 if (pitch - mouseDY >= 0 && pitch - mouseDY <= 30) {
  423.                     pitch += -mouseDY / 2;
  424.                 } else if (pitch - mouseDY < 0) {
  425.                     pitch = 0;
  426.                 } else if (pitch - mouseDY > 30) {
  427.                     pitch = 30;
  428.                 }
  429.                 super.setRotY(-yaw);
  430.             }
  431.  
  432.             Mouse.setCursorPosition((int) halfWidth, (int) halfHeight);
  433.  
  434.         }
  435.  
  436.     }
  437.  
  438. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement