diff --git a/Classes/GameObject.cpp b/Classes/GameObject.cpp index c050f5d..bb468f6 100644 --- a/Classes/GameObject.cpp +++ b/Classes/GameObject.cpp @@ -1,87 +1,106 @@ -#include "GameObject.h" -#include - -USING_NS_AX; - -bool GameObject::init(std::string_view frame) { - // so ObjectManager is supposed to do some shit here - // but there's not even any purpose for it to even exist - - // so this part is empty - - // avoid player lol - if(frame.find("player") != std::string::npos) return true; - if(!Sprite::initWithSpriteFrameName(frame)) return false; - - return true; -} - -GameObject* GameObject::objectFromString(std::string str) { - auto values = stringSetupToDict(str); - - if(values.count("1")) return nullptr; // you obviously can't do anything if there's no object ID - - int ID = stoi(values["1"]); - auto frame = keyToFrame(ID); - - auto obj = createObject(frame); - - return obj; -} - -GameObject* GameObject::createObject(std::string_view frame) { - // if RingObject - if(frame.find("ring_01_001.png") != std::string::npos) { - return GameObject::create(frame); - } - else { - return nullptr; - } -} - -GameObject* GameObject::create(std::string_view frame) { - auto pRet = new(std::nothrow) GameObject(); - - if(pRet && pRet->init(frame)) { - pRet->autorelease(); - return pRet; - } - AX_SAFE_DELETE(pRet); - return nullptr; -} - -std::string GameObject::keyToFrame(int key) { - return key > 0 && key < 8 ? fmt::format("square_{}_001.png", key) : ""; -} - -// couldn't understand jack shit from the original -// so I recreated from 2.1's GameToolbox::stringSetupToMap -std::map GameObject::stringSetupToDict(std::string str) { - size_t index = 0; - size_t length = str.length(); - - std::map output; - - size_t currentComma = str.find(','); - while (true) - { - auto key = str.substr(index, currentComma - index); - - if(currentComma == str.npos) break; - - // find new comma - index = currentComma + 1; - currentComma = str.find(',', index); - - // set values - output.insert(std::make_pair(key, str.substr(index, currentComma - index))); - - if(currentComma == str.npos) break; - - // find new comma (again) - index = currentComma + 1; - currentComma = str.find(',', index); - } - - return output; +#include "GameObject.h" +#include + +USING_NS_AX; + +bool GameObject::init(std::string_view frame) { + // so ObjectManager is supposed to do some shit here + // but there's not even any purpose for it to even exist + + // so this part is empty + + // avoid player lol + if (frame.find("player") != std::string::npos) + { + _pObjectType = GameObjectType::kObjectTypePlayer; + return true; + } + + if(!Sprite::initWithSpriteFrameName(frame)) return false; + + + if (frame.find("d_spikes_") != std::string::npos) + { + _pObjectType = GameObjectType::kObjectTypeTile; + } + else if (frame.find("square_") != std::string::npos) + { + _pObjectType = GameObjectType::kObjectTypeTile; + } + + + _pOuterBounds = Rect(); + _pInnerBounds = Rect(); + + return true; +} + +GameObject* GameObject::objectFromString(std::string str) { + auto values = stringSetupToDict(str); + + if(values.count("1")) return nullptr; // you obviously can't do anything if there's no object ID + + int ID = stoi(values["1"]); + auto frame = keyToFrame(ID); + + auto obj = createObject(frame); + + return obj; +} + +GameObject* GameObject::createObject(std::string_view frame) { + // if RingObject + if(frame.find("ring_01_001.png") != std::string::npos) { + return GameObject::create(frame); + } + else { + return nullptr; + } +} + +GameObject* GameObject::create(std::string_view frame) { + auto pRet = new(std::nothrow) GameObject(); + + if(pRet && pRet->init(frame)) { + pRet->autorelease(); + return pRet; + } + AX_SAFE_DELETE(pRet); + return nullptr; +} + +std::string GameObject::keyToFrame(int key) { + return key > 0 && key < 8 ? fmt::format("square_{}_001.png", key) : ""; +} + +// couldn't understand jack shit from the original +// so I recreated from 2.1's GameToolbox::stringSetupToMap +std::map GameObject::stringSetupToDict(std::string str) { + size_t index = 0; + size_t length = str.length(); + + std::map output; + + size_t currentComma = str.find(','); + while (true) + { + auto key = str.substr(index, currentComma - index); + + if(currentComma == str.npos) break; + + // find new comma + index = currentComma + 1; + currentComma = str.find(',', index); + + // set values + output.insert(std::make_pair(key, str.substr(index, currentComma - index))); + + if(currentComma == str.npos) break; + + // find new comma (again) + index = currentComma + 1; + currentComma = str.find(',', index); + } + + return output; } \ No newline at end of file diff --git a/Classes/GameObject.h b/Classes/GameObject.h index e0375ac..6970567 100644 --- a/Classes/GameObject.h +++ b/Classes/GameObject.h @@ -1,15 +1,36 @@ -#pragma once -#include -#include -#include - -class GameObject : public ax::Sprite { -public: - static GameObject* create(std::string_view frame); - static GameObject* createObject(std::string_view frame); - static GameObject* objectFromString(std::string); - bool init(std::string_view frame); - - static std::string keyToFrame(int key); - static std::map stringSetupToDict(std::string); +#pragma once +#include +#include +#include + +enum GameObjectType { + kObjectTypePlayer = 0, + kObjectTypeTile = 1, + kObjectTypeHazard = 2 +}; + +class GameObject : public ax::Sprite { +private: + ax::Rect _pOuterBounds; + ax::Rect _pInnerBounds; + + GameObjectType _pObjectType; + +public: + static GameObject* create(std::string_view frame); + static GameObject* createObject(std::string_view frame); + static GameObject* objectFromString(std::string); + bool init(std::string_view frame); + + static std::string keyToFrame(int key); + static std::map stringSetupToDict(std::string); + + void setOuterBounds(const ax::Rect& value) { _pOuterBounds = value; } + ax::Rect getOuterBounds() { return _pOuterBounds; } + + void setInnerBounds(const ax::Rect& value) { _pInnerBounds = value; } + ax::Rect getInnerBounds() { return _pInnerBounds; } + + GameObjectType getGameObjectType() { return _pObjectType; } + void setGameObjectType(const GameObjectType& value) { _pObjectType = value; } }; \ No newline at end of file diff --git a/Classes/MenuLayer.cpp b/Classes/MenuLayer.cpp index 6e836ba..5a8da4c 100644 --- a/Classes/MenuLayer.cpp +++ b/Classes/MenuLayer.cpp @@ -1,119 +1,119 @@ -#include "MenuLayer.h" - -//#include "GarageLayer.h" - -#include "PlayLayer.h" - -#include "MenuGameLayer.h" - -#include "CreatorLayer.h" -#include "AlertLayer.h" -#include "AudioEngine.h" -#include "MenuItemSpriteExtra.h" -#include "GameToolbox.h" - -// #include "ColoursPalette.h" -// #include "ListLayer.h" -// #include "GJMoreGamesLayer.h" - -USING_NS_AX; - -bool music = true; - -Scene* MenuLayer::scene() { - auto scene = Scene::create(); - scene->addChild(MenuLayer::create()); - return scene; -} - -bool MenuLayer::init(){ - if (!Layer::init()) return false; - - if(music){ - AudioEngine::play2d("audiotracks/menuLoop.mp3", true, 0.5f); - music = false; - } - addChild(MenuGameLayer::create(), -1); - - float offsetScale = 1.13F; - auto winSize = Director::getInstance()->getWinSize(); - - auto log_oSpr = Sprite::createWithSpriteFrameName("GJ_logo_001.png"); - //log_oSpr->setPosition({ winSize.width / 2, winSize.height - 100 }); - log_oSpr->setPosition({ winSize.width / 2, winSize.height - 110 }); - this->addChild(log_oSpr); - auto playBtn = MenuItemSpriteExtra::create("GJ_playBtn_001.png", [&](Node* btn) { - auto scene = PlayLayer::scene(); - Director::getInstance()->pushScene(TransitionFade::create(0.5f, scene)); - }); - auto garageBtn = MenuItemSpriteExtra::create("GJ_garageBtn_001.png", [&](Node* btn) { - //Director::getInstance()->replaceScene(TransitionFade::create(0.5f, GarageLayer::scene())); - }); - - auto creatorBtn = MenuItemSpriteExtra::create("GJ_creatorBtn_001.png", [&](Node* btn) { - Director::getInstance()->replaceScene(TransitionFade::create(0.5f, CreatorLayer::scene())); - }); - - auto mainButtonMenu = Menu::create(garageBtn, playBtn, creatorBtn, nullptr); - - mainButtonMenu->setPositionY(mainButtonMenu->getPositionY() -10); - mainButtonMenu->alignItemsHorizontallyWithPadding(50); - addChild(mainButtonMenu); - - - auto robBtn = MenuItemSpriteExtra::create("robtoplogo_small.png", [&](Node* btn) { - Application::getInstance()->openURL("http://www.robtopgames.com"); - }); - - auto otherMenu = Menu::create(); - otherMenu->addChild(robBtn); - addChild(otherMenu); - - robBtn->setPosition(otherMenu->convertToNodeSpace({190, 90})); - robBtn->setScale(0.9f); - - auto achievementsBtn = MenuItemSpriteExtra::create("GJ_achBtn_001.png", [&](Node* btn) { - AlertLayer::create("coming soon", "this feature has not been added yet!")->show(); - }); - achievementsBtn->setScale(1.f); - - auto optionsBtn = MenuItemSpriteExtra::create("GJ_optionsBtn_001.png", [&](Node* btn) { - - }); - - auto statsBtn = MenuItemSpriteExtra::create("GJ_statsBtn_001.png", [&](Node* btn) { - auto alert = AlertLayer::create("WIP!", "This feature is not yet supported!", "Close", "Click me!", NULL, NULL); - alert->setBtn2Callback([=](TextButton*){ - alert->close(); - AlertLayer::create("Woah hello ;)", "apparently you can do this now. its pretty cool!", "Close", "My mood rn", NULL, [=](TextButton*) { - Application::getInstance()->openURL("https://www.youtube.com/watch?v=XSsRrlM3tNg"); - })->show(); - }); - alert->show(); - }); - - auto bottomMenu = Menu::create(achievementsBtn, optionsBtn, statsBtn, nullptr); - - bottomMenu->setPositionY(90); - //bottomMenu->setPositionY(100); - bottomMenu->alignItemsHorizontallyWithPadding(10); - - this->addChild(bottomMenu); - - auto moreGamesBtn = MenuItemSpriteExtra::create("GJ_moreGamesBtn_001.png", [&](Node* btn) { - //auto a = GJMoreGamesLayer::create(); - //addChild(a); - }); - otherMenu->addChild(moreGamesBtn); - //moreGamesBtn->setPosition(menu->convertToNodeSpace({winSize.width - 86, 90})); - moreGamesBtn->setPosition(otherMenu->convertToNodeSpace({winSize.width - 175, 130})); - moreGamesBtn->setScale(1.f); - - // To make it close to GD - otherMenu->setScale(offsetScale); - - - otherMenu->setPosition({(winSize.width / 2) + 82, (winSize.height / 2) + 47}); - - return true; +#include "MenuLayer.h" +/* +#include "GarageLayer.h" +*/ + +#include "MenuGameLayer.h" +#include "CreatorLayer.h" +#include "AlertLayer.h" +#include "AudioEngine.h" +#include "MenuItemSpriteExtra.h" +#include "GameToolbox.h" +#include "PlayLayer.h" + +/* +#include "ColoursPalette.h" +#include "ListLayer.h" +#include "GJMoreGamesLayer.h" +*/ +USING_NS_AX; + +bool music = true; + +Scene* MenuLayer::scene() { + auto scene = Scene::create(); + scene->addChild(MenuLayer::create()); + return scene; +} + +bool MenuLayer::init(){ + if (!Layer::init()) return false; + + if(music){ + AudioEngine::play2d("audiotracks/menuLoop.mp3", true, 0.5f); + music = false; + } + addChild(MenuGameLayer::create(), -1); + + float offsetScale = 1.13F; + auto winSize = Director::getInstance()->getWinSize(); + + auto log_oSpr = Sprite::createWithSpriteFrameName("GJ_logo_001.png"); + //log_oSpr->setPosition({ winSize.width / 2, winSize.height - 100 }); + log_oSpr->setPosition({ winSize.width / 2, winSize.height - 110 }); + this->addChild(log_oSpr); + auto playBtn = MenuItemSpriteExtra::create("GJ_playBtn_001.png", [&](Node* btn) { + auto scene = PlayLayer::scene(new GJGameLevel("My awesome level", "MikaKC", 1)); + Director::getInstance()->pushScene(TransitionFade::create(0.5f, scene)); + }); + auto garageBtn = MenuItemSpriteExtra::create("GJ_garageBtn_001.png", [&](Node* btn) { + //Director::getInstance()->replaceScene(TransitionFade::create(0.5f, GarageLayer::scene())); + }); + + auto creatorBtn = MenuItemSpriteExtra::create("GJ_creatorBtn_001.png", [&](Node* btn) { + Director::getInstance()->replaceScene(TransitionFade::create(0.5f, CreatorLayer::scene())); + }); + + auto mainButtonMenu = Menu::create(garageBtn, playBtn, creatorBtn, nullptr); + + mainButtonMenu->setPositionY(mainButtonMenu->getPositionY() -10); + mainButtonMenu->alignItemsHorizontallyWithPadding(50); + addChild(mainButtonMenu); + + + auto robBtn = MenuItemSpriteExtra::create("robtoplogo_small.png", [&](Node* btn) { + Application::getInstance()->openURL("http://www.robtopgames.com"); + }); + + auto otherMenu = Menu::create(); + otherMenu->addChild(robBtn); + addChild(otherMenu); + + robBtn->setPosition(otherMenu->convertToNodeSpace({190, 90})); + robBtn->setScale(0.9f); + + auto achievementsBtn = MenuItemSpriteExtra::create("GJ_achBtn_001.png", [&](Node* btn) { + AlertLayer::create("coming soon", "this feature has not been added yet!")->show(); + }); + achievementsBtn->setScale(1.f); + + auto optionsBtn = MenuItemSpriteExtra::create("GJ_optionsBtn_001.png", [&](Node* btn) { + + }); + + auto statsBtn = MenuItemSpriteExtra::create("GJ_statsBtn_001.png", [&](Node* btn) { + auto alert = AlertLayer::create("WIP!", "This feature is not yet supported!", "Close", "Click me!", NULL, NULL); + alert->setBtn2Callback([=](TextButton*){ + alert->close(); + AlertLayer::create("Woah hello ;)", "apparently you can do this now. its pretty cool!", "Close", "My mood rn", NULL, [=](TextButton*) { + Application::getInstance()->openURL("https://www.youtube.com/watch?v=XSsRrlM3tNg"); + })->show(); + }); + alert->show(); + }); + + auto bottomMenu = Menu::create(achievementsBtn, optionsBtn, statsBtn, nullptr); + + bottomMenu->setPositionY(90); + //bottomMenu->setPositionY(100); + bottomMenu->alignItemsHorizontallyWithPadding(10); + + this->addChild(bottomMenu); + + auto moreGamesBtn = MenuItemSpriteExtra::create("GJ_moreGamesBtn_001.png", [&](Node* btn) { + //auto a = GJMoreGamesLayer::create(); + //addChild(a); + }); + otherMenu->addChild(moreGamesBtn); + //moreGamesBtn->setPosition(menu->convertToNodeSpace({winSize.width - 86, 90})); + moreGamesBtn->setPosition(otherMenu->convertToNodeSpace({winSize.width - 175, 130})); + moreGamesBtn->setScale(1.f); + + // To make it close to GD + otherMenu->setScale(offsetScale); + + + otherMenu->setPosition({(winSize.width / 2) + 82, (winSize.height / 2) + 47}); + + return true; } \ No newline at end of file diff --git a/Classes/MenuLayer.h b/Classes/MenuLayer.h index 067cd99..310a0f1 100644 --- a/Classes/MenuLayer.h +++ b/Classes/MenuLayer.h @@ -1,9 +1,9 @@ -#pragma once - -#include -class MenuLayer : public ax::Layer { -public: - static ax::Scene* scene(); - bool init(); - CREATE_FUNC(MenuLayer); +#pragma once + +#include +class MenuLayer : public ax::Layer { +public: + static ax::Scene* scene(); + bool init(); + CREATE_FUNC(MenuLayer); }; \ No newline at end of file diff --git a/Classes/PlayLayer.cpp b/Classes/PlayLayer.cpp index db2f126..f340b2b 100644 --- a/Classes/PlayLayer.cpp +++ b/Classes/PlayLayer.cpp @@ -1,125 +1,165 @@ -#include "PlayLayer.h" -#include "GameToolbox.h" -#include "MenuItemSpriteExtra.h" - -USING_NS_AX; - -Scene* PlayLayer::scene() { - auto scene = Scene::create(); - scene->addChild(PlayLayer::create()); - return scene; -} - -bool PlayLayer::init(){ - if (!Layer::init()) return false; - - auto winSize = Director::getInstance()->getWinSize(); - - this->m_pGround = GroundLayer::create(1); - this->addChild(this->m_pGround); - this->m_pGround->setSpeed(623); - - //temp back button - auto backbtn = MenuItemSpriteExtra::create("GJ_arrow_01_001.png", [&](Node* btn) { - Director::getInstance()->replaceScene(TransitionFade::create(0.5f, MenuLayer::scene())); - }); - auto menu = Menu::create(); - menu->addChild(backbtn); - menu->setPosition({50, winSize.height - 50}); - addChild(menu, 99999); - - this->m_pBG = Sprite::create(GameToolbox::getTextureString("game_bg_01_001.png")); - const Texture2D::TexParams texParams = { - backend::SamplerFilter::LINEAR, - backend::SamplerFilter::LINEAR, - backend::SamplerAddressMode::REPEAT, - backend::SamplerAddressMode::REPEAT - }; - this->m_pBG->getTexture()->setTexParameters(texParams); - this->m_pBG->setTextureRect(Rect(0, 0, 1024 * 5, 1024)); - this->m_pBG->setPosition(winSize / 2); - this->m_pBG->setColor({0, 102, 255}); - this->addChild(this->m_pBG, -1); - - this->m_pPlayer = PlayerObject::create(GameToolbox::randomInt(1, 12), this); - this->m_pPlayer->setPosition({0, 236}); - this->addChild(this->m_pPlayer); - this->m_pPlayer->setAnchorPoint({0, 0}); - - this->scheduleUpdate(); - - return true; -} - -void PlayLayer::update(float dt) { - float step = std::min(2.0f, dt * 60.0f); - - auto winSize = Director::getInstance()->getWinSize(); - - if (!this->m_pPlayer->isDead()) { - step /= 4.0f; - for (int i = 0; i < 4; i++) { - this->m_pPlayer->update(step); - - if (this->m_pPlayer->isDead()) - break; - } - } - - this->m_pBG->setPositionX(this->m_pBG->getPositionX() - dt * 62); - if (this->m_pBG->getPositionX() <= -1024) - this->m_pBG->setPositionX(this->m_pBG->getPositionX() + 1024); - - this->updateCamera(dt); -} - -void PlayLayer::updateCamera(float dt) { - auto winSize = Director::getInstance()->getWinSize(); - auto cam = m_obCamPos; - - auto player = this->m_pPlayer; - - float unk4; - - if (player->getPositionY() <= cam.y + winSize.height - 180) { - if (player->getPosition().y < cam.y + 240) - unk4 = player->getPosition().y - 240; - } else { - unk4 = player->getPosition().y - winSize.height + 180; - } - - if (player->getLastGroundPos().y == 236 && player->getPositionY() <= cam.y + winSize.height - 180) - unk4 = 0; - - cam.y += (unk4 - cam.y) / 20; - - if (cam.y < 0) - cam.y = 0; - - // if (cam.y - winSize.height) - // cam.y -= winSize.height; - - if (this->m_bFirstAttempt) - { - if (cam.x <= 30.0f) - cam.x = 30.0f; - } - - float temp = this->m_fEndOfLevel - winSize.width; - if (this->m_fEndOfLevel > 0.0f) - { - if (cam.x >= temp) - cam.x = temp; - } - - if (player->getPositionX() >= winSize.width / 2) { // wrong but works for now - cam.x += static_cast(dt * 60 * .9 * 5.77); - } - - this->m_pGround->setPositionX(this->m_pGround->getPositionX() + (cam.x - m_obCamPos.x)); - this->m_pBG->setPositionX(this->m_pBG->getPositionX() + (cam.x - m_obCamPos.x)); - - this->m_obCamPos = cam; - GameToolbox::log("camPosX: {}, camPosY: {}", m_obCamPos.x, m_obCamPos.y); - Camera::getDefaultCamera()->setPosition(this->m_obCamPos + winSize / 2); +#include "PlayLayer.h" +#include "GameToolbox.h" +#include "MenuItemSpriteExtra.h" + +USING_NS_AX; + +Scene* PlayLayer::scene(GJGameLevel* level) { + auto scene = Scene::create(); + scene->addChild(PlayLayer::create(level)); + return scene; +} + +bool PlayLayer::init(GJGameLevel* level) { + if (!Layer::init()) return false; + auto winSize = Director::getInstance()->getWinSize(); + + this->m_pGround = GroundLayer::create(1); + this->addChild(this->m_pGround); + this->m_pGround->setSpeed(623); + + //temp back button + auto backbtn = MenuItemSpriteExtra::create("GJ_arrow_01_001.png", [&](Node* btn) { + Director::getInstance()->replaceScene(TransitionFade::create(0.5f, MenuLayer::scene())); + }); + auto menu = Menu::create(); + menu->addChild(backbtn); + menu->setPosition({ 50, winSize.height - 50 }); + addChild(menu, 99999); + + this->m_pBG = Sprite::create(GameToolbox::getTextureString("game_bg_01_001.png")); + const Texture2D::TexParams texParams = { + backend::SamplerFilter::LINEAR, + backend::SamplerFilter::LINEAR, + backend::SamplerAddressMode::REPEAT, + backend::SamplerAddressMode::REPEAT + }; + this->m_pBG->getTexture()->setTexParameters(texParams); + this->m_pBG->setTextureRect(Rect(0, 0, 1024 * 5, 1024)); + this->m_pBG->setPosition(winSize / 2); + this->m_pBG->setColor({ 0, 102, 255 }); + this->addChild(this->m_pBG, -1); + + + auto testObject = GameObject::create("square_01_001.png"); + testObject->setPosition(winSize.width / 2, 237); + _pObjects = { testObject }; + + for(GameObject* object : _pObjects) + { + object->setOuterBounds(Rect(object->getPosition(), Vec2(60, 60))); + object->setInnerBounds(Rect(object->getPosition(), Vec2(60, 60))); + + this->addChild(object); + } + + this->m_pPlayer = PlayerObject::create(GameToolbox::randomInt(1, 12), this); + this->m_pPlayer->setPosition({ 0, 236 }); + this->addChild(this->m_pPlayer); + this->m_pPlayer->setAnchorPoint({ 0, 0 }); + + m_pPlayer->setOuterBounds(Rect(m_pPlayer->getPosition(), { 30, 30 })); + m_pPlayer->setInnerBounds(Rect(m_pPlayer->getPosition(), { 30, 30 })); + + this->scheduleUpdate(); + + return true; +} + +void PlayLayer::update(float dt) { + float step = std::min(2.0f, dt * 60.0f); + + m_pPlayer->setOuterBounds(Rect(m_pPlayer->getPosition(), { 60, 60 })); + m_pPlayer->setInnerBounds(Rect(m_pPlayer->getPosition() + Vec2(30, 30), {15, 15})); + + auto winSize = Director::getInstance()->getWinSize(); + + if (!this->m_pPlayer->isDead()) { + step /= 4.0f; + for (int i = 0; i < 4; i++) { + this->m_pPlayer->update(step); + + if (this->m_pPlayer->isDead()) + break; + } + } + + this->m_pBG->setPositionX(this->m_pBG->getPositionX() - dt * 62); + if (this->m_pBG->getPositionX() <= -1024) + this->m_pBG->setPositionX(this->m_pBG->getPositionX() + 1024); + + this->checkCollisions(dt); + this->updateCamera(dt); +} + +void PlayLayer::updateCamera(float dt) { + auto winSize = Director::getInstance()->getWinSize(); + auto cam = m_obCamPos; + + auto player = this->m_pPlayer; + + float unk4 = 0; + + if (player->getPositionY() <= cam.y + winSize.height - 180) { + if (player->getPosition().y < cam.y + 240) + unk4 = player->getPosition().y - 240; + } + else { + unk4 = player->getPosition().y - winSize.height + 180; + } + + if (player->getLastGroundPos().y == 236 && player->getPositionY() <= cam.y + winSize.height - 180) + unk4 = 0; + + cam.y += (unk4 - cam.y) / 20; + + if (cam.y < 0) + cam.y = 0; + + // if (cam.y - winSize.height) + // cam.y -= winSize.height; + + if (this->m_bFirstAttempt) + { + if (cam.x <= 30.0f) + cam.x = 30.0f; + } + + float temp = this->m_fEndOfLevel - winSize.width; + if (this->m_fEndOfLevel > 0.0f) + { + if (cam.x >= temp) + cam.x = temp; + } + + if (player->getPositionX() >= winSize.width / 2) { // wrong but works for now + cam.x += static_cast(dt * 60 * .9 * 5.77); + } + + this->m_pGround->setPositionX(this->m_pGround->getPositionX() + (cam.x - m_obCamPos.x)); + this->m_pBG->setPositionX(this->m_pBG->getPositionX() + (cam.x - m_obCamPos.x)); + + this->m_obCamPos = cam; + GameToolbox::log("camPosX: {}, camPosY: {}", m_obCamPos.x, m_obCamPos.y); + Camera::getDefaultCamera()->setPosition(this->m_obCamPos + winSize / 2); +} + +void PlayLayer::checkCollisions(float dt) +{ + unsigned int childCount = _pObjects.size(); + + for (unsigned int i = 0; i < childCount; i++) + { + auto object = _pObjects[i]; + + if (object->getGameObjectType() == GameObjectType::kObjectTypePlayer) return; + + + if (object->getGameObjectType() == GameObjectType::kObjectTypeTile) + { + this->m_pPlayer->setDead(object->getOuterBounds().intersectsRect(m_pPlayer->getInnerBounds())); + m_pPlayer->setOnGround(object->getOuterBounds().intersectsRect(m_pPlayer->getOuterBounds())); + + } + } } \ No newline at end of file diff --git a/Classes/PlayLayer.h b/Classes/PlayLayer.h index dd2a2f7..fdd0ff4 100644 --- a/Classes/PlayLayer.h +++ b/Classes/PlayLayer.h @@ -1,31 +1,76 @@ -#pragma once - -#include - -#include "PlayerObject.h" -#include "GroundLayer.h" -#include "MenuLayer.h" - -class PlayLayer : public ax::Layer { -private: - bool init(); - - ax::Sprite* m_pBG; - GroundLayer* m_pGround; - PlayerObject* m_pPlayer; - ax::Vec2 m_obCamPos; - - float m_fCameraYCenter; - bool m_bFirstAttempt = true; - bool m_bMoveCameraX; - bool m_bMoveCameraY; - bool m_bShakingCamera; - float m_fEndOfLevel = 2000; - float m_fShakeIntensity = 1; - -public: - void update(float delta); - void updateCamera(float dt); - static ax::Scene* scene(); - CREATE_FUNC(PlayLayer); -}; \ No newline at end of file +#pragma once + +#include + +#include "PlayerObject.h" +#include "GroundLayer.h" +#include "MenuLayer.h" + +// temporary +struct GJGameLevel +{ + std::string _pLevelName; + std::string _pLevelCreator; + std::string _pLevelData; + unsigned int _pLevelID; + + GJGameLevel() + : _pLevelName(""), + _pLevelCreator(""), + _pLevelData(""), + _pLevelID(1) + { + + } + + GJGameLevel(std::string_view levelName, std::string_view levelCreator, unsigned int levelID) + : _pLevelName(levelName), + _pLevelCreator(levelCreator), + _pLevelData(""), + _pLevelID(levelID) + { + + } +}; + +class PlayLayer : public ax::Layer { +private: + bool init(GJGameLevel* level); + + ax::Sprite* m_pBG; + GroundLayer* m_pGround; + PlayerObject* m_pPlayer; + ax::Vec2 m_obCamPos; + + std::vector _pObjects; + + float m_fCameraYCenter; + bool m_bFirstAttempt = true; + bool m_bMoveCameraX; + bool m_bMoveCameraY; + bool m_bShakingCamera; + float m_fEndOfLevel = INT_MAX; + float m_fShakeIntensity = 1; + +public: + void update(float delta); + void updateCamera(float dt); + + // dt? + void checkCollisions(float dtMaybe); + + static ax::Scene* scene(GJGameLevel* level); + static PlayLayer* create(GJGameLevel* level) + { + auto ret = new (std::nothrow) PlayLayer(); + if(ret && ret->init(level)) + { + ret->autorelease(); + return ret; + } + + AX_SAFE_DELETE(ret); + return nullptr; + } +}; + diff --git a/Classes/PlayerObject.cpp b/Classes/PlayerObject.cpp index ed45393..768e6f0 100644 --- a/Classes/PlayerObject.cpp +++ b/Classes/PlayerObject.cpp @@ -1,276 +1,300 @@ -#include "PlayerObject.h" -#include "GameToolbox.h" - -USING_NS_AX; - -bool PlayerObject::init(int playerFrame, Layer* gameLayer_) { - // cap the icon limit - int frame = GameToolbox::inRange(playerFrame, 1, 13); - - auto sprStr1 = StringUtils::format("player_%02d_001.png", frame); - auto sprStr2 = StringUtils::format("player_%02d_2_001.png", frame); - GameToolbox::log("1: {}, 2: {}", sprStr1, sprStr2); - - // initialize - if(!GameObject::init(sprStr1)) return false; - - if(gameLayer_ != nullptr) { - gameLayer = gameLayer_; - inPlayLayer = false; - } - else { - // to do after game manager - //this->gameLayer = ; - inPlayLayer = true; - } - - setTextureRect(Rect(0, 0, 60, 60)); // player hitbox lol - - m_pMainSprite = Sprite::createWithSpriteFrameName(sprStr1); - addChild(m_pMainSprite, 1); - - m_pSecondarySprite = Sprite::createWithSpriteFrameName(sprStr2); - - m_pMainSprite->addChild(m_pSecondarySprite, -1); - //secondarySprite->setPosition(mainSprite->convertToNodeSpace(Vec2(0, 0))); // this shit DONT WORK!! cuz rob made it a global var - //secondarySprite->setPosition(mainSprite->convertToNodeSpace(Vec2(15, 15))); - m_pSecondarySprite->setPosition(Vec2(30, 30)); - - m_pShipSprite = Sprite::createWithSpriteFrameName("ship_01_001.png"); - m_pShipSprite->setVisible(false); - addChild(m_pShipSprite, 2); - - // particles - dragEffect1 = ParticleSystemQuad::create("dragEffect.plist"); - dragEffect1->setPositionType(ParticleSystem::PositionType::FREE); - dragEffect1->stopSystem(); - - gameLayer->addChild(dragEffect1, 1); - - dragEffect2 = ParticleSystemQuad::create("dragEffect.plist"); - dragEffect2->setPositionType(ParticleSystem::PositionType::FREE); - dragEffect2->stopSystem(); - dragEffect2->setPositionY(2); - - gameLayer->addChild(dragEffect2, 1); - - dragEffect3 = ParticleSystemQuad::create("dragEffect.plist"); - dragEffect3->setPositionType(ParticleSystem::PositionType::FREE); - dragEffect3->stopSystem(); - dragEffect3->setPositionY(2); - - gameLayer->addChild(dragEffect3, 1); - - // particle properties - dragEffect2->setSpeed(dragEffect2->getSpeed() * 0.2f); - dragEffect2->setSpeedVar(dragEffect2->getSpeedVar() * 0.2f); - - dragEffect3->setSpeed(dragEffect3->getSpeed() * 0.2f); - dragEffect3->setSpeedVar(dragEffect3->getSpeedVar() * 0.2f); - dragEffect3->setAngleVar(dragEffect3->getAngleVar() * 2.f); - dragEffect3->setStartSize(dragEffect3->getStartSize() * 1.5f); - dragEffect3->setStartSizeVar(dragEffect3->getStartSizeVar() * 1.5f); - - dragEffect2->setStartColor({255, 255, 255, 100}); - dragEffect2->setEndColor({255, 255, 255, 0}); - - dragEffect3->setStartColor({255, 255, 255, 190}); - dragEffect3->setEndColor({255, 255, 255, 0}); - - // other particles - shipDragEffect = ParticleSystemQuad::create("shipDragEffect.plist"); - shipDragEffect->setPositionType(ParticleSystem::PositionType::GROUPED); - shipDragEffect->stopSystem(); - - gameLayer->addChild(shipDragEffect, 1); - - landEffect1 = ParticleSystemQuad::create("landEffect.plist"); - landEffect1->setPositionType(ParticleSystem::PositionType::GROUPED); - landEffect1->stopSystem(); - - gameLayer->addChild(landEffect1, 1); - - landEffect2 = ParticleSystemQuad::create("landEffect.plist"); - landEffect2->setPositionType(ParticleSystem::PositionType::GROUPED); - landEffect2->stopSystem(); - - gameLayer->addChild(landEffect2, 1); - - // streak - motionStreak = MotionStreak::create(0.3f, 3, 10, {255, 255, 255}, "streak.png"); - motionStreak->setBlendFunc(BlendFunc::ADDITIVE); - - gameLayer->addChild(motionStreak); - - motionStreak->setStartingPositionInitialized(false); - - // scheduleUpdate(); - - auto dir = Director::getInstance(); - auto listener = EventListenerTouchOneByOne::create(); - - listener->setEnabled(true); - listener->setSwallowTouches(true); - - // trigger when you start touch - listener->onTouchBegan = AX_CALLBACK_2(PlayerObject::onTouchBegan, this); - - dir->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); - - return true; -} - -void PlayerObject::setMainColor(Color3B col) { - this->m_pMainSprite->setColor(col); -} - -void PlayerObject::setSecondaryColor(Color3B col) { - this->m_pSecondarySprite->setColor(col); -} - -void PlayerObject::setShipColor(Color3B col) { - this->m_pShipSprite->setColor(col); -} - -Color3B PlayerObject::getMainColor() { - return this->m_pMainSprite->getColor(); -} - -Color3B PlayerObject::getSecondaryColor() { - return this->m_pSecondarySprite->getColor(); -} - -Color3B PlayerObject::getShipColor() { - return this->m_pShipSprite->getColor(); -} - -void PlayerObject::update(float dt) { - // dt *= 60; // rob :skull: - - if (this->m_bIsDead) - return; - - if (this->getPositionY() <= 236) { // TEMP ON GROUND CHECK - this->m_bOnGround = true; - this->stopRotation(); - this->setPositionY(236); - this->m_obLastGroundPos = this->getPosition(); - } - - if (this->getPositionX() >= 500) { - this->m_bIsHolding = true; - } - - if (!this->m_bIsLocked) { - this->updateJump(dt * 0.9f); - - this->setPosition(this->getPosition() + Vec2(static_cast(dt * this->m_fSpeed * this->m_dXVel), static_cast(dt * this->m_fSpeed * this->m_dYVel))); - } - - // if (!this->m_bFlyMode) - // this->motionStreak->setPosition(this->getPosition() + ccp({-10, 0})); - - // auto particle = Sprite::create("square.png"); - // particle->setScale(0.05); - // particle->setPosition(this->getPosition()); - // this->gameLayer->addChild(particle, 999); -} - -void PlayerObject::updateJump(float dt) { - - if (this->m_bIsHolding && this->m_bOnGround) { - this->m_bOnGround = false; - this->m_dYVel = this->m_dJumpHeight; // 0.43 - this->runRotateAction(); - return; - } - - if (!this->m_bOnGround) - this->m_dYVel -= this->m_dGravity * dt; -} - -bool PlayerObject::isFlying() { - return this->m_bFlying; -} - -bool PlayerObject::isUpsideDown() { - return this->m_bUpsideDown; -} - -bool PlayerObject::isDead() { - return this->m_bIsDead; -} - -ax::Vec2 PlayerObject::getLastGroundPos() { - return this->m_obLastGroundPos; -} - -void PlayerObject::logValues() { - GameToolbox::log("xVel: {} | yVel: {} | gravity: {} | jumpHeight: {} ", m_dXVel, m_dYVel, m_dGravity, m_dJumpHeight); -} - -void PlayerObject::runRotateAction() { - this->stopRotation(); - auto action = RotateBy::create(0.43333f, 180); - action->setTag(0); - this->runAction(action); -} - -void PlayerObject::stopRotation() { - this->stopActionByTag(0); - - if (this->getRotation() != 0) { - auto degrees = (int)this->getRotation() % 360; - this->setRotation(90 * roundf(degrees / 90.0f)); - } -} - -void PlayerObject::jump() { - this->m_dYVel = this->m_dJumpHeight; - } - -// void PlayerObject::jump() { - // this->runAction( - // Sequence::create( - // Spawn::create( - // Sequence::create( - // RotateBy::create(0.15, 80.f), - // RotateBy::create(0.025, 0.f), - // RotateBy::create(0.225, 80.f), - // RotateBy::create(0, -160), - // nullptr - // ), - // Sequence::create( - // //MoveBy::create(0.025, { 0, 80.f }), - // //MoveBy::create(0.15, { 0, 50.f }), - // //MoveBy::create(0.05, { 0.f, 0.f }), - // //MoveBy::create(0.15, { 0, -80.f }), - // //MoveBy::create(0.025, { 0, -50.f }), - // MoveBy::create(0.2, {0, 130.f}), - // MoveBy::create(0.2, { 0, -130.f }), - // nullptr - // ), - // nullptr - // ), - // nullptr - // ) - // ); -// } -bool PlayerObject::onTouchBegan(ax::Touch* touch, ax::Event* event) -{ - if (this->inPlayLayer) { - GameToolbox::log("Touch began."); - return true; - } - return false; -} -PlayerObject* PlayerObject::create(int playerFrame, Layer* gameLayer) { - auto pRet = new (std::nothrow) PlayerObject(); - - if(pRet && pRet->init(playerFrame, gameLayer)) { - pRet->autorelease(); - return pRet; - } - AX_SAFE_DELETE(pRet); - return pRet; +#include "PlayerObject.h" +#include "GameToolbox.h" +#include "PlayLayer.h" + +USING_NS_AX; + +bool PlayerObject::init(int playerFrame, Layer* gameLayer_) { + // cap the icon limit + int frame = GameToolbox::inRange(playerFrame, 1, 13); + + auto sprStr1 = StringUtils::format("player_%02d_001.png", frame); + auto sprStr2 = StringUtils::format("player_%02d_2_001.png", frame); + GameToolbox::log("1: {}, 2: {}", sprStr1, sprStr2); + + // initialize + if (!GameObject::init(sprStr1)) return false; + + gameLayer = gameLayer_; + + // Check if layer is playlayer + if (dynamic_cast(gameLayer_) == nullptr) { + inPlayLayer = false; + } + else { + inPlayLayer = true; + } + + setTextureRect(Rect(0, 0, 60, 60)); // player hitbox lol + + m_pMainSprite = Sprite::createWithSpriteFrameName(sprStr1); + addChild(m_pMainSprite, 1); + + m_pSecondarySprite = Sprite::createWithSpriteFrameName(sprStr2); + + m_pMainSprite->addChild(m_pSecondarySprite, -1); + //secondarySprite->setPosition(mainSprite->convertToNodeSpace(Vec2(0, 0))); // this shit DONT WORK!! cuz rob made it a global var + //secondarySprite->setPosition(mainSprite->convertToNodeSpace(Vec2(15, 15))); + m_pSecondarySprite->setPosition(Vec2(30, 30)); + + m_pShipSprite = Sprite::createWithSpriteFrameName("ship_01_001.png"); + m_pShipSprite->setVisible(false); + addChild(m_pShipSprite, 2); + + // particles + dragEffect1 = ParticleSystemQuad::create("dragEffect.plist"); + dragEffect1->setPositionType(ParticleSystem::PositionType::FREE); + dragEffect1->stopSystem(); + + gameLayer->addChild(dragEffect1, 1); + + dragEffect2 = ParticleSystemQuad::create("dragEffect.plist"); + dragEffect2->setPositionType(ParticleSystem::PositionType::FREE); + dragEffect2->stopSystem(); + dragEffect2->setPositionY(2); + + gameLayer->addChild(dragEffect2, 1); + + dragEffect3 = ParticleSystemQuad::create("dragEffect.plist"); + dragEffect3->setPositionType(ParticleSystem::PositionType::FREE); + dragEffect3->stopSystem(); + dragEffect3->setPositionY(2); + + gameLayer->addChild(dragEffect3, 1); + + // particle properties + dragEffect2->setSpeed(dragEffect2->getSpeed() * 0.2f); + dragEffect2->setSpeedVar(dragEffect2->getSpeedVar() * 0.2f); + + dragEffect3->setSpeed(dragEffect3->getSpeed() * 0.2f); + dragEffect3->setSpeedVar(dragEffect3->getSpeedVar() * 0.2f); + dragEffect3->setAngleVar(dragEffect3->getAngleVar() * 2.f); + dragEffect3->setStartSize(dragEffect3->getStartSize() * 1.5f); + dragEffect3->setStartSizeVar(dragEffect3->getStartSizeVar() * 1.5f); + + dragEffect2->setStartColor({ 255, 255, 255, 100 }); + dragEffect2->setEndColor({ 255, 255, 255, 0 }); + + dragEffect3->setStartColor({ 255, 255, 255, 190 }); + dragEffect3->setEndColor({ 255, 255, 255, 0 }); + + // other particles + shipDragEffect = ParticleSystemQuad::create("shipDragEffect.plist"); + shipDragEffect->setPositionType(ParticleSystem::PositionType::GROUPED); + shipDragEffect->stopSystem(); + + gameLayer->addChild(shipDragEffect, 1); + + landEffect1 = ParticleSystemQuad::create("landEffect.plist"); + landEffect1->setPositionType(ParticleSystem::PositionType::GROUPED); + landEffect1->stopSystem(); + + gameLayer->addChild(landEffect1, 1); + + landEffect2 = ParticleSystemQuad::create("landEffect.plist"); + landEffect2->setPositionType(ParticleSystem::PositionType::GROUPED); + landEffect2->stopSystem(); + + gameLayer->addChild(landEffect2, 1); + + // streak + motionStreak = MotionStreak::create(0.3f, 3, 10, { 255, 255, 255 }, "streak.png"); + motionStreak->setBlendFunc(BlendFunc::ADDITIVE); + + gameLayer->addChild(motionStreak); + + motionStreak->setStartingPositionInitialized(false); + + // scheduleUpdate(); + + auto dir = Director::getInstance(); + auto listener = EventListenerTouchOneByOne::create(); + + listener->setEnabled(true); + listener->setSwallowTouches(true); + + // trigger when you start touch + listener->onTouchBegan = AX_CALLBACK_2(PlayerObject::onTouchBegan, this); + listener->onTouchEnded = AX_CALLBACK_2(PlayerObject::onTouchEnded, this); + + dir->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); + + return true; +} + +void PlayerObject::setMainColor(Color3B col) { + this->m_pMainSprite->setColor(col); +} + +void PlayerObject::setSecondaryColor(Color3B col) { + this->m_pSecondarySprite->setColor(col); +} + +void PlayerObject::setShipColor(Color3B col) { + this->m_pShipSprite->setColor(col); +} + +Color3B PlayerObject::getMainColor() { + return this->m_pMainSprite->getColor(); +} + +Color3B PlayerObject::getSecondaryColor() { + return this->m_pSecondarySprite->getColor(); +} + +Color3B PlayerObject::getShipColor() { + return this->m_pShipSprite->getColor(); +} + +void PlayerObject::update(float dt) { + // dt *= 60; // rob :skull: + + if (this->m_bIsDead) + return; + + if (this->getPositionY() <= 236) { // TEMP ON GROUND CHECK + this->m_bOnGround = true; + this->stopRotation(); + this->m_dYVel = 0.f; + this->m_obLastGroundPos = this->getPosition(); + } + + if (this->getPositionX() >= 500 && !this->inPlayLayer) { + this->m_bIsHolding = true; + } + + if (!this->m_bIsLocked) { + this->updateJump(dt * 0.9f); + + this->setPosition(this->getPosition() + Vec2(static_cast(dt * this->m_fSpeed * this->m_dXVel), static_cast(dt * this->m_fSpeed * this->m_dYVel))); + } + + // if (!this->m_bFlyMode) + // this->motionStreak->setPosition(this->getPosition() + ccp({-10, 0})); + + // auto particle = Sprite::create("square.png"); + // particle->setScale(0.05); + // particle->setPosition(this->getPosition()); + // this->gameLayer->addChild(particle, 999); +} + +void PlayerObject::updateJump(float dt) { + + if (this->m_bIsHolding && this->m_bOnGround) { + this->m_bOnGround = false; + + this->jump(); + this->runRotateAction(); + + return; + } + + if (!this->m_bOnGround) + { + this->m_dYVel -= this->m_dGravity * dt; + } + else { + this->stopRotation(); + this->m_dYVel = 0; + } +} + +bool PlayerObject::isFlying() { + return this->m_bFlying; +} + +bool PlayerObject::isUpsideDown() { + return this->m_bUpsideDown; +} + +bool PlayerObject::isDead() { + return this->m_bIsDead; +} + +bool PlayerObject::isOnGround() +{ + return this->m_bOnGround; +} + +ax::Vec2 PlayerObject::getLastGroundPos() { + return this->m_obLastGroundPos; +} + +void PlayerObject::logValues() { + GameToolbox::log("xVel: {} | yVel: {} | gravity: {} | jumpHeight: {} ", m_dXVel, m_dYVel, m_dGravity, m_dJumpHeight); +} + +void PlayerObject::runRotateAction() { + this->stopRotation(); + auto action = RotateBy::create(0.43333f, 180); + action->setTag(0); + this->runAction(action); +} + +void PlayerObject::stopRotation() { + this->stopActionByTag(0); + + if (this->getRotation() != 0) { + auto degrees = (int)this->getRotation() % 360; + this->setRotation(90 * roundf(degrees / 90.0f)); + } +} + +void PlayerObject::jump() { + this->m_dYVel = this->m_dJumpHeight; +} + +// void PlayerObject::jump() { + // this->runAction( + // Sequence::create( + // Spawn::create( + // Sequence::create( + // RotateBy::create(0.15, 80.f), + // RotateBy::create(0.025, 0.f), + // RotateBy::create(0.225, 80.f), + // RotateBy::create(0, -160), + // nullptr + // ), + // Sequence::create( + // //MoveBy::create(0.025, { 0, 80.f }), + // //MoveBy::create(0.15, { 0, 50.f }), + // //MoveBy::create(0.05, { 0.f, 0.f }), + // //MoveBy::create(0.15, { 0, -80.f }), + // //MoveBy::create(0.025, { 0, -50.f }), + // MoveBy::create(0.2, {0, 130.f}), + // MoveBy::create(0.2, { 0, -130.f }), + // nullptr + // ), + // nullptr + // ), + // nullptr + // ) + // ); +// } +bool PlayerObject::onTouchBegan(ax::Touch* touch, ax::Event* event) +{ + if (this->inPlayLayer) { + m_bIsHolding = true; + return true; + } + return false; +} + +void PlayerObject::onTouchEnded(ax::Touch* touch, ax::Event* event) +{ + if (this->inPlayLayer) + { + m_bIsHolding = false; + } +} + +PlayerObject* PlayerObject::create(int playerFrame, Layer* gameLayer) { + auto pRet = new (std::nothrow) PlayerObject(); + + if (pRet && pRet->init(playerFrame, gameLayer)) { + pRet->autorelease(); + return pRet; + } + AX_SAFE_DELETE(pRet); + return pRet; } \ No newline at end of file diff --git a/Classes/PlayerObject.h b/Classes/PlayerObject.h index 9a0c929..6d3570b 100644 --- a/Classes/PlayerObject.h +++ b/Classes/PlayerObject.h @@ -1,71 +1,78 @@ -#pragma once -#include -#include "GameObject.h" -#include "CircleWave.h" - -class PlayerObject : public GameObject { -private: - void updateJump(float dt); - bool init(int, ax::Layer*); - void runRotateAction(); - void stopRotation(); - - void logValues(); - - ax::Layer* gameLayer; - bool inPlayLayer; - - ax::Sprite* m_pMainSprite; - ax::Sprite* m_pSecondarySprite; - ax::Sprite* m_pShipSprite; - - ax::ParticleSystemQuad* dragEffect1; - ax::ParticleSystemQuad* dragEffect2; - ax::ParticleSystemQuad* dragEffect3; - ax::ParticleSystemQuad* shipDragEffect; - ax::ParticleSystemQuad* landEffect1; - ax::ParticleSystemQuad* landEffect2; - - ax::MotionStreak* motionStreak; - - double m_dXVel = 5.770002; - double m_dYVel = 0; - double m_dGravity = 0.958199; - double m_dJumpHeight = 11.180032; - - bool m_bUpsideDown; - bool m_bOnGround; - - float m_fSpeed = 0.9f; - - bool m_bIsDead; - bool m_bIsLocked; - - bool m_bIsHolding; - - bool m_bFlying; - - ax::Vec2 m_obLastGroundPos; - -public: - static PlayerObject* create(int, ax::Layer*); - - bool onTouchBegan(ax::Touch* touch, ax::Event* event); - - void setMainColor(ax::Color3B col); - void setSecondaryColor(ax::Color3B col); - void setShipColor(ax::Color3B col); - - ax::Color3B getMainColor(); - ax::Color3B getSecondaryColor(); - ax::Color3B getShipColor(); - - void jump(); - - bool isFlying(); - bool isUpsideDown(); - bool isDead(); - - ax::Vec2 getLastGroundPos(); - void update(float dt); +#pragma once +#include +#include "GameObject.h" +#include "CircleWave.h" + +class PlayerObject : public GameObject { +private: + void updateJump(float dt); + bool init(int, ax::Layer*); + void runRotateAction(); + void stopRotation(); + + void logValues(); + + ax::Layer* gameLayer; + bool inPlayLayer; + + ax::Sprite* m_pMainSprite; + ax::Sprite* m_pSecondarySprite; + ax::Sprite* m_pShipSprite; + + ax::ParticleSystemQuad* dragEffect1; + ax::ParticleSystemQuad* dragEffect2; + ax::ParticleSystemQuad* dragEffect3; + ax::ParticleSystemQuad* shipDragEffect; + ax::ParticleSystemQuad* landEffect1; + ax::ParticleSystemQuad* landEffect2; + + ax::MotionStreak* motionStreak; + + double m_dXVel = 5.770002; + double m_dYVel = 0; + double m_dGravity = 0.958199; + double m_dJumpHeight = 11.180032; + + bool m_bUpsideDown; + bool m_bOnGround; + + float m_fSpeed = 0.9f; + + bool m_bIsDead; + bool m_bIsLocked; + + bool m_bIsHolding; + + bool m_bFlying; + + ax::Vec2 m_obLastGroundPos; + +public: + static PlayerObject* create(int, ax::Layer*); + + bool onTouchBegan(ax::Touch* touch, ax::Event* event); + void onTouchEnded(ax::Touch* touch, ax::Event* event); + + void setMainColor(ax::Color3B col); + void setSecondaryColor(ax::Color3B col); + void setShipColor(ax::Color3B col); + + ax::Color3B getMainColor(); + ax::Color3B getSecondaryColor(); + ax::Color3B getShipColor(); + + void jump(); + + bool isFlying(); + bool isUpsideDown(); + bool isDead(); + bool isOnGround(); + + + inline void setDead(bool const& value) { m_bIsDead = value; } + inline void setOnGround(bool const& value) { m_bOnGround = value; } + + + ax::Vec2 getLastGroundPos(); + void update(float dt); }; \ No newline at end of file diff --git a/Classes/SimplePlayer.cpp b/Classes/SimplePlayer.cpp index 8dd0db0..77fd856 100644 --- a/Classes/SimplePlayer.cpp +++ b/Classes/SimplePlayer.cpp @@ -1,49 +1,49 @@ -#include "SimplePlayer.h" -#include "GameToolbox.h" -USING_NS_AX; - -bool SimplePlayer::init(int cubeID) { - if (!Sprite::init()) return false; - - cubeID = GameToolbox::inRange(cubeID, 1, 13); - - auto mainFrame = StringUtils::format("player_%02d_001.png", cubeID); - auto secFrame = StringUtils::format("player_%02d_2_001.png", cubeID); - - this->m_pMainSprite = Sprite::createWithSpriteFrameName(mainFrame); - if (this->m_pMainSprite == nullptr) { - this->m_pMainSprite = Sprite::createWithSpriteFrameName("GJ_arrow_03_001.png"); - } - this->addChild(m_pMainSprite); - m_pMainSprite->setAnchorPoint({0, 0}); - - this->m_pSecondarySprite = Sprite::createWithSpriteFrameName(secFrame); - if (this->m_pSecondarySprite == nullptr) { - this->m_pSecondarySprite = Sprite::createWithSpriteFrameName("GJ_arrow_03_001.png"); - } - this->addChild(m_pSecondarySprite, -1); - m_pSecondarySprite->setPosition(this->m_pMainSprite->getContentSize() / 2); - - this->setContentSize({60, 60}); - - return true; -} - -void SimplePlayer::setMainColor(Color3B col) { - this->m_pMainSprite->setColor(col); -} - -void SimplePlayer::setSecondaryColor(Color3B col) { - this->m_pSecondarySprite->setColor(col); -} - -SimplePlayer* SimplePlayer::create(int cubeID) { - auto pRet = new (std::nothrow) SimplePlayer(); - - if(pRet && pRet->init(cubeID)) { - pRet->autorelease(); - return pRet; - } - AX_SAFE_DELETE(pRet); - return pRet; +#include "SimplePlayer.h" +#include "GameToolbox.h" +USING_NS_AX; + +bool SimplePlayer::init(int cubeID) { + if (!Sprite::init()) return false; + + cubeID = GameToolbox::inRange(cubeID, 1, 13); + + auto mainFrame = StringUtils::format("player_%02d_001.png", cubeID); + auto secFrame = StringUtils::format("player_%02d_2_001.png", cubeID); + + this->m_pMainSprite = Sprite::createWithSpriteFrameName(mainFrame); + if (this->m_pMainSprite == nullptr) { + this->m_pMainSprite = Sprite::createWithSpriteFrameName("GJ_arrow_03_001.png"); + } + this->addChild(m_pMainSprite); + m_pMainSprite->setAnchorPoint({ 0, 0 }); + + this->m_pSecondarySprite = Sprite::createWithSpriteFrameName(secFrame); + if (this->m_pSecondarySprite == nullptr) { + this->m_pSecondarySprite = Sprite::createWithSpriteFrameName("GJ_arrow_03_001.png"); + } + this->addChild(m_pSecondarySprite, -1); + m_pSecondarySprite->setPosition(this->m_pMainSprite->getContentSize() / 2); + + this->setContentSize({ 60, 60 }); + + return true; +} + +void SimplePlayer::setMainColor(Color3B col) { + this->m_pMainSprite->setColor(col); +} + +void SimplePlayer::setSecondaryColor(Color3B col) { + this->m_pSecondarySprite->setColor(col); +} + +SimplePlayer* SimplePlayer::create(int cubeID) { + auto pRet = new (std::nothrow) SimplePlayer(); + + if (pRet && pRet->init(cubeID)) { + pRet->autorelease(); + return pRet; + } + AX_SAFE_DELETE(pRet); + return pRet; } \ No newline at end of file diff --git a/Classes/SimplePlayer.h b/Classes/SimplePlayer.h index 782208b..ef96d59 100644 --- a/Classes/SimplePlayer.h +++ b/Classes/SimplePlayer.h @@ -1,16 +1,16 @@ -#pragma once -#include - -class SimplePlayer : public ax::Sprite { -private: - bool init(int cubeID); - - ax::Sprite* m_pMainSprite; - ax::Sprite* m_pSecondarySprite; - -public: - static SimplePlayer* create(int cubeID); - - void setMainColor(ax::Color3B col); - void setSecondaryColor(ax::Color3B col); -}; \ No newline at end of file +#pragma once +#include + +class SimplePlayer : public ax::Sprite { +private: + bool init(int cubeID); + + ax::Sprite* m_pMainSprite; + ax::Sprite* m_pSecondarySprite; + +public: + static SimplePlayer* create(int cubeID); + + void setMainColor(ax::Color3B col); + void setSecondaryColor(ax::Color3B col); +};