Ejemplo n.º 1
0
void Level::updateLeaves(float dt) {
    GameModel* gameModel = GameModel::getInstance();
    for (auto iter = this->drowningLeaves->begin(); iter != this->drowningLeaves->end(); iter++) {
        Leaf* drowningLeaf = (Leaf*) *iter;
        if (drowningLeaf->getDrowning()) {
            if (drowningLeaf->getCurrentDrownDuration() != -1) {
                drowningLeaf->setCurrentDrownDuration(drowningLeaf->getCurrentDrownDuration() - dt);
                if (drowningLeaf->getCurrentDrownDuration() < 0) {
                    drowningLeaf->setCurrentDrownDuration(-1);
                    
                    DataEvent* dataEvent = new DataEvent();
                    dataEvent->setEventCode(EVT_LEAF_DROWN);
                    dataEvent->setArgumentReference(drowningLeaf);
                    gameModel->fireEvent(dataEvent);
                    
                    this->leaves->removeObjectForKey(drowningLeaf->getPositionIndex());
                    this->drowningLeaves->eraseObject(drowningLeaf);
                    
                    if (this->leaves->count() == 1) {
                        this->done = true;
                    }
                    break;
                }
            }
            drowningLeaf->play(SPRITE_TYPE_MAIN, FRAME_LEAF_FALL);
        }
    }
}
Ejemplo n.º 2
0
void Level::frogStop() {
    this->frog->setPosition(this->frog->getCurrentTargetPoint());
    this->frog->getBody()->setVelocity(Point::ZERO);
    this->frog->setJumping(false);
    
    // check if has no ways to goes further
    int remainLeaf = 0;
    __Array* allLeavesKey = this->getLeaves()->allKeys();
    for (int i = 0; i < allLeavesKey->count(); i++) {
        __Integer* key = (__Integer*) allLeavesKey->getObjectAtIndex(i);
        Leaf* leaf = (Leaf*) this->getLeaves()->objectForKey(key->getValue());
        if (!leaf->getDrowning()) {
            remainLeaf++;
        }
    }
    
    if (remainLeaf > 1) {
        int frogRow = (this->frog->getPositionIndex() - 1) / CONFIG_LAGOON_WIDTH;
        int sizeOfLagoon = CONFIG_LAGOON_WIDTH * CONFIG_LAGOON_HEIGHT;
        bool deadLock = true;
        
        if (this->frog->getViewDirection() == VIEW_LEFT ||
            this->frog->getViewDirection() == VIEW_UP ||
            this->frog->getViewDirection() == VIEW_DOWN) {
            
            int checkIndex = this->frog->getPositionIndex();
            while ((--checkIndex - 1) / CONFIG_LAGOON_WIDTH == frogRow) {
                if (this->leaves->objectForKey(checkIndex) != NULL) {
                    deadLock = false;
                    break;
                }
            }
        }
        
        if (this->frog->getViewDirection() == VIEW_RIGHT ||
            this->frog->getViewDirection() == VIEW_UP ||
            this->frog->getViewDirection() == VIEW_DOWN) {
            
            int checkIndex = this->frog->getPositionIndex();
            while ((++checkIndex - 1) / CONFIG_LAGOON_WIDTH == frogRow) {
                if (this->leaves->objectForKey(checkIndex) != NULL) {
                    deadLock = false;
                    break;
                }
            }
        }
        
        if (this->frog->getViewDirection() == VIEW_LEFT ||
            this->frog->getViewDirection() == VIEW_RIGHT ||
            this->frog->getViewDirection() == VIEW_UP) {
            
            int checkIndex = this->frog->getPositionIndex();
            checkIndex += CONFIG_LAGOON_WIDTH;
            while ((checkIndex - 1) < sizeOfLagoon) {
                if (this->leaves->objectForKey(checkIndex) != NULL) {
                    deadLock = false;
                    break;
                }
                checkIndex += CONFIG_LAGOON_WIDTH;
            }
        }
        
        if (this->frog->getViewDirection() == VIEW_LEFT ||
            this->frog->getViewDirection() == VIEW_RIGHT ||
            this->frog->getViewDirection() == VIEW_DOWN) {
            
            int checkIndex = this->frog->getPositionIndex();
            checkIndex -= CONFIG_LAGOON_WIDTH;
            while ((checkIndex - 1) >= 0) {
                if (this->leaves->objectForKey(checkIndex) != NULL) {
                    deadLock = false;
                    break;
                }
                checkIndex -= CONFIG_LAGOON_WIDTH;
            }
        }
        
        if (deadLock) {
            this->hanged = true;
            GameModel* gameModel = GameModel::getInstance();
            GameEvent* gameEvent = new GameEvent();
            gameEvent->setEventCode(EVT_DEAD_LOCK_GAME);
            gameModel->fireEvent(gameEvent);
        }
    }
}