Skip to content

Commit

Permalink
🐛 fix(mario.js): adjust jumpHeight and gravity values to improve Mari…
Browse files Browse the repository at this point in the history
…o's jumping behavior

✨ feat(mario.js): refactor jump() method to use velocity-based jumping instead of fixed jump heights for more realistic jumping physics
  • Loading branch information
romantech committed Jan 20, 2024
1 parent ac57371 commit 1aa0258
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/mario.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import DomManager from './domManager.js';

class Mario {
static maxHeight = 250;
static jumpHeight = 10;
static jumpHeight = 18;
static gravity = 0.4; // 중력 가속도

constructor({ defaultBottom = 50, className = 'mario' } = {}) {
this.defaultBottom = defaultBottom;
Expand All @@ -16,26 +16,21 @@ class Mario {
}

jump() {
if (this.isJumping) return; // 이미 점프 중이면 추가 점프를 방지
if (this.isJumping) return;

this.isJumping = true;
let jumpCount = 0;

const down = () => {
const nextBottom = this.defaultBottom + --jumpCount * Mario.jumpHeight;
this.element.style.bottom = nextBottom + 'px';
if (nextBottom > this.defaultBottom) requestAnimationFrame(down);
else this.isJumping = false;
};
let velocity = Mario.jumpHeight;

const up = () => {
const nextBottom = this.defaultBottom + ++jumpCount * Mario.jumpHeight;
jumpCount++;
velocity = Math.max(velocity - Mario.gravity, 0);
let nextBottom = jumpCount * velocity + this.defaultBottom;

this.element.style.bottom = nextBottom + 'px';
if (nextBottom < Mario.maxHeight) {
requestAnimationFrame(up);
} else {
setTimeout(down, 50); // 최고점에 도달한 후에 지연
}

if (nextBottom > this.defaultBottom) requestAnimationFrame(up);
else this.isJumping = false;
};

up();
Expand Down

0 comments on commit 1aa0258

Please sign in to comment.