You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Player.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
  3. Use and copying of this software and preparation of derivative works based
  4. upon this software are permitted. Any distribution of this software or
  5. derivative works must comply with all applicable United States export control
  6. laws.
  7. This software is made available AS IS, and Xerox Corporation makes no warranty
  8. about the software, its performance or its conformity to any specification.
  9. |<--- this code is formatted to fit into 80 columns --->|
  10. |<--- this code is formatted to fit into 80 columns --->|
  11. |<--- this code is formatted to fit into 80 columns --->|
  12. */
  13. package spacewar;
  14. import java.awt.event.KeyListener;
  15. import java.awt.event.KeyEvent;
  16. class Player extends Pilot implements KeyListener {
  17. private KeyMapping keyMapping;
  18. /** current rotation key */
  19. private int rotation_direction = Ship.STOP; // current rotation key
  20. /** current thrust */
  21. private boolean thrust_on = false;
  22. Player(Game theGame, int number) {
  23. super(theGame,number);
  24. if (getNumber() == 1)
  25. keyMapping = KeyMapping.keyMapping1;
  26. else if (getNumber() == 2)
  27. keyMapping = KeyMapping.keyMapping2;
  28. }
  29. public void keyPressed(KeyEvent e) {
  30. int keyCode = e.getKeyCode();
  31. boolean consumed = true;
  32. if (keyCode == keyMapping.fire) {
  33. ship.fire();
  34. }
  35. else if (keyCode == keyMapping.thrust && !thrust_on) {
  36. ship.thrust(true);
  37. thrust_on = true;
  38. }
  39. else if (keyCode == keyMapping.right &&
  40. rotation_direction != Ship.COUNTERCLOCKWISE) {
  41. //start rotating clockwise unless already rotating in the
  42. //opposite direction
  43. rotation_direction = Ship.CLOCKWISE;
  44. ship.rotate(Ship.CLOCKWISE);
  45. }
  46. else if (keyCode == keyMapping.left &&
  47. rotation_direction != Ship.CLOCKWISE) {
  48. //start rotating counterclockwise unless already rotating in the
  49. //opposite direction
  50. rotation_direction = Ship.COUNTERCLOCKWISE;
  51. ship.rotate(Ship.COUNTERCLOCKWISE);
  52. }
  53. else {
  54. consumed = false;
  55. }
  56. if (consumed) e.consume();
  57. }
  58. public void keyReleased(KeyEvent e) {
  59. int keyCode = e.getKeyCode();
  60. if (keyCode == keyMapping.thrust) {
  61. ship.thrust(false); //engine off
  62. thrust_on = false;
  63. }
  64. else if (keyCode == keyMapping.right &&
  65. rotation_direction == Ship.CLOCKWISE
  66. ||
  67. keyCode == keyMapping.left &&
  68. rotation_direction == Ship.COUNTERCLOCKWISE) {
  69. ship.rotate(Ship.STOP); //stop rotation
  70. rotation_direction = Ship.STOP;
  71. }
  72. }
  73. public void keyTyped(KeyEvent e) {
  74. // have to implement this because it's in KeyListener
  75. }
  76. }
  77. class KeyMapping {
  78. static final KeyMapping keyMapping1 =
  79. new KeyMapping(KeyEvent.VK_LEFT,
  80. KeyEvent.VK_RIGHT,
  81. KeyEvent.VK_UP,
  82. KeyEvent.VK_SPACE);
  83. static final KeyMapping keyMapping2 =
  84. new KeyMapping(KeyEvent.VK_X,
  85. KeyEvent.VK_V,
  86. KeyEvent.VK_D,
  87. KeyEvent.VK_ALT);
  88. int left, right, thrust, fire;
  89. KeyMapping(int k_left, int k_right, int k_thrust, int k_fire) {
  90. left = k_left;
  91. right = k_right;
  92. thrust = k_thrust;
  93. fire = k_fire;
  94. }
  95. }