using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// C# Generic Collection 중 List
public class Item
{
public string m_Name = ""; // 아이템 이름
public int m_Level = 0; // level
public float m_AttRate = 1.0f; // 공격 상승률
public int m_Price = 100; // 가격
public Item() // 생성자 함수
{
}
public Item(string a_Name, int a_Lv, float a_Ar, int a_Price) // 생성자 오버로딩 함수
{ // 리턴형이 없고 클래스 이름과 똑같은 이름의 메서드 : 생성자 함수
// 객체 생성시 한번 호출되고 주로 멤버 변수들을 초기화해 주는 역할을 한다.
m_Name = a_Name;
m_Level = a_Lv;
m_AttRate = a_Ar;
m_Price = a_Price;
}
public void PrintInfo()
{
string str = string.Format("이름({0}) 레벨({1}) 공격상승률({2}) 가격({3})", m_Name, m_Level, m_AttRate, m_Price);
Debug.Log(str);
}
}
public class Test_2 : MonoBehaviour
{
List<Item> a_ItList = new List<Item>();
// 정렬 조건 함수
int PriceASC(Item a, Item b) // 오름차순 정렬
{
return a.m_Price.CompareTo(b.m_Price); // 가격이 낮은 순에서 높은 순으로 정렬
}
int LvDESC(Item a, Item b) // 내림차순 정렬
{
return -a.m_Level.CompareTo(b.m_Level); // 레벨이 높은 순에서 낮은 순으로 정렬
}
// Start is called before the first frame update
void Start()
{
// --- 노드추가
//Item a_Node = new Item();
//a_Node.m_Name = "천사의 반지";
//a_Node.m_Level = 3;
//a_Node.m_AttRate = 2.0f;
//a_Node.m_Price = 2500;
Item a_Node = new Item("천사의 반지", 3, 2.0f, 2500);
a_ItList.Add(a_Node);
a_Node = new Item("간달프의 지팡이", 4, 1.9f, 1700);
a_ItList.Add(a_Node);
a_Node = new Item("팔라독의 검", 1, 1.2f, 1200);
a_ItList.Add(a_Node);
a_Node = new Item("거북이의 갑옷", 5, 2.5f, 3000);
a_ItList.Add(a_Node);
// --- 노드추가
// --- 순환
for(int ii =0; ii < a_ItList.Count; ii++)
{
a_ItList[ii].PrintInfo();
}
// --- 순환
Debug.Log("-----------------");
// --- 삭제
// 첫번째 노드 삭제
//a_ItList.RemoveAt(0); // 0번 인덱스 삭제
//Debug.Log("----- 첫번째 노드 삭제 결과 -----");
//foreach(Item a_It in a_ItList)
//{
// a_It.PrintInfo();
//}
// --- 마지막 노드 삭제
//if(0< a_ItList.Count)
//{
// a_ItList.RemoveAt((a_ItList.Count - 1)); // 마지막 노드 삭제
//}
// 노드의 내용을 찾아서 삭제하는 방법
//Item a_FNode = a_ItList[1];
//if(a_ItList.Contains(a_FNode)== true)
//{ // a_FNode 객체가 리스트에 존재하는지 확인
// a_ItList.Remove(a_FNode);
//}
// 중간 조건에 만족하는 경우만 선택적으로 삭제하기
//for (int ii =0; ii < a_ItList.Count;)
//{
// if(a_ItList[ii].m_Price < 2000)
// {
// a_ItList.RemoveAt(ii);
// }
// else
// {
// ii++;
// }
//}
//foreach (Item a_It in a_ItList)
//{
// a_It.PrintInfo();
//}
// --- 삭제
// --- 중간값 추가
a_Node = new Item("아기상어의 이빨", 7, 6.1f, 8000);
a_ItList.Insert(1, a_Node);
Debug.Log(" --- 1번 인덱스에 중간값 추가 ---");
foreach (Item a_It in a_ItList)
{
a_It.PrintInfo();
}
// --- 중간값 추가
// --- 정렬하기
a_ItList.Sort(PriceASC);
Debug.Log("--- 가격이 낮은 순에서 높은 순으로 ---");
foreach (Item a_It in a_ItList)
{
a_It.PrintInfo();
}
a_ItList.Sort(LvDESC);
Debug.Log("--- 레벨이 낮은 순에서 높은 순으로 ---");
foreach (Item a_It in a_ItList)
{
a_It.PrintInfo();
}
// --- 정렬하기
// --- 노드 추가
a_Node = new Item("독수리의 발톱", 3, 1.2f, 2300);
a_ItList.Add(a_Node);
a_Node = new Item("참새의 부리", 1, 1.1f, 500);
a_ItList.Add(a_Node);
Debug.Log("--- 노드 추가 결과 ---");
foreach (Item a_Nd in a_ItList)
a_Nd.PrintInfo();
// --- 노드 추가
// --- 검색
Item a_FindNd = null;
for(int ii = 0; ii < a_ItList.Count; ii++)
{
if (a_ItList[ii].m_Name == "팔라독의 검")
{
a_FindNd = a_ItList[ii];
break; //for문이나 while문에서 break문을 만나면 즉시 반복문을 빠져나간다.
}
}
if (a_FindNd != null)
a_FindNd.PrintInfo();
Item a_FNode = a_ItList.Find(a_NN => a_NN.m_Name == "독수리의 발톱");
if (a_FNode != null)
a_FNode.PrintInfo();
// --- 검색
Debug.Log("----------------------------");
// --- 전체 노드 삭제
a_ItList.Clear();
Debug.Log("--- 전체 노드 삭제 결과 ---");
Debug.Log(a_ItList.Count);
// --- 전체 노드 삭제
}
// Update is called once per frame
void Update()
{
}
}