Advertisement
Guest User

Untitled

a guest
Jun 29th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. package net.flodro.minigame;
  2.  
  3. import java.awt.Graphics;
  4. import java.awt.Point;
  5. import java.awt.Rectangle;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8. import java.util.ArrayList;
  9. import net.flodro.minigame.graphics.DoubleRectangle;
  10. import net.flodro.minigame.graphics.Images;
  11. import net.flodro.minigame.Player;
  12. import net.flodro.minigame.MiniGame;
  13.  
  14. public class Player extends DoubleRectangle implements KeyListener {
  15.  
  16. // private int lives = 3;
  17. private int speed = 1;
  18.  
  19. public static int jumpTime = 92;
  20. private double jumpSpeed = 0.5;
  21. public static int jumpCount = 0;
  22.  
  23. public int moving = 0;
  24. public static boolean jumped = false;
  25. public static boolean isJumping = false;
  26. public static boolean isFalling = false;
  27.  
  28. public Point pt1;
  29. public Point pt2;
  30. public Point pt3;
  31. public Point pt4;
  32. public Point pt5;
  33. public Point pt6;
  34. public Point pt7;
  35. public Point pt8;
  36. public Point pt9;
  37. public Point pt10;
  38. public Point pt11;
  39. public Point pt12;
  40. public Point pt13;
  41. public Point pt14;
  42.  
  43. public static boolean topcollide = false;
  44. public static boolean leftcollide = false;
  45. public static boolean rightcollide = false;
  46. public static boolean bottomcollide = false;
  47. public int x;
  48. public int y;
  49.  
  50. public static ArrayList<Rectangle> floor = new ArrayList<Rectangle>();
  51. public static ArrayList<String> floorStatus = new ArrayList<String>();
  52.  
  53. public Player(int x, int y, int width, int height) {
  54. setBounds(x, y, width, height);
  55. this.x = x;
  56. this.y = y;
  57.  
  58. pt1 = new Point(0, 0); // Top Left
  59. pt2 = new Point(0, (height / 2)); // Middle Left
  60. pt3 = new Point(0, height); // Bottom Left
  61. pt4 = new Point(width, 0); // Top Right
  62. pt5 = new Point(width, (height / 2)); // Middle Right
  63. pt6 = new Point(width, height); // Bottom Right
  64. pt7 = new Point(1, 0); // Top Left left+1
  65. pt8 = new Point(0, 1); // Top Left down+1
  66. pt9 = new Point(1, height); // Bottom Left left+1
  67. pt10 = new Point(0, height - 1); // Bottom Left up+1
  68. pt11 = new Point(width - 1, 0); // Top Right right+1
  69. pt12 = new Point(width, 1); // Top Right down+1
  70. pt13 = new Point(width - 1, height); // Bottom Right right+1
  71. pt14 = new Point(width, height - 1); // Bottom Right up+1
  72.  
  73. if(MiniGame.currentScreen == 1) {
  74. floor.clear();
  75. floorStatus.clear();
  76. floor.add(new Rectangle(150, 220, 150, 30));
  77. floor.add(new Rectangle(0, 150, MiniGame.WIN_WIDTH, 30));
  78. x=0;
  79. y=0;
  80. for(int i = 0; i < floor.size(); i++) { floorStatus.add("Nothing"); }
  81. } else if(MiniGame.currentScreen == 2) {
  82. floor.clear();
  83. floorStatus.clear();
  84. floor.add(new Rectangle(0, 195, MiniGame.WIN_WIDTH, 30));
  85. floor.add(new Rectangle(0, 150, MiniGame.WIN_WIDTH, 30));
  86. x=0;
  87. y=0;
  88. for(int i = 0; i < floor.size(); i++) { floorStatus.add("Nothing"); }
  89. } else {
  90. floor.clear();
  91. floorStatus.clear();
  92. floor.add(new Rectangle(0, 150, MiniGame.WIN_WIDTH, 30));
  93. x=0;
  94. y=0;
  95. for(int i = 0; i < floor.size(); i++) { floorStatus.add("Nothing"); }
  96. }
  97.  
  98.  
  99. }
  100.  
  101. public void tick() {
  102. if (isJumping && !topcollide) {
  103. y -= jumpSpeed;
  104. jumpCount++;
  105. if(jumpCount == jumpTime) {
  106. jumpCount = 0;
  107. isJumping = false;
  108. jumped = false;
  109. isFalling = true;
  110. }
  111. } else if (isJumping && topcollide) {
  112. jumpCount = 0;
  113. topcollide = false;
  114. isFalling = true;
  115. }
  116. if(isFalling) {
  117. y++;
  118. }
  119. if(!bottomcollide && !isJumping) {
  120. y++;
  121. topcollide = false;
  122. isFalling = true;
  123. }
  124. if(x < 0) { } else {
  125. if(moving == 1 && !leftcollide) {
  126. x -= speed;
  127. }
  128. }
  129. if (x > (MiniGame.WIN_WIDTH - this.width) ) { } else {
  130. if(moving == 2 && !rightcollide) {
  131. x += speed;
  132. }
  133. }
  134.  
  135. pt1.setLocation(x, y);
  136. pt2.setLocation(x, y + (height / 2));
  137. pt3.setLocation(x, y + height);
  138. pt4.setLocation(x + width, y);
  139. pt5.setLocation(x + width, y + (height / 2));
  140. pt6.setLocation(x + width, y + height);
  141. pt7.setLocation(x + 1, y);
  142. pt8.setLocation(x, y + 1);
  143. pt9.setLocation(x + 1, y + height);
  144. pt10.setLocation(x, y + height - 1);
  145. pt11.setLocation(x + width - 1, y);
  146. pt12.setLocation(x + width, y + 1);
  147. pt13.setLocation(x + width - 1, y + height);
  148. pt14.setLocation(x + width, y + height - 1);
  149.  
  150. for (int i = 0; i < floor.size(); i++) {
  151. if (floor.get(i).contains(pt1) && floor.get(i).contains(pt8) || floor.get(i).contains(pt3) && floor.get(i).contains(pt10)) {
  152. floorStatus.set(i, "Left Collision");
  153. } else if (floor.get(i).contains(pt3) && floor.get(i).contains(pt9) || floor.get(i).contains(pt6) && floor.get(i).contains(pt13)) {
  154. floorStatus.set(i, "Bot Collision");
  155. } else if (floor.get(i).contains(pt4) && floor.get(i).contains(pt12) || floor.get(i).contains(pt6) && floor.get(i).contains(pt14)) {
  156. floorStatus.set(i, "Right Collision");
  157. } else if (floor.get(i).contains(pt1) && floor.get(i).contains(pt7) || floor.get(i).contains(pt4) && floor.get(i).contains(pt11)) {
  158. floorStatus.set(i, "Top Collision");
  159. } else {
  160. floorStatus.set(i, "Nothing");
  161. }
  162. }
  163.  
  164. if(floorStatus.contains("Bot Collision")) { bottomcollide = true; } else { bottomcollide = false; }
  165. if(floorStatus.contains("Right Collision")) { rightcollide = true; } else { rightcollide = false; }
  166. if(floorStatus.contains("Left Collision")) { leftcollide = true; } else { leftcollide = false; }
  167. if(floorStatus.contains("Top Collision")) { topcollide = true; } else { topcollide = false; }
  168. System.out.println("TOP " + topcollide + " LEFT " + leftcollide + " RIGHT " + rightcollide + " BOTTOM " + bottomcollide);
  169.  
  170. }
  171.  
  172. public void render(Graphics g) {
  173. g.drawImage(Images.player, (int) x, (int) y, (int) width, (int) height, null);
  174. }
  175.  
  176. @Override
  177. public void keyPressed(KeyEvent e) {
  178. int key = e.getKeyCode();
  179.  
  180. switch(key) {
  181. case KeyEvent.VK_UP:
  182. if(!jumped && !Player.isFalling) { Player.isJumping = true; jumped = true; }
  183. break;
  184. case KeyEvent.VK_LEFT:
  185. moving = 1;
  186. break;
  187. case KeyEvent.VK_RIGHT:
  188. moving = 2;
  189. break;
  190. }
  191. }
  192.  
  193. @Override
  194. public void keyReleased(KeyEvent e) {
  195. int key = e.getKeyCode();
  196.  
  197. switch(key) {
  198. case KeyEvent.VK_LEFT:
  199. moving = 0;
  200. break;
  201. case KeyEvent.VK_RIGHT:
  202. moving = 0;
  203. break;
  204. }
  205. }
  206.  
  207. @Override
  208. public void keyTyped(KeyEvent e) { }
  209.  
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement