Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2013
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics2D;
  3.  
  4. public class Player {
  5. private double x;
  6. private double y;
  7.  
  8. private double dx;
  9. private double dy;
  10. // player speed
  11. private double moveSpeed = 0.6;
  12. // player cant go faster than this speed
  13. private double maxSpeed = 4.2;
  14. // stop slowing by
  15. private double stopSpeed = 0.30;
  16.  
  17. private double maxFallingSpeed = 12;
  18. private double jumpStart = -11.0;
  19. // pull you down with this speed
  20. private double gravity = 0.64;
  21.  
  22. private int width = 22;
  23. private int height = 22;
  24.  
  25. // user press key
  26. private boolean left;
  27. private boolean right;
  28. private boolean jumping;
  29. private boolean falling;
  30.  
  31. // check if player can moves to these tiles
  32. // true = cant move
  33. private boolean topLeft;
  34. private boolean topRight;
  35. private boolean bottomLeft;
  36. private boolean bottomRight;
  37.  
  38. private TileMap tileMap;
  39.  
  40. /**************************/
  41. /*** CONSTRUCTOR METHOD ***/
  42. /**************************/
  43. public Player(TileMap tm) {
  44. this.tileMap = tm;
  45. }
  46.  
  47. // can jump when not falling
  48. public void setJumping(boolean b) {
  49. if (!falling) {
  50. jumping = true;
  51. }
  52. }
  53.  
  54. /*******************/
  55. /*** player Move ***/
  56. private void playerMove() {
  57. // Determine if Player is moving left or right
  58. if (left) { // is moving left
  59. dx -= moveSpeed; // move left
  60. if (dx < -maxSpeed) { // dont go faster than maxSpeed
  61. dx = -maxSpeed;
  62. }
  63. } else if (right) { // is moving right
  64. dx += moveSpeed; // move right
  65. if (dx > maxSpeed) { // dont go faster than maxSpeed
  66. dx = maxSpeed;
  67. }
  68. } else { // not moving so coming to stop
  69. if (dx > 0) { // still moving right
  70. dx -= stopSpeed; // start slowing down lil by lil
  71. if (dx < 0) { // full stop
  72. dx = 0;
  73. }
  74. } else if (dx < 0) { // was moving left
  75. dx += stopSpeed; // start slowing down lil by lil
  76. if (dx > 0) { // full stop
  77. dx = 0;
  78. }
  79. }
  80. }
  81.  
  82. // Determine if player is jumping or falling
  83. if (jumping) {
  84. dy = jumpStart; // start moving up fast
  85. falling = true;
  86. jumping = false;
  87. }
  88. if (falling) {
  89. dy += gravity; // slowly come down
  90. if (dy > maxFallingSpeed) { // dont go fall faster than maxSpeed
  91. dy = maxFallingSpeed;
  92. }
  93. } else { // on ground
  94. dy = 0;
  95. }
  96. }// End of playerMove method
  97.  
  98. /*******************************/
  99. /*** calculateCorners method ***/
  100. public void calculateCorners(double x, double y) {
  101. int leftTile = tileMap.getColTile((int) (x - width / 2));
  102. int rightTile = tileMap.getColTile((int) (x + width / 2) - 1);
  103. int topTile = tileMap.getColTile((int) (y - height / 2));
  104. int bottomTile = tileMap.getColTile((int) (y + height / 2) - 1);
  105.  
  106. // check if player can move to tiles
  107. // 0 = cant move up
  108. topLeft = tileMap.getTile(topTile, leftTile) == 0;
  109. topRight = tileMap.getTile(topTile, rightTile) == 0;
  110. bottomLeft = tileMap.getTile(bottomTile, leftTile) == 0;
  111. bottomRight = tileMap.getTile(bottomTile, rightTile) == 0;
  112. }// End of calculateCorners
  113.  
  114. /************************************/
  115. /*** playerCollision method ***/
  116. public void playerCollision() {
  117. // get current tile where player is on
  118. int currCol = tileMap.getColTile((int) x);
  119. int currRow = tileMap.getRowTile((int) y);
  120.  
  121. // player next distence x and y
  122. // just add dx to x
  123. double tox = x + dx;
  124. double toy = y + dy;
  125.  
  126. // dont way to change real x-y yet
  127. double tempX = x;
  128. double tempY = y;
  129.  
  130. // check if player can move up or down
  131. calculateCorners(x, toy);
  132. if (dy < 0) { // moving up
  133. if (topLeft || topRight) { // cant move up
  134. dy = 0;
  135. tempY = currRow * tileMap.getTileSize() + height / 2;
  136. } else { // free to move up
  137. tempY += dy;
  138. }
  139. }
  140. if (dy > 0) { // move down
  141. if (bottomLeft || bottomRight) { // cant move down
  142. dy = 0;
  143. falling = false;
  144. tempY = (currRow + 1) * tileMap.getTileSize() - height / 2;
  145. } else { // free to move down
  146. tempY += dy;
  147. }
  148. }
  149.  
  150. // check if player can move right or left
  151. calculateCorners(tox, y);
  152. if (dx < 0) { // moving left
  153. if (topLeft || bottomLeft) {
  154. dx = 0;
  155. tempX = currCol * tileMap.getTileSize() + width / 2;
  156. } else { // free to left
  157. tempX += dx;
  158. }
  159. }
  160. if (dx > 0) { // move right
  161. if (topRight || bottomRight) {
  162. dx = 0;
  163. tempX = (currCol + 1) * tileMap.getTileSize() - width / 2;
  164. } else {
  165. tempX += dx;
  166. }
  167. }
  168.  
  169. // check for falling in hole
  170. if (!falling) {
  171. calculateCorners(x, y + 1);
  172. if (!bottomLeft && !bottomRight) {
  173. falling = true;
  174. }
  175. }
  176.  
  177. // set x and y from temp values
  178. x = tempX;
  179. y = tempY;
  180.  
  181. // move the map
  182. tileMap.setX((int) (GamePanel.WINDOW_WIDTH / 2 - x));
  183. tileMap.setY((int) (GamePanel.WINDOW_HEIGHT / 2 - y));
  184. }// End of playerCollision method
  185.  
  186. /*********************/
  187. /*** update METHOD ***/
  188. /*********************/
  189. public void update() {
  190. playerMove();
  191. playerCollision();
  192. } // End of update method
  193.  
  194. /*********************/
  195. /*** draw METHOD *****/
  196. /*********************/
  197. public void draw(Graphics2D g) {
  198. int tx = tileMap.getX();
  199. int ty = tileMap.getY();
  200.  
  201. // (x-width/2) = so its middle point player
  202. g.setColor(Color.RED);
  203. g.fillRect((int) (tx + (x - width / 2)), (int) (ty + (y - height / 2)),
  204. width, height);
  205. }
  206.  
  207. /***************************/
  208. /***************************/
  209. /***************************/
  210. /***************************/
  211. /***************************/
  212. /*** GETTERS AND SETTERS ***/
  213. /***************************/
  214. /***************************/
  215. /***************************/
  216. /***************************/
  217. /***************************/
  218. public void setLeft(boolean left) {
  219. this.left = left;
  220. }
  221.  
  222. public void setRight(boolean right) {
  223. this.right = right;
  224. }
  225.  
  226. public void setX(double x) {
  227. this.x = x;
  228. }
  229.  
  230. public void setY(double y) {
  231. this.y = y;
  232. }
  233.  
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement