Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package asteroids.participants;
- import asteroids.Constants;
- import asteroids.Controller;
- import asteroids.Participant;
- import asteroids.ParticipantCountdownTimer;
- import asteroids.destroyers.AlienShipDestroyer;
- import asteroids.destroyers.AsteroidDestroyer;
- import asteroids.destroyers.SBulletDestroyer;
- import asteroids.destroyers.ShipDestroyer;
- import java.awt.Shape;
- import java.awt.geom.AffineTransform;
- import java.awt.geom.Path2D.Double;
- import java.util.Random;
- public class AlienShip extends Participant
- implements AsteroidDestroyer, SBulletDestroyer, ShipDestroyer
- {
- private Shape outline;
- private int size;
- private Controller controller;
- boolean changeDirection = false;
- public AlienShip(int size, Controller controller)
- {
- if ((size < 0) || (size > 1))
- {
- throw new IllegalArgumentException("Invalid alien ship size " + size);
- }
- this.size = size;
- this.controller = controller;
- Path2D.Double poly = new Path2D.Double();
- poly.moveTo(20.0D, 0.0D);
- poly.lineTo(9.0D, 9.0D);
- poly.lineTo(-9.0D, 9.0D);
- poly.lineTo(-20.0D, 0.0D);
- poly.lineTo(20.0D, 0.0D);
- poly.lineTo(-20.0D, 0.0D);
- poly.lineTo(-9.0D, -9.0D);
- poly.lineTo(9.0D, -9.0D);
- poly.lineTo(-9.0D, -9.0D);
- poly.lineTo(-5.0D, -17.0D);
- poly.lineTo(5.0D, -17.0D);
- poly.lineTo(9.0D, -9.0D);
- poly.closePath();
- this.outline = poly;
- double scale = Constants.ALIENSHIP_SCALE[size];
- poly.transform(AffineTransform.getScaleInstance(scale, scale));
- new ParticipantCountdownTimer(this, "shoot", 1500);
- new ParticipantCountdownTimer(this, "change", 1000);
- }
- protected Shape getOutline()
- {
- return this.outline;
- }
- public int getSize()
- {
- return this.size;
- }
- public void countdownComplete(Object payload)
- {
- if ("shoot".equals(payload))
- {
- Ship ship = this.controller.getShip();
- if (ship != null)
- {
- fireBullet();
- new ParticipantCountdownTimer(this, "shoot", 1500);
- }
- }
- else if ("change".equals(payload))
- {
- this.changeDirection = true;
- }
- }
- public void move()
- {
- super.move();
- if (this.changeDirection)
- {
- this.changeDirection = false;
- if (Math.cos(getDirection()) > 0.0D)
- {
- setDirection(Constants.RANDOM.nextInt(3) - 1);
- }
- else
- {
- setDirection(3.141592653589793D + Constants.RANDOM.nextInt(3) - 1.0D);
- }
- new ParticipantCountdownTimer(this, "change", 1000);
- }
- }
- public void fireBullet()
- {
- ABullet b = new ABullet(getX(), getY(), getShootingDirectionToShip());
- b.setSpeed(15.0D);
- this.controller.addParticipant(b);
- }
- public double getShootingDirectionToShip()
- {
- if (this.size == 1)
- {
- return Constants.RANDOM.nextDouble() * 2.0D * 3.141592653589793D;
- }
- Ship ship = this.controller.getShip();
- double deltaX = ship.getX() - getX();
- double deltaY = ship.getY() - getY();
- double distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
- double direction = Math.acos(deltaX / distance);
- return deltaY > 0.0D ? direction : -direction;
- }
- public void collidedWith(Participant p)
- {
- if ((p instanceof AlienShipDestroyer))
- {
- Participant.expire(this);
- this.controller.addParticipant(new DriftingLine(getX(), getY(), 10 * (this.size + 1)));
- this.controller.addParticipant(new DriftingLine(getX(), getY(), 10 * (this.size + 1)));
- this.controller.addParticipant(new DriftingLine(getX(), getY(), 10 * (this.size + 1)));
- this.controller.addParticipant(new DriftingLine(getX(), getY(), 10 * (this.size + 1)));
- this.controller.addParticipant(new DriftingLine(getX(), getY(), 5 * (this.size + 1)));
- this.controller.addParticipant(new DriftingLine(getX(), getY(), 5 * (this.size + 1)));
- this.controller.alienShipDestroyed(this.size);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement