using System;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using LearnDotnet;
namespace LearnDotnet
{
public class App
{
private Game game;
//생성자
public App()
{
//------------------준비---------------
DataManager.instance.LoadItemDatas();
DataManager.instance.LoadMonsterDatas();
//--------------------------------------
//-------------서비스 시작--------------
this.game = new Game();
this.game.Start();
}
}
}
using System;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using LearnDotnet;
namespace LearnDotnet
{
public class DataManager
{
public static readonly DataManager instance = new DataManager();
private Dictionary<int, ItemData> dicItemDatas = new Dictionary<int, ItemData>();
private Dictionary<int, MonsterData> dicMonsterDatas ;
private DataManager()
{
}
public void LoadItemDatas()
{
//파일 읽기
var json = File.ReadAllText("./item_data.json");
//Console.WriteLine(json);
//역직렬화 하면 ItemData객체들을 요소로 하는 배열 객체가 나온다
ItemData[] itemDatas = JsonConvert.DeserializeObject<ItemData[]>(json);
foreach (var data in itemDatas)
dicItemDatas.Add(data.id, data);
Console.WriteLine("아이템 데이터 로드 완료: {0}", dicItemDatas.Count);
}
public void LoadMonsterDatas()
{
//파일 읽기
var json = File.ReadAllText("./monster_data.json");
Console.WriteLine(json);
//역직렬화 하면 ItemData객체들을 요소로 하는 배열 객체가 나온다
MonsterData[] monsterDatas = JsonConvert.DeserializeObject<MonsterData[]>(json);
dicMonsterDatas = monsterDatas.ToDictionary(x => x.id); //새로운 사전객체가 만들어진다
Console.WriteLine("몬스터 데이터 로드 완료: {0}", dicMonsterDatas.Count);
}
public MonsterData GetMonsterData(int id)
{
return this.dicMonsterDatas[id];
}
public ItemData GetItemData(int id)
{
return this.dicItemDatas[id];
}
}
}
using Newtonsoft.Json.Bson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
public class Game
{
private Hero hero;
private Monster monster;
public Game()
{
}
public void Start()
{
Console.WriteLine("게임이 시작되었습니다");
//히어로 생성
this.hero = this.CreateHero("홍길동", 3);
//몬스터 생성( id가 1000번인 몬스터 생성)
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.Name);
this.hero.SetItem(dropItem);
int itemCount = this.hero.GetItemCount();
Console.WriteLine(itemCount);
};
Inventory bag = new Inventory(5);//(가방은 인벤토리꺼)크기가 5인 가방 만들기
this.hero.SetBag(bag); //가방을 지급
//무기 지급(장검)
Item item = this.CreateItem(100);//id 가 100번인 item을 받아와서 Item 타입인 item변수에 저장
this.hero.SetItem(item);//영웅에게 아이템을 지급, 가방에 넣기--> hero가 setItem 갖고 있어야함
this.hero.Equip(100);//100번 키를 가진 아이템을 착용
//몬스터 공격
this.hero.Attack(monster);
}
public Hero CreateHero(string name, int damage)
{
return new Hero(name, damage); //실제로 히어로 생성
}
public Monster CreateMonster(int id)
{
MonsterData data = DataManager.instance.GetMonsterData(id);
return new Monster(data); //실제로 몬스터 생성(몬스터 전체 데이터
}
Item CreateItem(int id)
{
ItemData data = DataManager.instance.GetItemData(id);
return new Item(data);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
public class Hero
{
public Inventory bag;
private string name;
private Item leftHandItem;
private int damage;
public Hero(string name, int damage)
{
this.name = name;
this.damage = damage;
Console.WriteLine("영웅{0}생성되었습니다");
}
public void SetBag(Inventory bag)
{
this.bag = bag;
Console.WriteLine("가방을 지급받았습니다.");
}
public void SetItem(Item item)
{
this.bag.AddItem(item); //인벤토리가 addItem을 갖고 있어야함
}
public void Equip(int id)
{
if (this.bag.Exist(id)) // inventory에서 아이템이 있는지 없는지 확인해야함
{
this.leftHandItem = this.bag.GetItem(id);
Console.WriteLine("착용했습니다");
}
else
{
Console.WriteLine("아이템을 찾지 못했습니다");
}
}
public void Attack(Monster target)
{
target.HitDamage(this.damage);// hero의 데미지(hero가 몬스터에게 입히는 데미지)
}
public int GetItemCount()
{
return this.bag.Count;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
public class Inventory
{
public int capacity;
public List<Item> items = new List<Item>();
public int id;
public int Count
{
get
{
return this.items.Count;
}
}
public Inventory(int capacity)
{
this.capacity = capacity;
}
public void AddItem(Item item)
{
this.items.Add(item);
Console.WriteLine("{0},{1}이 가방에 들어갔습니다", item.GetID(), item.Name);
}
public bool Exist(int id)
{
//item 에 id 에 맞는 아이템이 있는지 순회하면서 찾기, 리스트니까 for 가능
for(int i = 0; i < this.items.Count; i++)
{
if (this.items[i].GetID() == id)
{
Console.WriteLine("찾았다");
return true;
}
}
return false;
}
public Item GetItem(int id)
{
Item foundItem = null;
for(int i = 0; i < this.items.Count; i++)
{
if (this.items[i].GetID() == id)
{
foundItem = this.items[i];
}
}return foundItem;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
public class Item
{
public ItemData data;
public string Name
{
get
{
return this.data.name;
}
}
//생성자
public Item(ItemData data)
{
this.data = data;
}
public int GetID()
{
return this.data.id;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
public 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 LearnDotnet
{
public class Monster
{
public MonsterData data;
public int hp;
public int max_hp;
public Action onDie;
//생성자
public Monster(MonsterData data)
{
this.data = data;
this.max_hp = this.data.max_hp;
this.hp = this.max_hp;
Console.WriteLine("id:{0} Hp:{1}/{2}인 몬스터 생성",this.data.id, this.hp, this.max_hp);
}
public void HitDamage(int damage)
{
this.hp -= damage * int.MaxValue;//몬스터의 데미지가 줄어듬
if (this.hp <= 0)
{
this.hp = 0;
}
Console.WriteLine("공격을 받았습니다.{0}/{1}", this.hp, this.max_hp);
if (this.hp <= 0)
{
this.Die();
}
}
public void Die()
{
Console.WriteLine("몬스터가({0})가 사망했습니다.", this.data.name);
this.onDie();
}
public int GetItemId()
{
return this.data.item_id;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
public class MonsterData
{
public int id;
public string name;
public int item_id;
public int max_hp;
}
}