From d39a007a2ad3422da5109741c9fc30d09949296c Mon Sep 17 00:00:00 2001 From: BrandyNguyen2 Date: Wed, 16 Apr 2025 19:03:30 -0700 Subject: [PATCH] Apply format checker to network mapping --- .../src/game/gameObjs/tasks/networkMapping.ts | 152 ++++++++++-------- client/src/game/scenes/Sandbox.ts | 2 +- 2 files changed, 86 insertions(+), 68 deletions(-) diff --git a/client/src/game/gameObjs/tasks/networkMapping.ts b/client/src/game/gameObjs/tasks/networkMapping.ts index 94bc590..d18e40e 100644 --- a/client/src/game/gameObjs/tasks/networkMapping.ts +++ b/client/src/game/gameObjs/tasks/networkMapping.ts @@ -41,29 +41,32 @@ export class NetworkMapping extends Task { } async preload(): Promise { - if (this.scene.textures.exists("computer") && - this.scene.textures.exists("router") && - this.scene.textures.exists("server")) { + if ( + this.scene.textures.exists("computer") && + this.scene.textures.exists("router") && + this.scene.textures.exists("server") + ) { return Promise.resolve(); } - + return new Promise((resolve) => { if (!this.scene.textures.exists("computer")) { this.scene.load.image("computer", "/assets/computer.png"); } - + if (!this.scene.textures.exists("router")) { this.scene.load.image("router", "/assets/router.png"); } - + if (!this.scene.textures.exists("server")) { this.scene.load.image("server", "/assets/server.png"); } - - const needsLoading = !this.scene.textures.exists("computer") || - !this.scene.textures.exists("router") || - !this.scene.textures.exists("server"); - + + const needsLoading = + !this.scene.textures.exists("computer") || + !this.scene.textures.exists("router") || + !this.scene.textures.exists("server"); + if (needsLoading) { this.scene.load.once("complete", () => { console.log("All assets loaded successfully"); @@ -71,7 +74,7 @@ export class NetworkMapping extends Task { }); this.scene.load.start(); } else { - resolve(); + resolve(); } }); } @@ -79,7 +82,7 @@ export class NetworkMapping extends Task { async start(): Promise { console.log("NetworkMapping task starting"); await this.preload(); - + this.initializeDevices(); this.initializeWires(); @@ -104,16 +107,13 @@ export class NetworkMapping extends Task { this.scene.input.setDraggable(wire.rect); }); - this.scene.input.on( - "dragstart", - (pointer: Input.Pointer, gameObject: GameObjects.Image) => { - const wire = this.wires.find((w) => w.rect === gameObject); - if (wire) { - wire.line = this.scene.add.line(0, 0, wire.x, wire.y, pointer.x, pointer.y, wire.color); - wire.line.setLineWidth(this.wireThickness); - } - }, - ); + this.scene.input.on("dragstart", (pointer: Input.Pointer, gameObject: GameObjects.Image) => { + const wire = this.wires.find((w) => w.rect === gameObject); + if (wire) { + wire.line = this.scene.add.line(0, 0, wire.x, wire.y, pointer.x, pointer.y, wire.color); + wire.line.setLineWidth(this.wireThickness); + } + }); this.scene.input.on("drag", (pointer: Input.Pointer, gameObject: GameObjects.Image) => { const wire = this.wires.find((w) => w.rect === gameObject); @@ -149,47 +149,49 @@ export class NetworkMapping extends Task { cleanup(): void { try { super.cleanup(); - + if (this.computer) this.computer.destroy(); if (this.router) this.router.destroy(); if (this.server) this.server.destroy(); - + if (this.devices && this.devices.length) { - this.devices.forEach(device => { + this.devices.forEach((device) => { if (device && device.graphic) { - if (device.graphic === this.computer || - device.graphic === this.router || - device.graphic === this.server) { + if ( + device.graphic === this.computer || + device.graphic === this.router || + device.graphic === this.server + ) { return; } device.graphic.destroy(); } }); } - + if (this.wires && this.wires.length) { - this.wires.forEach(wire => { + this.wires.forEach((wire) => { if (wire) { if (wire.rect) wire.rect.destroy(); if (wire.line) wire.line.destroy(); } }); } - + if (this.instructionText) this.instructionText.destroy(); - + console.log("NetworkMapping cleanup completed successfully"); } catch (error) { console.error("Error during NetworkMapping cleanup:", error); } } - private getRandomPosition(isDevice: boolean): { x: number, y: number } { + private getRandomPosition(isDevice: boolean): { x: number; y: number } { const minX = 100; const maxX = this.scene.cameras.main.width - 100; - + let minY, maxY; - + if (isDevice) { minY = 80; maxY = this.scene.cameras.main.height / 2 - 50; @@ -197,42 +199,48 @@ export class NetworkMapping extends Task { minY = this.scene.cameras.main.height / 2 + 50; maxY = this.scene.cameras.main.height - 100; } - + // Generate random x,y coordinates const x = Phaser.Math.Between(minX, maxX); const y = Phaser.Math.Between(minY, maxY); - + return { x, y }; } private initializeDevices(): void { - const devicePositions: { x: number, y: number }[] = []; - + const devicePositions: { x: number; y: number }[] = []; + for (let i = 0; i < 3; i++) { let position; let tooClose; - + do { position = this.getRandomPosition(true); // true for device - tooClose = devicePositions.some(pos => - Phaser.Math.Distance.Between(position.x, position.y, pos.x, pos.y) < 100 + tooClose = devicePositions.some( + (pos) => Phaser.Math.Distance.Between(position.x, position.y, pos.x, pos.y) < 100, ); } while (tooClose); - + devicePositions.push(position); } - + this.devices = [ this.createDevice("Router", 0xff0000, devicePositions[0].x, devicePositions[0].y, "router"), this.createDevice("Server", 0x00ff00, devicePositions[1].x, devicePositions[1].y, "server"), - this.createDevice("Computer", 0x0000ff, devicePositions[2].x, devicePositions[2].y, "computer") + this.createDevice( + "Computer", + 0x0000ff, + devicePositions[2].x, + devicePositions[2].y, + "computer", + ), ]; - + // After creating devices, assign references to individual images for easier access - const routerDevice = this.devices.find(d => d.name === "Router"); - const serverDevice = this.devices.find(d => d.name === "Server"); - const computerDevice = this.devices.find(d => d.name === "Computer"); - + const routerDevice = this.devices.find((d) => d.name === "Router"); + const serverDevice = this.devices.find((d) => d.name === "Server"); + const computerDevice = this.devices.find((d) => d.name === "Computer"); + if (routerDevice) this.router = routerDevice.graphic as GameObjects.Image; if (serverDevice) this.server = serverDevice.graphic as GameObjects.Image; if (computerDevice) this.computer = computerDevice.graphic as GameObjects.Image; @@ -243,11 +251,11 @@ export class NetworkMapping extends Task { color: number, x: number, y: number, - textureKey: string + textureKey: string, ): Device { try { let graphic: Phaser.GameObjects.Image | Phaser.GameObjects.Rectangle; - + if (this.scene.textures.exists(textureKey)) { graphic = this.scene.add.image(x, y, textureKey).setOrigin(0.5, 0.5).setScale(0.2); console.log(`Created ${name} with texture ${textureKey}`); @@ -255,7 +263,7 @@ export class NetworkMapping extends Task { console.warn(`Texture ${textureKey} not found, using fallback rectangle`); graphic = this.scene.add.rectangle(x, y, 80, 60, color); } - + return { name, color, x, y, graphic }; } catch (error) { console.error(`Error creating device ${name}:`, error); @@ -266,23 +274,23 @@ export class NetworkMapping extends Task { private initializeWires(): void { // Create an array to store wire positions to avoid overlap - const wirePositions: { x: number, y: number }[] = []; - + const wirePositions: { x: number; y: number }[] = []; + // Get three random positions for wires for (let i = 0; i < 3; i++) { let position; let tooClose; - + do { position = this.getRandomPosition(false); // false for wire - tooClose = wirePositions.some(pos => - Phaser.Math.Distance.Between(position.x, position.y, pos.x, pos.y) < 100 + tooClose = wirePositions.some( + (pos) => Phaser.Math.Distance.Between(position.x, position.y, pos.x, pos.y) < 100, ); } while (tooClose); - + wirePositions.push(position); } - + // Create wires at random positions this.wires = [ { @@ -305,29 +313,39 @@ export class NetworkMapping extends Task { color: 0x0000ff, x: wirePositions[2].x, y: wirePositions[2].y, - rect: this.createWireConnector(wirePositions[2].x, wirePositions[2].y, 0x0000ff, "computer"), + rect: this.createWireConnector( + wirePositions[2].x, + wirePositions[2].y, + 0x0000ff, + "computer", + ), isConnected: false, line: null, }, ]; } - private createWireConnector(x: number, y: number, color: number, textureKey: string): GameObjects.Image { + private createWireConnector( + x: number, + y: number, + color: number, + textureKey: string, + ): GameObjects.Image { const connector = this.scene.add .image(x, y, textureKey) .setInteractive() .setData("color", color) - .setScale(0.12) // Smaller scale than destination devices - + .setScale(0.12); // Smaller scale than destination devices + this.scene.tweens.add({ targets: connector, scale: connector.scale * 1.1, duration: 800, yoyo: true, repeat: -1, - ease: 'Sine.easeInOut' + ease: "Sine.easeInOut", }); - + return connector; } // check if wire and device are overlapping diff --git a/client/src/game/scenes/Sandbox.ts b/client/src/game/scenes/Sandbox.ts index 13ecffb..0c8673e 100644 --- a/client/src/game/scenes/Sandbox.ts +++ b/client/src/game/scenes/Sandbox.ts @@ -36,7 +36,7 @@ export class Sandbox extends Scene { console.log("Rect2 clicked"); }); - this.taskManager.addTask(testId, createTask(this, testId, Tasks.VIRUS_CONTAINMENT)); + this.taskManager.addTask(testId, createTask(this, testId, Tasks.NETWORK_MAPPING)); console.log("Added task"); this.taskManager.startTask(testId);