要实现一副SLG地图,基本元素有5块地皮,有四块地皮上不能放东西,一块地皮上可以放主城或者是晶石。策划文档如上,要实现的效果如下:
源 代码:
local MapUI = class("MapUI", function () return cc.Layer:create() end)
function MapUI:ctor()
self.mapLayer =cc.Layer:create()
local image1 = ccui.ImageView:create("res/common/image/map/1.png",ccui.TextureResType.localType)
local image2 = ccui.ImageView:create("res/common/image/map/2.png",ccui.TextureResType.localType)
local image3 = ccui.ImageView:create("res/common/image/map/3.png",ccui.TextureResType.localType)
local image4 = ccui.ImageView:create("res/common/image/map/4.png",ccui.TextureResType.localType)
local image5 = ccui.ImageView:create("res/common/image/map/5.png",ccui.TextureResType.localType)
local building1 = ccui.ImageView:create("res/common/image/map/building1.png",ccui.TextureResType.localType)
local building2 = ccui.ImageView:create("res/common/image/map/building2.png",ccui.TextureResType.localType)
local building3 = ccui.ImageView:create("res/common/image/map/building3.png",ccui.TextureResType.localType)
local mineral1 = ccui.ImageView:create("res/common/image/map/mineral1.png",ccui.TextureResType.localType)
local mineral2 = ccui.ImageView:create("res/common/image/map/mineral2.png",ccui.TextureResType.localType)
local mineral3 = ccui.ImageView:create("res/common/image/map/mineral3.png",ccui.TextureResType.localType)
local imageList = {image1,image1,image1,image1,image1,image1,image2,image3,image4,image5}
local buildingList = {building1,building2,building3}
local posList = {{-20,20},{-10,25},{-30,40}}
local mineralList = {mineral1,mineral2,mineral3}
local totalCell = 99
self.cellSize = image1:getContentSize()
self.totalHeight = math.ceil(totalCell/2)*self.cellSize.height
self.totalWidth = math.ceil(totalCell/2)*self.cellSize.width
self:setContentSize(cc.size(self.totalWidth,self.totalHeight))
local row = math.ceil(totalCell/2)
local offsetX = self.cellSize.width/2
local offsetY = -(g_operateSize.height-g_uiSize.height)/2
self.myCityFlag = false --是否选中了自己主城标志
for i = 1 ,totalCell do
local y = self.totalHeight-i*self.cellSize.height/2+offsetY
for j=1,row do
if i % 2 == 1 or (i%2==0 and j<row) then
local image
local num
local x = (j-1)*self.cellSize.width+self.cellSize.width/2
if i%2 == 0 then
x = x + offsetX
end
if i==1 or i == totalCell or (i%2 ==1 and j == 1 or j==row )then--边界
num = g_random(7,10)
else
num = g_random(1,10)
end
image = imageList[num]:clone()
if num <= 6 then
--空地10%放主城
local randowNum = g_random(1,10)
if randowNum == 1 then
--选一个做自己的主城
if not self.myCityFlag and i >= math.floor(totalCell/2) and i <= math.ceil(totalCell/2)+1 and j>=15 and j <= 35 then
self.myCityFlag = true
self.myCityPos = cc.p(x,y)
randowNum = 1
else
randowNum = g_random(2,3)
end
--放主城
local building = buildingList[randowNum]:clone()
local posX = self.cellSize.width/2 + posList[randowNum][1]
local posY = self.cellSize.height/2 + posList[randowNum][2]
building:setPosition(cc.p(posX,posY))
building:addTo(image)
else
randowNum = g_random(1,20)
if randowNum <= 3 then --放矿石
local mineral = mineralList[randowNum]:clone()
mineral:setPosition(cc.p(self.cellSize.width/2-6,self.cellSize.height/2+10))
mineral:addTo(image)
end
end
end
image:setPosition(cc.p(x,y))
image:addTo(self.mapLayer)
end
end
end
if self.myCityFlag then
self.mapLayer:setPosition(cc.p(g_visibleSize.width/2-self.myCityPos.x,g_visibleSize.height/2-self.myCityPos.y))
else
self.mapLayer:setPosition(cc.p(-self.cellSize.width/2,-self.cellSize.height/2))
end
self:addChild(self.mapLayer,1)
self:registerTouch()
local operateLayer = cc.Layer:create()
local closeBtn = ccui.Button:create("ui/Resources/btn/btn_fanhui_n.png", nil, nil, g_texture_normal_format)
local closeSize = closeBtn:getContentSize()
closeBtn:setPosition(cc.p(g_visibleSize.width-closeSize.width/2,g_visibleSize.height-closeSize.height/2+offsetY))
g_addClickEventListener(closeBtn, g_handler(self, self.closeHandler))
closeBtn:addTo(operateLayer)
self:addChild(operateLayer,2)
local backBtn = ccui.Button:create("ui/Resources/btn/btn_sousuo_n.png", nil, nil, g_texture_normal_format)
backBtn:setPosition(cc.p(g_visibleSize.width-3*closeSize.width/2-20,g_visibleSize.height-closeSize.height/2+offsetY))
g_addClickEventListener(backBtn, g_handler(self, self.backHandler))
backBtn:addTo(operateLayer)
backBtn:loadTexturePressed("ui/Resources/btn/btn_sousuo_p.png",ccui.TextureResType.localType)
closeBtn:loadTexturePressed("ui/Resources/btn/btn_fanhui_p.png",ccui.TextureResType.localType)
end
function MapUI:backHandler()
if self.myCityFlag then
self.mapLayer:setPosition(cc.p(g_visibleSize.width/2-self.myCityPos.x,g_visibleSize.height/2-self.myCityPos.y))
else
self.mapLayer:setPosition(cc.p(-self.cellSize.width/2,-self.cellSize.height/2))
end
end
function MapUI:closeHandler()
g_dlgManager:popDlgById(g_dlgManager.dlgId_mapUI)
end
function MapUI:registerTouch()
local function onTouchBegan(touch,event)
local pos = touch:getLocation()
self.beginPos = pos
return true
end
local function onTouchMoved(touch,event)
local pos = touch:getLocation()
local layerPosX = self.mapLayer:getPositionX()
local layerPosY = self.mapLayer:getPositionY()
local x = layerPosX + pos.x-self.beginPos.x
local y = layerPosY + pos.y-self.beginPos.y
if x > - self.cellSize.width/2 or x < g_visibleSize.width-(self.totalWidth-self.cellSize.width/2) then
return
end
if y > - self.cellSize.height/2 or y < g_visibleSize.height-(self.totalHeight-self.cellSize.height/2) then
return
end
self.mapLayer:setPosition(cc.p(x,y))
self.beginPos = pos
end
local listener = cc.EventListenerTouchOneByOne:create()
listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )
local eventDispatcher = self:getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self)
end
return MapUI