Skip to content

Commit

Permalink
Electrician 9: big refactor (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdrzazga authored Dec 22, 2024
1 parent aa51558 commit 2b89a8a
Show file tree
Hide file tree
Showing 6 changed files with 317 additions and 330 deletions.
82 changes: 3 additions & 79 deletions other/electrician/files/classdef.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class Ladder {

class Building {

static GROUND_FLOOR_LEVEL = 589;

constructor(){
this.ladder = new Ladder();
this.floors = [];
Expand All @@ -84,28 +86,8 @@ class Building {
this.rightPowerLine = new PowerLine();
}

init(floorCount, physics){
init(physics){
this.ladder.init(physics);

const floorBuilder1 = new FloorBuilder();
this.floors.push(floorBuilder1.withName('attic').withBottomConnector(3).withBottomConnector(11)
.withCeilingConnector(5).withCeilingConnector(25).withBottomConnector(28).build());
const floorBuilder2 = new FloorBuilder();
this.floors.push(floorBuilder2.withName('living room').withCeilingConnector(2).withCeilingConnector(29)
.withLampInCenter().withTVInCenterLeft().build());
const kitchenBuilder = new FloorBuilder();
this.floors.push(kitchenBuilder.withName('kitchen').withFridgeOnLeft().withLampInCenter().withKitchenSegmentOnRight().build());

this.floors.forEach(floor => floor.init(physics));
this.floors.forEach(floor => floor.calculateFloorLevel());

const connectionPointsCounts = [2, 6, 5];
this.wires = this.floors.map((floor, i) => {
const aboveFloor = this.floors[i] || null;
const belowFloor = this.floors[i - 1] || null;
return new Wire(i, physics, belowFloor, aboveFloor, connectionPointsCounts[i]);
});

this.leftPowerLine.init(physics, 'left');
this.rightPowerLine.init(physics, 'right');
}
Expand Down Expand Up @@ -340,64 +322,6 @@ class Rat extends Enemy{
}
}

class Creator {
static create3storeBuilding(physics) {
const building = new Building();
building.init(3, physics);
building.includeWiresInInfoFrame();

const atticCeilingLevel = 104;
const livingRoomCeilingLevel = 328 - Floor.HEIGHT / 2;
const kitchenLevel = 438;
const groundFloorLevel = 589; //under kitchen

const ratsData = [
{ id: 1, active: true, y: groundFloorLevel },
{ id: 2, active: true, y: groundFloorLevel, minX: Floor.WIDTH / 2, maxX: 2 * Floor.WIDTH / 3, velocity: { x : 3} },
{ id: 3, active: true, y: atticCeilingLevel, minX: Floor.WIDTH / 3 + 30, maxX: 1.15*Floor.WIDTH, wireId: 0 },
{ id: 4, active: true, y: kitchenLevel, velocity: { x: 0.7 }, wireId: 2},
{ id: 5, active: true, y: groundFloorLevel, velocity: { x: 1.4 } },
{ id: 6, active: true, y: livingRoomCeilingLevel, minX: 2 * Floor.WIDTH / 4, maxX: 1.15*Floor.WIDTH, velocity: { x: 1.4 }, wireId: 1 },
{ id: 7, active: true, y: livingRoomCeilingLevel, minX: 2 * Ladder.WIDTH, velocity: { x: 0.85}, wireId: 1 },
];

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);

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;
};

const rats = ratsData.map(data => createEnemy(Rat, data, physics));
const bats = batsData.map(data => createEnemy(Bat, data, physics));

building.enemies.push(...rats, ...bats);

return building;
}
}

class FloorBuilder {

constructor(){
Expand Down
78 changes: 78 additions & 0 deletions other/electrician/files/creators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
class Creator {
static create3storeBuilding(physics) {
let building = new Building();
building.init(physics); // Initializes ladder and power lines

const floorBuilder1 = new FloorBuilder();
building.floors.push(floorBuilder1.withName('attic').withBottomConnector(3).withBottomConnector(11)
.withCeilingConnector(5).withCeilingConnector(25).withBottomConnector(28).build());

const floorBuilder2 = new FloorBuilder();
building.floors.push(floorBuilder2.withName('living room').withCeilingConnector(2).withCeilingConnector(29)
.withLampInCenter().withTVInCenterLeft().build());

const kitchenBuilder = new FloorBuilder();
building.floors.push(kitchenBuilder.withName('kitchen').withFridgeOnLeft().withLampInCenter().withKitchenSegmentOnRight().build());

building.floors.forEach(floor => floor.init(physics));
building.floors.forEach(floor => floor.calculateFloorLevel());

const connectionPointsCounts = [2, 6, 5];
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 atticCeilingLevel = 104;
const livingRoomCeilingLevel = 328 - Floor.HEIGHT / 2;
const kitchenLevel = 438;

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 }
];

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);

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;
};

const rats = ratsData.map(data => createEnemy(Rat, data, physics));
const bats = batsData.map(data => createEnemy(Bat, data, physics));

building.enemies.push(...rats, ...bats);

return building;
}
}
Loading

0 comments on commit 2b89a8a

Please sign in to comment.