●/섹션 5. TextRPG 2
1. TextRPG2 플레이어 생성
한국 /프랑스 웹툰 리뷰
2021. 7. 13. 00:31
728x90
일단 스크립트를 만든다
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Csharp
{
public enum PlayerType
{
None = 0,
Knight = 1,
Archer = 2,
Mage = 3
}
class Player
{
protected PlayerType type;
protected int hp;
protected int attack;
public void SetInfo(int _hp, int _attack)
{
this.hp = _hp;
this.attack = _attack;
}
public int GetHp() { return hp; }
public int GetAttack() { return attack; }
}
class Knight : Player
{
//생성자
Knight()
{
type = PlayerType.Knight;
}
}
class Archer : Player
{
//생성자
Archer()
{
type = PlayerType.Archer;
}
}
class Mage : Player
{
//생성자
Mage()
{
type = PlayerType.Mage;
}
}
}
using System;
namespace Csharp
{
class Program
{
static void Main(string[] args)
{
Player player = new Player();
}
}
}
이렇게 할 경우, player를 생성하긴 하는데,
이 player가 knight인지, archer인지, mage인지 모른다.
그냥 이상한 클래스가 생성된 것과 마찬가지이다.
그러므로 player를 생성할 때는 type을 고정시키도록 해야한다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Csharp
{
public enum PlayerType
{
None = 0,
Knight = 1,
Archer = 2,
Mage = 3
}
class Player
{
protected PlayerType type;
protected int hp;
protected int attack;
//player생성시, knight를 만드는지, archer를 만드는지, mage를 만드는지 알 수 없다.
//그러므로 player를 생성할 때는 항상 type을 고정시켜, 무엇을 만드는지 명확하게 한다.
public Player(PlayerType _type)
{
this.type = _type;
}
public void SetInfo(int _hp, int _attack)
{
this.hp = hp;
this.attack = attack;
}
public int GetHp() { return hp; }
public int GetAttack() { return attack; }
}
class Knight : Player
{
//생성자
Knight() //빨간줄
{
type = PlayerType.Knight;
}
}
class Archer : Player
{
//생성자
Archer() //빨간줄
{
type = PlayerType.Archer;
}
}
class Mage : Player
{
//생성자
Mage() //빨간줄
{
type = PlayerType.Mage;
}
}
}
using System;
namespace Csharp
{
class Program
{
static void Main(string[] args)
{
Player player = new Player(); //빨간줄
}
}
}
빨간줄이 뜨는 이유는
이제는 더 이상, 디폴트 버전 ( 즉, 인자가 없는 버전)을 사용할 수 없기 때문이다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Csharp
{
public enum PlayerType
{
None = 0,
Knight = 1,
Archer = 2,
Mage = 3
}
class Player
{
protected PlayerType type = PlayerType.None;
protected int hp = 0;
protected int attack = 0;
//player생성시, knight를 만드는지, archer를 만드는지, mage를 만드는지 알 수 없다.
//그러므로 player를 생성할 때는 항상 type을 고정시켜, 무엇을 만드는지 명확하게 한다.
protected Player(PlayerType _type) //protected로 설정하여 자식 클래스인 Knight, Archer, Mage에서만 접근하도록 한다.
{
this.type = _type;
}
public void SetInfo(int _hp, int _attack)
{
this.hp = _hp;
this.attack = _attack;
}
public PlayerType GetPlayerType() { return type; }
public int GetHp() { return hp; }
public int GetAttack() { return attack; }
public bool IsDead() { return hp <= 0; }
public void OnDamaged(int _damage)
{
hp -= _damage;
if (hp < 0)
{
hp = 0;
}
}
}
class Knight : Player
{
//public으로 설정. puclic으로 설정하지 않으면, 외부에서 Knight를 생성할 수 없다
public Knight() : base(PlayerType.Knight) //부모님의 생성자를 사용하도록 하자
{
SetInfo(100, 10);
}
}
class Archer : Player
{
//public으로 설정. puclic으로 설정하지 않으면, 외부에서 Archer를 생성할 수 없다
public Archer() : base(PlayerType.Archer) //부모님의 생성자를 사용하도록 하자
{
SetInfo(75, 12);
}
}
class Mage : Player
{
//public으로 설정. puclic으로 설정하지 않으면, 외부에서 Mage를 생성할 수 없다
public Mage() : base(PlayerType.Mage) //부모님의 생성자를 사용하도록 하자
{
SetInfo(50, 15);
}
}
}
using System;
namespace Csharp
{
class Program
{
static void Main(string[] args)
{
Player player = new Knight();
}
}
}
728x90