C#

게임시작

송현호 2023. 7. 28. 13:11
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.IO;
namespace Starcraft
{
    public class App
    {
        private Game game;
        //생성자
        public App()
        {
            //-------------준비---------------//
            DataManager.instance.LoadItemData();
            DataManager.instance.LoadMonsterData();

            //----------서비스 시작-----------//
            this.game = new Game();
            this.game.Start();

        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Starcraft
{
    internal class Game
    {
        private Hero hero;
        private Monster monster;
        private Inventory inventory;
        public Game()
        {
            
        }

        public void Start()
        {
            Console.WriteLine("게임이 시작되었습니다");
            //영웅이 생성됨
            this.hero = this.CreateHero();
            //몬스터가 생성됨
            this.monster = this.CreateMonster(1000);
            this.monster.onDie = () =>
            {
                int ItemId = this.monster.GetItemId();
                Item dropItem = this.CreateItem(ItemId);
                Console.WriteLine("아이템({0}){1}이 드롭 되었습니다.", dropItem.GetId(), dropItem.GetName());
                //아이템 획득 
                this.hero.SetItem(dropItem);
            };
            //배낭 생성됨
            Inventory inventory = new Inventory(5);
            this.hero.Setbag(inventory);
            Item item = this.CreateItem(100);
            this.hero.SetItem(item);
            this.hero.Equip(item.GetId());
            this.hero.Attack(monster);
            this.hero.Attack(monster);
            this.hero.Attack(monster);
            


        }

        public Hero CreateHero()
        {
            return new Hero();
        }

        public Monster CreateMonster(int id)
        {
            MonsterData data = DataManager.instance.GetMonsterData(id);
            return new Monster(data);
        }


        public Item CreateItem(int id)
        {
            return new Item(DataManager.instance.GetItemData(id));
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
namespace Starcraft
{

    internal class DataManager
    {
        public static DataManager instance = new DataManager();
        Dictionary<int, ItemData> dicItem = new Dictionary<int, ItemData>();
        Dictionary<int, MonsterData> dicMonster = new Dictionary<int, MonsterData>();
        //생성자
        private DataManager() { }

        public void LoadItemData()
        {
            //파일읽기
            string json = File.ReadAllText("./item.Data.json");
            Console.WriteLine(json);
            //역직렬화
            ItemData[] itemDatas = JsonConvert.DeserializeObject<ItemData[]>(json);
            //사전에 저장
            foreach (ItemData data in itemDatas)
            {
                dicItem.Add(data.id, data);
            }
            Console.WriteLine("{0}개의 정보 로드완료",dicItem.Count);
        }

        public void LoadMonsterData()
        {
            //파일읽기
            string json2 = File.ReadAllText("./Monster.Data.json");
            Console.WriteLine(json2);
            //역직렬화
            MonsterData[] monsterDatas = JsonConvert.DeserializeObject<MonsterData[]>(json2);
            //사전에 저장
            foreach (MonsterData data in monsterDatas)
            {
                dicMonster.Add(data.id, data);
            }
            Console.WriteLine("{0}개의 정보 로드완료", dicMonster.Count);
        }

        public MonsterData GetMonsterData(int id)
        {
            return dicMonster[id];
        }

        public ItemData GetItemData(int id)
        {
            return dicItem[id];
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Starcraft
{
    internal class Monster
    {
        public MonsterData data;
        private int maxHp;
        private int hp;
        public Action onDie;
        //생성자
        public Monster(MonsterData data)
        {
            this.data = data;
            this.maxHp = data.maxHp;
            this.hp = maxHp;
            Console.WriteLine("{0}가 생성되었습니다. ({1}/{2})", this.data.name, this.maxHp,this.hp);

        }

        public void HitDamage(int damage)
        {
            this.hp -= damage;
            if (this.hp <= 0) 
            {
                MonsterDie();
            }
            else
            {
                Console.WriteLine("{0}가 {1}의 데미지를 받았습니다 ({2}/{3})", data.name, damage, this.hp, this.maxHp);
            }
        }

        public void MonsterDie()
        {
            this.hp = 0;
            Console.WriteLine("몬스터가 사망했습니다 (0/{0})",this.maxHp);
            this.onDie();
        }

        public int GetItemId()
        {
            return data.item_id;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Starcraft
{
    internal class MonsterData
    {
        public int id;
        public string name;
        public int item_id;
        public int maxHp;
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Starcraft
{
    internal class Hero
    {
        int hp;
        int damage;
        Inventory inventory;
        Item leftHandItem;
        //생성자
        public Hero()
        {
            this.hp = 40;
            this.damage = 20;
            Console.WriteLine("영웅이 생성되었습니다.");
        }

        public void Attack(Monster target)
        {
            Console.WriteLine("영웅이 몬스터를 공격했습니다.");
            target.HitDamage(this.damage);
        }

        public void SetItem(Item item)
        {
            this.inventory.AddItem(item);
        }

        public void Setbag(Inventory inventory)
        {
            this.inventory = inventory;
            Console.WriteLine("가방을 받았습니다");
        }

        public void Equip(int id)
        {
            if (this.inventory.Exist(id))
            {
                if (leftHandItem == null)
                {
                    leftHandItem = this.inventory.GetItem(id);
                    this.damage += leftHandItem.GetDamage();
                    Console.WriteLine("왼손에 아이템을 장착했습니다");
                }
                else
                {
                    Console.WriteLine("이미 아이템을 장착하고 있습니다.");
                }
                
            }
            else
            {
                Console.WriteLine("아이템을 찾을 수 없습니다.");
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Starcraft
{
    internal class Item
    {
        ItemData data;
        public Item(ItemData data)
        {
            this.data = data;
        }

        public int GetId()
        {
            return this.data.id;
        }

        public string GetName()
        {
            return this.data.name;
        }

        public int GetDamage()
        {
            return this.data.damage;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Starcraft
{
    internal class ItemData
    {
        public int id;
        public string name;
        public int damage;
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Starcraft
{
    internal class Inventory
    {
        int capacity;
        List<Item> list = new List<Item>();
        //생성자
        public Inventory(int capacity)
        {
            this.capacity = capacity;
            Console.WriteLine("{0}칸의 배낭이 생성되었습니다.",capacity);
        }

        public void AddItem(Item item)
        {
            list.Add(item);
        }

        public bool Exist(int id)
        {
            Item item = this.list.Find(x => x.GetId() == id);
            if (item == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public Item GetItem(int id)
        {
            return this.list.Find(x => x.GetId() == id);
        }
    }
}