import Matrix from './matrix.js';

/**
 * Class representing a ship in battleships, it contains a matrix to
 * establish hit detection and health from the filled area of the matrix.
 */

function calculateImpactArea(matrix) {

    let count = 0;

    for (let row = 0; row < matrix.rows; row++) {
        for (let column = 0; column < matrix.columns; column++) {

            const value = matrix.data[row][column];

            if (value === 1) {
                count ++;
            }

        }
    }

    return count;

}

export default class Ship {

    constructor(matrix) {
        this._matrix = matrix;
        this._rotation = Matrix.ROTATION_0;
        this._computed = null;
        this.health = calculateImpactArea(matrix);
    }

    get matrix() {
        return this._matrix;
    }

    set matrix(value) {
        this._computed = null;
        this._matrix = value;
    }

    get position() {
        return this._position;
    }

    set position(value) {
        this._computed = null;
        this._position = value;
    }

    get rotation() {
        return this._rotation;
    }

    set rotation(value) {
        this._computed = null;
        this._rotation = value;
    }

    get computed() {

        // todo: if position or rotation haven't yet been set, then throw

        if (this._computed == null) {
            this._computed = this.matrix.placeAt(this.position).turn(this.rotation);
        }

        return this._computed;
    }

}

@@ -32,160 +32,8 @@
 ';%0A%0A
-/**%0A * Class representing a ship in battleships, it contains a matrix to%0A * establish hit detection and health from the filled area of the matrix.%0A */%0A%0A
 func
@@ -369,16 +369,167 @@
 nt;%0A%0A%7D%0A%0A
+/**%0A * Class representing a ship in battleships, it contains a matrix to%0A * establish hit detection and health from the filled area of the matrix.%0A */%0A
 export d

fix comment location