Skip to content

Commit

Permalink
Electrician 11 - Level 2 (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdrzazga authored Dec 23, 2024
1 parent 7dd702f commit f0a02ca
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 34 deletions.
3 changes: 2 additions & 1 deletion other/electrician/files/classdef.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
160 changes: 129 additions & 31 deletions other/electrician/files/creators.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -25,58 +29,152 @@ 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 = [
{ id: 0, active: true, speed: -0.017 },
{ 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

Expand Down
2 changes: 1 addition & 1 deletion other/electrician/files/electrician.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
27 changes: 26 additions & 1 deletion other/electrician/files/levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class Level2Scene extends LevelScene{

constructor() {
super('Level2');
this.nextLevel = 'lvl1'; //TODO: looped game
this.nextLevel = 'lvl3';
}

loadFloorImages(){
Expand All @@ -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();
}
}

0 comments on commit f0a02ca

Please sign in to comment.