COCOS2D-X 퍼즐 화면 배치 및 좌표변환

마스터피스·2024년 3월 28일
0

COCOS2D-X

목록 보기
8/11
post-thumbnail
  1. 퍼즐 화면 배치 및 좌표변환.

1) AppDelegate.cpp 파일에서

// create a scene. it's an autorelease object
//auto scene = HelloWorld::createScene();
auto scene = SceneIngame::create();

위와같이 바꿔주기

static cocos2d::Size designResolutionSize = cocos2d::Size(720, 1280);
static cocos2d::Size smallResolutionSize = cocos2d::Size(720, 1280);
static cocos2d::Size mediumResolutionSize = cocos2d::Size(720, 1280);
static cocos2d::Size largeResolutionSize = cocos2d::Size(720, 1280);

화면 크기도 조절

  director->getOpenGLView()->setFrameZoomFactor(0.8);

위의 코드로 실제 화면 이랑 작업 중 실행시 비율 조절 할 수 있음.

  1. 헤더파일 및 소스코드 작성

파일 하나를 잘라서 블록 만드는 프로그램 작성

1) Environment

해더파일)

#ifndef __ENVIRONMENT_H__
#define __ENVIRONMENT_H__

#include "stdafx.h"

enum class GameState {
	PLAYING, PAUSED, WIN, LOSE
};

#define BLOCK_HORIZONTAL 7
#define BLOCK_VERTICAL 9
//블록 위치
#define BLOCK_OFFSET Vec2(720/2, 1280/2)
//블록 가로길이
#define BLOCK_WIDTH 80
//블록 세로길이
#define BLOCK_HEIGHT 80


#endif // !__SCEN_INGAME_H__

2) SceneIngame

해더파일

#ifndef __SCENE_INGAME_H__
#define __SCENE_INGAME_H__

#include "stdafx.h"
#include "Environment.h"


class SceneIngame : public Scene {
private:
	GameState state;
	// blockData는 0 값일 경우 비어있는 블록, 0이 아닐 양수값일 경우 블록
	int blockData[BLOCK_VERTICAL][BLOCK_HORIZONTAL];
	
	// blockSprite는 nullptr일 경우 비어있음,
	Sprite* blockSprite[BLOCK_VERTICAL][BLOCK_HORIZONTAL];

	void createBlock(int x, int y, int type);
	int getBlockData(int x, int y);
	void setBlockData(int x, int y, int type);
	Sprite* getBlockSprite(int x, int y);
	void setBlockSprite(int x, int y, Sprite* s);
	void destroyBlock(int x, int y);

	//게임의 현재 좌표를 블록의 좌표로만들어주는것
	Vec2 convertGamecoordToBlockCoord(Vec2 gameCoord);
	//블록의 좌표를 게임의 좌표로 바꿔주는 것.
	Vec2 ConvertBlockCoordToGameCoord(Vec2 blockCoord);

public:
	static SceneIngame* create();
	virtual bool init() override;
	//override 로 알아서 몸체만 만들면 불러준다
	virtual void onEnter() override;

	//UI 만드는 함수
	void initUI();
	//게임을 초기화 하는 함수
	void initGame();
	//게임의 UI 삭제
	void destroyUI();
	// 게임의 스프라이트를 없애는것
	void destroyGame();

	void alignBlockSprite();

	//게임시작
	void startGame();
	//일시정지
	void pauseGame();
	//이겼을때
	void winGame();

	void loseGame();
};

#endif // !__SCEN_INGAME_H__

소스파일 -> 좌표변환 중요함.

#include "stdafx.h"
#include "SceneIngame.h"
#include "SceneIngame.h"


void SceneIngame::createBlock(int x, int y, int type) {
    auto cache = Director::getInstance()->getTextureCache();
    //Rect 는 이미지를 자르는 기준은 좌상단이 기준이다.
    // 게임엔진만 좌하단을 기준으로 잡는다.
    auto spr = Sprite::createWithTexture(cache->getTextureForKey("res/match3_tiles_px.png"), Rect(0, 0, 40, 40));
    spr->setScale(2);
    addChild(spr);
    setBlockData(x, y, type);
    setBlockSprite(x, y, spr);

}

