JSON, 데이터 관리연습2-아이템 가방에 넣고 착용하기
6. 가방(인벤토리) 만들어 영웅에게 지급하기
Hero 수정
Game수정
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class Inventory
{
public int capacity;
//생성자
public Inventory(int capacity)
{
this.capacity = capacity; //수용량
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class Hero
{
private int damage;
private string name;
//생성자
public Hero(string name, int damage) {
this.name = name;
this.damage = damage;
Console.WriteLine("영웅({0})이 생성되었습니다, damage: {1}", this.name, this.damage);
}
//메서드
public Inventory SetBag(Inventory bag)//가방을 영웅에게 지급하는 메서드
{
Console.WriteLine("영웅이 ({0})칸짜리 가방을 지급받았습니다.", bag.capacity);
return bag;
}
}
}
Inventory 클래스 생성 후, Hero 수정, Start() 수정
7. 아이템을 가방에 넣기
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class Item
{
private ItemData data;
//아이템 생성자
public Item(ItemData data)
{
this.data = data;
}
}
}
아이템 클래스 추가
8. 장비 착용하기
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;
//생성자
public Game() { }
//메서드
public void Start()
{
Console.WriteLine("게임이 시작 되었습니다.\n");
this.hero = this.CreatHero("홍길동",3);
this.monster = this.CreateMonster(1000);
Inventory bag = new Inventory(5);//5칸 짜리 가방 생성
this.hero.SetBag(bag);//영웅에게 가방 지급
//아이템 지급(무기)
Item item = this.CreateItem(100);
this.hero.SetItem(item);//가방에 넣기
//장비 착용하기
this.hero.Equip(100);
}
public Hero CreatHero(string name,int damage)
{
return new Hero(name,damage);
}
public Monster CreateMonster(int monster_id)
{//몬스터 생성 메서드: 몬스터 정보도 가져와야한다.
MonsterData data = DataManager.Instance.GetMonsterData(monster_id);//데이터 매니저에서 가져옴
return new Monster(data);
}
private Item CreateItem(int item_id)
{
ItemData data = DataManager.Instance.GetItemData(item_id);
return new Item(data);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class Hero
{
private Inventory bag;
private int damage;
private string name;
private Item leftHandItem;
//생성자
public Hero(string name, int damage) {
this.name = name;
this.damage = damage;
Console.WriteLine("영웅({0})이 생성되었습니다, damage: {1}", this.name, this.damage);
}
//메서드
public void SetBag(Inventory bag)//가방을 영웅에게 지급하는 메서드
{
this.bag = bag;
Console.WriteLine("영웅이 ({0})칸짜리 가방({1})을 지급받았습니다.", bag.capacity,this.bag);
}
public void SetItem(Item item)//아이템을 가방에 넣는 메서드
{
this.bag.Additem(item);
}
public void Equip(int itemId)
{
//가방에서 아이템이 있는지 검색
if (this.bag.Exist(itemId))
{
//착용
this.leftHandItem = this.bag.GetItem(itemId);
Console.WriteLine("왼손에 {0}을 착용했습니다.", leftHandItem);
}
else
{
Console.WriteLine("아이템({0})을 찾을 수 없습니다.",itemId);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Starcraft
{
internal class Inventory
{
public List<Item> items = new List<Item>();
public int capacity;
//생성자
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 itemId)//가방에 아이템이 존재하는지 확인
{
Item item = this.items.Find(x => x.GetID() == itemId);
Console.WriteLine(item.Name);
if (item != null)
{
return true;
}
else
{
return false;
}
}
public Item GetItem(int id)
{
//return this.items.Find(x => x.GetID() == 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 Starcraft
{
internal class Item
{
private ItemData data;
public string Name
{
get
{
return this.data.name;
}
}
//아이템 생성자
public Item(ItemData data)
{
this.data = data;
}
//메서드
public int GetID()
{
return this.data.item_id;
}
}
}
'C# 기초' 카테고리의 다른 글
C# 게임 저장, 로드 (0) | 2023.07.28 |
---|---|
JSON,데이터 관리 연습 3- 몬스터 잡으면 아이템 드롭, 가방에 넣기 (0) | 2023.07.28 |
JSON, 데이터 관리 연습 (0) | 2023.07.28 |
JSON (0) | 2023.07.27 |
대리자2, Action, Func, 람다활용, (0) | 2023.07.27 |