[ create a new paste ] login | about

Link: http://codepad.org/P0nFP1Dx    [ raw code | fork ]

Lua, pasted on Mar 11:
local TileMap=class("TileMap",function()
    local tilemap="scene/map/nearbg.tmx"
    return ccexp.TMXTiledMap:create(tilemap)
end)

TileMap.ctor=function(self)
    self._map={}
    self:init()
    self._rate=1.8 --TileMap在cc.ParallaxNode中的移动速率
end

TileMap.init=function(self)
    self._mapSize=self:getMapSize()
    local size=self._mapSize
    local obstacleLayer=self:getLayer("obstacle")
    local moveLayer=self:getLayer("move")
    obstacleLayer:setVisible(false)
    moveLayer:setVisible(false)

    for i=0,size.width-1 do
        self._map[i]={}
        for j=0,size.height-1 do
            if obstacleLayer:getTileAt(cc.p(i,j))~=nil then
                self._map[i][j]=0   --障碍物
            elseif moveLayer:getTileAt(cc.p(i,j))~=nil then
                self._map[i][j]=1   --可移动
            end
        end
    end

    self._scale=0.5
    self:setScale(self._scale)
    local s=self:getTileSize()
    self._tileSize=cc.size(s.width,s.height)
end

TileMap.getTileAtLayer=function(self,pTile)
    local obstacleLayer=self:getLayer("obstacle")
    local moveLayer=self:getLayer("move")
    if self._map[pTile.x][pTile.y]==0 then
        return obstacleLayer
    else
        return moveLayer
    end
end

--TileMap中添加Sprite
TileMap.addSprite=function(self,sprite,pTile)
    self:addChild(sprite, table.getn(self:getChildren()))
    sprite:retain()
    sprite:setPosition(self:tileToPixel(pTile))
    sprite:setAnchorPoint(cc.p(0.5,0.5))

    if sprite._name~=nil then
        sprite._moveBoard:setTileMap(self)
    end
end

--TileMap中删除Sprite
TileMap.removeSprite=function(self,sprite)
    self:removeChild(sprite)
end

----------------------------------------------------------------
--  说明:
--      由于使用tilemap,所有的sprite都是直接加入到tilemap中
--      对象使用的坐标系都是基于tilemap的,随着tilemap的移动
--      对象仍然是在地图上跟随地图移动,因此不需要加入地图相对屏幕
--      移动的相对坐标,使用sprite:getPosition()得到的坐标也是
--      基于tilemap的坐标系,例如,tilemap的格点大小为32*32,
--      sprite在tilemap上的格点为(2,11),同时地图缩放0.5,那么
--      使用sprite:getPosition得到的坐标为cc.p(2*32,11*32)
----------------------------------------------------------------
--TileMap坐标转换为TileMap格点坐标
TileMap.pixelToTile=function(self,point)
    --local pointMap=getRolePositionTable(self)
    --point=cc.pSub(point,pointMap)
    point.x =math.ceil(point.x / self._tileSize.width);
    point.y = math.ceil((self._tileSize.height * self._mapSize.height
        - point.y) /self._tileSize.height)
    return point
end

--TileMap格点坐标转换为屏幕坐标
TileMap.tileToPixel=function(self,pTile)
    local width = pTile.x * self._tileSize.width
    local height = (self._mapSize.height-pTile.y) * self._tileSize.height
    local point=cc.p(width,height)
    --local pointMap=getRolePositionTable(self)
    --point=cc.pAdd(pointMap,point)
    return point
end

TileMap.isTileMovable=function(self,pTile)
    if pTile.x >= self._mapSize.width or pTile.y >= self._mapSize.height then
        return false
    elseif self._map[pTile.x][pTile.y]==0 then
        return false
    else
        return true
    end
end

--在地图上添加Sword特效
TileMap.addSwordEffect=function(self,role,factor,callback)
    local pTile=self:pixelToTile(getRolePositionTable(role))
    local add=BaseDirection:getInstance():addWithDirection(factor,role._rotation)
    cclog(string.format("addSwordEffect:pTile(%f,%f),add:(%f,%f)",pTile.x,pTile.y,add.x,add.y))
    pTile=cc.pAdd(pTile,add)
    cclog(string.format("after added pTile(%f,%f)",pTile.x,pTile.y))
    if self:isTileMovable(pTile)==false then
        cclog("tilemap obstacle NA SwordEffect")
        if callback then
            callback()
        end
        return  --障碍物格点,不能释放技能
    end
    local sword=EffectManage:getInstance():trickEffectSword(callback)
    self:addSprite(sword,pTile)
    return pTile
end

TileMap.viewFollowX=function(self,point)
    local screenSize=cc.Director:getInstance():getVisibleSize()
    local mapSize=cc.size(self._mapSize.width*self._tileSize.width,
        self._mapSize.height * self._tileSize.height)
    local scale=self:getScale()
    mapSize=cc.size(mapSize.width*scale,mapSize.height*scale)
    local x=Max(point.x*scale,screenSize.width/2)
    local realPointX=cc.p(x,0)
    local scrollPoint=cc.pSub(cc.p(screenSize.width/2,0),realPointX)

    local mapXMin=-mapSize.width+screenSize.width
    if scrollPoint.x > mapXMin then --到达地图右边界,不能继续滑动
        return scrollPoint
    else
        return nil
    end
end

TileMap.create=function(self)
    return TileMap.new()
end

return TileMap


Create a new paste based on this one


Comments: