๐ ์ ํฌ ๊ฒฐ๊ณผ ๊ตฌํํ๊ธฐ
๊ณต๊ฒฉ์ ๋๋ฅด๋ฉด ์๋ก ๊ณต๊ฒฉํ๊ฒ ํด์, ๋ ์ค์ ๋จผ์ ์ฒด๋ ฅ์ด 0์ด ๋๋ ์ชฝ์ด ๋์ค๋ฉด ์น๋ถ๊ฐ ๊ฐ๋ ค์ง๋ค.
์ฃผ์ธ๊ณต์ ์ฒด๋ ฅ์ด 0์ด ๋๋ฉด ๊ฒ์ ์ค๋ฒ / ๋ชฌ์คํฐ์ ์ฒด๋ ฅ์ด 0์ด ๋๋ฉด ์ฃผ์ธ๊ณต์ ๊ฒฝํ์น๋ฅผ ์ป๋๋ค.
๊ฒฝํ์น๊ฐ ์ฃผ์ธ๊ณต ๋ ๋ฒจ x 15 ๋ณด๋ค ๋์ผ๋ฉด ์ฃผ์ธ๊ณต์ ๋ ๋ฒจ์ ์ ํ๋ค.
๋ ๋ฒจ์ ์ ํ๋ฉด ์ฃผ์ธ๊ณต์ ์ฒด๋ ฅ์ ๋ชจ๋ ํ๋ณตํ๊ณ , ์ต๋ ์ฒด๋ ฅ๊ณผ ๊ณต๊ฒฉ๋ ฅ์ด 5์ฉ ์ฆ๊ฐํ๋ค.
<script>
const $startScreen = document.querySelector('#start-screen');
const $gameMenu = document.querySelector('#game-menu');
const $battleMenu = document.querySelector('#battle-menu');
const $heroName = document.querySelector('#hero-name');
const $heroLevel = document.querySelector('#hero-level');
const $heroHp = document.querySelector('#hero-hp');
const $heroXp = document.querySelector('#hero-xp');
const $heroAtt = document.querySelector('#hero-att');
const $monsterName = document.querySelector('#monster-name');
const $monsterHp = document.querySelector('#monster-hp');
const $monsterAtt = document.querySelector('#monster-att');
const $message = document.querySelector('#message');
//๊ฒ์ ์์ฒด ํด๋์ค ๋ง๋ค๊ธฐ.
//Game,Hero,Monster 3๊ฐ์ ํด๋์ค ๋ง๋ค๊ณ ์ฃผ์ธ๊ณต ์ด๋ฆ ์
๋ ฅ ๋ฐ๊ธฐ.
class Game {
constructor (name){
this.monster = null;
this.hero = null;
this.monsterList = [
{name: '์ฌ๋ผ์',hp:25,att:10,xp:10},
{name: '์ค์ผ๋ ํค',hp:50,att:15,xp:20},
{name: '๋ง์',hp:150,att:35,xp:50},
];
this.start(name);
}
start(name){
$gameMenu.addEventListener('submit',this.onGameMenuInput);
$battleMenu.addEventListener('submit',this.onBattleMenuInput);
this.changeScreen('game');
this.hero = new Hero(this,name);
this.updateHeroStat();
}
changeScreen(screen){
if(screen === 'start'){
$startScreen.start.display = 'block';
$gameMenu.style.display = 'none';
$battleMenu.style.display = 'none';
} else if (screen === 'game'){
$startScreen.style.display = 'none';
$gameMenu.style.display = 'block';
$battleMenu.style.display = 'none';
} else if (screen === 'battle'){
$startScreen.style.display = 'none';
$gameMenu.style.display = 'none';
$battleMenu.style.display = 'block';
}
}
onGameMenuInput = (event)=>{
event.preventDefault();
const input = event.target['menu-input'].value;
if(input === '1'){ //๋ชจํ
this.changeScreen('battle');
const randomIndex = Math.floor(Math.random()*this.monsterList.length);
const randomMonster = this.monsterList[randomIndex];
this.monster = new Monster(
this,
randomMonster.name,
randomMonster.hp,
randomMonster.att,
randomMonster.xp,
);
this.updateHeroStat();
this.showMessage(`๋ชฌ์คํฐ์ ๋ง์ฃผ์ณค๋ค. ${this.monster.name}์ธ ๊ฒ ๊ฐ๋ค.`);
} else if (input === '2'){ //ํด์
} else if (input === '3'){ //์ข
๋ฃ
}
}
onBattleMenuInput = (event)=>{
event.preventDefault();
const input = event.target['battle-input'].value;
if(input === '1'){ //๊ณต๊ฒฉ
const {hero,monster}=this;
hero.attack(monster);
monster.attack(hero);
if(hero.hp <= 0){
this.showMessage(`${hero.lev} ๋ ๋ฒจ์์ ์ ์ฌ. ์ ์ฃผ์ธ๊ณต์ ์์ฑํ์ธ์.`);
this.quit();
} else if(monster.hp <= 0){
this.showMessage(`๋ชฌ์คํฐ๋ฅผ ์ก์ ${monster.xp} ๊ฒฝํ์น๋ฅผ ์ป์๋ค.`);
hero.getXp(monster.xp);
this.monster = null;
this.changeScreen('game');
}else{
this.showMessage(`${hero.att}์ ๋ฐ๋ฏธ์ง๋ฅผ ์ฃผ๊ณ ,${monster.att}์ ๋ฐ๋ฏธ์ง๋ฅผ ๋ฐ์๋ค.`);
}
this.updateHeroStat();
this.updateMonsterStat();
} else if (input === '2'){ //ํ๋ณต
} else if (input === '3'){ //๋๋ง
}
}
updateHeroStat(){
const {hero} = this;
if (hero === null){
$heroName.textContent='';
$heroLevel.textContent='';
$heroHp.textContent='';
$heroXp.textContent='';
$heroAtt.textContent='';
return;
}
$heroName.textContent=hero.name;
$heroLevel.textContent=`${hero.lev}lev`;
$heroHp.textContent=`HP: ${hero.hp}/${hero.maxHp}`;
$heroXp.textContent=`XP: ${hero.xp}/${15*hero.lev}`;
$heroAtt.textContent=`ATT: ${hero.att}`;
}
updateMonsterStat(){
const {monster} = this;
if (monster === null){
$monsterName.textContent='';
$monsterHp.textContent='';
$monsterAtt.textContent='';
return;
}
$monsterName.textContent=monster.name;
$monsterHp.textContent=`HP: ${monster.hp}/${monster.maxHp}`;
$monsterAtt.textContent=`ATT: ${monster.att}`;
}
showMessage(text){
$message.textContent=text;
}
quit(){
this.hero = null;
this.monster = null;
this.updateHeroStat();
this.updateMonsterStat();
$gameMenu.removeEventListener('submit',this.onGameMenuInput);
$battleMenu.removeEventListener('submit',this.onBattleMenuInput);
this.changeScreen('start');
game=null;
}
};//class game
//๊ฐ์ฒด ๋ฆฌํฐ๋ด๋ก ์ฃผ์ธ๊ณต ๋ง๋ค๊ธฐ.
class Hero{
constructor(game,name){
this.game = game;
this.name = name;
this.lev = 1;
this.maxHp = 100;
this.hp = 100;
this.xp = 0;
this.att =10;
}
//๊ฐ์ฒด์ ๋ฉ์๋์์๋ function ์์ฝ์ด ์๋ต๊ฐ๋ฅ.ex) attack : function(monster)
attack(target){ //๋ชฌ์คํฐ ๊ณต๊ฒฉ.
target.hp -= this.att;
}
heal(monster){ //์ฒด๋ ฅ ํ๋ณต.
this.hp += 20;
this.hp -= monster.att;
}
getXp(xp){
this.xp += xp;
if(this.xp >= this.lev * 15){ //๊ฒฝํ์น๋ฅผ ๋ค ์ฑ์ฐ๋ฉด
this.xp -= this.lev * 15;
this.lev += 1;
this.maxHp += 5;
this.att += 5;
this.hp = this.maxHp;
this.game.showMessage(`๋ ๋ฒจ์
! ๋ ๋ฒจ ${this.lev}`);
}
}
}//class hero
class Monster{
constructor(game,name,hp,att,xp){
this.game = game;
this.name = name;
this.maxHp = hp;
this.hp = hp;
this.xp = xp;
this.att = att;
}
attack(target){
target.hp -= this.att;
}
}//class monster
let game = null;
$startScreen.addEventListener('submit',(event)=>{
event.preventDefault();
const name = event.target['name-input'].value;
game = new Game(name);
});
</script>
<!--์ฝ๋์ถ์ฒ (๋์) Let's Get IT ์๋ฐ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋๋ฐ-->
๊ทธ๋ฐ๋ฐ Hero / Monster ํด๋์ค์ ๊ณตํต๋๋ ๋ถ๋ถ์ด ์๋ค.
์ด๋ฆ, ์ฒด๋ ฅ, ๊ณต๊ฒฉ๋ ฅ, ๊ฒฝํ์น ๊ฐ์ ๊ณตํต ์์ฑ์ด ์๊ณ , attack ๊ฐ์ ๊ณตํต ๋ฉ์๋๊ฐ ์๋ค.
์ด๋ฐ ์ค๋ณต์ ์ ๊ฑฐํ๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค.
โจ ํด๋์ค์ ์์
์ฌ๊ธฐ์ ํด๋์ค์ ์์์ด๋ผ๋ ๊ฐ๋ ์ด ๋ฑ์ฅํ๋ค.
Hero ์ Monster ํด๋์ค์์ ๊ณตํต๋๋ ๋ถ๋ถ๋ง ์ถ๋ ค ์๋ก์ด ํด๋์ค๋ก ๋ง๋ค๊ณ , Hero ์ Monster ํด๋์ค๋ ์ด ํด๋์ค๋ฅผ ๊ฐ์ ธ์ ์ฌ์ฉํ ์ ์๋ค.
์ด๋ฅผ ์์๋ฐ๋๋ค๊ณ ํ๋ค.
๊ณตํต ํด๋์ค์ธ Unit์ ๋ง๋ค์ด ๋ณด์.
//๊ณตํต ํด๋์ค ๋ง๋ค๊ธฐ (Hero & Monster)
class Unit {
constructor(game,name,hp,att,xp){
this.game = game;
this.name = name;
this.maxHp = hp;
this.hp = hp;
this.xp = xp;
this.att = att;
}
attack(target){
target.hp -= this.att;
}
}//class unit
//์ฝ๋์ถ์ฒ (๋์) Let's Get IT ์๋ฐ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋๋ฐ
๋ค๋ฅธ ํด๋์ค์์ ๊ณตํต ํด๋์ค์ธ Unit์ extends ์์ฝ์ด๋ก ์์๋ฐ์ ์ ์๋ค.
//๊ฐ์ฒด ๋ฆฌํฐ๋ด๋ก ์ฃผ์ธ๊ณต ๋ง๋ค๊ธฐ.
class Hero extends Unit{ //๊ณตํต ํด๋์ค ์์๋ฐ๊ธฐ.
constructor(game,name){
super(game,name,100,10,0) //๋ถ๋ชจ ํด๋์ค์ ์์ฑ์ ํธ์ถ.
this.lev = 1; //๊ทธ ์ธ ์์ฑ.
// this.game = game;
// this.name = name;
// this.maxHp = 100;
// this.hp = 100;
// this.xp = 0;
// this.att =10;
}
//๊ฐ์ฒด์ ๋ฉ์๋์์๋ function ์์ฝ์ด ์๋ต๊ฐ๋ฅ.ex) attack : function(monster)
attack(target){ //๋ชฌ์คํฐ ๊ณต๊ฒฉ.
super.attack(target); //๋ถ๋ชจ ํด๋์ค์ attack.
//๋ถ๋ชจ ํด๋์ค์ attack ์ธ์ ๋์.
//target.hp -= this.att;
}
heal(monster){ //์ฒด๋ ฅ ํ๋ณต.
this.hp += 20;
this.hp -= monster.att;
}
getXp(xp){
this.xp += xp;
if(this.xp >= this.lev * 15){ //๊ฒฝํ์น๋ฅผ ๋ค ์ฑ์ฐ๋ฉด
this.xp -= this.lev * 15;
this.lev += 1;
this.maxHp += 5;
this.att += 5;
this.hp = this.maxHp;
this.game.showMessage(`๋ ๋ฒจ์
! ๋ ๋ฒจ ${this.lev}`);
}
}
}//class hero
class Monster extends Unit{ //๊ณตํต ํด๋์ค ์์๋ฐ๊ธฐ.
constructor(game,name,hp,att,xp){
super(game,name,hp,att,xp); //๋ถ๋ชจ ํด๋์ค์ ์์ฑ์ ํธ์ถ.
// this.game = game;
// this.name = name;
// this.maxHp = hp;
// this.hp = hp;
// this.xp = xp;
// this.att = att;
}
//attack ๋ฉ์๋๋ฅผ ์์ฑํ์ง ์์ ๊ฒฝ์ฐ, ๋ถ๋ชจ ํด๋์ค์ attack ๋ฉ์๋๊ฐ ์กด์ฌํ๋ค๋ฉด
//๋ถ๋ชจ ํด๋์ค์ attack ๋ฉ์๋๋ฅผ ๋์ ํธ์ถํ๋ค.
//Hero์ attack ๋ฉ์๋ ๋ํ ์๋ตํด๋ ๋๋ค.
// attack(target){
// target.hp -= this.att;
// }
}//class monster
//์ฝ๋์ถ์ฒ (๋์) Let's Get IT ์๋ฐ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋๋ฐ
Hero ์ Monster ์ constructor ๋ฉ์๋๋ฅผ ๋ณด์.
๋ ๋ค super ์ด๋ผ๋ ํจ์๋ฅผ ํธ์ถํ๊ณ ์๋ค. super ํจ์๋ ๋ถ๋ชจ ํด๋์ค ( Unit ) ์ ์๋ฏธํ๋ค.
์ฆ, ๋ถ๋ชจ ํด๋์ค์ ์์ฑ์์ ์ธ์๋ฅผ ์ ๋ฌํ๋ ๊ฒ์ด๋ค.
๋ถ๋ชจ ํด๋์ค์์ game, name, maxHp, hp, xp, att ์์ฑ์ this ์ ์ ๋ ฅํ๋ค.
Hero์ lev์์ฑ์ ๋ถ๋ชจ ํด๋์ค์๋ ์กด์ฌํ์ง ์๋ ์์ฑ์ด๋ค. ๋ฐ๋ผ์ lev์์ฑ์ super ์๋์ ๋ฐ๋ก ์ ๋๋ค.
์ฆ, ๊ณตํต ์์ฑ์ super ๋ก ์ฒ๋ฆฌํ ๊ฒ์ด๋ค.
Hero์ attack ๋ฉ์๋๋ฅผ ๋ณด์.
super.attack ์ ํธ์ถํ๋๋ฐ, ์ด๊ฒ์ ๋ถ๋ชจ ํด๋์ค์ attack ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ๊ฒ๊ณผ ๊ฐ๋ค.
๋ฉ์๋ ๋ด๋ถ์์ super.attack ์ ํธ์ถํ ๋ค ๋ค๋ฅธ ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ๋ถ๋ชจ ํด๋์ค์ ๋ฉ์๋๋ฅผ ํธ์ถํ ํ ์์ ๋ง์ ๋ค๋ฅธ ์์ ์ ํ ์ ์๋ค.
๋ง์ฝ!
super.attack ์ ํธ์ถํ์ง ์๊ณ ๋ค๋ฅธ ์ฝ๋๋ฅผ ์์ฑํ๋ฉด ๋ถ๋ชจ ํด๋์ค์๋ ๋ค๋ฅธ ์์ ์ ํ ์ ์๋ค.
Monster ์ฒ๋ผ attack ๋ฉ์๋๋ฅผ ์์ฑํ์ง ์์ ๊ฒฝ์ฐ์ ๋ถ๋ชจ ํด๋์ค์ attack ๋ฉ์๋๊ฐ ์กด์ฌํ๋ค๋ฉด?
๋ถ๋ชจํด๋์ค์ attack ๋ฉ์๋๋ฅผ ๋์ ํธ์ถํ๋ค.
์ด๋ ๊ธฐ ๋๋ฌธ์ ์ฌ์ค,
Hero์ attack ๋ฉ์๋ ๋ํ ๋ถ๋ชจ ํด๋์ค์ attack ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ๊ฒ ์ธ์๋ ๋ค๋ฅธ ์์ ์ ํ์ง ์์ผ๋ฏ๋ก ์๋ตํด๋ ๋๋ค.
class A extends B {
method() {
super.method(); //๋ฉ์๋ ๋ด์์ ๋ถ๋ชจ์ ๋ฉ์๋๋ง ํธ์ถํ๋ฏ๋ก ์๋ต ๊ฐ๋ฅ
}
}
//์ฝ๋์ถ์ฒ (๋์) Let's Get IT ์๋ฐ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋๋ฐ
๐ ๋๋จธ์ง ๊ธฐ๋ฅ ๊ตฌํํ๊ธฐ
์ผ๋ฐ ๋ฉ๋ด : ํด์, ์ข ๋ฃ ๊ธฐ๋ฅ / ์ ํฌ ๋ฉ๋ด : ํ๋ณต, ๋๋ง ๊ธฐ๋ฅ์ ๊ตฌํํ๋ฉด ๋๋ค.
ํด์ ๊ธฐ๋ฅ | ์ฃผ์ธ๊ณต์ ์ฒด๋ ฅ์ ์ต๋๋ก ํ๋ณตํ๋ ๊ธฐ๋ฅ. |
์ข ๋ฃ ๊ธฐ๋ฅ | ๊ฒ์์ ์ข ๋ฃํ๊ณ ์ฃผ์ธ๊ณต์ ์๋ก ์์ฑํ๋ ํ๋ฉด์ผ๋ก ๋๋๋ฆฐ๋ค. |
ํ๋ณต ๊ธฐ๋ฅ | ์ ํฌ ์ค์ ์ฒด๋ ฅ์ 20 ํ๋ณตํ๋ ๊ธฐ๋ฅ. ๋ค๋ง, ํ๋ณต ํ์ ๋ชฌ์คํฐ์๊ฒ ํ๋ฒ ๊ณต๊ฒฉ์ ๋นํ๋ค. / ์ฒด๋ ฅ์ ์ต๋ ์ฒด๋ ฅ(maxHp) ๊ฐ์ ๋์ ์ ์๋ค. |
๋๋ง ๊ธฐ๋ฅ | ์ผ๋ฐ ๋ฉ๋ด๋ก ๋๋์๊ฐ๋ค. |
๐ค ํด์๊ณผ ์ข ๋ฃ ๊ธฐ๋ฅ ๋ง๋ค๊ธฐ
class Game {
...
onGameMenuInput = (event)=>{
event.preventDefault();
const input = event.target['menu-input'].value;
if(input === '1'){ //๋ชจํ
this.changeScreen('battle');
const randomIndex = Math.floor(Math.random()*this.monsterList.length);
const randomMonster = this.monsterList[randomIndex];
this.monster = new Monster(
this,
randomMonster.name,
randomMonster.hp,
randomMonster.att,
randomMonster.xp,
);
this.updateHeroStat();
this.showMessage(`๋ชฌ์คํฐ์ ๋ง์ฃผ์ณค๋ค. ${this.monster.name}์ธ ๊ฒ ๊ฐ๋ค.`);
} else if (input === '2'){ //ํด์
this.hero.hp = this.hero.maxHp;
this.updateHeroStat();
this.showMessage('์ถฉ๋ถํ ํด์์ ์ทจํ๋ค.');
} else if (input === '3'){ //์ข
๋ฃ
this.showMessage('');
this.quit();
}
}
...
}
//์ฝ๋์ถ์ฒ (๋์) Let's Get IT ์๋ฐ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋๋ฐ
๐ค ํ๋ณต๊ณผ ๋๋ง๊ธฐ๋ฅ ๋ง๋ค๊ธฐ
class Game {
...
onBattleMenuInput = (event)=>{
event.preventDefault();
const input = event.target['battle-input'].value;
if(input === '1'){ //๊ณต๊ฒฉ
const {hero,monster}=this;
hero.attack(monster);
monster.attack(hero);
if(hero.hp <= 0){
this.showMessage(`${hero.lev} ๋ ๋ฒจ์์ ์ ์ฌ. ์ ์ฃผ์ธ๊ณต์ ์์ฑํ์ธ์.`);
this.quit();
} else if(monster.hp <= 0){
this.showMessage(`๋ชฌ์คํฐ๋ฅผ ์ก์ ${monster.xp} ๊ฒฝํ์น๋ฅผ ์ป์๋ค.`);
hero.getXp(monster.xp);
this.monster = null;
this.changeScreen('game');
}else{
this.showMessage(`${hero.att}์ ๋ฐ๋ฏธ์ง๋ฅผ ์ฃผ๊ณ ,${monster.att}์ ๋ฐ๋ฏธ์ง๋ฅผ ๋ฐ์๋ค.`);
}
this.updateHeroStat();
this.updateMonsterStat();
} else if (input === '2'){ //ํ๋ณต
const{hero,monster} = this;
hero.hp = Math.min(hero.maxHp,hero.hp + 20);
monster.attack(hero);
this.showMessage('์ฒด๋ ฅ์ ์กฐ๊ธ ํ๋ณตํ๋ค.');
this.updateHeroStat();
} else if (input === '3'){ //๋๋ง
this.changeScreen('game');
this.showMessage('๋ถ๋ฆฌ๋์ผ ๋๋ง์ณค๋ค.');
this.monster = null;
this.updateMonsterStat();
}
}
...
}
//์ฝ๋์ถ์ฒ (๋์) Let's Get IT ์๋ฐ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋๋ฐ
๐ ์ ์ฒด ์ฝ๋
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ํ
์คํธ RPG</title>
</head>
<body>
<form id="start-screen">
<input id="name-input" placeholder="์ฃผ์ธ๊ณต ์ด๋ฆ์ ์
๋ ฅํ์ธ์">
<button id="start">์์</button>
</form>
<div id="screen">
<div id="hero-stat">
<span id="hero-name"></span>
<span id="hero-level"></span>
<span id="hero-hp"></span>
<span id="hero-xp"></span>
<span id="hero-att"></span>
</div>
<form id="game-menu" style="display: none;">
<div id="menu-1">1.๋ชจํ</div>
<div id="menu-2">2.ํด์</div>
<div id="menu-3">3.์ข
๋ฃ</div>
<input id="menu-input">
<button id="menu-button">์
๋ ฅ</button>
</form>
<form id="battle-menu" style="display: none;">
<div id="battle-1">1.๊ณต๊ฒฉ</div>
<div id="battle-2">2.ํ๋ณต</div>
<div id="battle-3">3.๋๋ง</div>
<input id="battle-input">
<button id="battle-button">์
๋ ฅ</button>
</form>
<div id="message"></div>
<div id="monster-stat">
<span id="monster-name"></span>
<span id="monster-hp"></span>
<span id="monster-att"></span>
</div>
</div>
<script>
const $startScreen = document.querySelector('#start-screen');
const $gameMenu = document.querySelector('#game-menu');
const $battleMenu = document.querySelector('#battle-menu');
const $heroName = document.querySelector('#hero-name');
const $heroLevel = document.querySelector('#hero-level');
const $heroHp = document.querySelector('#hero-hp');
const $heroXp = document.querySelector('#hero-xp');
const $heroAtt = document.querySelector('#hero-att');
const $monsterName = document.querySelector('#monster-name');
const $monsterHp = document.querySelector('#monster-hp');
const $monsterAtt = document.querySelector('#monster-att');
const $message = document.querySelector('#message');
//๊ฒ์ ์์ฒด ํด๋์ค ๋ง๋ค๊ธฐ.
//Game,Hero,Monster 3๊ฐ์ ํด๋์ค ๋ง๋ค๊ณ ์ฃผ์ธ๊ณต ์ด๋ฆ ์
๋ ฅ ๋ฐ๊ธฐ.
class Game {
constructor (name){
this.monster = null;
this.hero = null;
this.monsterList = [
{name: '์ฌ๋ผ์',hp:25,att:10,xp:10},
{name: '์ค์ผ๋ ํค',hp:50,att:15,xp:20},
{name: '๋ง์',hp:150,att:35,xp:50},
];
this.start(name);
}
start(name){
$gameMenu.addEventListener('submit',this.onGameMenuInput);
$battleMenu.addEventListener('submit',this.onBattleMenuInput);
this.changeScreen('game');
this.hero = new Hero(this,name);
this.updateHeroStat();
}
changeScreen(screen){
if(screen === 'start'){
$startScreen.start.display = 'block';
$gameMenu.style.display = 'none';
$battleMenu.style.display = 'none';
} else if (screen === 'game'){
$startScreen.style.display = 'none';
$gameMenu.style.display = 'block';
$battleMenu.style.display = 'none';
} else if (screen === 'battle'){
$startScreen.style.display = 'none';
$gameMenu.style.display = 'none';
$battleMenu.style.display = 'block';
}
}
onGameMenuInput = (event)=>{
event.preventDefault();
const input = event.target['menu-input'].value;
if(input === '1'){ //๋ชจํ
this.changeScreen('battle');
const randomIndex = Math.floor(Math.random()*this.monsterList.length);
const randomMonster = this.monsterList[randomIndex];
this.monster = new Monster(
this,
randomMonster.name,
randomMonster.hp,
randomMonster.att,
randomMonster.xp,
);
this.updateHeroStat();
this.showMessage(`๋ชฌ์คํฐ์ ๋ง์ฃผ์ณค๋ค. ${this.monster.name}์ธ ๊ฒ ๊ฐ๋ค.`);
} else if (input === '2'){ //ํด์
this.hero.hp = this.hero.maxHp;
this.updateHeroStat();
this.showMessage('์ถฉ๋ถํ ํด์์ ์ทจํ๋ค.');
} else if (input === '3'){ //์ข
๋ฃ
this.showMessage('');
this.quit();
}
}
onBattleMenuInput = (event)=>{
event.preventDefault();
const input = event.target['battle-input'].value;
if(input === '1'){ //๊ณต๊ฒฉ
const {hero,monster}=this;
hero.attack(monster);
monster.attack(hero);
if(hero.hp <= 0){
this.showMessage(`${hero.lev} ๋ ๋ฒจ์์ ์ ์ฌ. ์ ์ฃผ์ธ๊ณต์ ์์ฑํ์ธ์.`);
this.quit();
} else if(monster.hp <= 0){
this.showMessage(`๋ชฌ์คํฐ๋ฅผ ์ก์ ${monster.xp} ๊ฒฝํ์น๋ฅผ ์ป์๋ค.`);
hero.getXp(monster.xp);
this.monster = null;
this.changeScreen('game');
}else{
this.showMessage(`${hero.att}์ ๋ฐ๋ฏธ์ง๋ฅผ ์ฃผ๊ณ ,${monster.att}์ ๋ฐ๋ฏธ์ง๋ฅผ ๋ฐ์๋ค.`);
}
this.updateHeroStat();
this.updateMonsterStat();
} else if (input === '2'){ //ํ๋ณต
const{hero,monster} = this;
hero.hp = Math.min(hero.maxHp,hero.hp + 20);
monster.attack(hero);
this.showMessage('์ฒด๋ ฅ์ ์กฐ๊ธ ํ๋ณตํ๋ค.');
this.updateHeroStat();
} else if (input === '3'){ //๋๋ง
this.changeScreen('game');
this.showMessage('๋ถ๋ฆฌ๋์ผ ๋๋ง์ณค๋ค.');
this.monster = null;
this.updateMonsterStat();
}
}
updateHeroStat(){
const {hero} = this;
if (hero === null){
$heroName.textContent='';
$heroLevel.textContent='';
$heroHp.textContent='';
$heroXp.textContent='';
$heroAtt.textContent='';
return;
}
$heroName.textContent=hero.name;
$heroLevel.textContent=`${hero.lev}lev`;
$heroHp.textContent=`HP: ${hero.hp}/${hero.maxHp}`;
$heroXp.textContent=`XP: ${hero.xp}/${15*hero.lev}`;
$heroAtt.textContent=`ATT: ${hero.att}`;
}
updateMonsterStat(){
const {monster} = this;
if (monster === null){
$monsterName.textContent='';
$monsterHp.textContent='';
$monsterAtt.textContent='';
return;
}
$monsterName.textContent=monster.name;
$monsterHp.textContent=`HP: ${monster.hp}/${monster.maxHp}`;
$monsterAtt.textContent=`ATT: ${monster.att}`;
}
showMessage(text){
$message.textContent=text;
}
quit(){
this.hero = null;
this.monster = null;
this.updateHeroStat();
this.updateMonsterStat();
$gameMenu.removeEventListener('submit',this.onGameMenuInput);
$battleMenu.removeEventListener('submit',this.onBattleMenuInput);
this.changeScreen('start');
game=null;
}
};//class game
//๊ณตํต ํด๋์ค ๋ง๋ค๊ธฐ (Hero & Monster)
class Unit {
constructor(game,name,hp,att,xp){
this.game = game;
this.name = name;
this.maxHp = hp;
this.hp = hp;
this.xp = xp;
this.att = att;
}
attack(target){
target.hp -= this.att;
}
}//class unit
//๊ฐ์ฒด ๋ฆฌํฐ๋ด๋ก ์ฃผ์ธ๊ณต ๋ง๋ค๊ธฐ.
class Hero extends Unit{ //๊ณตํต ํด๋์ค ์์๋ฐ๊ธฐ.
constructor(game,name){
super(game,name,100,10,0) //๋ถ๋ชจ ํด๋์ค์ ์์ฑ์ ํธ์ถ.
this.lev = 1; //๊ทธ ์ธ ์์ฑ.
// this.game = game;
// this.name = name;
// this.maxHp = 100;
// this.hp = 100;
// this.xp = 0;
// this.att =10;
}
//๊ฐ์ฒด์ ๋ฉ์๋์์๋ function ์์ฝ์ด ์๋ต๊ฐ๋ฅ.ex) attack : function(monster)
attack(target){ //๋ชฌ์คํฐ ๊ณต๊ฒฉ.
super.attack(target); //๋ถ๋ชจ ํด๋์ค์ attack.
//๋ถ๋ชจ ํด๋์ค์ attack ์ธ์ ๋์.
//target.hp -= this.att;
}
heal(monster){ //์ฒด๋ ฅ ํ๋ณต.
this.hp += 20;
this.hp -= monster.att;
}
getXp(xp){
this.xp += xp;
if(this.xp >= this.lev * 15){ //๊ฒฝํ์น๋ฅผ ๋ค ์ฑ์ฐ๋ฉด
this.xp -= this.lev * 15;
this.lev += 1;
this.maxHp += 5;
this.att += 5;
this.hp = this.maxHp;
this.game.showMessage(`๋ ๋ฒจ์
! ๋ ๋ฒจ ${this.lev}`);
}
}
}//class hero
class Monster extends Unit{ //๊ณตํต ํด๋์ค ์์๋ฐ๊ธฐ.
constructor(game,name,hp,att,xp){
super(game,name,hp,att,xp); //๋ถ๋ชจ ํด๋์ค์ ์์ฑ์ ํธ์ถ.
// this.game = game;
// this.name = name;
// this.maxHp = hp;
// this.hp = hp;
// this.xp = xp;
// this.att = att;
}
//attack ๋ฉ์๋๋ฅผ ์์ฑํ์ง ์์ ๊ฒฝ์ฐ, ๋ถ๋ชจ ํด๋์ค์ attack ๋ฉ์๋๊ฐ ์กด์ฌํ๋ค๋ฉด
//๋ถ๋ชจ ํด๋์ค์ attack ๋ฉ์๋๋ฅผ ๋์ ํธ์ถํ๋ค.
//Hero์ attack ๋ฉ์๋ ๋ํ ์๋ตํด๋ ๋๋ค.
// attack(target){
// target.hp -= this.att;
// }
}//class monster
let game = null;
$startScreen.addEventListener('submit',(event)=>{
event.preventDefault();
const name = event.target['name-input'].value;
game = new Game(name);
});
</script>
</body>
</html>
<!--์ฝ๋์ถ์ฒ (๋์) Let's Get IT ์๋ฐ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋๋ฐ-->
-(๋์) Let's Get IT ์๋ฐ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋๋ฐ
Let's Get IT ์๋ฐ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋๋ฐ
๋๋ถ(TheBook): (์ฃผ)๋์์ถํ ๊ธธ๋ฒ์์ ์ ๊ณตํ๋ IT ๋์ ์ด๋ ์๋น์ค์ ๋๋ค.
thebook.io
-(๊ฐ์ข) ๋ ์ธ ๊ธฐ๋ฆฟ ์๋ฐ์คํฌ๋ฆฝํธ
[๋ฌด๋ฃ] [๋ฆฌ๋ด์ผ] ๋ ์ธ ๊ธฐ๋ฆฟ ์๋ฐ์คํฌ๋ฆฝํธ - ์ธํ๋ฐ | ๊ฐ์
๋ณธ ๊ฐ์์์๋ ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ํ์ฉํด ํ๋ก๊ทธ๋๋ฐ ์ฌ๊ณ ๋ ฅ์ ๊ธฐ๋ฅด๋ ์ฐ์ต์ ํฉ๋๋ค. ์น ๊ฒ์์ธ ๊ตฌ๊ตฌ๋จ์ ์์์ผ๋ก ๋๋ง์๊ธฐ, ์ซ์ ์ผ๊ตฌ, ๋ฐ์ ์๋ ํ ์คํธ, ํฑํํ , ๋ก๋ ์ถ์ฒจ๊ธฐ, ๊ฐ์๋ฐ์๋ณด, ์นด
www.inflearn.com