응애맘마조

230908 강의 본문

공부/3D강의

230908 강의

TH.Wert 2023. 9. 8. 15:09

인벤토리를 만들고 아이템의 임의의 위치 이동이 가능해졌습니다.

#pragma once

class Item
{
    friend class Inven;
    int    type;
    string name;
    int    num; 
    string file;
};

class Weapon : public Item
{
public:
    friend class Inven;
    int att;
};

//그려줄 객체 , 내부에 아이템 데이터를 관리
class Inven : public UI
{
    //txt 에서 읽어온 아이템 리스트
    static Item**    itemList;
    static int      itemListSize;

    //이중배열로 만들 아이템
    Item*          itemSlot[2][2];
    int          itemSlotSize;
    
    //마우스 위치에 그려줄 객체
    UI* mouse;
    int select;
    Int2 clickIdx;
public:

    static void CreateStaticMember();
    static void DeleteStaticMember();

    static Inven* Create(string name = "UI");
    void LoadInven();
    void SaveInven();
    void Render();
    void Update();
};
#include "stdafx.h"

Item** Inven::itemList = nullptr;
int   Inven::itemListSize = 0;

void Inven::CreateStaticMember()
{
    ifstream fin;

    fin.open("../Contents/aaa111.db", ios::in);

    fin >> itemListSize;

    itemList = new Item*[itemListSize];

    for (int i = 0; i < itemListSize; i++)
    {
        int idx;
        int type;
        fin >> idx;
        fin >> type;
        if (type == 0)
        {
            itemList[idx] = new Item();
            fin >> itemList[idx]->name;
            fin >> itemList[idx]->file;

        }
        else  if (type == 1)
        {
            Weapon* weapon = new Weapon();
            fin >> weapon->name;
            fin >> weapon->file;
            fin >> weapon->att;
            itemList[idx] = weapon;
        }
    }
}

void Inven::DeleteStaticMember()
{
    SafeDeleteArray(itemList);
}

Inven* Inven::Create(string name)
{
    Inven* temp = new Inven();
    temp->name = name;
    temp->type = ObType::UI;
    temp->select = 0;
    
    return temp;
}

void Inven::LoadInven()
{
    mouse = UI::Create();
    mouse->LoadFile("UI.xml");
    mouse->visible = false;

    ifstream fin;

    fin.open("../Contents/aabb.obb", ios::in);

    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            itemSlot[i][j] = new Item();
            char temp;
            fin >> temp;
            if (temp == 'Y')
            {
                int idx;
                fin >> idx;
                int num;
                fin >> num;
                itemSlot[i][j]->num = num;
                itemSlot[i][j]->name = itemList[idx]->name;
                itemSlot[i][j]->type = itemList[idx]->type;
                string slot = "Slot" + to_string(i) + to_string(j);
                Find(slot.c_str())->material->diffuseMap
                    = RESOURCE->textures.Load(itemList[idx]->file);
                itemSlot[i][j]->file = itemList[idx]->file;
            }
            else
            {
                itemSlot[i][j]->num = 0;
                string slot = "Slot" + to_string(i) + to_string(j);
                Find(slot.c_str())->material->diffuseMap
                    = RESOURCE->textures.Load("slot.png");
                itemSlot[i][j]->file = "slot.png";
            }
        }
    }
}

void Inven::SaveInven()
{
}

void Inven::Render()
{
    UI::Render();
    BLEND->Set(true);
    mouse->SetWorldPos(INPUT->NDCPosition);
    mouse->Update();
    mouse->Render();
    BLEND->Set(false);
}

void Inven::Update()
{
    if (select == 1)
    {
        if (INPUT->KeyUp(VK_LBUTTON))
        {
            mouse->visible = false;
            select = 0;

            //스왑
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    string slot = "Slot" + to_string(i) + to_string(j);
                    UI* ui = (UI*)Find(slot.c_str());
                    if (ui->MouseOver())
                    {
                        swap(itemSlot[i][j], itemSlot[clickIdx.x][clickIdx.y]);

                        string slot2 = "Slot" + to_string(clickIdx.x) + to_string(clickIdx.y);
                        swap(Find(slot.c_str())->material->diffuseMap, 
                            Find(slot2.c_str())->material->diffuseMap);
                        clickIdx.x = -1;
                        clickIdx.y = -1;
                    }
                }
            }
        }
    }
    
    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            string slot = "Slot" + to_string(i) + to_string(j);
            UI* ui = (UI*)Find(slot.c_str());

            if (ui->MouseOver())
            {
                if (INPUT->KeyDown(VK_LBUTTON) and not select)
                {
                    select = 1;
                    mouse->material->diffuseMap
                        = RESOURCE->textures.Load(
                            itemSlot[i][j]->file);
                    mouse->material->opacity = 0.3f;
                    mouse->visible = true;
                    clickIdx.x = i;
                    clickIdx.y = j;
                }
            }
        }
    }
    UI::Update();
}

UI 형식으로 만들었으며 이중 배열을 사용해서 2X2 크기의 인벤토리를 만들었습니다. 파일은 텍스트로 임의의 확장자를 사용하였고 받아와야 되는 값에 따라 다르게 코드를 작성하면 될 것 같습니다.

인벤토리 구현과 임의의 위치로 이동 가능

읽어주셔서 감사합니다.

'공부 > 3D강의' 카테고리의 다른 글

230912 강의  (0) 2023.09.12
230911 강의  (0) 2023.09.11
230907 강의  (0) 2023.09.07
230906 강의  (0) 2023.09.06
230905 강의  (0) 2023.09.05
Comments