Skip to content

Commit

Permalink
๐ŸŽจ chore(audio-manager.js): refactor AudioManager constructor to use sโ€ฆ
Browse files Browse the repository at this point in the history
โ€ฆtatic paths for sound and image files

๐Ÿ› fix(audio-manager.js): fix error handling in preloadImages method
๐Ÿ”Š feat(audio-manager.js): add playJumpSound method to play jump sound when Mario jumps
๐Ÿ”ง refactor(background.js): remove unnecessary instance variables from Background class
๐Ÿ”ง refactor(game.js): remove unnecessary instance variables from Game class
๐Ÿ”ง refactor(mario.js): remove unnecessary instance variables from Mario class
๐Ÿ”ง refactor(obstacle.js): remove unnecessary instance variables from ObstacleManager class
๐Ÿ”ง refactor(utils.js): rename src parameter to srcset in loadImages function
  • Loading branch information
romantech committed Feb 1, 2024
1 parent fa5d8b8 commit 4f4d16f
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 42 deletions.
File renamed without changes.
Binary file added assets/audio/jump-sound.mp3
Binary file not shown.
2 changes: 1 addition & 1 deletion main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Game } from './src/index.js';

const init = () => {
const game = new Game({ defaultBottom: 50, speed: 5 });
const game = new Game();
};

window.onload = init;
36 changes: 20 additions & 16 deletions src/audio-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,21 @@ import DomManager from './dom-manager.js';
import { loadImages } from './utils.js';

