Skip to content

Commit

Permalink
Electrician 1 (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdrzazga authored Dec 5, 2024
1 parent 51ff2bf commit 4382407
Show file tree
Hide file tree
Showing 8 changed files with 301 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<a href = 'https://kdrzazga.github.io/js-playwright/other/c64blackbox/directF6ClassicC64.html' target="_blank">K&A+</a> |
<a href = 'https://kdrzazga.github.io/js-playwright/other/cube' target="_blank">Cube</a> |
<a href = 'https://kdrzazga.github.io/js-playwright/other/brainfuck' target="_blank">Brainfuck</a><br/>
<a href = 'https://kdrzazga.github.io/js-playwright/other/arkanoid' target="_blank">Arkanoid</a> |
<a href = 'https://kdrzazga.github.io/js-playwright/other/electrician' target="_blank">Electrician</a> |
<a href = 'https://kdrzazga.github.io/js-playwright/other/xmas' target="_blank">X-Mas</a> |
<a href = '' target="_blank"></a><br/>
</div>
Expand Down
53 changes: 53 additions & 0 deletions other/electrician/files/classdef.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class Floor {
static COUNT = 0;
static BUILDING_HEIGHT = 500;

constructor() {
this.id = Floor.COUNT;
Floor.COUNT++;
this.floorLevel = 0;
}

calculateFloorLevel(){
//should be called only after all floors are created
this.floorLevel = Math.ceil((this.id + 1) * Floor.BUILDING_HEIGHT / (Floor.COUNT + 1));
console.log(`Floor ${this.id} is on level = ${this.floorLevel}`);
}

onFloor(y) {
const tolerance = this.floorLevel * 0.10;
const lowerBound = this.floorLevel;
const upperBound = this.floorLevel + tolerance;
return y >= lowerBound && y <= upperBound;
}

}

class Ladder {
static WIDTH = 60;

constructor() {
this.x = 30;
}

onLadder(x1){
const edge1 = this.x - Ladder.WIDTH /2;
const edge2 = this.x + Ladder.WIDTH /2;
return x1 > edge1 && x1 < edge2;
}
}

class Building {
constructor(floorCount){
this.ladder = new Ladder();

this.floors = [];
for (let i = 0; i < floorCount; i++) {
const floor = new Floor();
this.floors.push(floor);
}
this.floors.forEach(f => f.calculateFloorLevel());
}
}

module.exports = { Floor, Ladder, Building};
117 changes: 117 additions & 0 deletions other/electrician/files/electrician.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
class MainScene extends Phaser.Scene {
constructor() {
super({ key: 'MainScene' });
this.floors = [];
this.ladder = new Ladder();
this.spriteCanJump = true;
}

preload() {
this.load.image('sprite', 'files/electrician.png');
}

create() {
this.physics.world.setBounds(0, 0, 800, 600);

this.building = new Building(3);

this.sprite = this.physics.add.sprite(100, 400, 'sprite');
this.sprite.setCollideWorldBounds(true);

this.cursors = this.input.keyboard.createCursorKeys();
}

update() {
this.handleMovement();
this.conditionalFallDown();
}

jump(direction) {
if (!this.spriteCanJump) return;

this.spriteCanJump = false;
const jumpHeight = 45;
const duration = 750;

const jumpTween = {
targets: this.sprite,
y: this.sprite.y - jumpHeight, // Move up
duration: duration / 2, // Up for half the duration
ease: 'Linear',
onComplete: () => {

const comeDownTween = {
targets: this.sprite,
y: this.sprite.y + jumpHeight, // Move down
duration: duration / 2,
ease: 'Linear',
onComplete: () => {
// Re-enable jumping after 1 second
this.spriteCanJump = true;
}
};
this.tweens.add(comeDownTween);
}
}

this.tweens.add(jumpTween);

if (direction === 'left') {
this.sprite.setVelocityX(-160);
} else if (direction === 'right') {
this.sprite.setVelocityX(160);
} else {
this.sprite.setVelocityX(0); // No horizontal movement if just jumping
}
}

conditionalFallDown(){
if (this.ladder.onLadder(this.sprite.x))
return; //Ladder prevents from falling;
else this.sprite.setVelocityY(160);
}

handleMovement() {
let velocityX = 0;
let velocityY = 0;

if (this.cursors.left.isDown) {
velocityX = -160;
if (this.cursors.up.isDown) {
this.jump('left');
}
} else if (this.cursors.right.isDown) {
velocityX = 160;
if (this.cursors.up.isDown) {
this.jump('right');
}
}

if (velocityX === 0 && this.ladder.onLadder(this.sprite.x)) {
if (this.cursors.up.isDown) {
velocityY = -160;
} else if (this.cursors.down.isDown) {
velocityY = 160;
}
}

this.sprite.setVelocityX(velocityX);
this.sprite.setVelocityY(velocityY);
}
}

const config = {
type: Phaser.AUTO,
width: 800,
height: 600,
physics: {
default: 'arcade',
arcade: {
gravity: { y: 0 },
debug: false
}
},
scene: MainScene
};

const game = new Phaser.Game(config);
Binary file added other/electrician/files/electrician.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions other/electrician/files/electrician.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const { Floor, Ladder, Building } = require('./classdef');

describe('Floor Class', () => {
beforeEach(() => {
Floor.COUNT = 0; // Reset the static COUNT before each test
});

test('should create a Floor instance with a unique id', () => {
const floor1 = new Floor();
const floor2 = new Floor();

expect(floor1.id).toBe(0);
expect(floor2.id).toBe(1);
});

test('should increment the static COUNT correctly', () => {
const floor1 = new Floor();
const floor2 = new Floor();
const floor3 = new Floor();

expect(Floor.COUNT).toBe(3);
});
});

describe('Ladder Class', () => {
let ladder;

beforeEach(() => {
ladder = new Ladder();
});

test('should be initialized with correct x position', () => {
expect(ladder.x).toBe(30);
});

test('should report if a given x position is on the ladder', () => {
expect(ladder.onLadder(0)).toBe(false); // Outside WIDTH (30 - 60)
expect(ladder.onLadder(100)).toBe(false); // Outside WIDTH (30 + 60)
expect(ladder.onLadder(30)).toBe(true); // Exact position
expect(ladder.onLadder(45)).toBe(true); // Within WIDTH (30 + 30)
});

test('should correctly handle different x positions for being on the ladder', () => {
expect(ladder.onLadder(90)).toBe(false); // Way outside
expect(ladder.onLadder(29)).toBe(true); // Just inside
expect(ladder.onLadder(31)).toBe(true); // Just inside
});
});

describe('Building', () => {
let building;

beforeEach(() => {
Floor.COUNT = 0;
building = new Building(5);
});

test('should create the correct number of floors', () => {
expect(building.floors.length).toBe(5);
});

test('should correctly calculate floor levels', () => {
const expectedLevels = [84, 167, 250, 334, 417];
building.floors.forEach((floor, index) => {
expect(floor.floorLevel).toBe(expectedLevels[index]);
});
});

test('should create a ladder with correct position', () => {
expect(building.ladder.x).toBe(30);
});

});
13 changes: 13 additions & 0 deletions other/electrician/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html>
<head>

</head>
<body>
<div id = "board">

</div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/3.55.2/phaser.min.js"></script>
<script src="files/classdef.js"></script>
<script src="files/electrician.js"></script>
</html>
29 changes: 29 additions & 0 deletions other/electrician/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions other/electrician/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "electrician",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"phaser": "^3.87.0"
}
}

0 comments on commit 4382407

Please sign in to comment.