HSubGameScene.h 6.33 KB
//
//  HSubGameScene.h
//  SteveAndMaggieGame
//
//  Created by Katarzyna Kalinowska-Górska on 07/05/2019.
//

#ifndef HSubGameScene_h
#define HSubGameScene_h

#include "HalloweenParentScene.h"
#include "HGameLifeIndicatorView.h"
#include "HSettingsLayer.h"

class HSubGameScene : public HalloweenParentScene
{
public:
    
    class GameCreator {
        public:
            virtual HSubGameScene* createGameScene(std::string layoutFilePath) = 0;
    };
    
    struct SoundConfig {
        std::string soundsFolder;
        std::map<std::string, float> soundDurations;
        float soundPadding;
    };
    
    struct SoundInfo {
        SoundInfo(std::string fp, float sd){
            filePath = fp;
            soundDuration = sd;
        };
        std::string filePath;
        float soundDuration;
    };
    
    struct CommonConfig {
        SoundConfig soundConfig;
        std::vector<std::string> itemPicturePaths;
        int maxItemsAtATime;
        int screenPadding;
        float maxSecondsInactivity;
        int lives;
    };
    
    virtual void onEnter() override;
    virtual void onExit() override;
//    virtual void onEnterTransitionDidFinish() override;
    
    virtual bool initWithConfiguration(std::string layoutFilePath, CommonConfig commonConfig);
    virtual ~HSubGameScene();
    
    virtual void pauseGame() = 0;
    virtual void resumeGame() = 0;
    virtual void presentGameResumeLayer();
    virtual bool cleanupVideoPLayerIfNeeded();
//    virtual void resumeVideoPlayerIfNeeded();
//    void pauseVideo();
//    void resumeVideo();
    
protected:
    
    enum class PlayState
    {
        INIT,
        PRE_PLAYING,
        PLAYING,
        PLAYING_VIDEO,
        FINISHED_PLAYING_VIDEO,
        CHANGING_LEVEL,
        LOST,
        WON
    };
    
    enum class ItemType : int {
        NONE = 0,
        ONE = 1,
        TWO = 2,
        THREE = 3,
        FOUR = 4
    };
    
    class Item {
    public:
        cocos2d::Sprite* sprite;
        cocos2d::Rect rect;
        ItemType type;
        
        bool operator==(const Item& item2) {
            if(sprite == nullptr || item2.sprite == nullptr){
                return false;
            }
            return sprite->getTag() == item2.sprite->getTag();
        }
        
        virtual ~Item(){
            if(sprite != nullptr){
                sprite->removeFromParent();
            }
        }
    };
    
    // state variables
    class GameState {
        public:
            PlayState playState;
            bool settingsMenuShown;
            bool tosAcceptMenuShown;
            std::map<int, Item*> items;
            int currentLevel;
            int currentItemTypeIndex;
            std::vector<ItemType> itemTypeOrder; //the order of the types to catch (shuffled randomly at the beginning of each level)
            bool itemVanishingMode;
            float timeCount;
            float lossTimeCount;
            bool lossHalfTime;
            int wrongItemCount;
        virtual ~GameState(){}
    };
    
    GameState* gameState;
    CommonConfig commonConfig;
    HGameLifeIndicatorView* lifeIndicatorView;
    HSettingsLayer* settingsMenu;

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_TIZEN) && !defined(CC_PLATFORM_OS_TVOS)

    cocos2d::ui::VideoPlayer* videoPlayer;
    void videoPlayerCallback(cocos2d::Ref* sender, cocos2d::ui::VideoPlayer::EventType eventType);
#endif

    
    virtual void prepareSettingsMenu();
    virtual void moveSettingsButtonToFront(int localZOrder);
    virtual void showSettingsMenu(bool animated = true);
    virtual void hideSettingsMenu(bool animated = true);
    virtual void hideSettingsMenuWithLevelReset();
    
    virtual void addLivesIndicatorView();
    virtual GameState* createGameState();
    virtual void removeGameState();
    virtual void clearGameState();
    virtual void clearAllItems();
    virtual bool generateItemTypeAndId(ItemType& itemType, int& itemId, std::string& itemPicturePath, ItemType preferredType = ItemType::NONE);
    
    virtual bool touchHandlerForWidget(std::string objectName, cocos2d::ui::Widget::TouchEventType touchEventType) override;
    virtual bool onPrevLevelButtonClicked();
    virtual bool onReplayButtonClicked();
    virtual bool onNextLevelButtonClicked();

    virtual SoundInfo intro1Sound();
    virtual SoundInfo intro2Sound();
    virtual SoundInfo hooraySound();
    virtual SoundInfo oopsSound();
    virtual SoundInfo pureOopsSound();
    virtual SoundInfo noSound();
    virtual SoundInfo pickLevelSound();
    virtual SoundInfo gameFinishedSound();
    virtual SoundInfo gameLostSound();
    virtual SoundInfo typeRequestSound(ItemType itemType);
    virtual SoundInfo typeConfirmSound(ItemType itemType);
    virtual SoundInfo typeWrongSound(ItemType itemType);
    virtual SoundInfo wrongItemEffectSound();
    virtual SoundInfo rightItemEffectSound();
    
    float soundDuration(const std::string soundFilepath);
    void preloadAllLoadedSoundEffects();
    
    virtual void prepareVideoPlayerIfNeeded();
    virtual void removeVideoPlayer();
    virtual void playVideo(std::string videoFilePath);
   
    virtual void videoPLayerFinishedPlayback();
        
    virtual void startGame(bool playIntro = true) = 0;
    virtual void gameWon();
    virtual void gameLost();
    virtual void resetGame();
    
    virtual void showTOSAcceptPopup(std::function<void()> onAccept);
    virtual void showHLevelPickerLayer(std::function<void()> onLayerDismissed);
    virtual void addTouchBlockingLayer(cocos2d::Node* layer);

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

    cocos2d::EventListenerKeyboard* _keyboardListener;

    void addBackButtonListener(){
        _keyboardListener = cocos2d::EventListenerKeyboard::create();
        _keyboardListener->onKeyReleased = [&](cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event* event){
            if(keyCode == cocos2d::EventKeyboard::KeyCode::KEY_BACK){
            if(!gameState->settingsMenuShown && !gameState->tosAcceptMenuShown){
                pauseGame();
                HMiscUtils::showAppCloseConfirmDialog([&](){
                    resumeGame();
                });
                }
            }
        };
        cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(_keyboardListener, this);
    }
#endif
};

#endif /* HSubGameScene_h */