Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package asteroids.model;
- import java.util.HashSet;
- import java.util.Set;
- import be.kuleuven.cs.som.annotate.*;
- /**
- *
- * A class of ships with a position, a velocity, a radius and an orientation.
- *
- * @invar The x-and y-coordinates of the position of each ship must be valid numbers for
- * | isValidNumber(x) && isValidNumber(y)
- * @invar The x-and y-coordinates of each ship must be valid for any
- ship.The calculated speed of the ship must be a valid speed for any ship.
- * | isValidNumber(getVelocityx()) && isValidNumber(getVelocityy())
- * | isValidSpeed(new.speed)
- * @invar The orientation of each ship must be a valid orientation for any
- * ship.
- * | isValidorientation(getOrientation())
- * @invar the radius of each ship must be a valid radius for any ship.
- * | isValidRadius(getRadius())
- *
- */
- public class Ship extends Entity {
- /**
- * Initialize this new ship with given x- and y-coordinates for the position, x-and y-coordinate
- * for the velocity, given orientation and the given radius.
- *
- * @param x
- * the x-coordinate of the position for this new ship.
- * @param y
- * the y-coordinate of the position for this new ship.
- * @param velocityx
- * The x-coordinate of the velocity for this new ship.
- * @param velocityy
- * The y-coordinate of the velocity for this new ship.
- * @param orientation
- * The orientation for this new ship.
- * @param radius
- * The radius for this new ship.
- * @throws IllegalNumberException
- * The given number is not valid
- * | ! isValidNumber(number)
- * @pre The given orientation must be a valid orientation for any ship.
- * | isValidorientation(orientation)
- * @post The position of this new ship is equal to a double[] with coordinates the given x and y.
- * |new.getPosition()=={x,y}.
- * @post If the given x-coordinate and y-coordinate are valid coordinates for the velocity of any ship
- * and the speed is valid for any ship, the velocity of this new ship is equal to
- * a double[] with coordinates the given x and y.
- * Otherwise, the velocity of this new ship is equal to {300000/sqrt(2) , 300000/sqrt(2)}.
- * | if (isValidNumber(velocityx) && IsValidNumber(velocityy) && isValidSpeed(new.speed) )
- * | then new.getVelocity() == {velocityx , velocityy}
- * | else new.getVelocity() == {300000/Math.sqrt(2) , 300000/Math.sqrt(2)}
- * @post The orientation of this new ship is equal to the given orientation.
- * | new.getOrientation() == orientation
- * @post The radius of this new ship is equal to the given radius.
- * | new.getRadius()== radius
- */
- public Ship(double x, double y, double velocityx, double velocityy, double radius, double orientation)
- throws IllegalNumberException {
- super(x,y,velocityx,velocityy,radius);
- setOrientation(orientation);
- setRadius(radius);
- if (! isValidMass(mass))
- mass = 0;
- setMass(mass);
- //while (bullets.size() < 15)
- //addBullet(bullets);
- }
- private final Set<Bullet> standardbullets = new HashSet<Bullet>();
- public void createStandardbullets() {
- while (standardbullets.size() < 15) {
- }
- }
- //methods about the orientation of the entity
- /**
- * @Return the orientation of this entity.
- */
- @Basic @Raw
- public double getOrientation() {
- return this.orientation;
- }
- /**
- * Check whether the given orientation is a valid orientation for
- * any entity.
- *
- * @param orientation
- * The orientation to check.
- * @return true if the orientation of the entity is between 0 and 2*pi.
- * | result == (orientation >= 0 && orientation <=2pi && isValidNumber(orientation))
- */
- public static boolean isValidOrientation(double orientation) {
- return ((orientation >= 0) && (orientation <= 2*Math.PI)) &&(isValidNumber(orientation));
- }
- /**
- * Set the orientation of this entity to the given orientation.
- *
- * @param orientation
- * The new orientation for this entity.
- * @pre The given orientation must be a valid orientation for any
- * entity.
- * | isValidorientation(orientation)
- * @post The orientation of this entity is equal to the given orientation.
- * | new.getOrientation() == orientation
- */
- @Raw
- public void setOrientation(double orientation) {
- assert isValidOrientation(orientation);
- this.orientation = orientation;
- }
- /**
- * Variable registering the orientation of this entity.
- */
- private double orientation;
- @Raw
- public void setRadius(double radius) throws IllegalNumberException {
- if (! isValidRadius(radius))
- throw new IllegalNumberException(radius);
- this.radius=radius;
- }
- /**
- * Check whether the given radius is valid for any entity.
- * @param radius
- * The radius to check.
- * @return true if the radius of the entity is larger than or equal to 10 and the radius is a valid number.
- * | result == (radius>=10 && isValidNumber(radius)
- */
- public static boolean isValidRadius(double radius){
- return !(radius <10 || !isValidNumber(radius));
- }
- /**
- * variable registering the radius of the entity.
- */
- private double radius;
- /**
- * @invar The mass of each ship must be a valid mass for any
- * ship.
- * | isValidMass(getMass())
- */
- /**
- * Initialize this new ship with given mass.
- *
- * @param mass
- * The mass for this new ship.
- * @post If the given mass is a valid mass for any ship,
- * the mass of this new ship is equal to the given
- * mass. Otherwise, the mass of this new ship is equal
- * to 0.
- * | if (isValidMass(mass))
- * | then new.getMass() == mass
- * | else new.getMass() == 0
- */
- /**
- * Return the mass of this ship.
- */
- @Basic @Raw
- public double getMass() {
- return this.mass;
- }
- /**
- * Check whether the given mass is a valid mass for
- * any ship.
- *
- * @param mass
- * The mass to check.
- * @return
- * | result ==
- */
- public boolean isValidMass(double mass) {
- double m = 4/3 * Math.PI * 1.42 * Math.pow(10, 12) * Math.pow(this.getRadius(), 3);
- return (mass >= m && isValidRadius(this.getRadius()));
- }
- /**
- * Set the mass of this ship to the given mass.
- *
- * @param mass
- * The new mass for this ship.
- * @post If the given mass is a valid mass for any ship,
- * the mass of this new ship is equal to the given
- * mass.
- * | if (isValidMass(mass))
- * | then new.getMass() == mass
- */
- @Raw
- public void setMass(double mass) {
- if (isValidMass(mass) && bullets.size() == 0)
- this.mass = mass;
- if (isValidMass(mass) && bullets.size() > 0)
- this.mass = mass;
- for (Bullet bullet : bullets)
- this.mass += bullet.getMass();
- }
- /**
- * Variable registering the mass of this ship.
- */
- private double mass;
- //method turn
- /**
- * Turn the entity by adding a given angle to the current orientation of this entity.
- * @param angle
- * the angle to be added to the current orientation.
- * @pre the sum of the current orientation and the given angle must be a valid orientation.
- * | isValidOrientation(angle+orientation)
- * @post the new orientation is equal to the sum of the old orientation and the given angle.
- * |new.orientation= getOrientation()+angle
- */
- public void turn(double angle) {
- double NewAngle = (angle + this.getOrientation());
- assert (isValidOrientation(NewAngle));
- this.orientation = NewAngle;
- }
- private double acceleration;
- public void thrust(double amount) {
- this.acceleration = (double) 1.1 * Math.pow(10, 21) / this.getMass();
- if (this.acceleration < 0)
- this.acceleration = 0;
- this.setVelocity((double) this.getVelocityx() + (this.acceleration * Math.cos(this.getOrientation())*amount),(double) this.getVelocityy() + (this.acceleration * Math.sin(this.getOrientation())* amount));
- }
- public void thrustOn() {
- this.thrust = true;
- }
- public void thrustOff() {
- this.thrust = false;
- }
- private boolean thrust = false;
- private final Set<Bullet> bullets = new HashSet<Bullet>();
- public void addBullet(Bullet... bullets2) throws IllegalArgumentException,IllegalNumberException {
- for (Bullet bullet : bullets2)
- bullets.add(bullet);
- }
- public boolean canHaveAsBullet(Bullet bullet) {
- return (Math.sqrt(Math.pow(this.getPosition()[0] - bullet.getPosition()[0],2) + Math.pow(this.getPosition()[1] - bullet.getPosition()[1],2)) <= Math.abs(this.getRadius() - bullet.getRadius()) );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement