게임 캐릭터 빌더
Coddy JavaScript 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨 — 56개 중 53번째.
챌린지
이것은 캐릭터가 무기를 장착하고 전투에 참여할 수 있는 간단한 게임 캐릭터 시스템입니다.
여러분의 작업은 Character 클래스에 다음 두 가지 메서드를 추가하는 것입니다:
메서드 1: takeDamage(amount)
- 이 메서드는 캐릭터의 체력을 amount만큼 감소시킵니다.
- 로그 출력:
${this.name} takes ${amount} damage
메서드 2: equipWeapon(weapon)
- 이 메서드는 캐릭터에게 무기를 부여합니다 (컴포지션: "has-a" 관계)
- 로그 출력:
${this.name} equipped ${weapon.name}
직접 해보기
import { Character } from './character.js';
import { Weapon } from './weapon.js';
const hero = new Character('Knight');
const sword = new Weapon('Sword', 10);
hero.equipWeapon(sword); // 출력되어야 함: "Knight equipped Sword"
hero.takeDamage(20); // 출력되어야 함: "Knight takes 20 damage"
console.log(hero.health); // 표시되어야 함: 80