From f0a02ca9a4bd6d91e9de69b9a4de1c4ce4076595 Mon Sep 17 00:00:00 2001 From: kdrzazga Date: Mon, 23 Dec 2024 15:17:19 +0100 Subject: [PATCH] Electrician 11 - Level 2 (#90) --- other/electrician/files/classdef.js | 3 +- other/electrician/files/creators.js | 160 ++++++++++++++++++++----- other/electrician/files/electrician.js | 2 +- other/electrician/files/levels.js | 27 ++++- 4 files changed, 158 insertions(+), 34 deletions(-) diff --git a/other/electrician/files/classdef.js b/other/electrician/files/classdef.js index c24caba..89a67d2 100644 --- a/other/electrician/files/classdef.js +++ b/other/electrician/files/classdef.js @@ -77,7 +77,8 @@ class Building { static GROUND_FLOOR_LEVEL = 589; - constructor(){ + constructor(label){ + this.label = label; this.ladder = new Ladder(); this.floors = []; this.wires = []; diff --git a/other/electrician/files/creators.js b/other/electrician/files/creators.js index f370219..a5b61b8 100644 --- a/other/electrician/files/creators.js +++ b/other/electrician/files/creators.js @@ -1,6 +1,10 @@ class Creator { + + static HIGH_FLOOR_LEVEL = 104; + static LOW_FLOOR_LEVEL = 438; + static create3storeBuilding(physics) { - let building = new Building(); + let building = new Building('House'); building.init(physics); // Initializes ladder and power lines const floorBuilder1 = new FloorBuilder(); @@ -25,19 +29,16 @@ class Creator { }); building.includeWiresInInfoFrame(); - - const atticCeilingLevel = 104; - const livingRoomCeilingLevel = 328 - Floor.HEIGHT / 2; - const kitchenLevel = 438; + const MID_FLOOR_LEVEL = 328 - Floor.HEIGHT / 2; const ratsData = [ { id: 1, active: true, y: Building.GROUND_FLOOR_LEVEL }, { id: 2, active: true, y: Building.GROUND_FLOOR_LEVEL, minX: Floor.WIDTH / 2, maxX: 2 * Floor.WIDTH / 3, velocity: { x : 3} }, { id: 3, active: true, y: Building.GROUND_FLOOR_LEVEL, velocity: { x: 1.4 } }, - { id: 4, active: true, y: kitchenLevel, velocity: { x: 0.7 }, wireId: 2}, - { id: 5, active: true, y: livingRoomCeilingLevel, minX: 2 * Floor.WIDTH / 4, maxX: 1.15*Floor.WIDTH, velocity: { x: 1.4 }, wireId: 1 }, - { id: 6, active: true, y: livingRoomCeilingLevel, minX: 2 * Ladder.WIDTH, velocity: { x: 0.85}, wireId: 1 }, - { id: 7, active: true, y: atticCeilingLevel, minX: Floor.WIDTH / 3 + 30, maxX: 1.15*Floor.WIDTH, wireId: 0 } + { id: 4, active: true, y: Creator.LOW_FLOOR_LEVEL, velocity: { x: 0.7 }, wireId: 2}, + { id: 5, active: true, y: MID_FLOOR_LEVEL, minX: 2 * Floor.WIDTH / 4, maxX: 1.15*Floor.WIDTH, velocity: { x: 1.4 }, wireId: 1 }, + { id: 6, active: true, y: MID_FLOOR_LEVEL, minX: 2 * Ladder.WIDTH, velocity: { x: 0.85}, wireId: 1 }, + { id: 7, active: true, y: Creator.HIGH_FLOOR_LEVEL, minX: Floor.WIDTH / 3 + 30, maxX: 1.15*Floor.WIDTH, wireId: 0 } ]; const batsData = [ @@ -45,38 +46,135 @@ class Creator { { id: 1, active: true, currentAngle: Math.PI / 2, /*speed: 0.001*/ } ]; - const createEnemy = (EnemyClass, data, physics, positionAdjustment = 0) => { - const enemy = new EnemyClass(data.id); - enemy.active = data.active; - const y = EnemyClass === Rat ? data.y : 555 + 7 * data.id + positionAdjustment; - enemy.init(physics, y); + building.enemies = Creator.createEnemies(ratsData, batsData, physics); + + return building; + } + + static createEnemies(ratsData, batsData, physics){ + let enemies = []; + const rats = ratsData.map(data => Creator.createEnemy(Rat, data, physics)); + const bats = batsData.map(data => Creator.createEnemy(Bat, data, physics)); + + enemies.push(...rats, ...bats); + return enemies; + } + + static createOfficeGymGarage(physics){ + let building = new Building('Office Gym Garage'); + building.init(physics); // Initializes ladder and power lines + + const officeBuilder = new FloorBuilder(); + building.floors.push(officeBuilder.withName('office').withBottomConnector(5).withBottomConnector(9) + .withBottomConnector(13).withBottomConnector(16).withBottomConnector(19).withBottomConnector(26) + .withCeilingConnector(5).withCeilingConnector(12).withCeilingConnector(20).withCeilingConnector(26).build()); + + const gymBuilder = new FloorBuilder(); + building.floors.push(gymBuilder.withName('gym').withCeilingConnector(7).withCeilingConnector(23) + .build()); + + const garageBuilder = new FloorBuilder(); + building.floors.push(garageBuilder.withName('garage').withCeilingConnector(5).withCeilingConnector(20) + .withCeilingConnector(22) + .build()); - Object.assign(enemy, { - minX: data.minX || enemy.minX, - maxX: data.maxX || enemy.maxX, - angularSpeed: data.speed || enemy.angularSpeed, - currentAngle: data.currentAngle || enemy.currentAngle, - wireId: EnemyClass === Rat ? data.wireId : undefined - }); + building.floors.forEach(floor => floor.init(physics)); + building.floors.forEach(floor => floor.calculateFloorLevel()); - if (EnemyClass === Bat) { - enemy.centerX = Constants.SCREEN_WIDTH / 2 + 50 - 39 * data.id; - } + const connectionPointsCounts = [4, 8, 3]; + building.wires = building.floors.map((floor, index) => { + const aboveFloor = building.floors[index] || null; + const belowFloor = building.floors[index - 1] || null; + return new Wire(index, physics, belowFloor, aboveFloor, connectionPointsCounts[index]); + }); - if (data.velocity) enemy.sprite.velocity = data.velocity; + building.includeWiresInInfoFrame(); - return enemy; - }; + const ratsData = [ + { id: 1, active: true, y: Building.GROUND_FLOOR_LEVEL }, + { id: 2, active: true, y: Creator.LOW_FLOOR_LEVEL, velocity: { x: 0.7 }, wireId: 2}, + //{ id: 4, active: true, y: MID_FLOOR_LEVEL, velocity: { x: 0.85}, wireId: 1}, + { id: 3, active: true, y: Creator.HIGH_FLOOR_LEVEL, wireId: 0} + ]; - const rats = ratsData.map(data => createEnemy(Rat, data, physics)); - const bats = batsData.map(data => createEnemy(Bat, data, physics)); + const batsData = [ + { id: 0, active: true, speed: -0.007 } + ]; - building.enemies.push(...rats, ...bats); + building.enemies = Creator.createEnemies(ratsData, batsData, physics); return building; } - static createOfficeGymGarage(physics){ + static createBuilding(physics){ + let building = new Building('Home 2'); + building.init(physics); // Initializes ladder and power lines + + const atticBuilder = new FloorBuilder(); + building.floors.push(atticBuilder.withName('attic').withCeilingConnector(12).build()); + + const gymBuilder = new FloorBuilder(); + building.floors.push(gymBuilder.withName('gym').withCeilingConnector(7).withCeilingConnector(23) + .build()); + + const garageBuilder = new FloorBuilder(); + building.floors.push(garageBuilder.withName('garage').withCeilingConnector(5).withCeilingConnector(20) + .withCeilingConnector(22) + .build()); + + building.floors.forEach(floor => floor.init(physics)); + building.floors.forEach(floor => floor.calculateFloorLevel()); + + const connectionPointsCounts = [4, 8, 3]; + building.wires = building.floors.map((floor, index) => { + const aboveFloor = building.floors[index] || null; + const belowFloor = building.floors[index - 1] || null; + return new Wire(index, physics, belowFloor, aboveFloor, connectionPointsCounts[index]); + }); + + building.includeWiresInInfoFrame(); + + //const MID_FLOOR_LEVEL = 328 - Floor.HEIGHT / 2; + const ratsData = [ + { id: 1, active: true, y: Building.GROUND_FLOOR_LEVEL }, + { id: 2, active: true, y: Creator.LOW_FLOOR_LEVEL, velocity: { x: 0.7 }, wireId: 2}, + //{ id: 4, active: true, y: MID_FLOOR_LEVEL, velocity: { x: 0.85}, wireId: 1}, + { id: 3, active: true, y: Creator.HIGH_FLOOR_LEVEL, wireId: 0} + ]; + + const batsData = [ + { id: 0, active: true, speed: -0.007 } + ]; + + building.enemies = Creator.createEnemies(ratsData, batsData, physics); + + return building; + } + + static createEnemy(EnemyClass, data, physics, positionAdjustment = 0) { + const enemy = new EnemyClass(data.id); + enemy.active = data.active; + const y = EnemyClass === Rat ? data.y : 555 + 7 * data.id + positionAdjustment; + enemy.init(physics, y); + + Object.assign(enemy, { + minX: data.minX || enemy.minX, + maxX: data.maxX || enemy.maxX, + angularSpeed: data.speed || enemy.angularSpeed, + currentAngle: data.currentAngle || enemy.currentAngle, + wireId: EnemyClass === Rat ? data.wireId : undefined + }); + + if (EnemyClass === Bat) { + enemy.centerX = Constants.SCREEN_WIDTH / 2 + 50 - 39 * data.id; + } + + if (data.velocity) enemy.sprite.velocity = data.velocity; + + return enemy; + } + + static createOfficeGymGarage(physics){ let building = new Building(); building.init(physics); // Initializes ladder and power lines diff --git a/other/electrician/files/electrician.js b/other/electrician/files/electrician.js index 07e2036..1639af5 100644 --- a/other/electrician/files/electrician.js +++ b/other/electrician/files/electrician.js @@ -1,4 +1,4 @@ -const levelsJson = {'lvl1' : Level1Scene, 'lvl2': Level2Scene} +const levelsJson = {'lvl1' : Level1Scene, 'lvl2': Level2Scene, 'lvl3': Level3Scene} let level = sessionStorage.getItem("level"); let levelObject = Level1Scene; diff --git a/other/electrician/files/levels.js b/other/electrician/files/levels.js index 32bc323..6972514 100644 --- a/other/electrician/files/levels.js +++ b/other/electrician/files/levels.js @@ -248,7 +248,7 @@ class Level2Scene extends LevelScene{ constructor() { super('Level2'); - this.nextLevel = 'lvl1'; //TODO: looped game + this.nextLevel = 'lvl3'; } loadFloorImages(){ @@ -268,3 +268,28 @@ class Level2Scene extends LevelScene{ this.cursors = this.input.keyboard.createCursorKeys(); } } + +class Level3Scene extends LevelScene { + + constructor() { + super('Level3'); + this.nextLevel = 'lvl1'; //TODO: looped game + } + + loadFloorImages(){ + this.load.image('floor0', 'files/attic2.png'); + this.load.image('floor1', 'files/home1.png'); + this.load.image('floor2', 'files/basement.png'); + } + + create() { + this.physics.world.setBounds(0, 0, 800, 600); + + this.building = Creator.createBuilding(this.physics); + + this.player = this.physics.add.sprite(100, 400, 'sprite'); + this.player.setCollideWorldBounds(true); + + this.cursors = this.input.keyboard.createCursorKeys(); + } +}