Advertisement
Erudar

Untitled

Apr 25th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.95 KB | None | 0 0
  1. package asteroids.participants;
  2.  
  3. import asteroids.Constants;
  4. import asteroids.Controller;
  5. import asteroids.Participant;
  6. import asteroids.ParticipantCountdownTimer;
  7. import asteroids.destroyers.AlienShipDestroyer;
  8. import asteroids.destroyers.AsteroidDestroyer;
  9. import asteroids.destroyers.SBulletDestroyer;
  10. import asteroids.destroyers.ShipDestroyer;
  11. import java.awt.Shape;
  12. import java.awt.geom.AffineTransform;
  13. import java.awt.geom.Path2D.Double;
  14. import java.util.Random;
  15.  
  16. public class AlienShip extends Participant
  17.   implements AsteroidDestroyer, SBulletDestroyer, ShipDestroyer
  18. {
  19.   private Shape outline;
  20.   private int size;
  21.   private Controller controller;
  22.   boolean changeDirection = false;
  23.  
  24.   public AlienShip(int size, Controller controller)
  25.   {
  26.     if ((size < 0) || (size > 1))
  27.     {
  28.       throw new IllegalArgumentException("Invalid alien ship size " + size);
  29.     }
  30.  
  31.     this.size = size;
  32.     this.controller = controller;
  33.  
  34.     Path2D.Double poly = new Path2D.Double();
  35.     poly.moveTo(20.0D, 0.0D);
  36.     poly.lineTo(9.0D, 9.0D);
  37.     poly.lineTo(-9.0D, 9.0D);
  38.     poly.lineTo(-20.0D, 0.0D);
  39.     poly.lineTo(20.0D, 0.0D);
  40.     poly.lineTo(-20.0D, 0.0D);
  41.     poly.lineTo(-9.0D, -9.0D);
  42.     poly.lineTo(9.0D, -9.0D);
  43.     poly.lineTo(-9.0D, -9.0D);
  44.     poly.lineTo(-5.0D, -17.0D);
  45.     poly.lineTo(5.0D, -17.0D);
  46.     poly.lineTo(9.0D, -9.0D);
  47.     poly.closePath();
  48.     this.outline = poly;
  49.  
  50.     double scale = Constants.ALIENSHIP_SCALE[size];
  51.     poly.transform(AffineTransform.getScaleInstance(scale, scale));
  52.  
  53.     new ParticipantCountdownTimer(this, "shoot", 1500);
  54.  
  55.     new ParticipantCountdownTimer(this, "change", 1000);
  56.   }
  57.  
  58.   protected Shape getOutline()
  59.   {
  60.     return this.outline;
  61.   }
  62.  
  63.   public int getSize()
  64.   {
  65.     return this.size;
  66.   }
  67.  
  68.   public void countdownComplete(Object payload)
  69.   {
  70.     if ("shoot".equals(payload))
  71.     {
  72.       Ship ship = this.controller.getShip();
  73.       if (ship != null)
  74.       {
  75.         fireBullet();
  76.         new ParticipantCountdownTimer(this, "shoot", 1500);
  77.       }
  78.  
  79.     }
  80.     else if ("change".equals(payload))
  81.     {
  82.       this.changeDirection = true;
  83.     }
  84.   }
  85.  
  86.   public void move()
  87.   {
  88.     super.move();
  89.  
  90.     if (this.changeDirection)
  91.     {
  92.       this.changeDirection = false;
  93.  
  94.       if (Math.cos(getDirection()) > 0.0D)
  95.       {
  96.         setDirection(Constants.RANDOM.nextInt(3) - 1);
  97.       }
  98.       else
  99.       {
  100.         setDirection(3.141592653589793D + Constants.RANDOM.nextInt(3) - 1.0D);
  101.       }
  102.  
  103.       new ParticipantCountdownTimer(this, "change", 1000);
  104.     }
  105.   }
  106.  
  107.   public void fireBullet()
  108.   {
  109.     ABullet b = new ABullet(getX(), getY(), getShootingDirectionToShip());
  110.     b.setSpeed(15.0D);
  111.     this.controller.addParticipant(b);
  112.   }
  113.  
  114.   public double getShootingDirectionToShip()
  115.   {
  116.     if (this.size == 1)
  117.     {
  118.       return Constants.RANDOM.nextDouble() * 2.0D * 3.141592653589793D;
  119.     }
  120.  
  121.     Ship ship = this.controller.getShip();
  122.     double deltaX = ship.getX() - getX();
  123.     double deltaY = ship.getY() - getY();
  124.     double distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
  125.     double direction = Math.acos(deltaX / distance);
  126.     return deltaY > 0.0D ? direction : -direction;
  127.   }
  128.  
  129.   public void collidedWith(Participant p)
  130.   {
  131.     if ((p instanceof AlienShipDestroyer))
  132.     {
  133.       Participant.expire(this);
  134.  
  135.       this.controller.addParticipant(new DriftingLine(getX(), getY(), 10 * (this.size + 1)));
  136.       this.controller.addParticipant(new DriftingLine(getX(), getY(), 10 * (this.size + 1)));
  137.       this.controller.addParticipant(new DriftingLine(getX(), getY(), 10 * (this.size + 1)));
  138.       this.controller.addParticipant(new DriftingLine(getX(), getY(), 10 * (this.size + 1)));
  139.       this.controller.addParticipant(new DriftingLine(getX(), getY(), 5 * (this.size + 1)));
  140.       this.controller.addParticipant(new DriftingLine(getX(), getY(), 5 * (this.size + 1)));
  141.  
  142.       this.controller.alienShipDestroyed(this.size);
  143.     }
  144.   }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement