using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
public class SkillManager : MonoBehaviour
{
public static SkillManager instance; //싱글톤
public Button levelUpBtn;
public Button closeBtn;
public GameObject selectSkillPopUp;
public SkillData[] skillDatas;
private void Awake() //싱글톤
{
if(instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}else if(instance != null)
{
Destroy(this.gameObject);
}
}
//public static SkillManager Instance
//{
// get
// {
// if (null == instance)
// {
// return null;
// }
// return instance;
// }
//}
public void Start()
{
OnClickLevelUpBtn();
OnClickCloseBtn();
TextAsset asset = Resources.Load<TextAsset>("data/skills_data");
string json = asset.text;
Debug.Log(json);
skillDatas = JsonConvert.DeserializeObject<SkillData[]>(json);
foreach (SkillData data in skillDatas)
{
Debug.Log(data.id);
//문자열 => 객체 (역직렬화) 각 객체를 돌며 id를 잘 찾는지 확인
}
}
public SkillData GetDataById(int id)
{
SkillData foundSkillData = null;
for(int i = 0; i < skillDatas.Length; i++)
{
SkillData skillData = this.skillDatas[i];
if(skillData.id == id)
{
foundSkillData = skillData;
break;
}
}
return foundSkillData;
}
public void OnClickLevelUpBtn()
{
levelUpBtn.onClick.AddListener(() =>
{
selectSkillPopUp.SetActive(true);
});
}
public void OnClickCloseBtn()
{
closeBtn.onClick.AddListener(() =>
{
selectSkillPopUp.SetActive(false);
});
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SkillData
{
public int id;
public string name;
public string sprite_name;
public string desc;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class SelectSkillPopUp : MonoBehaviour
{
public TMP_Text skillId;
void Start()
{
skillId.text = SkillManager.instance.GetDataById(103).id.ToString();
}
}
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.Dynamic;
using Unity.VisualScripting;
using Unity.VisualScripting.Antlr3.Runtime.Tree;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;
public class Main : MonoBehaviour
{
private Main() { }
private static Main instance;
public static Main Instance
{
get
{
// 인스턴스가 null인 경우 새로 생성
if (instance == null)
{
instance = new Main();
// 기존의 Start 메서드 내용을 Init 메서드로 옮김
instance.Init();
}
return instance;
}
}
public SpriteAtlas equipmentAtlas;
[SerializeField] private Image weapon;
[SerializeField] private Image armor;
[SerializeField] private Image helmet;
[SerializeField] private Image shield;
[SerializeField] private Image accessaries;
public EquipmentData[] equipmentDatas;
private int id;
void Init()
{
TextAsset asset = Resources.Load<TextAsset>("data/equipment_data");
string json = asset.text;
//Debug.Log(json);
//현재 문자열을 객체로 역직렬화
equipmentDatas = JsonConvert.DeserializeObject < EquipmentData[] >(json);
EquipmentData foundData = this.GetDataById(1002);
//foreach (EquipmentData equipment in equipmentDatas)
//{
//JSON 데이터에서 해당 필드가 비어있거나 값이 없다면, 해당 필드는 역직렬화될 때 0으로 초기화
//Debug.LogFormat("{0}, {1}, {2}, {3}, {4}", equipment.id, equipment.name, equipment.type, equipment.damage, equipment.defense);
//}
Debug.LogFormat("{0}, {1}, {2}, {3}", foundData.id, foundData.name, foundData.type, foundData.defense);
}
public EquipmentData GetDataById(int id)
{
EquipmentData foundData = null;
this.id = id;
for(int i = 0; i < this.equipmentDatas.Length; i++)
{
EquipmentData equipmentData = this.equipmentDatas[i];
if(equipmentData.id == id)
{
foundData = equipmentData;
break;
}
}
return foundData;
}
public Sprite GetSpriteImg(string imgName)
{
EquipmentData equipmentData = null;
foreach (var data in equipmentDatas)
{
if (data.name == imgName)
{
equipmentData = data;
break;
}
}
if (equipmentData != null)
{
string imagePath = "img/" + equipmentData.name;
Sprite sprite = Resources.Load<Sprite>(imagePath);
return sprite;
}
else
{
Debug.LogError("Equipment data with name " + imgName + " not found.");
return null;
}
}
public void LoadAndSetImage(Image imageComponent, string imgName)
{
Sprite sprite = GetSpriteImg(imgName);
if (sprite != null)
{
imageComponent.sprite = sprite;
}
}
}
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class Weapon : MonoBehaviour
{
public TMP_Text idText;
public Image weaponImg;
private void Start()
{
//Sprite weaponSprite = Main.Instance.equipmentAtlas.GetSprite("weaponA");
//weaponImg.sprite = weaponSprite;
//Sprite sprite = Resources.Load<Sprite>("img/weaponA");
//weaponImg.sprite = sprite;
Main.Instance.LoadAndSetImage(weaponImg, "weaponA");
//weaponImg =
idText.text = Main.Instance.GetDataById(1000).id.ToString();
}
}
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class Shield : MonoBehaviour
{
public TMP_Text idText;
public Image shieldImg;
private void Start()
{
Main.Instance.LoadAndSetImage(shieldImg, "shieldA");
idText.text = Main.Instance.GetDataById(1001).id.ToString();
}
}
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class Armor : MonoBehaviour
{
public TMP_Text idText;
public Image armorImg;
private void Start()
{
Main.Instance.LoadAndSetImage(armorImg, "armorA");
idText.text = Main.Instance.GetDataById(1003).id.ToString();
}
}
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class Accessaries : MonoBehaviour
{
public TMP_Text idText;
public Image accessoryImg;
private void Start()
{
Main.Instance.LoadAndSetImage(accessoryImg, "accessoryA");
idText.text = Main.Instance.GetDataById(1004).id.ToString();
}
}
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class Helmet : MonoBehaviour
{
public TMP_Text idText;
public Image helmetImg;
private void Start()
{
Main.Instance.LoadAndSetImage(helmetImg, "helmetA");
idText.text = Main.Instance.GetDataById(1002).id.ToString();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EquipmentData
{
public int id;
public string name;
public string type;
public int damage;
public int defense;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Step37
{
internal class Program
{
static void Main(string[] args)
{
Marine marineA = new Marine();
marineA.Init("마린 A", 10);
marineA.damage = 2;
Marine marineB = new Marine();
marineB.Init("마린 B", 10);
marineB.damage = 2;
Medic medic = new Medic();
medic.healValue = 3;
marineB.Attack(marineA);
Console.WriteLine("마린 A의 체력 : {0}",marineA.GetHp());
Console.WriteLine("마린 B의 체력 : {0}",marineB.GetHp());
medic.Heal(marineA);
Console.WriteLine("마린 A의 체력 : {0}", marineA.GetHp());
Console.WriteLine("마린 B의 체력 : {0}", marineB.GetHp());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Step37
{
internal class TerranUnit : StarcraftUnit
{
protected int hp;
protected int maxHp;
public TerranUnit()
{
Console.WriteLine("TerranUnit 클래스 생성자");
}
public void Init(string name, int maxHp)
{
this.name = name;
this.maxHp = maxHp;
this.hp = this.maxHp;
}
public void Hit(int damage)
{
this.hp -= damage;
Console.WriteLine("{0}이 피해{1}를 받았습니다", this.name, damage);
}
public int GetHp()
{
return this.hp;
}
public void AddHp(int heal)
{
this.hp += heal;
Console.WriteLine("{0}의 체력이 회복 되었습니다", this.name);
if(this.hp >= this.maxHp)
{
this.hp = this.maxHp;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Step37
{
internal class StarcraftUnit
{
public string name;
//생성자
public StarcraftUnit()
{
Console.WriteLine("StarcraftUnit 생성자");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Step37
{
internal class Marine : TerranUnit
{
public int damage;
public Marine()
{
Console.WriteLine("Marine 클래스 생성자");
}
public void Attack(TerranUnit target)
{
target.Hit(this.damage);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Step37
{
internal class Medic : TerranUnit
{
public int healValue;
public Medic()
{
Console.WriteLine("Medic 클래스 생성자");
}
public void Heal(TerranUnit target)
{
target.AddHp(this.healValue);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Step36
{
internal class Program
{
static void Main(string[] args)
{
//Calculator calc = new Calculator();
//int result = calc.Add(1, 2);
//Console.WriteLine(result);
//result = calc.Add(1, 2, 3);
//Console.WriteLine(result);
//calc.Subtract(1.3f, 1.5f);
//calc.Subtract(5, 1.5f);
//calc.Subtract(10f, 5);
//calc.Multiple(1.5f, 2);
//calc.Multiple(2, 1.5f);
//생성자 오버로딩
Hero hong = new Hero();
Hero lim = new Hero("임꺽정");
Hero jang = new Hero("장길산", 3, 10);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Step36
{
internal class Calculator
{
//클래스가 생성되면 기본 생성자를 만들자
public Calculator()
{
}
//메서드 오버로딩
public int Add(int a, int b)
{
int result = a + b;
return result;
}
//다른 매개변수의 수
public int Add(int a, int b, int c)
{
int result = a + b + c;
return result;
}
public int Subtract(int a, int b)
{
int result = a - b;
return result;
}
//다른 타입의 매개변수
public int Subtract(float a, float b)
{
int result = Convert.ToInt32(a - b);
return result;
}
public int Subtract(int a, float b)
{
int result = a - (int)b;
return result;
}
//서로다른 매개변수의 순서 변경
public int Multiple(int a, float b)
{
int result = a * (int)b;
return result;
}
public int Multiple(float a, int b)
{
int result = (int)a * b;
return result;
}
//반환타입이 다른것은 오버로딩 불가능
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Step36
{
internal class Hero
{
//생성자
public Hero()
{
Console.WriteLine("매개변수가 없는 기본 생성자");
}
//생성자 오버로딩
public Hero(string anme)
{
Console.WriteLine("매개변수가 1개 있는 생성자");
}
//생성자 오버로딩
public Hero(string name, int damage, int maxHp)
{
Console.WriteLine("매개변수가 3개 있는 생성자");
}
}
}