Skip to content

Commit

Permalink
๐Ÿ”ง chore(audio): update jump and coin sound files
Browse files Browse the repository at this point in the history
๐Ÿ”ง chore(audio-manager.js): update jump and coin sound file paths
๐Ÿ”ง chore(event-handler.js): rename handleKeyDown to handleJump and handleTouch to handleJump
๐Ÿ”ง chore(event-handler.js): add condition to play jump sound only on keyboard jump
๐Ÿ”ง chore(event-handler.js): prevent default touchstart event to avoid button click on mobile
๐Ÿ”ง chore(game.js): remove audio parameter from Mario constructor
๐Ÿ”ง chore(mario.js): remove audio parameter from Mario constructor
๐Ÿ”ง chore(mario.js): remove audio.playEffect('jump') from jump method
  • Loading branch information
romantech committed Feb 4, 2024
1 parent 9508ece commit 94bf3c1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 23 deletions.
File renamed without changes.
File renamed without changes.
8 changes: 6 additions & 2 deletions src/audio-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ 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 COIN_SOUND_PATH = './assets/audio/coin-sound.mp3';
static JUMP_SOUND_PATH = './assets/audio/jump.mp3';
static COIN_SOUND_PATH = './assets/audio/coin.mp3';
static BGM_PATH = './assets/audio/bgm.mp3';

soundOnImage = new Image();
Expand All @@ -19,8 +19,12 @@ class AudioManager {
this.audio.autoplay = autoplay;
this.audio.loop = loop;
this.audio.muted = muted;

this.jumpSound.volume = 0.5;
this.jumpSound.playbackRate = 2;

this.coinSound.volume = 0.5;
this.coinSound.playbackRate = 2;

this.preloadImages();
}
Expand Down
32 changes: 16 additions & 16 deletions src/event-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,40 @@ export default class EventHandler {
constructor(game) {
this.game = game;
// ๋™์ผํ•œ ์ฐธ์กฐ์˜ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ์ด๋ฒคํŠธ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ this.handleKeyDown ๋ฉ”์„œ๋“œ ๋ฐ”์ธ๋”ฉ
this.handleKeyDown = this.handleKeyDown.bind(this);
this.handleTouch = this.handleTouch.bind(this);
this.handleJump = this.handleJump.bind(this);
this.handleDialogClose = this.handleDialogClose.bind(this);
}

setupEventListeners() {
document.addEventListener('keydown', this.handleKeyDown);
document.addEventListener('keydown', this.handleJump);
DomManager.dialog.addEventListener('close', this.handleDialogClose);
DomManager.gameArea.addEventListener('touchstart', this.handleTouch, {
DomManager.gameArea.addEventListener('touchstart', this.handleJump, {
passive: false, // event.preventDefault() ํ˜ธ์ถœํ•  ๊ฒƒ์ด๋ผ๊ณ  ๋ธŒ๋ผ์šฐ์ €์—๊ฒŒ ์•Œ๋ฆผ
});
}

removeEventListeners() {
document.removeEventListener('keydown', this.handleKeyDown);
DomManager.gameArea.removeEventListener('touchstart', this.handleTouch);
document.removeEventListener('keydown', this.handleJump);
DomManager.gameArea.removeEventListener('touchstart', this.handleJump);
}

handleDialogClose(e) {
if (e.target.returnValue === 'restart') this.game.restart();
}

handleKeyDown(e) {
// "์‹œ์ž‘" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋ฒ„ํŠผ์— ํฌ์ปค์Šค๋œ ์ƒํƒœ๊ฐ€ ๋˜๊ณ ,
// ์ŠคํŽ˜์ด์Šค๋ฅผ ๋ˆ„๋ฅด๋ฉด ๊ธฐ๋ณธ ๋™์ž‘์œผ๋กœ ์ธํ•ด ์‹œ์ž‘ ๋ฒ„ํŠผ์ด ํด๋ฆญ๋˜๋Š” ๋ฌธ์ œ ์žˆ์Œ
// preventDefault()๋ฅผ ์ด์šฉํ•ด ๊ธฐ๋ณธ ๋™์ž‘์„ ํ•ด์ œํ•˜๋ฉด ๋ฌธ์ œ ๋ฐœ์ƒ ์•ˆํ•จ
if (e.code === 'Space') {
handleJump(e) {
const isSpaceKeyPressed = e instanceof KeyboardEvent && e.code === 'Space';
const isTouchEvent = e instanceof TouchEvent && e.type === 'touchstart';

if (isSpaceKeyPressed || isTouchEvent) {
// "์‹œ์ž‘" ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋ฒ„ํŠผ์— ํฌ์ปค์Šค๋œ ์ƒํƒœ๊ฐ€ ๋˜๊ณ ,
// ์ŠคํŽ˜์ด์Šค๋ฅผ ๋ˆ„๋ฅด๋ฉด ๊ธฐ๋ณธ ๋™์ž‘์œผ๋กœ ์ธํ•ด ์‹œ์ž‘ ๋ฒ„ํŠผ์ด ํด๋ฆญ๋˜๋Š” ๋ฌธ์ œ ์žˆ์Œ
// preventDefault()๋ฅผ ์ด์šฉํ•ด ๊ธฐ๋ณธ ๋™์ž‘์„ ํ•ด์ œํ•˜๋ฉด ๋ฌธ์ œ ๋ฐœ์ƒ ์•ˆํ•จ
e.preventDefault();

// ๋ชจ๋ฐ”์ผ์—์„  ์ ํ”„, ์ฝ”์ธ ์‚ฌ์šด๋“œ๋ฅผ ๋‘˜๋‹ค ์žฌ์ƒํ•˜๋ฉด ์‚ด์ง ๋Š๊น€ ํ˜„์ƒํ•ด์„œ ์ ํ”„ ์†Œ๋ฆฌ ๋น„ํ™œ์„ฑ
!isTouchEvent && this.game.audio.playEffect('jump');
this.game.mario.jump();
}
}

handleTouch(e) {
e.preventDefault();
this.game.mario.jump();
}
}
2 changes: 1 addition & 1 deletion src/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Game {
this.score = new Score();
this.background = new Background({ speed });
this.entityManager = new EntityManager({ speed, bottom });
this.mario = new Mario({ bottom, audio: this.audio });
this.mario = new Mario({ bottom });
this.eventHandler = new EventHandler(this);

// ๋™์ผํ•œ ์ฐธ์กฐ์˜ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ์ด๋ฒคํŠธ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ this.handleKeyDown ๋ฉ”์„œ๋“œ ๋ฐ”์ธ๋”ฉ
Expand Down
5 changes: 1 addition & 4 deletions src/mario.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ class Mario {
runImage = new Image();
element = new Image();

audio;
bottom;
isJumping = false;

constructor({ audio, bottom, className = 'mario' }) {
this.audio = audio;
constructor({ bottom, className = 'mario' }) {
this.bottom = bottom;

this.preloadImages()
Expand Down Expand Up @@ -53,7 +51,6 @@ class Mario {
jump() {
if (this.isJumping) return;

this.audio.playEffect('jump');
this.isJumping = true;
let jumpCount = 0;
let velocity = Mario.JUMP_HEIGHT;
Expand Down

0 comments on commit 94bf3c1

Please sign in to comment.