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.

EnergyPacketProducer.java 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. EnergyPacketProducer.java
  13. Part of the Spacewar system.
  14. This implementation creates booby-trapped packets 20% of the time.
  15. */
  16. package spacewar;
  17. class EnergyPacketProducer extends Thread {
  18. private final static int MIN = -20;
  19. private final static int MAX = 80;
  20. private final static int EXPECTEDINTERVAL = 15;
  21. private Game game;
  22. Game getGame() { return game; }
  23. EnergyPacketProducer(Game theGame) {
  24. super("EnergyPacketProducer");
  25. game = theGame;
  26. }
  27. public void run() {
  28. while(true) {
  29. produceAPacket();
  30. waitForABit();
  31. }
  32. }
  33. void waitForABit() {
  34. try { Thread.sleep((int)(Math.random() * EXPECTEDINTERVAL * 2000)); }
  35. catch (InterruptedException e) {}
  36. }
  37. void produceAPacket() {
  38. EnergyPacket pkt =
  39. new EnergyPacket(game,
  40. Math.random() * getGame().getWidth(),
  41. Math.random() * getGame().getHeight(),
  42. Math.random() * 2 - 1,
  43. Math.random() * 2 - 1,
  44. Math.random() * (MAX - MIN) + MIN);
  45. }
  46. }