import '../css/style.css'; import { MAX_TARGETS, TARGET_WIDTH, TOT_SHOTS } from './Constants.js'; import { ScoreDisplay } from './ScoreDisplay.js'; import { Gun } from './Gun.js'; import { TargetFactory } from './Target.js'; // Globals let gun; let score; let targets = []; function setup() { createCanvas(800, 600); // 1. Init gun and score display gun = new Gun(TOT_SHOTS); gun.setup(); score = new ScoreDisplay(TOT_SHOTS); // 2. Init the targets initTargets(); // 3. Subscribe gun gun.subscribe(score); // Subscribe each target to gun for (let target of targets) { gun.subscribe(target); target.subscribe(score); } } function draw() { background('#eeeeee'); for (let target of targets) { target.draw(); } gun.draw(); score.draw(); // draw targets, gun, bullets, score } // Shoot function mousePressed() { gun.shoot(); // shoot } // Spacebar to reload function keyPressed() { if (key === ' ') { // reset score and targets gun.remainingShots = TOT_SHOTS; score.shotLeft = TOT_SHOTS; score.score = 0; initTargets(); } } // init the targets function initTargets() { // Create new targets from the factory const factory = TargetFactory.getInstance(); // Remember to unsubscribe the previous targets and to subscribe the new ones gun.unsubscribeAll(); targets = factory.getRandomTargets(MAX_TARGETS, TARGET_WIDTH, 200); gun.subscribe(score); for (let target of targets) { gun.subscribe(target); target.subscribe(score); } } // Do not touch these window.setup = setup; window.draw = draw; window.mousePressed = mousePressed; window.keyPressed = keyPressed;