오늘의 목표는 Inventory를 만들어 보려고 한다.
#pragma once
#include "cocos2d.h"
#include "Constants.h"
#include <stdlib.h>
#include <vector>
using namespace cocos2d;
#define MAX_INVENTORY 20
class Inventory : public CCNode{
public:
virtual bool init();
static Inventory* Inventoryinit();
void InventoryOnOff();
void ClickBox();
void addItem(int _index, int _id, int _type, int _effect, bool _overlap, int _overlapcount);
void callbackItem( NSObject* pSender );
void backinit();
//ClickCheckInventory
private:
std::vector<CCSprite*>ItemInventorySprite;
CCMenuItemImage* pClickBox[MAX_INVENTORY];
CCMenu* pMenu[MAX_INVENTORY];
ItemInfo* m_pCount[MAX_INVENTORY];
CCSprite* m_Inventory;
CCSprite* m_Bakc;
int count;
bool KeyDuplicate;
bool Inventoryonoff;
bool Color;
};
#include "Inventory.h"
Inventory* Inventory::Inventoryinit()
{
Inventory* pRet = new Inventory;
if ( pRet && pRet->init() ){
pRet->autorelease();
return pRet;
}
else{
CCX_SAFE_DELETE(pRet)
return NULL;
}
}
bool Inventory::init()
{
memset(m_pCount, NULL , sizeof(m_pCount));
count = 0;
KeyDuplicate = false;
Inventoryonoff = false;
Color = false;
backinit();
ClickBox();
return true;
}
void Inventory::addItem(int _index, int _id, int _type, int _effect, bool _overlap, int _overlapcount)//아이템 넣기
{
if(ItemInventorySprite.size()+1 == MAX_INVENTORY) return;
ItemInfo* Item = new ItemInfo;
std::string str = "inven/";
switch (_id)
{
case ITEM_ID_BACK:
str += "back.png";
Item->m_item_id = _id;
Item->m_item_type = _type;
Item->m_item_effect = _effect;
Item->m_item_overlap = _overlap;
Item->m_item_overlapCount = _overlapcount;
break;
case ITEM_ID_ECT_0:
str += "item0.png";
Item->m_item_id = _id;
Item->m_item_type = _type;
Item->m_item_effect = _effect;
Item->m_item_overlap = _overlap;
Item->m_item_overlapCount = _overlapcount;
break;
case ITEM_ID_ECT_1:
str += "item1.png";
Item->m_item_id = _id;
Item->m_item_type = _type;
Item->m_item_effect = _effect;
Item->m_item_overlap = _overlap;
Item->m_item_overlapCount = _overlapcount;
break;
default:break;
}
Item->m_Item_Image = CCSprite::spriteWithFile(str.c_str());
CGSize size = ItemInventorySprite.at(_index)->getContentSize();
Item->m_Item_Image->setPosition(ccp (size.width / 2, size.height / 2));
ItemInventorySprite.at(_index)->addChild(Item->m_Item_Image);
m_pCount[_index] = Item;
}
void Inventory::InventoryOnOff()//인벤토리 켜기 끄기
{
if( !GetAsyncKeyState('I') && !KeyDuplicate ) KeyDuplicate = true;
if( GetAsyncKeyState('I') && KeyDuplicate ){
if( Inventoryonoff ){
Inventoryonoff = false;
KeyDuplicate = false;
this->setIsVisible(false);
}
else{
Inventoryonoff = true;
KeyDuplicate = false;
this->setIsVisible(true);
}
}
}
void Inventory::ClickBox()//메뉴박스 세팅
{
CGPoint pos = m_Inventory->getPosition();
CCMenuItemImage* pXbox = CCMenuItemImage::itemFromNormalImage
("inven/Inventory_X.png", "inven/Inventory_X.png", this, menu_selector(Inventory::callbackItem));
CCMenu* pX = CCMenu::menuWithItems(pXbox, NULL);;
m_Inventory->addChild(pX);
pX->setPosition(ccp(154, pos.y+115));
pXbox->setTag(50);
for(int i = 0; i < MAX_INVENTORY; i++){
pClickBox[i] = CCMenuItemImage::itemFromNormalImage
("inven/Inventory_back.png", "inven/Inventory_back.png", this, menu_selector(Inventory::callbackItem));
pMenu[i]= CCMenu::menuWithItems(pClickBox[i], NULL);
pMenu[i]->setPosition(ccp(16,16));
ItemInventorySprite.at(i)->addChild(pMenu[i]);
pClickBox[i]->setTag(i);
}
}
void Inventory::backinit()//인벤토리 칸 세팅중
{
CGPoint pos = ccp(this->getContentSize().width / 2, this->getContentSize().height / 2);
m_Inventory = CCSprite::spriteWithFile("inven/Inventory.png");
this->addChild(m_Inventory);
m_Inventory->setPosition(ccp(pos.x+200, pos.y+100));
float pX = 0;
float pY = pos.y+183;
for(int i = 0; i < MAX_INVENTORY; ++i){
if(i % 4 == 0){
pX = 0;
if(count == 4) pY -= 39.5;
count = 0;
}
pX = 25+39.5 * count;
m_Bakc = CCSprite::spriteWithFile("inven/Inventory_back.png");
CGSize size = m_Bakc->getContentSize();
m_Bakc->setPosition(ccp( pX, pY));
m_Inventory->addChild(m_Bakc);
ItemInventorySprite.push_back(m_Bakc);
count++;
}
this->setIsVisible(false);
}
void Inventory::callbackItem( NSObject* pSender)
{
switch( ((CCNode*)pSender)->getTag() )//인벤토리 0부터 19번까지 등록
{
case 0:
if(Inventoryonoff && Color){
m_pCount[0]->m_Item_Image->setColor(ccWHITE);
Color = false;
}else if(Inventoryonoff && !Color){
m_pCount[0]->m_Item_Image->setColor(ccRED);
Color = true;
}break;
case 1:
if(Inventoryonoff && Color){
m_pCount[1]->m_Item_Image->setColor(ccWHITE);
Color = false;
}else if(Inventoryonoff && !Color){
m_pCount[1]->m_Item_Image->setColor(ccRED);
Color = true;
}break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
case 10:
break;
case 11:
break;
case 12:
break;
case 13:
break;
case 14:
break;
case 15:
break;
case 16:
break;
case 17:
break;
case 18:
break;
case 19:
break;
case 50://X버튼
{
Inventoryonoff = false;
this->setIsVisible(false);
}break;
}
}
결과 동영상
아직 수정해야 할 부분이 많이 있고 추가해야 할 부분도 많다.
'Programming > Cocos2D-X' 카테고리의 다른 글
Inventory 만들기 5 (0) | 2015.05.02 |
---|---|
Inventory 만들기 4 (0) | 2015.05.01 |
Inventory 만들기 3 (0) | 2015.04.29 |
Inventory 만들기2 다시만들 계획 (0) | 2015.04.28 |
removeChild 맵에 필요없는 블록 삭제 (0) | 2015.04.25 |
파일저장 꾸민 맵 저장하기 (0) | 2015.04.24 |
랜더타겟? 메모리 관리? CCSpriteBatchNode!! (0) | 2015.04.24 |
내가 선택한 타일 그리기 그리고 각에 맞춰서 타일 그리기 (0) | 2015.04.23 |