Advertisement
Guest User

Untitled

a guest
Mar 26th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. import java.awt.event.KeyEvent;
  2. import java.awt.event.KeyListener;
  3.  
  4. public class ControlledObject extends SimpleSpaceObject implements KeyListener{
  5.  
  6. public ControlledObject(Point[] inShape, Point inOffset, double inRotation) {
  7. super(inShape, inOffset, inRotation);
  8. }
  9.  
  10.  
  11. boolean KeyHeld = false;
  12.  
  13. public void PressedW(boolean keyW)
  14. {
  15. double RotateX = Math.cos(Math.toRadians(super.shape.getRotation()));
  16. double RotateY = Math.sin(Math.toRadians(super.shape.getRotation()));
  17.  
  18. if(keyW)
  19. super.shape.move(2*RotateX,2*RotateY);
  20. else
  21. super.shape.move(0,0);
  22. }
  23.  
  24. public void PressedD(boolean keyD)
  25. {
  26. if(keyD)
  27. super.shape.rotate(5);
  28. else
  29. super.shape.rotate(0);
  30. }
  31.  
  32. public void PressedA(boolean keyA)
  33. {
  34. if(keyA)
  35. super.shape.rotate(-5);
  36. else
  37. super.shape.rotate(0);
  38. }
  39.  
  40. public void PressedS(boolean keyS)
  41. {
  42. double RotateX = Math.cos(Math.toRadians(super.shape.getRotation()));
  43. double RotateY = Math.sin(Math.toRadians(super.shape.getRotation()));
  44.  
  45. if(keyS)
  46. super.shape.move(-2*RotateX,-2*RotateY);
  47. else
  48. super.shape.move(0, 0);
  49. }
  50.  
  51. @Override
  52. public void keyPressed(KeyEvent e) {
  53. KeyHeld = true;
  54. if(e.getKeyChar() == 'w')
  55. PressedW(KeyHeld);
  56. if(e.getKeyChar() == 'd')
  57. PressedD(KeyHeld);
  58. if(e.getKeyChar() == 'a')
  59. PressedA(KeyHeld);
  60. if(e.getKeyChar() == 's')
  61. PressedS(KeyHeld);
  62. }
  63.  
  64. @Override
  65. public void keyReleased(KeyEvent e) {
  66. KeyHeld = false;
  67. PressedW(KeyHeld);
  68. PressedD(KeyHeld);
  69. PressedA(KeyHeld);
  70. PressedS(KeyHeld);
  71. }
  72.  
  73. @Override
  74. public void keyTyped(KeyEvent e) {
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement