<commit_before>import Vector from "./Vector";
import Body from "./Body";
import * as canvasDraw from "./canvasDraw";
import * as constants from "./constants";

let initNum = 0;

export default class Cannon {
    constructor(game, center, toPoint) {
        this.game = game;
        this.type = "cannon";
        this.id = initNum;
        this.isAlive = true;
        this.color = "red";
        this.center = center;
        this.mass = constants.EarthMass / 2;
        this.radius = this.mass / constants.EarthMass * constants.EarthRadius;

        this.updates = 0;
        this.rate = 50;

        this.select(toPoint); // creates this.shootVector

        initNum++;
    }
    vectorToPoint() {
        let scaledVector = this.shootVector.scale(10e3);

        let vectorToPoint = {
            x: this.center.x + scaledVector.getDeltaX(),
            y: this.center.y + scaledVector.getDeltaY()
        };

        return vectorToPoint;
    }
    update() {
        if (this.updates <= 0) {
            this.updates = this.rate;

            this.game.bodies.push(new Body(
                this.game,
                { x: this.center.x, y: this.center.y },
                true,
                {
                    initialSpeed: this.shootVector.clone()
                }
            ));
        }

        if (this.updates > this.rate) {
            this.updates = this.rate;
        }

        canvasDraw.drawLine(this.center, this.vectorToPoint());

        --this.updates;
    }
    select(point) {
        this.shootVector = new Vector(
            7000,//pointUtils.distanceBetween(this.center, point),
            point === undefined ? Math.PI :
            Math.atan2(
                this.center.y - point.y,
                this.center.x - point.x
            ) + Math.PI,
            "m/s"
        );
    }
    deselect() {
        this.selected = false;
    }
}<commit_msg>Make Cannon do less work in update() to hopefully reduce workload with large amounts of Cannons.<commit_after>import Vector from "./Vector";
import Body from "./Body";
import * as canvasDraw from "./canvasDraw";
import * as constants from "./constants";

let initNum = 0;

export default class Cannon {
    constructor(game, center, toPoint) {
        this.game = game;
        this.type = "cannon";
        this.id = initNum;
        this.isAlive = true;
        this.color = "red";
        this.center = center;
        this.mass = constants.EarthMass / 2;
        this.radius = this.mass / constants.EarthMass * constants.EarthRadius;

        this.updates = 0;
        this.rate = 50;

        this.shootPointDelta = null;
        this.select(toPoint); // creates this.shootVector

        initNum++;
    }
    vectorToPoint() {
        return {
            x: this.center.x + this.shootPointDelta.x,
            y: this.center.y + this.shootPointDelta.y
        };
    }
    update() {
        if (this.updates <= 0 || this.updates > this.rate) {
            this.updates = this.rate;

            this.game.bodies.push(new Body(
                this.game,
                { x: this.center.x, y: this.center.y },
                true,
                {
                    initialSpeed: this.shootVector.clone()
                }
            ));
        }

        canvasDraw.drawLine(this.center, this.vectorToPoint());

        --this.updates;
    }
    select(point) {
        this.shootVector = new Vector(
            7000,
            point === undefined ? Math.PI :
            Math.atan2(
                this.center.y - point.y,
                this.center.x - point.x
            ) + Math.PI,
            "m/s"
        );

        const scaledVector = this.shootVector.scale(10e3);
        this.shootPointDelta = {
            x: scaledVector.getDeltaX(),
            y: scaledVector.getDeltaY()
        };
    }
    deselect() {
        this.selected = false;
    }
}
{'fraction_non_alphanumeric': 0.12019602785659014, 'fraction_numerical': 0.007222078927005417, 'mean_word_length': 1.5019354838709678, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6858511', 'n_tokens_mistral': 1183, 'n_tokens_neox': 1102, 'n_words': 239}