Ejemplo n.º 1
0
bool MapLayer::onTouchBegan(Touch *touch, Event *unused_event)
{
    Point beginPos = touch->getLocationInView();
    beginPos = Director::getInstance()->convertToGL(beginPos);

    TMXTiledMap* map = (TMXTiledMap*)this->getChildByTag(kTagTileMap);

    // 获取触摸点在地图位置上的索引
    Point mapPos = map->getPosition();
    Point aimMapIndex = convertTo2d(Point(beginPos.x - mapPos.x, beginPos.y - mapPos.y));
    if(aimMapIndex.x < 0 || aimMapIndex.y < 0 || aimMapIndex.x >= map->getMapSize().width || aimMapIndex.y >= map->getMapSize().height) {
        // 超出地图边界
        return false;
    }

    Point herop = _tamara->getPosition();
    Point mapIndex = convertTo2d(herop);
    TMXLayer* layer = map->getLayer("grass");
    int tilegid = layer->getTileGIDAt(aimMapIndex);
    Value tileValue = map->getPropertiesForGID(tilegid);
    int touchValue = tileValue.asValueMap().at("conflict").asInt();
    if(touchValue == 1) {
        return false;
    }

    _path = _myAstar->findPath(mapIndex.x, mapIndex.y, aimMapIndex.x, aimMapIndex.y, map);
    if(nullptr != _path && _path->count() >= 2) {
        _stepIndex = 1;
    } else {
        _stepIndex = -1;
    }
    _smallStepIndex = 0;

    return true;
}
Ejemplo n.º 2
0
Point MapLayer::convertTo2d(Point position)
{
    TMXTiledMap* map = (TMXTiledMap*) getChildByTag(kTagTileMap);
    int mapWidth = map->getMapSize().width * map->getTileSize().width;
    int mapHeight = map->getMapSize().height * map->getTileSize().height;

    int x = position.x;
    int y = position.y;

    double distanse;
    double sin1;
    double sin11;
    double sin22;
    double cos11;
    double cos1;
    int d2x;
    int d2y;
    double mystatic5 = sqrt(5.0);
    double mystatic = 16 * mystatic5;
    //char mch[256];

    if(x > mapWidth/2){
        distanse = sqrt((x - mapWidth/2) * (x - mapWidth/2) + (mapHeight - y) * (mapHeight - y));
        sin1 = (mapHeight - y)/distanse;
        cos1 = (x - mapWidth/2)/distanse;
        sin11 = (sin1 * 2 - cos1) / mystatic5;
        cos11 = (sin1 + cos1 * 2) / mystatic5;
        d2y = distanse * 5 / 4 * sin11 / mystatic;
        sin22 = (2 * sin1 + cos1) / mystatic5;
        d2x = distanse * 5 / 4 * sin22 / mystatic;
        return Point(d2x,d2y);

    }else{
        distanse = sqrt((mapWidth/2 - x) * (mapWidth/2 - x) + (mapHeight - y) * (mapHeight - y));
        sin1 = (mapHeight - y)/distanse;
        cos1 = (mapWidth/2 - x)/distanse;
        sin11 = (sin1 * 2 - cos1) / mystatic5;
        cos11 = (sin1 + cos1 * 2) / mystatic5;
        d2x = distanse * 5 / 4 * sin11 / mystatic;
        //sin22 = 4.0 * cos11 / 5 + 3.0 * sin11 / 5;
        sin22 = (2 * sin1 + cos1) / mystatic5;
        d2y = distanse * 5 / 4 * sin22 / mystatic;
        return Point(d2x,d2y);
    }

}
Ejemplo n.º 3
0
bool MapLayer::init()
{
    bool bRet = false;

    do 
    {
        CC_BREAK_IF(!Layer::init());

        Size winSize = Director::getInstance()->getWinSize();

        // 初始化地图
        TMXTiledMap* map = TMXTiledMap::create("iso-test-zorder.tmx");
        map->setPosition((winSize.width - map->getContentSize().width)/2, 0);
        this->addChild(map, 0, kTagTileMap);

        // 初始化任务
        _tamara = Sprite::create("grossinis_sister1.png");
        map->addChild(_tamara, map->getChildren().size());
        _tamara->retain();
        int mapWidth = map->getMapSize().width * map->getTileSize().width;
        int mapHeight = map->getMapSize().height * map->getTileSize().height;
        _tamara->setPosition(mapWidth/2, 112);
        _tamara->setAnchorPoint(Point(0.5f, 0));
        

        _vmove = -1;
        _hmove = -1;
        _stepIndex = -1;
        _myAstar = new Astar();

        this->scheduleUpdate();

        auto listener = EventListenerTouchOneByOne::create();
        listener->onTouchBegan = CC_CALLBACK_2(MapLayer::onTouchBegan, this);
        this->_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

        bRet = true;
    } while (0);

    return bRet;
}
Ejemplo n.º 4
0
void CRole::updateVertexZ()
{
    do
    {
        CBackgroundManager* bkg = getBackGround();
        BREAK_IF(nullptr == bkg);
        TMXTiledMap* map = bkg->getTiledMap();
        BREAK_IF(nullptr == map);
        const Size& szMap = map->getMapSize();
        float lowestZ = -(szMap.width + szMap.height);

        const Point& tilePos = getLogicGrid()->getGridPos();
        float currentZ = tilePos.x + tilePos.y;
        
        
        setSpriteVertexZ(lowestZ + currentZ - 1);
        
//        setSpriteZOrder(lowestZ + currentZ - 1);
    } while (false);
}
Ejemplo n.º 5
0
void LDZGameLayer::initLevel(int level){
    
    Size vs = Director::getInstance()->getVisibleSize();
    Vec2 vo = Director::getInstance()->getVisibleOrigin();
    
    _mode = MODE_BEGAIN;
    
    TMXTiledMap *map = TMXTiledMap::create("back.tmx");
    _map = map;
    _mapSize = Size(map->getMapSize().width * map->getTileSize().width , map->getMapSize().height * map->getTileSize().height);
    map->setPosition(Vec2(0, 0));
    this->addChild(map);
    
    //main char
    _mainC = Sprite::createWithSpriteFrameName("1.png");
    _mainC->setPosition(_mapSize.width/2, _mainC->getContentSize().height/2 + 10);
    this->addChild(_mainC,1);
    
    //progress
    auto progress = ProgressTimer::create(Sprite::createWithSpriteFrameName("tempb.png"));
    float progx = _mapSize.width/2 - _mainC->getContentSize().width/2 - progress->getContentSize().width/2 - 5;
    float progy = _mainC->getContentSize().height/2 + 10;
    progress->setPosition( progx, progy);
    progress->setType(cocos2d::ProgressTimer::Type::BAR);
    progress->setBarChangeRate(Vec2(0, 1));
    progress->setMidpoint(Vec2(0, 0));
    auto progback = Sprite::createWithSpriteFrameName("temp.png");
    progback->setPosition(progx, progy);
    this->addChild(progback);
    this->addChild(progress,1);
    progress->setVisible(false);
    progback->setVisible(false);
    _powerProg = progress;
    _powerProgBack = progback;
    
    //limit
    float height = 50 + arc4random()%100;
    float uy = _mapSize.height/3 + _mapSize.height/3*2*CCRANDOM_0_1() - 5;
    float dy = uy - height;
    _upLimit = Sprite::createWithSpriteFrameName("limit.png");
    _upLimit->setPosition(_mapSize.width/2, uy);
    this->addChild(_upLimit);
    _downLimit = Sprite::createWithSpriteFrameName("limit.png");
    _downLimit->setPosition(_mapSize.width/2, dy);
    this->addChild(_downLimit);
    
    //guide
    auto gt = ui::Text::create(LHLocalizedCString("guidetext"), Common_Font, 25);
    gt->setColor(Color3B::RED);
    gt->setPosition(Vec2(_mainC->getPosition().x , _mainC->getBoundingBox().getMaxY() + gt->getContentSize().height/2));
    this->addChild(gt);
    auto bl = Blink::create(1, 1);
    auto rp = RepeatForever::create(bl);
    gt->runAction(rp);
    
    auto da = Sprite::create("da.png");
    da->setScaleY(height/da->getContentSize().height);
    da->setPosition(Vec2(_mainC->getPosition().x,_downLimit->getPositionY()+height/2));
    this->addChild(da);
    
    auto lis = EventListenerTouchOneByOne::create();
    lis->onTouchBegan = [this,gt,progress,progback,da](Touch* tmpTouch, Event*){
        if (_mode == MODE_VIEW) {
            Vec2 loca = tmpTouch->getLocation();
            loca = this->convertToNodeSpace(loca);
            if (_mainC->getBoundingBox().containsPoint(loca)) {
                CallFunc *call = CallFunc::create([this](){
                    _mode = MODE_POWER;
                    this->scheduleUpdate();
                    CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("wiso.wav");
                });
                
                float focusMargin = 20;
                this->focusOn(_mainC->getPosition(), _mainC->getContentSize().height + focusMargin, true , call);
                _mode = MODE_BEGAIN;
                
                gt->removeFromParent();
                da->removeFromParent();
                
                progress->setVisible(true);
                progback->setVisible(true);
                progress->setPercentage(0);
            }
        }else if (_mode == MODE_POWER_END){
            _mode = MODE_POWER;
        }
        
        return true;
    };
    lis->onTouchMoved = [this](Touch* tmpTouch, Event*){
        Vec2 cu = tmpTouch->getLocation();
        Vec2 last = tmpTouch->getPreviousLocation();
        if (_mode == MODE_VIEW) {
            float toy = _focusCenter.y + (last.y - cu.y);
            if (toy+_focusLen/2 > _upLimit->getPositionY() + 20) {
                toy = _upLimit->getPositionY()+20 - _focusLen/2;
            }
            this->focusOn(Vec2(_focusCenter.x , toy),_focusLen, false);
        }else if (_mode == MODE_POWER){
            float dy = fabsf(cu.y - last.y);
            _powerProg->setPercentage(_powerProg->getPercentage()+dy/(_powerProg->getPercentage()+1)/MOVE_FACTOR);
        }
    };
    lis->onTouchEnded = [this](Touch*, Event*){
        if (_mode == MODE_POWER) {
            _mode = MODE_POWER_END;
            _powerCount = 0.1f;
        }
    };
    this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(lis, this);
    
    focusOn(Vec2(_mapSize.width/2,(uy - height/2)), vs.width, false);
    auto dl2 = DelayTime::create(1);
    CallFunc *call = CallFunc::create([this](){
        auto tmpcall = CallFunc::create([this](){
            _mode = MODE_VIEW;
        });
        this->focusOn(_mainC->getPosition(), _focusLen, true , tmpcall);
    });
    auto sq = Sequence::create(dl2,call, NULL);
    this->runAction(sq);
}