Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 85 additions & 67 deletions client/src/game/gameObjs/tasks/networkMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,45 +41,48 @@ export class NetworkMapping extends Task {
}

async preload(): Promise<void> {
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");
resolve();
});
this.scene.load.start();
} else {
resolve();
resolve();
}
});
}

async start(): Promise<void> {
console.log("NetworkMapping task starting");
await this.preload();

this.initializeDevices();
this.initializeWires();

Expand All @@ -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);
Expand Down Expand Up @@ -149,90 +149,98 @@ 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;
} else {
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;
Expand All @@ -243,19 +251,19 @@ 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}`);
} else {
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);
Expand All @@ -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 = [
{
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion client/src/game/scenes/Sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down