import java.awt.*; import java.awt.geom.*; public class Shooter { private double x; private double y; private double turretAngle; private double direction; private boolean isPlayerShip = false; private int health = DEFAULT_HEALTH; private final static double TURRET_LENGTH = 30.0; private final static double MOVE_DISTANCE = 1.0; private final static int DEFAULT_HEALTH = 1; private final static int TURRET_THICKNESS = 3; private final static Color PLAYER_COLOR = Color.green; private final static Color ENEMY_COLOR = Color.red; private final static Color TURRET_COLOR = Color.black; public Shooter(double x_, double y_) { x = x_; y = y_; } public void setPosition(double x_, double y_) { x = x_; y = y_; } public void draw(Graphics2D g2) { if(isPlayerShip) { g2.setColor(PLAYER_COLOR); } else { g2.setColor(ENEMY_COLOR); } g2.fill(new Ellipse2D.Double(x - TURRET_LENGTH / 4, y - TURRET_LENGTH / 4, TURRET_LENGTH / 2, TURRET_LENGTH / 2)); g2.setColor(TURRET_COLOR); g2.setStroke(new BasicStroke(TURRET_THICKNESS)); g2.draw(new Line2D.Double(x, y, x + TURRET_LENGTH * Math.cos(turretAngle), y + TURRET_LENGTH * Math.sin(turretAngle))); } public Laser fire() { return new Laser(x + TURRET_LENGTH * Math.cos(turretAngle), y + TURRET_LENGTH * Math.sin(turretAngle), turretAngle); } public void setTurretAngle(double angleX, double angleY) { double width = angleX - x; double height = angleY - y; turretAngle = Math.atan(height / width) + Math.PI; if(angleX > x) turretAngle -= Math.PI; } public void setTurretAngle(double turretAngle_) { turretAngle = turretAngle_; } public void setShipDirection(double direction_) { direction = direction_; } public void move() { x += MOVE_DISTANCE * Math.cos(direction); y += MOVE_DISTANCE * Math.sin(direction); } public double getX() { return x; } public double getY() { return y; } public boolean isHitting(double hitX1, double hitY1, double hitX2, double hitY2) { Ellipse2D.Double shipBody = new Ellipse2D.Double(x - TURRET_LENGTH / 4, y - TURRET_LENGTH / 4, TURRET_LENGTH / 2, TURRET_LENGTH / 2); return shipBody.contains(hitX1, hitY1) || shipBody.contains(hitX2, hitY2); } public void takeHit() { health--; } public boolean isDead() { return health == 0; } public void setPlayerShip() { isPlayerShip = true; } public boolean isPlayerShip() { return isPlayerShip; } public double getTurretAngle() { return turretAngle; } public static double findAngle(double angleX, double angleY, double refX, double refY) { double width = angleX - refX; double height = angleY - refY; double ret = Math.atan(height / width) + Math.PI; if(angleX > refX) ret -= Math.PI; return ret; } }