[C#] 객체지향개념을 적용하여 간단한 TextRPG 게임 만들기

Yerin·2022년 1월 19일
0

해당 글은 <C#과 유니티로 만드는 MMORPG 게임 개발 시리즈> 강의를 정리한 글입니다.

C#으로 간단한 TextRPG 만들기에서 만든 게임을 객체지향 개념을 적용하여 수정하였다.

Creature.cs

살아있는 객체의 공통 부분을 정의한다.

 public enum CreatureType
    { 
        None,
        Player = 1,
        Monster = 2
    }
  class Creature
    {
        CreatureType type;
        protected Creature(CreatureType type) {
            this.type = type;
        }
        protected int hp = 0;
        protected int attack = 0;

        public void SetInfo(int hp, int attack)
        {
            this.hp = hp;
            this.attack = attack;
        }

        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;
        }
    }

Player.cs

플레이어의 속성을 정의한다. (기사, 궁수, 법사)

  public enum PlayerType
    { 
        None = 0,
        Knight = 1,
        Archer = 2,
        Mage = 3
    }

    class Player :Creature
    {
        protected PlayerType type = PlayerType.None;
       

        protected Player(PlayerType type) : base(CreatureType.Player)
        {
            this.type = type;
        }

        public PlayerType GetPlayerType() { return type; }
    }

    class Knight : Player
    {
        public Knight(): base(PlayerType.Knight) {
            SetInfo(100, 10);
        }
    }
    class Archer : Player
    {
        public Archer() : base(PlayerType.Archer)
        {
            SetInfo(75, 12);
        }
    }
  
    class Mage : Player
    {
        public Mage() : base(PlayerType.Mage)
        {
            SetInfo(50, 15);
        }
    }

Monster.cs

몬스터의 속성을 정의한다. (슬라임, 오크, 스켈레톤)

   public enum MonsterType
    { 
        None = 0,
        Slime = 1,
        Orc = 2,
        Skeleton = 3
    }
    class Monster : Creature
    {
        protected MonsterType type;
        protected Monster(MonsterType type) : base(CreatureType.Monster)
        {
            this.type = type;
        }

        public MonsterType GetMonsterType() { return type; }

       
    }
    class Slime : Monster
    {
        public Slime() : base(MonsterType.Slime)
        {
            SetInfo(10, 10);
        }
    }
    class Orc : Monster
    {
        public Orc() : base(MonsterType.Orc)
        {
            SetInfo(20, 15);
        }
    }
    class Skeleton : Monster
    {
        public Skeleton() : base(MonsterType.Skeleton)
        {
            SetInfo(15, 25);
        }
    }

Game.cs

게임 모드에 따라 달라지는 프로세스 정의

 public enum GameMode
    { 
        None,
        Lobby,
        Town,
        Field
    }
    class Game
    {
        private GameMode mode = GameMode.Lobby;
        private Player player = null;
        private Monster monster = null;
        Random rand = new Random();

        public void Process()
        {
            switch (mode)
            {
                case GameMode.Lobby:
                    ProcessLobby();
                    break;
                case GameMode.Town:
                    ProcessTown();
                    break;
                case GameMode.Field:
                    ProcessFiled();
                    break;
            }
        }

        public void ProcessLobby()
        {
            Console.WriteLine("직업을 선택하세요");
            Console.WriteLine("[1] 기사");
            Console.WriteLine("[2] 궁수");
            Console.WriteLine("[3] 법사");
            string input = Console.ReadLine();
            switch (input)
            {
                case "1":
                    player = new Knight();
                    mode = GameMode.Town;
                    break;
                case "2":
                    player = new Archer();
                    mode = GameMode.Town;
                    break;
                case "3":
                    player = new Mage();
                    mode = GameMode.Town;
                    break;
            }
        }

        public void ProcessTown()
        {
            Console.WriteLine("마을에 입장했습니다!");
            Console.WriteLine("[1] 필드로 가기");
            Console.WriteLine("[2] 로비로 돌아가기");
            string input = Console.ReadLine();
            switch (input)
            {
                case "1":
                    mode = GameMode.Field;
                    break;
                case "2":
                    mode = GameMode.Lobby;
                    break;
            }
        }

        public void ProcessFiled()
        {
            Console.WriteLine("필드에 입장했습니다!");
            Console.WriteLine("[1] 싸우기");
            Console.WriteLine("[2] 일정 확률로 마을 돌아가기");
            string input = Console.ReadLine();
            CreateRandomMonster();
            switch (input)
            {
                case "1":
                    ProcessFight();
                    break;
                case "2":
                    TryEscape();
                    break;
            }
        }

        private void TryEscape(){
            int randValue = rand.Next(0,101);
            if (randValue < 33)
            {
                mode = GameMode.Town;
            }
            else {
                ProcessFight();
            }
}
        private void ProcessFight() {

            while (true) {
                int damage = player.GetAttack();
                monster.OnDamaged(damage);
                if (monster.isDead())
                {
                    Console.WriteLine("승리했습니다.");
                    Console.WriteLine($"남은 체력 {player.GetHp()}");
                    break;
                }
                damage = monster.GetAttack();
                player.OnDamaged(damage);
                if (player.isDead())
                {
                    Console.WriteLine("패배했습니다.");
                    mode = GameMode.Lobby;
                    break;
                }
            }
        }
        private void CreateRandomMonster() {
            int randValue = rand.Next(0, 3);
            switch (randValue)
            {
                case 0:
                    monster = new Slime();
                    Console.WriteLine("슬라임이 생성되었습니다.");
                    break;
                case 1:
                    monster = new Orc();
                    Console.WriteLine("오크가 생성되었습니다.");
                    break;
                case 2:
                    monster = new Skeleton();
                    Console.WriteLine("스켈레톤이 생성되었습니다.");
                    break;
            }
        }

    }

Program.cs

 class Program
    {
       static void Main(string[] args)
        {
            Game game = new Game();
            while (true)
            {
                game.Process();
            }
        }
    }
profile
재밌는 코딩 공부

1개의 댓글

comment-user-thumbnail
2024년 9월 15일

Sam had always enjoyed video games as a hobby, but one evening, he decided to explore online video poker after a recommendation from a friend. Starting with a modest bet on a simple Jacks or Better machine, Sam was captivated by the https://crococasino1.com game’s blend of strategy and chance. He spent hours learning the ins and outs, analyzing which cards to keep or discard. His early attempts were met with small wins and a few losses, but he was determined to master the game.

답글 달기