Advertisement
podoko_Lua

ground reloaded

Oct 4th, 2014
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.33 KB | None | 0 0
  1. --[[ ground reloaded ]]--
  2. -- Projet non achevé d'éditeur de carte.
  3.  
  4. --[[
  5.     liste id TextArea
  6.     0 base en haut
  7.     1 type de sol selectionné
  8.     2 liste des sols
  9.     3 prop du sol selectionné (fond)
  10.     4 type et id du sol selectionné
  11.     5 X et Y du sol selectionné
  12.     6 L et H du sol selectionné
  13.     7-28 proppriétés du sol selectionné
  14.     29 selection prop avancées sol
  15.     30 selection tu type de sol
  16.     31-34 points déjà cliqués
  17.     35 obtenir xml et jouer map
  18.    
  19. ]]--
  20.     sol = {}
  21.     sol[0] = {name="bois", prop={type=0,friction=0.3,restitution=0.2,miceCollision=true,groundCollision=true,angle=0,foreground=false,dynamic=false}, fix={'color'}}
  22.     sol[1] = {name="glace", prop={type=1,friction=0,restitution=0.2,miceCollision=true,groundCollision=true,angle=0,foreground=false,dynamic=false}, fix={'color'}}
  23.     sol[2] = {name="tramp", prop={type=2,friction=0,restitution=1.2,miceCollision=true,groundCollision=true,angle=0,foreground=false,dynamic=false}, fix={'color'}}
  24.     sol[3] = {name="lave", prop={type=3,friction=0,restitution=20,miceCollision=true,groundCollision=true,angle=0,foreground=false,dynamic=false}, fix={'color'}}
  25.     sol[4] = {name="choco", prop={type=4,friction=20,restitution=0.2,miceCollision=true,groundCollision=true,angle=0,foreground=false,dynamic=false}, fix={'color'}}
  26.     sol[5] = {name="terre", prop={type=5,friction=0.3,restitution=0.2,miceCollision=true,groundCollision=true,angle=0,foreground=false,dynamic=false}, fix={'color'}}
  27.     sol[6] = {name="herbe", prop={type=6,friction=0.3,restitution=0.2,miceCollision=true,groundCollision=true,angle=0,foreground=false,dynamic=false}, fix={'color'}}
  28.     sol[7] = {name="sable", prop={type=7,friction=0.1,restitution=0.2,miceCollision=true,groundCollision=true,angle=0,foreground=false,dynamic=false}, fix={'color'}}
  29.     sol[8] = {name="nuage", prop={type=8,friction=0.3,restitution=0.2,miceCollision=false,groundCollision=true,angle=0,foreground=false,dynamic=false}, fix={'color'}}
  30.     sol[9] = {name="eau", prop={type=9,friction=0.3,restitution=0.2,miceCollision=true,groundCollision=true,angle=0,foreground=false,dynamic=false}, fix={'color','dynamic','mass','angle','fixedRotation','linearDamping','angularDamping','miceCollision','groundCollision','foreground'}}
  31.     sol[10] = {name="pierre", prop={type=10,friction=0.3,restitution=0,miceCollision=true,groundCollision=true,angle=0,foreground=false,dynamic=false}, fix={'color'}}
  32.     sol[11] = {name="neige", prop={type=11,friction=0.05,restitution=0.1,miceCollision=true,groundCollision=true,angle=0,foreground=false,dynamic=false}, fix={'color'}}
  33.     sol[12] = {name="rect", prop={type=12,friction=0.3,restitution=0.2,miceCollision=true,groundCollision=true,angle=0,foreground=false,color=0xaaaaaa,dynamic=false}, fix={}}
  34.     sol[13] = {name="cercle", prop={type=13,friction=0.3,restitution=0.2,miceCollision=true,groundCollision=true,angle=0,foreground=false,color=0xaaaaaa,dynamic=false}, fix={'height'}}
  35.  
  36.     editor = {}
  37.     editor.ground = {}
  38.     editor.joint = {}
  39.     editor.object = {}
  40.     editor.setting = {}
  41.     editor.other = {}
  42.     editor.xml = {}
  43.     interface = {}
  44.     interface.ground = {}
  45.     interface.ground.edit = {}
  46.     interface.joint = {}
  47.     interface.object = {}
  48.     interface.setting = {}
  49.     interface.other = {}
  50.     interface.prop = { dynamic=8, mass=10, friction=12, restitution=14, angle=16, fixedRotation=18, linearDamping=20, angularDamping=22, color=24, miceCollision=26, groundCollision=28 }
  51.  
  52.  
  53. function init()
  54.    
  55.     tfm.exec.disableAutoNewGame(true)
  56.     tfm.exec.disableAutoShaman(true)
  57.     tfm.exec.disableAutoScore(true)
  58.    
  59.     info = {}
  60.    
  61.     editor.other.newMap()
  62.    
  63.     for name in pairs(tfm.get.room.playerList) do eventNewPlayer(name) end
  64.    
  65.  
  66.    
  67. end
  68.  
  69.  
  70. function editor.other.newMap ()
  71.  
  72.     tfm.exec.newGame('<C><P DS="y;500" /><Z><S /><D /><O /></Z></C>')
  73.    
  74.     xml = {}
  75.     xml.ground = {}
  76.     xml.joint = {}
  77.     xml.object = {}
  78.     xml.setting = {}
  79.    
  80. end
  81.  
  82.  
  83. function eventNewPlayer(name)
  84.    
  85.     system.bindMouse(name, true)
  86.    
  87.     info[name] = {}
  88.     info[name].point = {}
  89.     info[name].mode = "ground"
  90.     info[name].type_g = 0
  91.     info[name].page_g = 1
  92.     info[name].lock = false
  93.    
  94.    
  95.     ui.addTextArea(0,"", name, 5, -40, 790, 35, 0x324650, 0x324650, 1, true)
  96.    
  97.     interface.ground.base_show(name)
  98.    
  99.    
  100. end
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107. function interface.ground.base_show(name)
  108.    
  109.     ui.addTextArea(1, "", name, 10, -40, 80, 20, 0, 0, 0, true)
  110.     ui.addTextArea(2, "", name, 805,5, 150, 200, 0x324650, 0x324650, 1, true)
  111.     ui.addTextArea(35, "<b><a href='event:xml get'>xml</a>\n<a href='event:xml play'>jouer</a></b>", name, 750, -40, 45, 40, 1, 1, 1, true)
  112.    
  113.    
  114.     interface.ground.type_refresh(name)
  115.     interface.ground.list_refresh(name, 1)
  116.    
  117.    
  118.    
  119. end
  120.  
  121. function interface.ground.type_refresh(name)
  122.     ui.updateTextArea(1,"<b><a href='event:ground type'>"..sol[info[name].type_g].name.."</a></b>", name)  
  123. end
  124.  
  125. function interface.ground.list_refresh(name, page)
  126.    
  127.     local nPage = math.floor(#xml.ground/10)+1
  128.    
  129.     local str = "<p align='center'><a href='event:ground list -1'>◀</a> page "..page.."/"..nPage.." <a href='event:ground list +1'>▶</a></p>\n<j>Id \ttype\tcoord<n>\n"
  130.    
  131.     for id=10*(page-1)+1,10*page do
  132.         local g = xml.ground[id]
  133.         if g then
  134.             str = str..'\n'..tostring(id).."  \t<j><b><a href='event:ground select "..id.."'>"..sol[g.prop.type].name.."</a></b><n> \t"..tostring(g.x)..","..tostring(g.y)
  135.         end
  136.     end
  137.    
  138.     ui.updateTextArea(2, str, name)
  139.     info[name].page_g = page
  140.    
  141. end
  142.  
  143.  
  144. function interface.ground.select_show(id, name)
  145.    
  146.    
  147.     local text = ui.addTextArea
  148.     text(3, "", name, 805, 215, 150, 380, 0x324650, 0x324650, 1, true)
  149.     text(4, "<b>Trampo \t + 5 - </b>", name, 805, 220, 150, 20, 0, 0, 0, true)
  150.     text(5, "X :  \tY : ", name, 805, 245, 150, 20, 0x000000, 0, 0, true)
  151.     text(6, "L : 1600 \tH : 30", name, 805, 260, 150, 20, 0, 0, 0, true)
  152.     text(7, "Frottement :\nRestitution :\nAngle :\nCollision souris :\nCollision sols :\nAvant plan :\nCouleur \n\nDynamique :\nMasse :\nAngle fixe :\nAmort linéaire :\nAmort angulaire :", name, 805, 285, 150, 200, 0, 0, 0, true)
  153.  
  154.     text(8, "Non", name, 910, 285, 60, 200, 0, 0, 0, true)
  155.  
  156.    
  157.     interface.ground.select_update(name, id)
  158.    
  159.    
  160. end
  161.  
  162. --[[type
  163. x 3
  164. y 4
  165. width 5
  166. height 6
  167. dynamic 7
  168. mass 8
  169. friction 9
  170. restitution 10
  171. angle 11
  172. fixedRotation 12
  173. linearDamping 13
  174. angularDamping 14
  175. color 15
  176. miceCollision 16
  177. groundCollision 17
  178. m 18
  179. v 19
  180. i 20]]--
  181.  
  182.  
  183.  
  184.  
  185. interface.ground.prop_base = { "friction", "restitution", "angle", "miceCollision", "groundCollision", "foreground" }
  186. interface.ground.prop_dyna = { "dynamic", "mass", "fixedRotation", "linearDamping", "angularDamping" }
  187.  
  188. function interface.ground.select_update(name, id)
  189.    
  190.     local typ = sol[xml.ground[id].prop.type].name
  191.     local prop = xml.ground[id].prop
  192.     local fix = sol[prop.type].fix
  193.    
  194.     ui.updateTextArea(4, "<b><a href='event:ground edit "..id.." type'>"..typ.."</a> \t <a href='event:ground edit "..id.." id -1'>-</a> "..id.." <a href='event:ground edit "..id.." id +1'>+</a></b>", name)
  195.     ui.updateTextArea(5, "<b><a href='event:ground edit "..id.." x'>"..xml.ground[id].x.."</a> \t<a href='event:ground edit "..id.." y'>"..xml.ground[id].y.."</a></b>", name)
  196.     ui.updateTextArea(6, "<b><a href='event:ground edit "..id.." witdh'>"..prop.width.."</a> \t<a href='event:ground edit "..id.." height'>"..prop.height.."</a></b>", name)
  197.    
  198.     local strB = "<b>"
  199.     for _, v in ipairs(interface.ground.prop_base) do
  200.         if table.find(fix, v) then
  201.             strB = strB.."-"
  202.         else
  203.             strB = strB.."<a href='event:groud edit "..id.." "..v.."'>"
  204.             if type(prop[v]) == "boolean" then
  205.                 strB = strB..((prop[v] and "oui") or "non")
  206.             else
  207.                 strB = strB..tostring(prop[v])
  208.             end
  209.             strB = strB.."</a>"
  210.         end
  211.         strB = strB.."\n"
  212.     end
  213.    
  214.     if prop.type == 12 or prop.type == 13 then
  215.         strB = strB.."<a href='event:ground edit "..id.." color'>"..string.format("%x", prop.color or 0xaaaaaa).."</a>\n\n"
  216.     else
  217.         strB = strB.."-\n\n"
  218.     end
  219.    
  220.    
  221.     for _, v in ipairs(interface.ground.prop_dyna) do
  222.        
  223.         if table.find(fix, v) then
  224.             strB = strB.."-"
  225.         elseif v == "dynamic" then
  226.             strB = strB.. "<a href='event:ground edit "..id.." dynamic'>"..((prop[v] and "oui") or "non").."</a>\n"
  227.         elseif prop.dynamic then
  228.             strB = strB.. "<a href='event:ground edit "..id.." "..v.."'>"
  229.             if type(prop[v]) == "boolean" then
  230.                 strB = strB..((prop[v] and "oui") or "non")
  231.             else
  232.                 strB = strB..tostring(prop[v])
  233.             end
  234.             strB = strB.."</a>\n"
  235.         else
  236.             strB = strB.."-\n"
  237.         end
  238.            
  239.     end
  240.     ui.updateTextArea(8, strB, name)
  241.    
  242. end
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257. function eventTextAreaCallback(id, name, call)
  258.    
  259.     local lw = {}
  260.     for w in call:gmatch('%S+') do table.insert(lw, w) end
  261.    
  262.     if not info[name].lock then
  263.         print(name..' - text : '..call)
  264.         if lw[1] == "ground" then
  265.             if lw[2] == "type" then
  266.                 if lw[3] then
  267.                     ui.removeTextArea(id, name)
  268.                     info[name].type_g = tonumber(lw[3])
  269.                     interface.ground.type_refresh(name)
  270.                 else
  271.                     local str = "<b><a href='event:ground type 0'>bois</a>\n"
  272.                     for k,v in ipairs(sol) do
  273.                         str = str.."<a href='event:ground type "..k.."'>"..v.name.."</a>\n"
  274.                     end
  275.                     str = str.."</b>"
  276.                     ui.addTextArea(30, str, name, -75, -40, 70, 59, 0x324650, 0x324650, 1, true)
  277.                 end
  278.             elseif lw[2] == "list" then
  279.                 local nPage = math.floor(#xml.ground/10)+1
  280.                 local page = info[name].page_g + tonumber(lw[3])
  281.                 if page < 1 then page = 1 elseif page > nPage then page = nPage end
  282.                 interface.ground.list_refresh(name, page)
  283.             elseif lw[2] == "select" then
  284.                 interface.ground.select_show(tonumber(lw[3]), name)
  285.             elseif lw[2] == "edit" then
  286.                 if lw[4]=="dynamic" or lw[4]=="foreground" or lw[4]=="miceCollision" or lw[4]=="groundCollision" or lw[4]=="fixedRotation" then
  287.                     editor.ground.edit(tonumber(lw[3]), lw[4], not xml.ground[tonumber(lw[3])].prop[lw[4]])
  288.                     interface.ground.select_update(name, tonumber(lw[3]))
  289.                 else
  290.                     info[name].lock = true
  291.                     info[name].ing = "ground "..lw[3]..' '..lw[4]
  292.                     ui.addPopup(1, 2, "<p align='center'>Entrer la nouvelle valeur</p>", name, 300, 200, 200, true)
  293.                 end
  294.             end
  295.         elseif lw[1]=="xml" then
  296.             local str = editor.xml.get()
  297.             if lw[2]=="get" then
  298.                 str = string.gsub (string.gsub (str, '<', '&lt;'), '>', '&gt;')
  299.                 comp = 0
  300.                 while comp < #str do
  301.                     print(str:sub(comp, comp+4500))
  302.                     comp = comp+4501
  303.                 end
  304.             elseif lw[2]=="play" then
  305.                 tfm.exec.newGame(str)
  306.             end
  307.         end
  308.     end
  309. end
  310.  
  311. function eventPopupAnswer(id, name, ans)
  312.    
  313.     print(name..' - popup : '..info[name].ing..' / '..ans)
  314.     local lw = {}
  315.     for w in info[name].ing:gmatch('%S+') do table.insert(lw, w) end
  316.    
  317.     ans = tonumber(ans)
  318.     if ans then editor.ground.edit(tonumber(lw[2]), lw[3], ans) end
  319.    
  320.     info[name].lock = false
  321.    
  322.    
  323.     interface.ground.select_update(name, tonumber(lw[2]))
  324.    
  325.    
  326.    
  327. end
  328.  
  329.  
  330.  
  331.  
  332. function eventMouse(name, x, y)
  333.    
  334.     if not info[name].lock then
  335.         print(name..' - mouse : '..x..' '..y)
  336.         if info[name].mode == "ground" then
  337.             if info[name].point[1] then
  338.                 local x1, y1 = info[name].point[1].x, info[name].point[1].y
  339.                 local cx, cy = math.floor((x+x1)/2), math.floor((y+y1)/2)
  340.                 local l, h = math.abs(x-x1), math.abs(y-y1)
  341.                 local prop = table.copy(sol[info[name].type_g].prop)
  342.                 if prop.type == 13 then
  343.                     prop.width = math.floor(math.sqrt(l*l+h*h)/2)
  344.                 else
  345.                     prop.width, prop.height = l, h
  346.                 end
  347.                 editor.ground.add(cx, cy, prop)
  348.                 info[name].point[1] = nil
  349.             else
  350.                 info[name].point[1] = {['x']=x, ['y']=y}
  351.             end
  352.         end
  353.     end
  354.    
  355. end
  356.  
  357.  
  358.  
  359.  
  360. function editor.xml.ground (id)
  361.    
  362.     local g = xml.ground[id]
  363.     local pro = g.prop
  364.     local str = '<S T="'..pro.type..'" X="'..g.x..'" Y="'..g.y..'" L="'..pro.width..'" '..((pro.type~=13 and ('H="'..pro.height..'" ')) or '')
  365.     if pro.miceCollision then
  366.         if not pro.groundCollision then
  367.             str = str..'c="3" '
  368.         end
  369.     elseif pro.groundCollision then
  370.         str = str..'c="2" '
  371.     else
  372.         str = str..'c="4" '
  373.     end
  374.     if pro.type == 12 or pro.type == 13 then
  375.         str = str..'o="'..(string.format('%x', pro.color) or '')..'" '
  376.     end
  377.     str = (pro.v and (str..'v="'..pro.v..'" ')) or str
  378.     str = (pro.m and (str..'m="" ')) or str
  379.     str = (pro.foreground and (str..'N="" ')) or str
  380.     str = (pro.i and (str..'i="'..pro.i..'" ')) or str
  381.     str = (pro.lua and (str..'lua="'..pro.lua..'" ')) or str
  382.    
  383.     local str2 = 'P="'
  384.     local d = pro.dynamic
  385.     str2 = str2..((d and '1') or '')..','..((d and (pro.mass or '')) or 0)..','..(pro.friction or 0)..','..(pro.restituion or 0)..','..(pro.angle or 0)..','..((d and (pro.fixedRotation or '')) or '')..','..((d and (pro.linearDamping or '')) or '')..','..((d and (angularDamping or '')) or '')..'" '
  386.    
  387.    
  388.    
  389.    
  390.     return str..str2..'/>'
  391.    
  392.    
  393. end
  394.  
  395.  
  396.  
  397. function editor.xml.setting()
  398.    
  399.     return ''
  400.    
  401. end
  402.  
  403. function editor.xml.joint(id)
  404.    
  405.     return ''
  406.    
  407. end
  408.  
  409. function editor.xml.get()
  410.    
  411.     local str = '<C><P /><Z><S>'
  412.     local S = editor.xml.ground
  413.     for id in ipairs(xml.ground) do str = str..S(id) end
  414.    
  415.     str = str..'</S><D /><O /></Z></C>'
  416.    
  417.     return str
  418.    
  419.     --<C><P /><Z><S></S><D /><O /></Z></C>
  420. end
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434. function editor.ground.add(x, y, prop, id)
  435.    
  436.     id = id or #xml.ground+1
  437.     local inf = {}
  438.     inf.x, inf.y, inf.prop = x, y, prop
  439.     xml.ground[id] = inf
  440.    
  441.     editor.ground.show(id)
  442.    
  443. end
  444.  
  445. function editor.ground.show(id)
  446.     local inf = xml.ground[id]
  447.     local pro = inf.prop
  448.     tfm.exec.addPhysicObject(id, inf.x, inf.y, { ["type"]=pro.type, width=pro.width, height=pro.height, color=pro.color, miceCollision=false, objectCollision=false, angle=pro.angle, foreground=pro.foreground})
  449. end
  450.  
  451. function editor.ground.delete(id)
  452.    
  453.     table.remove(xml.ground, id)
  454.     tfm.exec.removePhysicObject(id)
  455.     for i=id, #xml.ground do
  456.         tfm.exec.removePhysicObject(i)
  457.         editor.ground.show(i)
  458.     end
  459.     tfm.exec.removePhysicObject(#xml.ground+1)
  460.    
  461. end
  462.  
  463. function editor.ground.edit(id, prop, val)
  464.    
  465.     if prop == "x" or prop == "y" then
  466.         xml.ground[id][prop] = val
  467.     else
  468.         xml.ground[id].prop[prop] = val
  469.     end
  470.     editor.ground.show(id)
  471.    
  472. end
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499. function table.copy(tab)
  500.     local res = {}
  501.     for k, v in pairs(tab) do
  502.         if type(v)=="table" then
  503.             res[k] = table.copy(v)
  504.         else
  505.             res[k] = v
  506.         end
  507.     end
  508.     return res
  509. end
  510.  
  511. function table.find(tab, elmt)
  512.     for k,v in pairs(tab) do if v==elmt then return true,k end end return false
  513. end
  514.  
  515.  
  516. updateTextArea = ui.updateTextArea
  517.  
  518. function ui.updateTextArea(id, text, name)
  519.     if type(name) == "table" then
  520.         for _, n in ipairs(name) do
  521.             updateTextArea(id, text, n)
  522.         end
  523.     else
  524.         updateTextArea(id, text, name)
  525.     end
  526. end
  527.  
  528. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement