Guest User

Untitled

a guest
Apr 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.23 KB | None | 0 0
  1. public class GameState extends JFrame implements KeyListener {
  2. Container contentPane=this.getContentPane();
  3. Graphics bufferGraphics;
  4.  
  5. int characterX=463;
  6. int characterY=486;
  7. int oldCharacterX=463;
  8. int oldCharacterY=486;
  9. int xAxis;
  10. int yAxis;
  11. Image characterNorth = CustomImages.createImageIcon("Images/characterNorth.jpg").getImage();
  12. Image characterEast = CustomImages.createImageIcon("Images/characterEast.jpg").getImage();
  13. Image characterSouth = CustomImages.createImageIcon("Images/characterSouth.jpg").getImage();
  14. Image characterWest = CustomImages.createImageIcon("Images/characterWest.jpg").getImage();
  15. Image brickWall = CustomImages.createImageIcon("Images/brickWall.jpg").getImage();
  16. Image brickFloor = CustomImages.createImageIcon("Images/brickFloor.jpg").getImage();
  17. Image character=characterNorth;
  18. boolean pressed=false;
  19.  
  20. ArrayList<RoomState> map = new ArrayList<RoomState>();
  21. RoomState currentRoom = new RoomState();
  22. RoomState currentRoomState=new RoomState();
  23.  
  24.  
  25. GameState() {
  26. this.setBounds(0, 0, 1680, 1050);
  27. this.setVisible(true);
  28. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29. addKeyListener(this);
  30. setFocusable(true);
  31. requestFocusInWindow();
  32. }
  33.  
  34. public void move(int x, int y) { //Check Move
  35. currentRoomState=currentRoomState.MoveToNextRoom(true, false, false, false);
  36. currentRoomState=currentRoomState.MoveToNextRoom(false, true, false, false);
  37. currentRoomState=currentRoomState.MoveToNextRoom(false, false, true, false);
  38. currentRoomState=currentRoomState.MoveToNextRoom(false, false, false, true);
  39. }
  40.  
  41. public void paint(Graphics g) { //Graphics
  42. for(xAxis=58;xAxis<=858;xAxis=xAxis+50) {
  43. for(yAxis=81;yAxis<=881;yAxis=yAxis+50) {
  44. g.drawImage(brickFloor,xAxis,yAxis,null);
  45. }
  46. yAxis=31;
  47. }
  48. for(xAxis=8;xAxis<958;xAxis=xAxis+50) {
  49. g.drawImage(brickWall,xAxis,yAxis,null);
  50. }
  51. yAxis=931;
  52. for(xAxis=8;xAxis<=908;xAxis=xAxis+50) {
  53. g.drawImage(brickWall,xAxis,yAxis,null);
  54. }
  55. xAxis=8;
  56. for(yAxis=81;yAxis<=881;yAxis=yAxis+50) {
  57. g.drawImage(brickWall,xAxis,yAxis,null);
  58. }
  59. xAxis=908;
  60. for(yAxis=81;yAxis<=881;yAxis=yAxis+50) {
  61. g.drawImage(brickWall,xAxis,yAxis,null);
  62. }
  63.  
  64. if(currentRoom.northDoor) {
  65. g.drawImage(brickFloor,458,31,null);
  66. }
  67. if(currentRoom.eastDoor) {
  68. g.drawImage(brickFloor,908,481,null);
  69. }
  70. if(currentRoom.southDoor) {
  71. g.drawImage(brickFloor,458,931,null);
  72. }
  73. if(currentRoom.westDoor) {
  74. g.drawImage(brickFloor,8,481,null);
  75. }
  76.  
  77. g.drawImage(character,characterX,characterY,null);
  78.  
  79. String minimap=Arrays.toString(map.toArray());
  80. g.drawString(minimap,975,500);
  81.  
  82. }
  83.  
  84. @Override
  85. public void keyPressed(KeyEvent arg0) { //Character Rotation/Movement.
  86. if(pressed==false) {
  87. pressed=true;
  88. oldCharacterX=characterX;
  89. oldCharacterY=characterY;
  90. if(arg0.getKeyCode() == KeyEvent.VK_W || arg0.getKeyCode() == KeyEvent.VK_UP) {
  91. if(character==characterNorth) {
  92. if(characterY>86 && characterX>13 && characterX<913) {
  93. characterY=characterY-50;
  94. }else if(currentRoom.northDoor && characterX==463) {
  95. oldCharacterY=characterY;
  96. characterY=characterY-50;
  97. if(characterY==-14) {
  98. if(currentRoom.rs_NorthDoor != null) {
  99. currentRoom=currentRoom.rs_NorthDoor;
  100. }else {
  101. RoomState nextRoom = new RoomState(currentRoom,false, false, true, false);
  102. currentRoom.rs_NorthDoor = nextRoom;
  103. map.add(nextRoom);
  104. currentRoom = nextRoom;
  105. nextRoom = null;
  106. }
  107. characterX=463;
  108. characterY=936;
  109. repaint();
  110. }
  111. }
  112. }else {
  113. character=characterNorth;
  114. }
  115. }
  116. if(arg0.getKeyCode() == KeyEvent.VK_A || arg0.getKeyCode() == KeyEvent.VK_LEFT) {
  117. if(character==characterWest && characterY>36 && characterY<926) {
  118. if(characterX>63) {
  119. oldCharacterX=characterX;
  120. characterX=characterX-50;
  121. }else if(currentRoom.westDoor && characterY==486) {
  122. oldCharacterX=characterX;
  123. characterX=characterX-50;
  124. if(characterX==-37) {
  125. if(currentRoom.rs_WestDoor != null) {
  126. currentRoom = currentRoom.rs_WestDoor;
  127. }else {
  128. RoomState nextRoom = new RoomState(currentRoom,false, true, false, false);
  129. currentRoom.rs_WestDoor = nextRoom;
  130. map.add(nextRoom);
  131. currentRoom = nextRoom;
  132. nextRoom = null;
  133. }
  134. characterX=913;
  135. characterY=486;
  136. repaint();
  137. }
  138. }
  139. }else {
  140. character=characterWest;
  141. }
  142. }
  143. if(arg0.getKeyCode() == KeyEvent.VK_S || arg0.getKeyCode() == KeyEvent.VK_DOWN) {
  144. if(character==characterSouth) {
  145. if(characterY<871 && characterX>13 && characterX<913) {
  146. oldCharacterY=characterY;
  147. characterY=characterY+50;
  148. }else if(currentRoom.southDoor && characterX==463) {
  149. oldCharacterY=characterY;
  150. characterY=characterY+50;
  151. if(characterY==986) {
  152. if(currentRoom.rs_SouthDoor != null) {
  153. currentRoom=currentRoom.rs_SouthDoor;
  154. }else {
  155. RoomState nextRoom = new RoomState(currentRoom,true, false, false, false);
  156. currentRoom.rs_SouthDoor = nextRoom;
  157. map.add(nextRoom);
  158. currentRoom = nextRoom;
  159. nextRoom = null;
  160. }
  161. characterX=463;
  162. characterY=36;
  163. repaint();
  164. }
  165. }
  166. }else {
  167. character=characterSouth;
  168. }
  169. }
  170. if(arg0.getKeyCode() == KeyEvent.VK_D || arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
  171. if(character==characterEast && characterY>36 && characterY<926) {
  172. if(characterX<848) {
  173. oldCharacterX=characterX;
  174. characterX=characterX+50;
  175. }else if(currentRoom.eastDoor && characterY==486) {
  176. oldCharacterX=characterX;
  177. characterX=characterX+50;
  178. if(characterX==963) {
  179. if(currentRoom.rs_EastDoor != null) {
  180. currentRoom = currentRoom.rs_EastDoor;
  181. }else {
  182. RoomState nextRoom = new RoomState(currentRoom,false, false, false, true);
  183. currentRoom.rs_EastDoor = nextRoom;
  184. map.add(nextRoom);
  185. currentRoom = nextRoom;
  186. nextRoom = null;
  187. }
  188. characterX=13;
  189. characterY=486;
  190. repaint();
  191. }
  192. }
  193. }else {
  194. character=characterEast;
  195. }
  196. }
  197. if(oldCharacterX != characterX || oldCharacterY != characterY) {
  198. repaint(oldCharacterX,oldCharacterY,40,40);
  199. }
  200. repaint(characterX,characterY,40,40);
  201. }
  202. }
  203.  
  204. @Override
  205. public void keyReleased(KeyEvent arg0) { //Prevents Holding Down Keys.
  206. if(arg0.getKeyCode() == KeyEvent.VK_W || arg0.getKeyCode() == KeyEvent.VK_UP) {
  207. pressed=false;
  208. }
  209. if(arg0.getKeyCode() == KeyEvent.VK_A || arg0.getKeyCode() == KeyEvent.VK_LEFT) {
  210. pressed=false;
  211. }
  212. if(arg0.getKeyCode() == KeyEvent.VK_S || arg0.getKeyCode() == KeyEvent.VK_DOWN) {
  213. pressed=false;
  214. }
  215. if(arg0.getKeyCode() == KeyEvent.VK_D || arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
  216. pressed=false;
  217. }
  218. }
  219.  
  220. @Override
  221. public void keyTyped(KeyEvent arg0) {
  222. }
  223. }
Add Comment
Please, Sign In to add comment