-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
301 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |