之前在网上一直没搜到用coco2dx(lua)实现战争迷雾的文章,就有了下文。
tilemap的美术资源(战争迷雾)
战争迷雾的原理在网上已经有相关资料,就不补充了。
--战争迷雾用
--t1中索引与tilemap的GID对应,右边值(表)与tilemap的资源对应。
p.t1 = {
[0] = {0,0,0,0},
[2] = {0,0,0,4},
[3] = {0,0,8,0},
[5] = {0,0,8,4},
[6] = {0,1,0,0},
[8] = {0,1,0,4},
[13] = {0,1,8,0},
[15] = {0,1,8,4},
[7] = {2,0,0,0},
[12] = {2,0,0,4},
[4] = {2,0,8,0},
[14] = {2,0,8,4},
[9] = {2,1,0,0},
[11] = {2,1,0,4},
[10] = {2,1,8,0},
[1] = {2,1,8,4},
}
-- t2索引与t1的右边的表的数字的和对应,值与t1的索引对应
p.t2 = {
[0] = 0,
[4] = 2,
[8] = 3,
[12] = 5,
[1] = 6,
[5] = 8,
[9] = 13,
[13] = 15,
[2] = 7,
[6] = 12,
[10] = 4,
[14] = 14,
[3] = 9,
[7] = 11,
[11] = 10,
[15] = 1,
}
--先利用t1获得对应坐标的资源信息(t1中的值),再利用t2中的索引返回新的GID(t2中的值)。
--战争迷雾
--已探索地图
--61即tilemap中战争迷雾资源的第一个图素的GID
function p.newMap(x,y,num)p.cloudLayer:setTileGID(61+num,ccp(x,y))
end
function p.oldMap(x,y)
if x > -1 and y > -1 and x < p.size.width and y < p.size.height then
local id = p.cloudLayer:tileGIDAt(ccp(x,y))
return id - 61
else
return false
end
end
function p.tableTotal(t)
if t ~= nil and type(t) == "table" then
local total = 0
for k,v in pairs(t) do
total = total + v
end
return total
end
end
--对周围战争迷雾的处理,按照x坐标及y坐标的差值的绝对值的和(即按照距离)进行分类处理
function p.setCloudInfo(x,y,dx,dy)local newx,newy,num,total
local t = p.t1
local tab = {2,1,8,4}
local idx,idx1,idx2
local oldNum,oldNum1,oldNum2
newx,newy = x+dx,y+dy
num = p.oldMap(newx,newy)
if(num) then
local distance = math.abs(dx) + math.abs(dy)
if distance == 3 then
if dx > 0 and dy > 0 then
idx = 1
elseif dx > 0 and dy < 0 then
idx = 3
elseif dx < 0 and dy > 0 then
idx = 2
elseif dx < 0 and dy < 0 then
idx = 4
end
elseif distance == 2 then
if dx == 0 or dy == 0 then
if dx == 0 then
if dy > 0 then
idx1,idx2 = 1,2
else
idx1,idx2 = 3,4
end
else
if dx > 0 then
idx1,idx2 = 1,3
else
idx1,idx2 = 2,4
end
end
else
if dx > 0 and dy > 0 then
idx = 4
elseif dx > 0 and dy < 0 then
idx = 2
elseif dx < 0 and dy > 0 then
idx = 3
elseif dx < 0 and dy < 0 then
idx = 1
end
end
elseif distance <= 1 then
total = 1
p.newMap(newx,newy,total)
return
end
if(idx) then
local nNum = 0
local value = tab[idx]
if distance == 2 then
nNum = t[num][idx]
num = 1
value = 0
end
oldNum = t[num][idx]
t[num][idx] = value
total = p.tableTotal(t[num])
total = total + nNum
total = p.t2[total]
p.newMap(newx,newy,total)
t[num][idx] = oldNum
else
oldNum1 = t[num][idx1]
oldNum2 = t[num][idx2]
t[num][idx1] = tab[idx1]
t[num][idx2] = tab[idx2]
total = p.tableTotal(t[num])
total = p.t2[total]
p.newMap(newx,newy,total)
t[num][idx1],t[num][idx2] = oldNum1,oldNum2
end
end
end
function p.setCloud(x,y)
for i = -2,2 do
for j = -2,2 do
local total = math.abs(i) + math.abs(j)
if total ~= 4 then
p.setCloudInfo(x,y,j,i)
end
end
end
end
--end