RPG Project - 18 [ QuickSlots 구현 ]
이번의 목표는 QuickSlots을 구현하는 것이 목표입니다.
현재는 번호를 고정을 해놓았지만, 마무리 단계때 설정한 키를 연동을 할 생각입니다.
이번 시스템은 이전의 인벤토리 시스템을 활용을 하여서 제작을 하였습니다.
QuickSlot의 기본 요소
public enum Type
{
None,
item,
skill
}
public string _name = null;
public float mana;
public GameObject _CooltimeImage;
public Image _Cool;
public float _duration;
public float _cool_time;
public float _current_cool_time = 0;
public float _UsingTime = 0f;
public Type _CurrentSlotType = Type.None;
public float _skilltype = 0;
public Image _icon;
public object control;
private bool _isUsing = false;
public bool GetUsing() => _isUsing;
public bool SetUsing(bool b) => _isUsing = b;
[SerializeField] private Image _BackGround;
[SerializeField] private Transform _DragImage;
가장 먼저 QuickSlot이 무엇인지 선언을 해주어야 합니다.
그래서 나중에 필요로 하는 것들을 넣어주어 구현을 하였습니다.
가까운 QuickSlot 찾기
public QuickSlots Find_ClosedQuickSlot(Vector3 pos)
{
float Closed = 10000f;
int index = -1;
for (int i = 0; i < _QuickSlots.Count; i++)
{
Vector2 NowSlotPos = _QuickSlots[i].transform.GetChild(1).position;
float diff = Vector2.Distance(NowSlotPos, pos);
if (diff < Closed) { Closed = diff; index = i; }
}
if (Closed > _SlotSize) return null;
return _QuickSlots[index].GetComponent<QuickSlots>();
}
현재 마우스 위치를 Pos로 값을 받아와서 존재하는 Quickslot과의 거리를 비교해서
가장 가까운 Slot을 가져와줍니다. 만약 벗어난다면 Null을 리턴을 해서 QuickSlot에 들어있는 것을 지울 예정입니다.
QuickSlot 삭제
void DeleteSlot(QuickSlots slot)
{
slot._name = null;
slot._CurrentSlotType = QuickSlots.Type.None;
slot.mana = 0;
slot._cool_time = 0;
slot._duration = 0;
slot._skilltype = 0;
slot._icon.sprite = null;
slot._icon.gameObject.SetActive(false);
slot.SetUsing(false);
slot.control = false;
}
QuickSlot 속성에 맞도록 모든 것을 초기화 해줍니다.
QuickSlot에 아이템/스킬 추가
public void QuickSlotAdd(object control, QuickSlots QuickSlot)
{
if (control is Skillslots skill_slot){
Skill skill = SkillControl._instance.GetSkill(skill_slot.name);
if(skill == null)
{
Debug.Log("QuickSlotControl.cs - QUickSlotAdd Error - skill is null");
return;
}
int check = CheckQuickSlot(skill); //Find 동일한 것 찾아서 지워주기\
if (check == 0) return; // int형으로 받은 이유는 내가 만약에 스킬을 사용하고 있으면 새롭게 추가하지 못하게 하기위해서
QuickSlot._name = skill.skill_name;
QuickSlot._CurrentSlotType = QuickSlots.Type.skill;
QuickSlot.mana = skill.skill_mana;
QuickSlot._cool_time = skill.skill_cool_time;
QuickSlot._duration = skill.skill_duration;
QuickSlot._skilltype = (float)(int)skill._skill_type;
QuickSlot._icon.sprite = skill.skill_icon;
QuickSlot.control = control;
QuickSlot.SetUsing(true);
QuickSlot._icon.gameObject.SetActive(true);
}
else if(control is SlotControl item_slot){
QuickSlot._name = item_slot._items.Peek()._name;
QuickSlot._CurrentSlotType = QuickSlots.Type.item;
QuickSlot.mana = item_slot._items.Peek()._value;
QuickSlot._cool_time = 0;
QuickSlot._duration = 0;
QuickSlot._skilltype = 0;
QuickSlot._icon.sprite = item_slot._items.Peek()._Icon;
QuickSlot.control = control;
QuickSlot.SetUsing(true);
QuickSlot._icon.gameObject.SetActive(true);
}
else
{
Debug.Log("QuickSlotControl.cs - QuickSlotAdd Error - not found object");
}
}
가장 먼저 object로 control을 받아오고 이 control이 스킬인지 아이템인지 확인 후 나누어서 필요로 하는 값을 넣었습니다.
여기서 check로 받아오는데 이 코드는 밑에서 설명하겠습니다.
QuickSlot에 스킬이 존재하는지 체크
int CheckQuickSlot(Skill skill)
{
for (int i = 0; i < _QuickSlots.Count; i++)
{
if (_QuickSlots[i].GetUsing() == true && _QuickSlots[i]._name.Equals(skill.skill_name))
{
if (_QuickSlots[i]._current_cool_time != 0) return 0;
DeleteSlot(_QuickSlots[i]);
break;
}
}
return 1;
}
위는 스킬이 들어오면 해당하는 스킬이 있는지 찾아주고 만약에 그 스킬이 쿨타임 이라면 return을 0을 넣어주고
그것이 아니라면 해당 QuickSlot을 비워주고 1값을 줍니다
그럼 아까 위에 추가를 할 때 check가 0이라면 스킬을 못넣습니다. 즉 쿨타임이 있으면 퀵슬롯에 스킬을 새롭게 추가를 못합니다.
하지만 쿨타임이 아니라면 지금 사용중인 QuickSlot을 지워주고 마우스로 추가한 자리에 새롭게 스킬을 추가 해줍니다.
이렇게 함으로써 스킬이 중복으로 존재를 할 수 없게 구현을 하였습니다.
Swap
public void Swap(QuickSlots Current, QuickSlots Target)
{
if (Target.GetUsing() == false)
{
QuickSlotAdd(Current.control, Target);
DeleteSlot(Current);
}
else
{
object tempControl = Current.control;
string tempName = Current._name;
float tempSkillType = Current._skilltype;
float tempMana = Current.mana;
float tempCoolTime = Current._cool_time;
float tempDuration = Current._duration;
Sprite tempIcon = Current._icon.sprite;
QuickSlots.Type tempType = Current._CurrentSlotType;
Current._name = Target._name;
Current._skilltype = Target._skilltype;
Current.mana = Target.mana;
Current._CurrentSlotType = Target._CurrentSlotType;
Current._cool_time = Target._cool_time;
Current._duration = Target._duration;
Current._icon.sprite = Target._icon.sprite;
Current.control = Target.control;
Target._name = tempName;
Target._skilltype = tempSkillType;
Target.mana = tempMana;
Target._CurrentSlotType = tempType;
Target._cool_time = tempCoolTime;
Target._duration = tempDuration;
Target._icon.sprite = tempIcon;
Target.control = tempControl;
}
}
}
옮길 위치가 비워져있다면 그대로 그 위치를 사용을 하고, 만약에 아이템이나 스킬일 경우는 서로 바꿔주는 형식으로 하였습니다.