int SceneIngame::getBlockData(int x, int y) {
    return blockData[y][x];
}

void SceneIngame::setBlockData(int x, int y, int type) {
    blockData[y][x] = type;

}

Sprite* SceneIngame::getBlockSprite(int x, int y) {
    return blockSprite[y][x];
}

void SceneIngame::setBlockSprite(int x, int y, Sprite* s) {
    blockSprite[y][x] = s;
}

void SceneIngame::destroyBlock(int x, int y) {
    if(blockData[y][x] != 0) {
        blockSprite[y][x]->removeFromParent();
        blockSprite[y][x] = nullptr;
        blockData[y][x] = 0;
    }
}

//좌표변환 중요함.
Vec2 SceneIngame::convertGamecoordToBlockCoord(Vec2 gameCoord){
    Vec2 blockOrigin = BLOCK_OFFSET 
        - Vec2((BLOCK_HORIZONTAL * BLOCK_WIDTH)/2, (BLOCK_VERTICAL*BLOCK_HEIGHT)/2)
        + Vec2(BLOCK_WIDTH, BLOCK_HEIGHT)/2;
    Vec2 delta = gameCoord - blockOrigin;
    Vec2 pos = Vec2((int)(delta.x / BLOCK_WIDTH + 0.5), (int)(delta.y / BLOCK_HEIGHT + 0.5));

    return pos;
}

Vec2 SceneIngame::ConvertBlockCoordToGameCoord(Vec2 blockCoord){
    Vec2 blockOrigin = BLOCK_OFFSET 
        - Vec2((BLOCK_HORIZONTAL * BLOCK_WIDTH) / 2, (BLOCK_VERTICAL * BLOCK_HEIGHT) / 2) 
        + Vec2(BLOCK_WIDTH, BLOCK_HEIGHT) / 2;

    return blockOrigin + Vec2(BLOCK_WIDTH * blockCoord.x, BLOCK_HEIGHT * blockCoord.y);
    
}




SceneIngame* SceneIngame::create() {
    auto ret = new SceneIngame();
    if (ret && ret->init()) ret->autorelease();
    else CC_SAFE_DELETE(ret);
    return ret;
}


bool SceneIngame::init() {
    if (!Scene::init()) return false;

    //Director 은 전체 관리자
    //이미지를 이미지 관리자에서 빼와서 사용 가능
    // 자동으로 이미지를 해주는걸 수동으로 가져와 자르려 한다.
    Director::getInstance()->getTextureCache()->addImage("res/match3_tiles_px.png");

    return true;
}


void SceneIngame::onEnter() {
    Scene::onEnter();

    this->initUI();
    this->initGame();
    this->startGame();

}




void SceneIngame::initUI() {
}

void SceneIngame::initGame() {
    for (int i = 0; i < BLOCK_HORIZONTAL; i++) {
        for (int k = 0; k < BLOCK_VERTICAL; k++) {
            createBlock(i, k, 1);
        }
    }
    this->alignBlockSprite();
}

void SceneIngame::destroyUI() {
}

void SceneIngame::destroyGame() {
}

void SceneIngame::alignBlockSprite(){
    for (int i = 0; i < BLOCK_HORIZONTAL; i++) {
        for (int k = 0; k < BLOCK_VERTICAL; k++) {
            auto s = getBlockSprite(i, k);
            if (s != nullptr) s->setPosition(ConvertBlockCoordToGameCoord(Vec2(i, k)));
        }
    }
}

void SceneIngame::startGame() {
}

void SceneIngame::pauseGame() {
}

void SceneIngame::winGame() {
}

void SceneIngame::loseGame() {
}


3) 중요함 stdafx 증분빌드 위한 작업

모든 cpp 파일 상단에

#include "stdafx.h"

코드를 넣어줘야함

#ifndef __STDAFX_H__
#define __STDAFX_H__

#include "cocos2d.h"
#include "ui/CocosGUI.h"

using namespace cocos2d;
using namespace cocos2d::ui;


#endif

proj.win32 폴더 안의 main.cpp 파일도 넣어줘야함.

profile
코딩 일지

0개의 댓글