class AudioManager {
static SOUND_ON_PATH = './assets/sound-on.png';
static SOUND_OFF_PATH = './assets/sound-off.png';
static JUMP_SOUND_PATH = './assets/audio/jump-sound.mp3';
static BGM_PATH = './assets/audio/bgm.mp3';

soundOnImage = new Image();
soundOffImage = new Image();

constructor({
filePath = './assets/mario-bgm.mp3',
iconSources = ['./assets/sound-on.png', './assets/sound-off.png'],
autoplay = true,
loop = true,
muted = true,
} = {}) {
this.audio = new Audio(filePath);
audio = new Audio(AudioManager.BGM_PATH);
jumpSound = new Audio(AudioManager.JUMP_SOUND_PATH);

constructor({ autoplay = true, loop = true, muted = true } = {}) {
this.audio.autoplay = autoplay;
this.audio.loop = loop;
this.audio.muted = muted;
this.iconSources = iconSources;

this.audio.onerror = () => {
console.error('Failed to load audio:', filePath);
};

this.preloadImages();
}
Expand All @@ -34,17 +30,25 @@ class AudioManager {
}

async preloadImages() {
const srcset = [AudioManager.SOUND_ON_PATH, AudioManager.SOUND_OFF_PATH];
try {
const [soundOn, soundOff] = await loadImages(this.iconSources);
const [soundOn, soundOff] = await loadImages(srcset);
this.soundOnImage.src = soundOn.src;
this.soundOffImage.src = soundOff.src;
} catch (error) {
console.error(error);
console.error('Error preloading audio images:', error);
}
}

playJumpSound() {
if (this.isMuted) return;
this.jumpSound.play();
}

#toggleIcon() {
const src = this.isMuted ? this.iconSources[1] : this.iconSources[0];
const src = this.isMuted
? AudioManager.SOUND_OFF_PATH
: AudioManager.SOUND_ON_PATH;
DomManager.audioToggle.style.backgroundImage = `url(${src})`;
}

Expand Down
6 changes: 4 additions & 2 deletions src/background.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import DomManager from './dom-manager.js';

class Background {
speed;
positionX = 0;
frameId = null;

constructor({ speed }) {
this.speed = speed;
this.positionX = 0;
this.frameId = null;
this.move = this.move.bind(this);
}

Expand Down
21 changes: 17 additions & 4 deletions src/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,30 @@ import {
} from './index.js';

class Game {
static DEFAULT_SPEED = 5;
static DEFAULT_BOTTOM = 50;

isPlaying = false;
obstacleTimerId = null;
collisionFrameId = null;
lastPassedObstacle = null;

constructor({ speed, defaultBottom }) {
this.mario = new Mario({ defaultBottom });
audio;
score;
mario;
background;
obstacles;
eventHandler;

constructor({
speed = Game.DEFAULT_SPEED,
defaultBottom = Game.DEFAULT_BOTTOM,
} = {}) {
this.audio = new AudioManager();
this.score = new Score();
this.background = new Background({ speed });
this.obstacles = new ObstacleManager({ speed, defaultBottom });
this.score = new Score();
this.audio = new AudioManager();
this.mario = new Mario({ defaultBottom, audio: this.audio });
this.eventHandler = new EventHandler(this);

// ๋™์ผํ•œ ์ฐธ์กฐ์˜ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ์ด๋ฒคํŠธ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ this.handleKeyDown ๋ฉ”์„œ๋“œ ๋ฐ”์ธ๋”ฉ
Expand Down
34 changes: 19 additions & 15 deletions src/mario.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,43 @@ import DomManager from './dom-manager.js';
import { loadImages } from './utils.js';

class Mario {
static jumpHeight = 18; // ์ ํ”„ ๋†’์ด. ๋†’์„์ˆ˜๋ก ๋” ๋†’์ด ์ ํ”„
static gravity = 0.4; // ์ค‘๋ ฅ ๊ฐ€์†๋„. ๋‚ฎ์„์ˆ˜๋ก ๋” ์˜ค๋ž˜ ์ ํ”„
static JUMP_HEIGHT = 18; // ์ ํ”„ ๋†’์ด. ๋†’์„์ˆ˜๋ก ๋” ๋†’์ด ์ ํ”„
static GRAVITY = 0.4; // ์ค‘๋ ฅ ๊ฐ€์†๋„. ๋‚ฎ์„์ˆ˜๋ก ๋” ์˜ค๋ž˜ ์ ํ”„
static STOP_IMAGE_PATH = './assets/mario-stop.png';
static RUN_IMAGE_PATH = './assets/mario-run.gif';

stopImage = new Image();
runImage = new Image();
element = new Image();

constructor({
defaultBottom,
className = 'mario',
imgSources = ['./assets/mario-stop.png', './assets/mario-run.gif'],
}) {
audio;
defaultBottom;
isJumping = false;

constructor({ audio, defaultBottom, className = 'mario' }) {
this.audio = audio;
this.defaultBottom = defaultBottom;
this.imgSources = imgSources;
this.isJumping = false;

this.preloadImages().then(() => this.initializeImage(className));
this.preloadImages()
.then(() => this.initializeImage(className))
.catch(error => console.error('Error initializing Mario:', error));
}

initializeImage(className) {
this.element.src = this.stopImage.src;
this.element.classList.add(className);
this.element.style.bottom = this.defaultBottom + 'px';

DomManager.gameArea.appendChild(this.element);
}

async preloadImages() {
const srcset = [Mario.STOP_IMAGE_PATH, Mario.RUN_IMAGE_PATH];
try {
const [stopImage, runImage] = await loadImages(this.imgSources);
const [stopImage, runImage] = await loadImages(srcset);
this.stopImage.src = stopImage.src;
this.runImage.src = runImage.src;
} catch (error) {
console.error(error);
console.error('Error preloading Mario images:', error);
}
}

Expand All @@ -50,9 +53,10 @@ class Mario {
jump() {
if (this.isJumping) return;

this.audio.playJumpSound();
this.isJumping = true;
let jumpCount = 0;
let velocity = Mario.jumpHeight;
let velocity = Mario.JUMP_HEIGHT;

/**
* ์ƒ์Šน(Fast) -> ์ •์ (Slow) -> ํ•˜๊ฐ•(Fast) ์ค‘๋ ฅ ์ž‘์šฉ์ด ์œ ์‚ฌํ•˜๊ฒŒ ์ ์šฉ๋œ ์ ํ”„ ๋ฉ”์„œ๋“œ
Expand All @@ -70,7 +74,7 @@ class Mario {
* */
const up = () => {
jumpCount++;
velocity = Math.max(velocity - Mario.gravity, 0);
velocity = Math.max(velocity - Mario.GRAVITY, 0);

let nextBottom = jumpCount * velocity + this.defaultBottom;
this.element.style.bottom = nextBottom + 'px';
Expand Down
5 changes: 3 additions & 2 deletions src/obstacle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ class ObstacleManager {
list = new Set();
frameId = null;
isMonitoring = false;
options;

constructor(options) {
this.defaultOptions = options;
this.options = options;
}

add() {
const obstacle = new Obstacle(this.defaultOptions);
const obstacle = new Obstacle(this.options);
DomManager.gameArea.appendChild(obstacle.element);

this.list.add(obstacle);
Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export const generateRandomNumber = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
};

export const loadImages = src => {
export const loadImages = srcset => {
// image.src ์†์„ฑ์— ๊ฐ’์„ ํ• ๋‹นํ•˜๋ฉด ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์ด๋ฏธ์ง€ ๋กœ๋“œ ์‹œ์ž‘
const loadImage = src => {
return new Promise((resolve, reject) => {
Expand All @@ -13,5 +13,5 @@ export const loadImages = src => {
});
};

return Promise.all(src.map(loadImage));
return Promise.all(srcset.map(loadImage));
};

0 comments on commit 4f4d16f

Please sign in to comment.